dodo
0.0.1
A C++ library to create containerized Linux services
|
HTTPRequest class represents a HTTP request. More...
#include <httprequest.hpp>
Data Structures | |
class | HTTPRequestLine |
HTTPRequestLine class. More... | |
Public Types | |
enum | Method { meGET, meHEAD, mePOST, mePUT, mePATCH, meDELETE, meCONNECT, meOPTIONS, meTRACE, meINVALID } |
The HTTP request method. More... | |
![]() | |
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... | |
Public Member Functions | |
virtual std::string | asString () const |
Return the HTTPRequest as a string. More... | |
HTTPRequestLine & | getRequestLine () |
Get the HTTPRequestLine. More... | |
virtual ParseResult | parse (VirtualReadBuffer &data) |
Read the HTTPRequest from the socket. More... | |
virtual ParseResult | parseBody (VirtualReadBuffer &data) |
Parse a body and resturn it as a single string. More... | |
virtual common::SystemError | send (BaseSocket *socket) |
Send this HTTPMessage to the socket. More... | |
void | setRequestLine (const HTTPRequestLine &req) |
Set the HTTPRequestLine. More... | |
common::SystemError | write (BaseSocket *socket) const |
Write the HTTPRequest to the socket. More... | |
![]() | |
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... | |
void | setBody (const std::string &body) |
Set the body. More... | |
Static Public Member Functions | |
static bool | methodAllowsBody (Method method) |
Return true if the Method allows a body in the message. More... | |
static std::string | methodAsString (Method method) |
Translate a Method enum to a human readable string. More... | |
static Method | methodFromString (const std::string &s) |
Translate an uppercase string ('GET', "POST', ..) into a Method enum. More... | |
![]() | |
static std::string | getParseResultAsString (ParseError error) |
Return the string description of a ParseError. More... | |
Protected Attributes | |
HTTPRequestLine | request_line_ |
The HTTPRequestLine of the HTTPRequest. More... | |
![]() | |
common::Bytes | body_ |
The message body (if any). More... | |
std::map< std::string, std::string > | headers_ |
The message headers. More... | |
Additional Inherited Members | |
![]() | |
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... | |
![]() | |
ParseResult | eatCRLF (VirtualReadBuffer &data) |
Consume a CR LF sequence only when it is there. 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 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... | |
HTTPRequest class represents a HTTP request.
A HTTPRequest contains
Definition at line 42 of file httprequest.hpp.
The HTTP request method.
Enumerator | |
---|---|
meINVALID | The parser was presented an invalid method. |
Definition at line 48 of file httprequest.hpp.
|
virtual |
Return the HTTPRequest as a string.
Implements dodo::network::protocol::http::HTTPFragment.
Definition at line 86 of file httprequest.cpp.
References dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::asString(), dodo::common::Bytes::asString(), dodo::network::protocol::http::HTTPMessage::body_, dodo::network::protocol::http::HTTPMessage::charCR, dodo::network::protocol::http::HTTPMessage::charLF, dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::getMethod(), dodo::common::Bytes::getSize(), dodo::network::protocol::http::HTTPMessage::headers_, methodAllowsBody(), and request_line_.
Referenced by write().
|
inline |
Get the HTTPRequestLine.
Definition at line 170 of file httprequest.hpp.
References request_line_.
|
static |
Return true if the Method allows a body in the message.
method | the Method to check. |
Definition at line 60 of file httprequest.cpp.
Referenced by asString(), and send().
|
static |
Translate a Method enum to a human readable string.
method | The method to translate. |
Definition at line 32 of file httprequest.cpp.
Referenced by dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::asString().
|
static |
Translate an uppercase string ('GET', "POST', ..) into a Method enum.
s | The uppercase string to translate. |
Definition at line 47 of file httprequest.cpp.
References meINVALID.
Referenced by dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::parse().
|
virtual |
Read the HTTPRequest from the socket.
data | The VirtualReadBuffer to read from. |
Implements dodo::network::protocol::http::HTTPFragment.
Definition at line 114 of file httprequest.cpp.
References dodo::network::protocol::http::HTTPMessage::charCR, dodo::network::protocol::http::HTTPMessage::charLF, 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(), dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::parse(), parseBody(), dodo::network::protocol::http::HTTPMessage::parseHeaders(), dodo::network::protocol::http::HTTPFragment::peOk, request_line_, and dodo::network::protocol::http::HTTPFragment::ParseResult::setSystemError().
|
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. |
Implements dodo::network::protocol::http::HTTPMessage.
Definition at line 139 of file httprequest.cpp.
References dodo::common::Bytes::append(), dodo::network::protocol::http::HTTPMessage::body_, dodo::common::SystemError::ecOK, dodo::network::VirtualReadBuffer::get(), dodo::network::protocol::http::HTTPMessage::getHeaderValue(), dodo::network::VirtualReadBuffer::next(), dodo::network::protocol::http::HTTPFragment::ParseResult::ok(), dodo::network::protocol::http::HTTPMessage::parseChunkedBody(), dodo::network::protocol::http::HTTPFragment::peInvalidTransferEncoding, dodo::network::protocol::http::HTTPFragment::peUnexpectedBody, and dodo::network::protocol::http::HTTPFragment::ParseResult::setSystemError().
Referenced by parse().
|
virtual |
Send this HTTPMessage to the socket.
socket | The socket to write to. |
Implements dodo::network::protocol::http::HTTPMessage.
Definition at line 99 of file httprequest.cpp.
References dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::asString(), dodo::network::protocol::http::HTTPMessage::body_, dodo::network::protocol::http::HTTPMessage::charCR, dodo::network::protocol::http::HTTPMessage::charLF, dodo::common::Bytes::getArray(), dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::getMethod(), dodo::common::Bytes::getSize(), dodo::network::protocol::http::HTTPMessage::headers_, methodAllowsBody(), dodo::common::SystemError::ok(), request_line_, and dodo::network::BaseSocket::send().
void dodo::network::protocol::http::HTTPRequest::setRequestLine | ( | const HTTPRequestLine & | req | ) |
Set the HTTPRequestLine.
req | The HTTPRequestLine to set. |
common::SystemError dodo::network::protocol::http::HTTPRequest::write | ( | BaseSocket * | socket | ) | const |
Write the HTTPRequest to the socket.
socket | The BaseSocket to write to. |
Definition at line 249 of file httprequest.cpp.
References asString(), and dodo::network::BaseSocket::send().
|
protected |
The HTTPRequestLine of the HTTPRequest.
Definition at line 208 of file httprequest.hpp.
Referenced by asString(), getRequestLine(), parse(), and send().