dodo
0.0.1
A C++ library to create containerized Linux services
|
Go to the documentation of this file.
30 namespace network::protocol::http {
34 case meGET :
return "GET";
35 case meHEAD :
return "HEAD";
36 case mePOST :
return "POST";
37 case mePUT :
return "PUT";
38 case mePATCH :
return "PATCH";
39 case meDELETE :
return "DELETE";
40 case meCONNECT :
return "CONNECT";
41 case meOPTIONS :
return "OPTIONS";
42 case meTRACE :
return "TRACE";
43 default :
return "INVALID_METHOD";
48 if ( s ==
"GET" )
return meGET;
49 else if ( s ==
"HEAD" )
return meHEAD;
50 else if ( s ==
"POST" )
return mePOST;
51 else if ( s ==
"PUT" )
return mePUT;
52 else if ( s ==
"PATCH" )
return mePATCH;
53 else if ( s ==
"DELETE" )
return meDELETE;
54 else if ( s ==
"CONNECT" )
return meCONNECT;
55 else if ( s ==
"OPTIONS" )
return meOPTIONS;
56 else if ( s ==
"TRACE" )
return meTRACE;
73 default :
return false;
100 std::stringstream ss;
107 if ( !rc.
ok() )
return rc;
116 if ( parseResult.
ok() ) {
121 if ( parseResult.
ok() )
return parseBody( data );
else return parseResult;
126 if ( !parseResult.
ok() )
return parseResult;
129 if ( parseResult.
ok() ) {
132 else return parseResult;
141 size_t content_length;
143 for (
size_t i = 0; i < content_length; i++ ) {
145 if ( i < content_length -1 ) {
147 if ( ! parseResult.
ok() )
return parseResult;
151 std::string transfer_encoding;
153 if ( transfer_encoding ==
"chunked" ) {
155 if ( ! parseResult.
ok() )
return parseResult;
174 ParseState state = psMethodStart;
175 std::string method_string =
"";
176 std::string version_string =
"";
179 while ( state != psDone && state != psError && parseResult.
ok() ) {
183 if ( std::isspace( data.
get() ) ) {
186 method_string += data.
get();
188 if ( ! parseResult.
ok() )
return parseResult;
194 if ( ! parseResult.
ok() )
return parseResult;
195 state = psRequestURIStart;
198 case psRequestURIStart:
199 if ( std::isspace( data.
get() ) ) {
200 state = psRequestURIEnd;
202 request_uri_ += data.
get();
204 if ( ! parseResult.
ok() )
return parseResult;
208 case psRequestURIEnd:
209 parseResult = http_version_.parse( data );
210 if ( parseResult.
ok() ) {
211 state = psHTTPVersionCR;
213 }
else return parseResult;
216 case psHTTPVersionCR:
218 state = psHTTPVersionLF;
220 if ( !parseResult.
ok() )
return parseResult;
226 case psHTTPVersionLF:
230 if ( !parseResult.
ok() )
return parseResult;
237 "HTTPRequest::HTTPRequestLine::parse unhandled state " <<
242 if ( state == psDone ) {
virtual ParseResult parse(VirtualReadBuffer &data)
Read the HTTPRequest from the socket.
ParseResult parseChunkedBody(VirtualReadBuffer &data)
Parse a chunked body (transfer-encoding: chunked) and resturn it as a single string.
@ meINVALID
The parser was presented an invalid method.
HTTPRequest::Method method_
The request method.
static const char charCR
CR character.
Octet * getArray() const
Return the array.
static const char charLF
LF character.
bool ok() const
Test if parseError == peOk && systemError == common::SystemError::ecOK.
Interface to read individual bytes whilst the implementation can read from an actual source (such as ...
void setSystemError(common::SystemError se)
Set the systemError.
bool getHeaderValue(const std::string &key, std::string &value) const
Get a header value as a string into value.
std::map< std::string, std::string > headers_
The message headers.
static Method methodFromString(const std::string &s)
Translate an uppercase string ('GET', "POST', ..) into a Method enum.
virtual std::string asString() const
Convert the HTTPRequestLine to a HTTP string.
virtual std::string asString() const
Convert to string as 'HTTP/major_.minor_'.
virtual common::SystemError next()=0
Move to the next char from the VirtualReadBuffer.
virtual char get() const =0
Get the current char from VirtualReadBuffer.
HTTPRequest::Method getMethod() const
Get the HTTPRequest::Method.
static const char charSP
Space character.
bool ok() const
Returns true when this->errorcode_ == ecOK.
virtual common::SystemError send(BaseSocket *socket)
Send this HTTPMessage to the socket.
Method
The HTTP request method.
void append(const Bytes &src)
Append another Bytes.
A C++ platform interface to lean Linux services tailored for containerized deployment.
@ peInvalidRequestLine
The request line is invalid.
@ peInvalidTransferEncoding
The specified transfer-encoding header is invalid.
@ peUnexpectedBody
A message body is present but should not be there.
Used to convey parsing succces.
#define throw_SystemException(what, errno)
Throws an Exception with errno, passes FILE and LINE to constructor.
static bool methodAllowsBody(Method method)
Return true if the Method allows a body in the message.
bool eof() const
Test if systemError == common::SystemError::ecEAGAIN
@ ecOK
0 Not an error, success
std::string asString() const
Convert to a std::string.
std::string request_uri_
The request 'uri'.
static std::string methodAsString(Method method)
Translate a Method enum to a human readable string.
static common::SystemError eatSpace(VirtualReadBuffer &data)
Call buffer.next() as long as buffer.get() is whitespace ( charSP or charHT).
@ peInvalidMethod
An invalid method was specified in the request line.
ParseResult parseHeaders(VirtualReadBuffer &data)
Parse a header section and update headers_.
Helper class to write strings in stream format, eg.
Linux system error primitive to provide a consistent interface to Linux error codes.
@ peExpectCRLF
A CR was not followed by an LF.
common::Bytes body_
The message body (if any).
common::SystemError write(BaseSocket *socket) const
Write the HTTPRequest to the socket.
HTTPRequestLine request_line_
The HTTPRequestLine of the HTTPRequest.
size_t getSize() const
Return the array size.
HTTPVersion http_version_
The HTTP version.
virtual std::string asString() const
Return the HTTPRequest as a string.
virtual ParseResult parse(VirtualReadBuffer &data)
Parses a HTTPMessage.
virtual ParseResult parseBody(VirtualReadBuffer &data)
Parse a body and resturn it as a single string.
Interface to and common implementation of concrete sockets (Socket, TLSSocket).
virtual common::SystemError send(const void *buf, ssize_t len, bool more=false)=0
Send bytes on the socket.