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 if ( parseResult.
ok() ) {
39 if ( parseResult.
ok() )
return parseBody( data );
else return parseResult;
44 if ( !parseResult.
ok() )
return parseResult;
48 if ( parseResult.
ok() ) {
60 unsigned long content_length = 0;
62 if ( content_length > 0 )
return true;
64 std::string transfer_encoding;
65 std::string connection;
66 if (
getHeaderValue(
"transfer-encoding", transfer_encoding ) )
if ( transfer_encoding ==
"chunked" )
return true;
67 if (
getHeaderValue(
"connection", connection ) )
if ( connection ==
"close" )
return true;
92 if ( !rc.
ok() )
return rc;
100 std::stringstream ss;
113 parseResult = version_.parse( data );
114 if ( parseResult.
ok()) {
116 if ( !parseResult.
ok() )
return parseResult;
119 if ( parseResult.
ok() ) {
120 http_code_ =
static_cast<HTTPCode>(i);
122 while ( parseResult.
ok() && data.
get() !=
charCR ) {
124 if ( ! parseResult.
ok() )
return parseResult;
130 if ( ! parseResult.
ok() )
return parseResult;
136 if ( parseResult.
ok() )
return parseResult;
145 size_t content_length;
147 for (
size_t i = 0; i < content_length; i++ ) {
149 if ( i < content_length -1 ) {
151 if ( ! parseResult.
ok() )
return parseResult;
155 std::string transfer_encoding;
156 std::string connection_close;
158 if ( transfer_encoding ==
"chunked" ) {
160 if ( ! parseResult.
ok() )
return parseResult;
163 if ( connection_close ==
"close" ) {
165 while ( parseResult.
ok() ) {
179 case hcContinue :
return "CONTINUE";
180 case hcSwitchingProtocols :
return "SWITCHING PROTOCOLS";
181 case hcProcessing :
return "PROCESSING";
182 case hcEarlyHints :
return "EARLY HINTS";
184 case hcOK :
return "OK";
185 case hcCreated :
return "CREATED";
186 case hcAccepted :
return "ACCEPTED";
187 case hcNonAuthoritiveInformation :
return "NON-AUTHORITATIVE INFORMATION";
188 case hcNoContent :
return "NO CONTENT";
189 case hcResetContent :
return "RESET CONTENT";
190 case hcPartialContent :
return "PARTIAL CONTENT";
191 case hcMultiStatus :
return "MULTI-STATUS";
192 case hcAlreadyReported :
return "ALREADY REPORTED";
193 case hcIMUsed :
return "IM USED";
195 case hcMultipleChoices :
return "MULTIPLE CHOICES";
196 case hcMovedPermanently :
return "MOVED PERMANENTLY";
197 case hcFound :
return "FOUND";
198 case hcSeeOther :
return "SEE OTHER";
199 case hcNotModified :
return "NOT MODIFIED";
200 case hcUseProxy :
return "USE PROXY";
201 case hcSwitchProxy :
return "SWITCH PROXY";
202 case hcTemporaryRedirect :
return "TEMPORARY REDIRECT";
203 case hcPermanentRedirect :
return "PERMANENT REDIRECT";
205 case hcBadRequest :
return "BAD REQUEST";
206 case hcUnAuthorized :
return "UNAUTHORIZED";
207 case hcPaymentRequired :
return "PAYMENT REQUIRED";
208 case hcForbidden :
return "FORBIDDEN";
209 case hcNotFound :
return "NOT FOUND";
210 case hcMethodNotAllowed :
return "METHOD NOT ALLOWED";
211 case hcNotAcceptable :
return "NOT ACCEPTABLE";
212 case hcProxyAuthenticationRequired :
return "PROXY AUTHENTICATION REQUIRED";
213 case hcRequestTimeout :
return "REQUEST TIMEOUT";
214 case hcConflict :
return "CONFLICT";
215 case hcGone :
return "GONE";
216 case hcLengthRequired :
return "LENGTH REQUIRED";
217 case hcPreconditionFailed :
return "PRECONDITION FAILED";
218 case hcPayloadTooLarge :
return "PAYLOAD TOO LARGE";
219 case hcURITooLong :
return "URI TOO LONG";
220 case hcUnsupportedMediaType :
return "UNSUPPORTED MEDIA TYPE";
221 case hcRangeNotSatisfiable :
return "RANGE NOT SATISFIABLE";
222 case hcExpectationFailed :
return "EXPECTATION FAILED";
223 case hcIAmATeapot :
return "I'M A TEAPOT";
224 case hcMisdirectRequest :
return "MISDIRECTED REQUEST";
225 case hcUnporessableEntity :
return "UNPROCESSABLE ENTITY";
226 case hcLocked :
return "LOCKED";
227 case hcFailedDependency :
return "FAILED DEPENDENCY";
228 case hcUpgradeRequired :
return "UPGRADE REQUIRED";
229 case hcPreconditionRequired :
return "PRECONDITION REQUIRED";
230 case hcTooManyRequests :
return "TOO MANY REQUESTS";
231 case hcRequestHeaderFieldsTooLarge :
return "REQUEST HEADER FIELDS TOO LARGE";
232 case hcUnavailableForLegalReasons :
return "UNAVAILABLE FOR LEGAL REASONS";
234 case hcInternalServerError :
return "INTERNAL SERVER ERROR";
235 case hcNotImplemented :
return "NOT IMPLEMENTED";
236 case hcBadGateway :
return "BAD GATEWAY";
237 case hcServiceUnavailable :
return "SERVICE UNAVAILABLE";
238 case hcGatewayTimeout :
return "GATEWAY TIMEOUT";
239 case hcHTTPVersionNotSupported :
return "HTTP VERSION NOT SUPPORTED";
240 case hcVariantAlsoNegotiates :
return "VARIANT ALSO NEGOTIATES";
241 case hcInsufficientStorage :
return "INSUFFICIENT STORAGE";
242 case hcLoopDetected :
return "LOOP DETECTED";
243 case hcNotExtended :
return "NOT EXTENDED";
244 case hcNetworkAuthenticationRequired :
return "NETWORK AUTHENTICATION REQUIRED";
246 case hcThisIsFine :
return "THIS IS FINE";
247 case hcPageExpired :
return "PAGE EXPIRED";
248 case hcMethodFailure :
return "METHOD FAILURE";
249 case hcBlockedByWindowsParentalControls :
return "BLOCKED BY WINDOWS PARENTAL CONTROLS";
250 case hcInvalidToken :
return "INVALID TOKEN";
252 case hcBandwidthLimitExceeded :
return "BANDWIDTH LIMIT EXCEEDED";
253 case hcInvalidSSLCertificate :
return "INVALID SSL CERTIFICATE";
255 case hcNetworkReadTimeoutError :
return "NETWORK READ TIMEOUT ERROR";
257 case hcLoginTimeout :
return "LOGIN TIME-OUT";
258 case hcRetryWith :
return "RETRY WITH";
260 case hcNoRepsonse :
return "NO RESPONSE";
261 case hcRequestHeaderTooLarge :
return "REQUEST HEADER TOO LARGE";
262 case hcSSLCertificateError :
return "SSL CERTIFICATE ERROR";
263 case hcSSLCertificateRequired :
return "SSL CERTIFICATE REQUIRED";
264 case hcHHTPRequestSentToHTTPSPort :
return "HTTP REQUEST SENT TO HTTPS PORT";
265 case hcClientClosedrequest :
return "CLIENT CLOSED REQUEST";
267 case hcUnknownError :
return "UNKNOWN ERROR";
268 case hcWebServerIsDown :
return "WEB SERVER IS DOWN";
269 case hcConnectionTimedOut :
return "CONNECTION TIMED OUT";
270 case hcOriginIsUnreachable :
return "ORIGIN IS UNREACHABLE";
271 case hcATimeoutOccured :
return "A TIMEOUT OCCURRED";
272 case hcSSLHandshakeFailed :
return "SSL HANDSHAKE FAILED";
273 case hcRailgunError :
return "RAILGUN ERROR";
274 case hcOriginDNSError :
return "ORIGIN DNS ERROR";
276 default :
return "UNKNOWN HTTP CODE";
ParseResult parseChunkedBody(VirtualReadBuffer &data)
Parse a chunked body (transfer-encoding: chunked) and resturn it as a single string.
virtual std::string asString() const
Convert the HTTPResponseLine to a HTTP string.
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.
HTTPCode http_code_
The http_code.
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.
static const char charSP
Space character.
static ParseResult parseUInt(VirtualReadBuffer &data, unsigned int &value)
Parse an unsigned integer.
bool ok() const
Returns true when this->errorcode_ == ecOK.
HTTPVersion version_
The HTTPVersion.
virtual ParseResult parse(VirtualReadBuffer &buffer)
Read a complete HTTPFragment from a VirtualReadBuffer.
void append(const Bytes &src)
Append another Bytes.
A C++ platform interface to lean Linux services tailored for containerized deployment.
HTTPResponseLine response_line_
The HTTPResponseLIne of the HTTPResponse.
@ peInvalidTransferEncoding
The specified transfer-encoding header is invalid.
@ peUnexpectedBody
A message body is present but should not be there.
static std::string HTTPCodeAsString(HTTPCode code)
Return the (upper case) string representation of the http error code.
Used to convey parsing succces.
bool eof() const
Test if systemError == common::SystemError::ecEAGAIN
virtual std::string asString() const
Return the HTTPResponse as a string.
virtual ParseResult parseBody(VirtualReadBuffer &data)
Parse a body and resturn it as a single string.
@ ecOK
0 Not an error, success
std::string asString() const
Convert to a std::string.
static common::SystemError eatSpace(VirtualReadBuffer &data)
Call buffer.next() as long as buffer.get() is whitespace ( charSP or charHT).
ParseResult parseHeaders(VirtualReadBuffer &data)
Parse a header section and update headers_.
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).
virtual common::SystemError send(BaseSocket *socket)
Send this HTTPMessage to the socket.
size_t getSize() const
Return the array size.
virtual ParseResult parse(VirtualReadBuffer &data)
Read a complete HTTPFragment from a VirtualReadBuffer.
bool hasBody() const
Return true when the response has a body.
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.