dodo
0.0.1
A C++ library to create containerized Linux services
|
Generic HTTPMessage, parent to HTTPRequest and HTTPResponse. More...
#include <httpmessage.hpp>
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::Bytes & | getBody () 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... | |
![]() | |
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 | |
![]() | |
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 std::string | getParseResultAsString (ParseError error) |
Return the string description of a ParseError. More... | |
Generic HTTPMessage, parent to HTTPRequest and HTTPResponse.
Definition at line 40 of file httpmessage.hpp.
|
inline |
Default constructor.
Definition at line 67 of file httpmessage.hpp.
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
is equivalent to
and this class transforms the former into the latter automatically.
key | The header key. |
value | The header value. |
Definition at line 37 of file httpmessage.cpp.
References headers_.
Referenced by parseHeaders().
|
inline |
|
protected |
Consume a CR LF sequence only when it is there.
Unlike parseCRLF this function will not fail when the CRLF is not found.
data | The VirtualReadBuffer to read from. |
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().
|
staticprotected |
Call buffer.next() as long as buffer.get() is whitespace ( charSP or charHT).
data | The VirtualReadBuffer to read from. |
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().
|
inline |
Return the HTTTMessage body.
Definition at line 133 of file httpmessage.hpp.
References body_.
|
inline |
Return a reference to the headers map.
Definition at line 98 of file httpmessage.hpp.
References headers_.
bool dodo::network::protocol::http::HTTPMessage::getHeaderValue | ( | const std::string & | key, |
std::string & | value | ||
) | const |
Get a header value as a string into value.
key | The header key. |
value | Receives the header value. |
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().
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.
key | The header key. |
value | Receives the header value. |
Definition at line 64 of file httpmessage.cpp.
References headers_.
|
inline |
Return true when this header key exists in this HTTPMessage's headers.
header | The header key to check for (lowercase). |
Definition at line 105 of file httpmessage.hpp.
References headers_.
|
inlinestaticprotected |
Check is the char is a CTL character.
c | The character to check. |
Definition at line 164 of file httpmessage.hpp.
Referenced by parseToken().
|
inlinestaticprotected |
Return true if the char is a separator.
c | The char to check. |
Definition at line 242 of file httpmessage.hpp.
References charHT, and charSP.
Referenced by parseToken().
|
inlinestaticprotected |
Return true if the char is whitespace character (charSP or charHT)
c | The char to check. |
Definition at line 255 of file httpmessage.hpp.
References charHT, and charSP.
Referenced by parseFieldValue().
|
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.
data | The VirtualReadBuffer to read from. |
Implemented in dodo::network::protocol::http::HTTPResponse, and dodo::network::protocol::http::HTTPRequest.
|
protected |
Parse a chunked body (transfer-encoding: chunked) and resturn it as a single string.
data | The VirtualReadBuffer to read from. |
Definition at line 224 of file httpmessage.cpp.
References dodo::common::Bytes::append(), body_, dodo::common::SystemError::ecOK, dodo::network::protocol::http::HTTPFragment::ParseResult::eof(), dodo::network::VirtualReadBuffer::get(), dodo::network::VirtualReadBuffer::next(), dodo::network::protocol::http::HTTPFragment::ParseResult::ok(), parseChunkHex(), parseCRLF(), dodo::network::protocol::http::HTTPFragment::peInvalidLastChunk, dodo::network::protocol::http::HTTPFragment::peOk, and dodo::network::protocol::http::HTTPFragment::ParseResult::setSystemError().
Referenced by dodo::network::protocol::http::HTTPRequest::parseBody(), and dodo::network::protocol::http::HTTPResponse::parseBody().
|
protected |
Parse a hexadecimal chunk size (a hex value followed by CR LF).
data | The VirtualReadBuffer to read from. |
value | The value receiving the chunk size. |
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().
|
protected |
Consume a CR LF sequence.
data | The VirtualReadBuffer to read from. |
Definition at line 115 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().
Referenced by parseChunkedBody(), and parseChunkHex().
|
protected |
Parse a header field value.
data | The VirtualReadBuffer to read from. |
value | The value receiving the field-value. |
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().
|
protected |
Parse a header section and update headers_.
data | The VirtualReadBuffer to read from. |
Definition at line 83 of file httpmessage.cpp.
References addHeader(), charCR, charLF, eatSpace(), dodo::common::SystemError::ecOK, dodo::network::protocol::http::HTTPFragment::ParseResult::eof(), dodo::network::VirtualReadBuffer::get(), dodo::network::VirtualReadBuffer::next(), dodo::network::protocol::http::HTTPFragment::ParseResult::ok(), parseFieldValue(), parseToken(), dodo::network::protocol::http::HTTPFragment::peExpectingHeaderColon, dodo::network::protocol::http::HTTPFragment::peInvalidHeaderListEnd, and dodo::network::protocol::http::HTTPFragment::ParseResult::setSystemError().
Referenced by dodo::network::protocol::http::HTTPRequest::parse(), and dodo::network::protocol::http::HTTPResponse::parse().
|
protected |
Parse a token (such as a header field name).
buffer | The HTTP data source. |
token | The value receiving the token. |
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().
|
staticprotected |
Parse an unsigned integer.
data | The VirtualReadBuffer to read from. |
value | The value receiving the unsigned int. |
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().
void dodo::network::protocol::http::HTTPMessage::replaceHeader | ( | const std::string & | key, |
const std::string & | value | ||
) |
Replace the value of a header.
key | the key to replace. |
value | the value to set. |
Definition at line 244 of file httpmessage.cpp.
References headers_.
Referenced by setBody().
|
pure virtual |
Send this HTTPMessage to the socket.
socket | The socket to write to. |
Implemented in dodo::network::protocol::http::HTTPResponse, and dodo::network::protocol::http::HTTPRequest.
void dodo::network::protocol::http::HTTPMessage::setBody | ( | const std::string & | body | ) |
Set the body.
body | The body to set. |
Definition at line 248 of file httpmessage.cpp.
References body_, dodo::common::Bytes::getSize(), and replaceHeader().
|
protected |
The message body (if any).
Definition at line 267 of file httpmessage.hpp.
Referenced by dodo::network::protocol::http::HTTPRequest::asString(), dodo::network::protocol::http::HTTPResponse::asString(), getBody(), dodo::network::protocol::http::HTTPRequest::parseBody(), dodo::network::protocol::http::HTTPResponse::parseBody(), parseChunkedBody(), dodo::network::protocol::http::HTTPRequest::send(), dodo::network::protocol::http::HTTPResponse::send(), and setBody().
|
static |
CR character.
Definition at line 47 of file httpmessage.hpp.
Referenced by dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::asString(), dodo::network::protocol::http::HTTPResponse::HTTPResponseLine::asString(), dodo::network::protocol::http::HTTPRequest::asString(), dodo::network::protocol::http::HTTPResponse::asString(), eatCRLF(), dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::parse(), dodo::network::protocol::http::HTTPRequest::parse(), dodo::network::protocol::http::HTTPResponse::HTTPResponseLine::parse(), dodo::network::protocol::http::HTTPResponse::parse(), parseCRLF(), parseFieldValue(), parseHeaders(), dodo::network::protocol::http::HTTPRequest::send(), and dodo::network::protocol::http::HTTPResponse::send().
|
static |
Horizontal tab character.
Definition at line 62 of file httpmessage.hpp.
Referenced by eatSpace(), isSeparator(), and isSP().
|
static |
LF character.
Definition at line 52 of file httpmessage.hpp.
Referenced by dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::asString(), dodo::network::protocol::http::HTTPResponse::HTTPResponseLine::asString(), dodo::network::protocol::http::HTTPRequest::asString(), dodo::network::protocol::http::HTTPResponse::asString(), eatCRLF(), dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::parse(), dodo::network::protocol::http::HTTPRequest::parse(), dodo::network::protocol::http::HTTPResponse::HTTPResponseLine::parse(), dodo::network::protocol::http::HTTPResponse::parse(), parseCRLF(), parseFieldValue(), parseHeaders(), dodo::network::protocol::http::HTTPRequest::send(), and dodo::network::protocol::http::HTTPResponse::send().
|
static |
Space character.
Definition at line 57 of file httpmessage.hpp.
Referenced by dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::asString(), dodo::network::protocol::http::HTTPResponse::HTTPResponseLine::asString(), eatSpace(), isSeparator(), isSP(), and parseFieldValue().
|
protected |
The message headers.
Definition at line 262 of file httpmessage.hpp.
Referenced by addHeader(), dodo::network::protocol::http::HTTPRequest::asString(), dodo::network::protocol::http::HTTPResponse::asString(), clearHeaders(), getHeaders(), getHeaderValue(), hasHeader(), replaceHeader(), dodo::network::protocol::http::HTTPRequest::send(), and dodo::network::protocol::http::HTTPResponse::send().