PcapPlusPlus  24.09
pcpp::HttpResponseLayer Class Reference

#include <HttpLayer.h>

Inheritance diagram for pcpp::HttpResponseLayer:
pcpp::HttpMessage pcpp::TextBasedProtocolMessage pcpp::Layer pcpp::IDataContainer

Public Member Functions

 HttpResponseLayer (uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
 
 HttpResponseLayer (HttpVersion version, const HttpResponseStatusCode &statusCode, const std::string &statusCodeString)
 
 HttpResponseLayer (HttpVersion version, const HttpResponseStatusCode &statusCode)
 
 HttpResponseLayer (const HttpResponseLayer &other)
 
HttpResponseLayeroperator= (const HttpResponseLayer &other)
 
HttpResponseFirstLinegetFirstLine () const
 
HeaderFieldsetContentLength (int contentLength, const std::string &prevFieldName="")
 
int getContentLength () const
 
std::string toString () const
 
- Public Member Functions inherited from pcpp::HttpMessage
virtual HeaderFieldaddField (const std::string &fieldName, const std::string &fieldValue)
 
virtual HeaderFieldaddField (const HeaderField &newField)
 
virtual HeaderFieldinsertField (HeaderField *prevField, const std::string &fieldName, const std::string &fieldValue)
 
virtual HeaderFieldinsertField (HeaderField *prevField, const HeaderField &newField)
 
OsiModelLayer getOsiModelLayer () const
 
- Public Member Functions inherited from pcpp::TextBasedProtocolMessage
HeaderFieldgetFieldByName (std::string fieldName, int index=0) const
 
HeaderFieldgetFirstField () const
 
HeaderFieldgetNextField (HeaderField *prevField) const
 
int getFieldCount () const
 
HeaderFieldaddEndOfHeader ()
 
virtual HeaderFieldinsertField (std::string prevFieldName, const std::string &fieldName, const std::string &fieldValue)
 
bool removeField (HeaderField *fieldToRemove)
 
bool removeField (std::string fieldName, int index=0)
 
bool isHeaderComplete () const
 
virtual void parseNextLayer ()
 
size_t getHeaderLen () const
 
virtual void computeCalculateFields ()
 
- Public Member Functions inherited from pcpp::Layer
virtual ~Layer ()
 
LayergetNextLayer () const
 
LayergetPrevLayer () const
 
ProtocolType getProtocol () const
 
bool isMemberOfProtocolFamily (ProtocolTypeFamily protocolTypeFamily) const
 
uint8_t * getData () const
 
size_t getDataLen () const
 
uint8_t * getLayerPayload () const
 
size_t getLayerPayloadSize () const
 
bool isAllocatedToPacket () const
 
void copyData (uint8_t *toArr) const
 
uint8_t * getDataPtr (size_t offset=0) const
 

Additional Inherited Members

- Static Public Member Functions inherited from pcpp::HttpMessage
static bool isHttpPort (uint16_t port)
 

Detailed Description

Represents an HTTP response header and inherits all basic functionality of HttpMessage and TextBasedProtocolMessage. The functionality that is added for this class is the HTTP first line concept. An HTTP response has the following first line: 200 OK HTTP/1.1 Since it's not an "ordinary" HTTP field, it requires a special treatment and gets a class of it's own: HttpResponseFirstLine. Unlike most L2-4 protocols, an HTTP response header can spread over more than 1 packet. PcapPlusPlus currently doesn't support a header that is spread over more than 1 packet so in such cases: 1) only the first packet will be parsed as HttpResponseLayer (the other packets won't be recognized as HttpResponseLayer) and 2) the HTTP header for the first packet won't be complete (as it continues in the following packets), this why PcapPlusPlus can indicate that HTTP response header is complete or not (doesn't end with "\r\n\r\n" or "\n\n") using HttpMessage::isHeaderComplete()

Constructor & Destructor Documentation

◆ HttpResponseLayer() [1/4]

pcpp::HttpResponseLayer::HttpResponseLayer ( uint8_t *  data,
size_t  dataLen,
Layer prevLayer,
Packet packet 
)

A constructor that creates the layer from an existing packet raw data

Parameters
[in]dataA pointer to the raw data
[in]dataLenSize of the data in bytes
[in]prevLayerA pointer to the previous layer
[in]packetA pointer to the Packet instance where layer will be stored in

◆ HttpResponseLayer() [2/4]

pcpp::HttpResponseLayer::HttpResponseLayer ( HttpVersion  version,
const HttpResponseStatusCode statusCode,
const std::string &  statusCodeString 
)
explicit

A constructor that allocates a new HTTP response header with only the first line filled. Object will be created without further fields. The user can then add fields using addField() methods

Parameters
[in]versionHTTP version to be used
[in]statusCodeStatus code to be used
[in]statusCodeStringMost status codes have their default string, e.g 200 is usually "OK", 404 is usually "Not Found", etc. But the user can set a non-default status code string and it will be written in the header first line. Empty string ("") means using the default status code string
Deprecated:
Use other constructors instead.

◆ HttpResponseLayer() [3/4]

pcpp::HttpResponseLayer::HttpResponseLayer ( HttpVersion  version,
const HttpResponseStatusCode statusCode 
)
explicit

A constructor that allocates a new HTTP response header with only the first line filled. Object will be created without further fields. The user can then add fields using addField() methods

Parameters
[in]versionHTTP version to be used
[in]statusCodeStatus code to be used

◆ HttpResponseLayer() [4/4]

pcpp::HttpResponseLayer::HttpResponseLayer ( const HttpResponseLayer other)

A copy constructor for this layer. This copy constructor inherits base copy constructor HttpMessage::HttpMessage() and adds the functionality of copying the first line as well

Parameters
[in]otherThe instance to copy from

Member Function Documentation

◆ getContentLength()

int pcpp::HttpResponseLayer::getContentLength ( ) const

The length of the body of many HTTP response messages is determined by a HTTP header field called "Content-Length". This method parses this field, extracts its value and return it. If this field doesn't exist the method will return 0

Returns
HTTP response body length determined by "Content-Length" field

◆ getFirstLine()

HttpResponseFirstLine* pcpp::HttpResponseLayer::getFirstLine ( ) const
inline
Returns
A pointer to the first line instance for this message

◆ operator=()

HttpResponseLayer& pcpp::HttpResponseLayer::operator= ( const HttpResponseLayer other)

An assignment operator overload for this layer. This method inherits base assignment operator HttpMessage::operator=() and adds the functionality of copying the first line as well

Parameters
[in]otherThe instance to copy from
Returns
A reference to the assignee

◆ setContentLength()

HeaderField* pcpp::HttpResponseLayer::setContentLength ( int  contentLength,
const std::string &  prevFieldName = "" 
)

The length of the body of many HTTP response messages is determined by a HTTP header field called "Content-Length". This method sets The content-length field value. The method supports several cases:

  • If the "Content-Length" field exists - the method will only replace the existing value with the new value
  • If the "Content-Length" field doesn't exist - the method will create this field and put the value in it. Here are also 2 cases:
  • If prevFieldName is specified - the new "Content-Length" field will be created after it
  • If prevFieldName isn't specified or doesn't exist - the new "Content-Length" field will be created as the last field before end-of-header field
Parameters
[in]contentLengthThe content length value to set
[in]prevFieldNameOptional field, if specified and "Content-Length" field doesn't exist, it will be created after it
Returns
A pointer to the "Content-Length" field, or nullptr if creation failed for some reason

◆ toString()

std::string pcpp::HttpResponseLayer::toString ( ) const
virtual
Returns
A string representation of the layer most important data (should look like the layer description in Wireshark)

Implements pcpp::Layer.