dodo  0.0.1
A C++ library to create containerized Linux services
dodo::network::protocol::http::HTTPMessage Class Referenceabstract

Generic HTTPMessage, parent to HTTPRequest and HTTPResponse. More...

#include <httpmessage.hpp>

Inheritance diagram for dodo::network::protocol::http::HTTPMessage:
Collaboration diagram for dodo::network::protocol::http::HTTPMessage:

Public Member Functions

 HTTPMessage ()
 Default constructor. More...
 
void addHeader (const std::string &key, const std::string &value)
 Add a header to the HTTPMessage. More...
 
void clearHeaders ()
 Clear all headers. More...
 
const common::BytesgetBody () const
 Return the HTTTMessage body. More...
 
const std::map< std::string, std::string > & getHeaders () const
 Return a reference to the headers map. More...
 
bool getHeaderValue (const std::string &key, std::string &value) const
 Get a header value as a string into value. More...
 
bool getHeaderValue (const std::string &key, unsigned long &value) const
 Get a header value as an unsigned long into value. More...
 
bool hasHeader (const std::string &header) const
 Return true when this header key exists in this HTTPMessage's headers. More...
 
void replaceHeader (const std::string &key, const std::string &value)
 Replace the value of a header. More...
 
virtual common::SystemError send (BaseSocket *socket)=0
 Send this HTTPMessage to the socket. More...
 
void setBody (const std::string &body)
 Set the body. More...
 
- Public Member Functions inherited from dodo::network::protocol::http::HTTPFragment
virtual std::string asString () const =0
 Convert the HTTPFragment in a HTTP protocol string. More...
 
virtual ParseResult parse (VirtualReadBuffer &buffer)=0
 Read a complete HTTPFragment from a VirtualReadBuffer. More...
 

Static Public Attributes

static const char charCR = char(13)
 CR character. More...
 
static const char charHT = char(9)
 Horizontal tab character. More...
 
static const char charLF = char(10)
 LF character. More...
 
static const char charSP = char(32)
 Space character. More...
 

Protected Member Functions

ParseResult eatCRLF (VirtualReadBuffer &data)
 Consume a CR LF sequence only when it is there. More...
 
virtual ParseResult parseBody (VirtualReadBuffer &data)=0
 Parse a body and resturn it as a single string. More...
 
ParseResult parseChunkedBody (VirtualReadBuffer &data)
 Parse a chunked body (transfer-encoding: chunked) and resturn it as a single string. More...
 
ParseResult parseChunkHex (VirtualReadBuffer &data, unsigned long &value)
 Parse a hexadecimal chunk size (a hex value followed by CR LF). More...
 
ParseResult parseCRLF (VirtualReadBuffer &data)
 Consume a CR LF sequence. More...
 
ParseResult parseFieldValue (VirtualReadBuffer &data, std::string &value)
 Parse a header field value. More...
 
ParseResult parseHeaders (VirtualReadBuffer &data)
 Parse a header section and update headers_. More...
 
ParseResult parseToken (VirtualReadBuffer &buffer, std::string &token)
 Parse a token (such as a header field name). More...
 

Static Protected Member Functions

static common::SystemError eatSpace (VirtualReadBuffer &data)
 Call buffer.next() as long as buffer.get() is whitespace ( charSP or charHT). More...
 
static bool isCTL (char c)
 Check is the char is a CTL character. More...
 
static bool isSeparator (char c)
 Return true if the char is a separator. More...
 
static bool isSP (char c)
 Return true if the char is whitespace character (charSP or charHT) More...
 
static ParseResult parseUInt (VirtualReadBuffer &data, unsigned int &value)
 Parse an unsigned integer. More...
 

Protected Attributes

common::Bytes body_
 The message body (if any). More...
 
std::map< std::string, std::string > headers_
 The message headers. More...
 

Additional Inherited Members

- Public Types inherited from dodo::network::protocol::http::HTTPFragment
enum  ParseError : unsigned int {
  peOk = 0, peIncomplete, peSystemError, peExpectCRLF,
  peUnFinishedToken, peExpectingHeaderColon, peInvalidHeaderFieldValue, peInvalidHeaderListEnd,
  peInvalidMethod, peInvalidHTTPVersion, peInvalidRequestLine, peInvalidContentLength,
  peUnexpectedBody, peInvalidTransferEncoding, peInvalidChunkHex, peInvalidLastChunk,
  peExpectingUnsignedInt
}
 Parse results returned by the parse functions. More...
 
- Static Public Member Functions inherited from dodo::network::protocol::http::HTTPFragment
static std::string getParseResultAsString (ParseError error)
 Return the string description of a ParseError. More...
 

Detailed Description

Generic HTTPMessage, parent to HTTPRequest and HTTPResponse.

See also
https://tools.ietf.org/html/rfc2616#page-15
Remarks
This class is not thread safe, but does not use any global variables.

Definition at line 40 of file httpmessage.hpp.

Constructor & Destructor Documentation

◆ HTTPMessage()

dodo::network::protocol::http::HTTPMessage::HTTPMessage ( )
inline

Default constructor.

Definition at line 67 of file httpmessage.hpp.

Member Function Documentation

◆ addHeader()

void dodo::network::protocol::http::HTTPMessage::addHeader ( const std::string &  key,
const std::string &  value 
)

Add a header to the HTTPMessage.

Note that multtiple headers may be specified with the same key but are supposed to be equavalent to a comma seperated list, so

addHeader( "key", "1" );
addHeader( "key", "2" );
addHeader( "key", "3" );

is equivalent to

addHeader( "key", "1,2,3" );

and this class transforms the former into the latter automatically.

Parameters
keyThe header key.
valueThe header value.

Definition at line 37 of file httpmessage.cpp.

References headers_.

Referenced by parseHeaders().

Here is the caller graph for this function:

◆ clearHeaders()

void dodo::network::protocol::http::HTTPMessage::clearHeaders ( )
inline

Clear all headers.

Definition at line 110 of file httpmessage.hpp.

References headers_.

◆ eatCRLF()

HTTPMessage::ParseResult dodo::network::protocol::http::HTTPMessage::eatCRLF ( VirtualReadBuffer data)
protected

Consume a CR LF sequence only when it is there.

Unlike parseCRLF this function will not fail when the CRLF is not found.

Parameters
dataThe VirtualReadBuffer to read from.
Returns
peOk or peExpectCRLF or peSystemError. The latter can only be returned when a CR is encountered without a LF after it.

Definition at line 46 of file httpmessage.cpp.

References charCR, charLF, dodo::common::SystemError::ecOK, dodo::network::VirtualReadBuffer::get(), dodo::network::VirtualReadBuffer::next(), dodo::network::protocol::http::HTTPFragment::ParseResult::ok(), dodo::network::protocol::http::HTTPFragment::peExpectCRLF, and dodo::network::protocol::http::HTTPFragment::ParseResult::setSystemError().

Here is the call graph for this function:

◆ eatSpace()

common::SystemError dodo::network::protocol::http::HTTPMessage::eatSpace ( VirtualReadBuffer data)
staticprotected

Call buffer.next() as long as buffer.get() is whitespace ( charSP or charHT).

Parameters
dataThe VirtualReadBuffer to read from.
Returns
A SystemError, like data tata presenting a CR but no LF after it.

Definition at line 56 of file httpmessage.cpp.

References charHT, charSP, dodo::common::SystemError::ecOK, dodo::network::VirtualReadBuffer::get(), and dodo::network::VirtualReadBuffer::next().

Referenced by dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::parse(), dodo::network::protocol::http::HTTPResponse::HTTPResponseLine::parse(), and parseHeaders().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getBody()

const common::Bytes& dodo::network::protocol::http::HTTPMessage::getBody ( ) const
inline

Return the HTTTMessage body.

Returns
The HTTPMessage body.

Definition at line 133 of file httpmessage.hpp.

References body_.

◆ getHeaders()

const std::map<std::string,std::string>& dodo::network::protocol::http::HTTPMessage::getHeaders ( ) const
inline

Return a reference to the headers map.

Returns
A reference to headers_;

Definition at line 98 of file httpmessage.hpp.

References headers_.

◆ getHeaderValue() [1/2]

bool dodo::network::protocol::http::HTTPMessage::getHeaderValue ( const std::string &  key,
std::string &  value 
) const

Get a header value as a string into value.

Parameters
keyThe header key.
valueReceives the header value.
Returns
True if the key was found, false otherwise, in which case value is undefined.

Definition at line 74 of file httpmessage.cpp.

References headers_.

Referenced by dodo::network::protocol::http::HTTPResponse::hasBody(), dodo::network::protocol::http::HTTPRequest::parseBody(), and dodo::network::protocol::http::HTTPResponse::parseBody().

Here is the caller graph for this function:

◆ getHeaderValue() [2/2]

bool dodo::network::protocol::http::HTTPMessage::getHeaderValue ( const std::string &  key,
unsigned long &  value 
) const

Get a header value as an unsigned long into value.

Parameters
keyThe header key.
valueReceives the header value.
Returns
True if the key was found and the value an integer, false otherwise, in which case value is undefined.

Definition at line 64 of file httpmessage.cpp.

References headers_.

◆ hasHeader()

bool dodo::network::protocol::http::HTTPMessage::hasHeader ( const std::string &  header) const
inline

Return true when this header key exists in this HTTPMessage's headers.

Parameters
headerThe header key to check for (lowercase).
Returns
True then the header exists in this HTTPMessage's headers.

Definition at line 105 of file httpmessage.hpp.

References headers_.

◆ isCTL()

static bool dodo::network::protocol::http::HTTPMessage::isCTL ( char  c)
inlinestaticprotected

Check is the char is a CTL character.

Parameters
cThe character to check.
Returns
true is c is a control character
See also
https://tools.ietf.org/html/rfc2616#page-15

Definition at line 164 of file httpmessage.hpp.

Referenced by parseToken().

Here is the caller graph for this function:

◆ isSeparator()

static bool dodo::network::protocol::http::HTTPMessage::isSeparator ( char  c)
inlinestaticprotected

Return true if the char is a separator.

Parameters
cThe char to check.
Returns
True if c is a separator, false otherwise.
Todo:
the order of checks can be optimized on character frequency.

Definition at line 242 of file httpmessage.hpp.

References charHT, and charSP.

Referenced by parseToken().

Here is the caller graph for this function:

◆ isSP()

static bool dodo::network::protocol::http::HTTPMessage::isSP ( char  c)
inlinestaticprotected

Return true if the char is whitespace character (charSP or charHT)

Parameters
cThe char to check.
Returns
True if c is whitespace, false otherwise.
Todo:
the order of checks can be optimized on character frequency.

Definition at line 255 of file httpmessage.hpp.

References charHT, and charSP.

Referenced by parseFieldValue().

Here is the caller graph for this function:

◆ parseBody()

virtual ParseResult dodo::network::protocol::http::HTTPMessage::parseBody ( VirtualReadBuffer data)
protectedpure virtual

Parse a body and resturn it as a single string.

Note that the content-length header must be present and specify the correct body length.

Parameters
dataThe VirtualReadBuffer to read from.
Returns
The ParseError.

Implemented in dodo::network::protocol::http::HTTPResponse, and dodo::network::protocol::http::HTTPRequest.

◆ parseChunkedBody()

◆ parseChunkHex()

HTTPMessage::ParseResult dodo::network::protocol::http::HTTPMessage::parseChunkHex ( VirtualReadBuffer data,
unsigned long &  value 
)
protected

Parse a hexadecimal chunk size (a hex value followed by CR LF).

Parameters
dataThe VirtualReadBuffer to read from.
valueThe value receiving the chunk size.
Returns
The ParseResult, either peOk, peExpectCRLF or peInvalidChunkHex.

Definition at line 125 of file httpmessage.cpp.

References dodo::common::SystemError::ecOK, dodo::network::VirtualReadBuffer::get(), dodo::network::VirtualReadBuffer::next(), dodo::network::protocol::http::HTTPFragment::ParseResult::ok(), parseCRLF(), dodo::network::protocol::http::HTTPFragment::peInvalidChunkHex, and dodo::network::protocol::http::HTTPFragment::ParseResult::setSystemError().

Referenced by parseChunkedBody().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parseCRLF()

HTTPMessage::ParseResult dodo::network::protocol::http::HTTPMessage::parseCRLF ( VirtualReadBuffer data)
protected

◆ parseFieldValue()

HTTPMessage::ParseResult dodo::network::protocol::http::HTTPMessage::parseFieldValue ( VirtualReadBuffer data,
std::string &  value 
)
protected

Parse a header field value.

Parameters
dataThe VirtualReadBuffer to read from.
valueThe value receiving the field-value.
Returns
The ParseResult.

Definition at line 141 of file httpmessage.cpp.

References charCR, charLF, charSP, dodo::common::SystemError::ecOK, dodo::network::VirtualReadBuffer::get(), isSP(), dodo::network::VirtualReadBuffer::next(), dodo::network::protocol::http::HTTPFragment::ParseResult::ok(), dodo::network::protocol::http::HTTPFragment::peExpectCRLF, dodo::network::protocol::http::HTTPFragment::peInvalidHeaderFieldValue, and dodo::network::protocol::http::HTTPFragment::ParseResult::setSystemError().

Referenced by parseHeaders().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parseHeaders()

◆ parseToken()

HTTPMessage::ParseResult dodo::network::protocol::http::HTTPMessage::parseToken ( VirtualReadBuffer buffer,
std::string &  token 
)
protected

Parse a token (such as a header field name).

Parameters
bufferThe HTTP data source.
tokenThe value receiving the token.
Returns
The ParseResult.

Definition at line 196 of file httpmessage.cpp.

References dodo::common::SystemError::ecOK, dodo::network::VirtualReadBuffer::get(), isCTL(), isSeparator(), dodo::network::VirtualReadBuffer::next(), dodo::network::protocol::http::HTTPFragment::ParseResult::ok(), dodo::network::protocol::http::HTTPFragment::peOk, dodo::network::protocol::http::HTTPFragment::peUnFinishedToken, and dodo::network::protocol::http::HTTPFragment::ParseResult::setSystemError().

Referenced by parseHeaders().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parseUInt()

HTTPMessage::ParseResult dodo::network::protocol::http::HTTPMessage::parseUInt ( VirtualReadBuffer data,
unsigned int &  value 
)
staticprotected

Parse an unsigned integer.

Parameters
dataThe VirtualReadBuffer to read from.
valueThe value receiving the unsigned int.
Returns
The ParseResult.

Definition at line 211 of file httpmessage.cpp.

References dodo::common::SystemError::ecOK, dodo::network::VirtualReadBuffer::get(), dodo::network::VirtualReadBuffer::next(), dodo::network::protocol::http::HTTPFragment::ParseResult::ok(), dodo::network::protocol::http::HTTPFragment::peExpectingUnsignedInt, and dodo::network::protocol::http::HTTPFragment::ParseResult::setSystemError().

Referenced by dodo::network::protocol::http::HTTPResponse::HTTPResponseLine::parse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ replaceHeader()

void dodo::network::protocol::http::HTTPMessage::replaceHeader ( const std::string &  key,
const std::string &  value 
)

Replace the value of a header.

Parameters
keythe key to replace.
valuethe value to set.

Definition at line 244 of file httpmessage.cpp.

References headers_.

Referenced by setBody().

Here is the caller graph for this function:

◆ send()

virtual common::SystemError dodo::network::protocol::http::HTTPMessage::send ( BaseSocket socket)
pure virtual

Send this HTTPMessage to the socket.

Parameters
socketThe socket to write to.
Returns
The commonSystemError that might accour.

Implemented in dodo::network::protocol::http::HTTPResponse, and dodo::network::protocol::http::HTTPRequest.

◆ setBody()

void dodo::network::protocol::http::HTTPMessage::setBody ( const std::string &  body)

Set the body.

Parameters
bodyThe body to set.

Definition at line 248 of file httpmessage.cpp.

References body_, dodo::common::Bytes::getSize(), and replaceHeader().

Here is the call graph for this function:

Field Documentation

◆ body_

◆ charCR

◆ charHT

const char dodo::network::protocol::http::HTTPMessage::charHT = char(9)
static

Horizontal tab character.

Definition at line 62 of file httpmessage.hpp.

Referenced by eatSpace(), isSeparator(), and isSP().

◆ charLF

◆ charSP

const char dodo::network::protocol::http::HTTPMessage::charSP = char(32)
static

◆ headers_


The documentation for this class was generated from the following files:
dodo::network::protocol::http::HTTPMessage::addHeader
void addHeader(const std::string &key, const std::string &value)
Add a header to the HTTPMessage.
Definition: httpmessage.cpp:37