dodo  0.0.1
A C++ library to create containerized Linux services
httpresponse.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 
19 /**
20  * @file httpresponse.hpp
21  * Defines the dodo::network::protocol::http::HTTPResponse class.
22  */
23 
24 #ifndef dodo_network_protocol_http_httpresponse_hpp
25 #define dodo_network_protocol_http_httpresponse_hpp
26 
29 #include <string>
30 
31 namespace dodo {
32 
33  namespace network::protocol::http {
34 
35  /**
36  * HTTPResponse class. Contains
37  * - A HTTPResponseLine
38  * - An optional body
39  */
40  class HTTPResponse : public HTTPMessage {
41  public:
42 
43  /**
44  * @see https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
45  */
46  enum HTTPCode : unsigned int {
47  // 1xx Informational response
48  hcContinue = 100,
49  hcSwitchingProtocols = 101,
50  hcProcessing = 102,
51  hcEarlyHints = 103,
52 
53  // 2xx Success
54  hcOK = 200,
55  hcCreated = 201,
56  hcAccepted = 202,
57  hcNonAuthoritiveInformation = 203,
58  hcNoContent = 204,
59  hcResetContent = 205,
60  hcPartialContent = 206,
61  hcMultiStatus = 207,
62  hcAlreadyReported = 208,
63  hcIMUsed = 226,
64 
65  // 3xx Redirection
66  hcMultipleChoices = 300,
67  hcMovedPermanently = 301,
68  hcFound = 302,
69  hcSeeOther = 303,
70  hcNotModified = 304,
71  hcUseProxy = 305,
72  hcSwitchProxy = 306,
73  hcTemporaryRedirect = 307,
74  hcPermanentRedirect = 308,
75 
76  // 4xx Client errors
77  hcBadRequest = 400,
78  hcUnAuthorized = 401,
79  hcPaymentRequired = 402,
80  hcForbidden = 403,
81  hcNotFound = 404,
82  hcMethodNotAllowed = 405,
83  hcNotAcceptable = 406,
84  hcProxyAuthenticationRequired = 407,
85  hcRequestTimeout = 408,
86  hcConflict = 409,
87  hcGone = 410,
88  hcLengthRequired = 411,
89  hcPreconditionFailed = 412,
90  hcPayloadTooLarge = 413,
91  hcURITooLong = 414,
92  hcUnsupportedMediaType = 415,
93  hcRangeNotSatisfiable = 416,
94  hcExpectationFailed = 417,
95  hcIAmATeapot = 418,
96  hcMisdirectRequest = 421,
97  hcUnporessableEntity = 422,
98  hcLocked = 423,
99  hcFailedDependency = 424,
100  hcUpgradeRequired = 426,
101  hcPreconditionRequired = 428,
102  hcTooManyRequests = 429,
103  hcRequestHeaderFieldsTooLarge = 431,
104  hcUnavailableForLegalReasons = 451,
105 
106  // 5xx Server errors
107  hcInternalServerError = 500,
108  hcNotImplemented = 501,
109  hcBadGateway = 502,
110  hcServiceUnavailable = 503,
111  hcGatewayTimeout = 504,
112  hcHTTPVersionNotSupported = 505,
113  hcVariantAlsoNegotiates = 506,
114  hcInsufficientStorage = 507,
115  hcLoopDetected = 508,
116  hcNotExtended = 510,
117  hcNetworkAuthenticationRequired = 511,
118 
119  // Unofficial codes
120  hcCheckpoint = 103,
121  hcThisIsFine = 218,
122  hcPageExpired = 419,
123  hcMethodFailure = 420,
124  hcEnhanceYourCalm = 420,
125  hcBlockedByWindowsParentalControls = 450,
126  hcInvalidToken = 498,
127  hcTokenRequired = 499,
128  hcBandwidthLimitExceeded = 509,
129  hcInvalidSSLCertificate = 526,
130  hcSiteIsFrozen = 530,
131  hcNetworkReadTimeoutError = 598,
132 
133  // Microsoft Internet Information Services
134  hcLoginTimeout = 440,
135  hcRetryWith = 449,
136  hcRedirect = 451,
137 
138  // nginx
139  hcNoRepsonse = 444,
140  hcRequestHeaderTooLarge = 494,
141  hcSSLCertificateError = 495,
142  hcSSLCertificateRequired = 496,
143  hcHHTPRequestSentToHTTPSPort = 497,
144  hcClientClosedrequest = 499,
145 
146  // Cloudflare
147  hcUnknownError = 520,
148  hcWebServerIsDown = 521,
149  hcConnectionTimedOut = 522,
150  hcOriginIsUnreachable = 523,
151  hcATimeoutOccured = 524,
152  hcSSLHandshakeFailed = 525,
153  hcRailgunError = 527,
154  hcOriginDNSError = 530,
155 
156  };
157 
158  /**
159  * A HTTP response line.
160  */
162  public:
163 
164  /**
165  * Default constructor.
166  */
168 
169  /**
170  * Explicit consructor.
171  * @param version The version to set.
172  * @param http_code The HTTP code to set.
173  */
174  explicit HTTPResponseLine( const HTTPVersion &version, HTTPCode http_code ) : version_(version), http_code_(http_code) {};
175 
176  /**
177  * Convert the HTTPResponseLine to a HTTP string.
178  * @return The HTTPResponseLine as a string.
179  */
180  virtual std::string asString() const;
181 
182  virtual ParseResult parse( VirtualReadBuffer& data );
183 
184  /**
185  * The HTTPVersion of the response line.
186  * @return The HTTPVersion
187  */
188  const HTTPVersion& getHTTPVersion() const { return version_; };
189 
190  /**
191  * The HTTP return code of the HTTPResponse.
192  * @return The HTTPCode.
193  */
194  HTTPCode getHTTPCode() const { return http_code_; };
195 
196  private:
197  /** The HTTPVersion. */
199  /** The http_code */
201  };
202 
203  /**
204  * Return a const reference to the HTTPResponseLine of this HTTPResponse.
205  * @return a const reference to the HTTPResponseLine of this HTTPResponse.
206  */
207  const HTTPResponseLine& getResponseLine() const { return response_line_; };
208 
209  virtual ParseResult parse( VirtualReadBuffer& buffer );
210 
211  virtual ParseResult parseBody( VirtualReadBuffer &data );
212 
213  virtual common::SystemError send( BaseSocket* socket );
214 
215  /**
216  * Return true when the response has a body.
217  * @return true when the response has a body.
218  */
219  bool hasBody() const;
220 
221  /**
222  * Return the HTTPResponse as a string.
223  * @return the HTTP response as a string.
224  */
225  virtual std::string asString() const;
226 
227  /**
228  * Return the (upper case) string representation of the http error code.
229  * @param code The HTTP error code.
230  * @return The string representation.
231  */
232  static std::string HTTPCodeAsString( HTTPCode code );
233 
234  protected:
235  /** The HTTPResponseLIne of the HTTPResponse. */
237 
238  };
239 
240  }
241 
242 }
243 
244 #endif
httpmessage.hpp
dodo::network::protocol::http::HTTPResponse::HTTPResponseLine::asString
virtual std::string asString() const
Convert the HTTPResponseLine to a HTTP string.
Definition: httpresponse.cpp:99
dodo::network::VirtualReadBuffer
Interface to read individual bytes whilst the implementation can read from an actual source (such as ...
Definition: socketreadbuffer.hpp:44
dodo::network::protocol::http::HTTPResponse::HTTPResponseLine::http_code_
HTTPCode http_code_
The http_code.
Definition: httpresponse.hpp:200
dodo::network::protocol::http::HTTPVersion
HHTP version comprising a major and minor number, convertable from and to string as in HTTP requests.
Definition: httpversion.hpp:38
dodo::network::protocol::http::HTTPFragment
Generic HTTP fragment, either a complete (such as HTTPRequest) or incomplete HTTP fragment (ssuch as ...
Definition: httpfragment.hpp:45
dodo::network::protocol::http::HTTPResponse::HTTPResponseLine::version_
HTTPVersion version_
The HTTPVersion.
Definition: httpresponse.hpp:194
dodo::network::protocol::http::HTTPResponse::parse
virtual ParseResult parse(VirtualReadBuffer &buffer)
Read a complete HTTPFragment from a VirtualReadBuffer.
Definition: httpresponse.cpp:32
httpversion.hpp
dodo::network::protocol::http::HTTPResponse::HTTPCode
HTTPCode
Definition: httpresponse.hpp:46
dodo::network::protocol::http::HTTPResponse::HTTPResponseLine::getHTTPCode
HTTPCode getHTTPCode() const
The HTTP return code of the HTTPResponse.
Definition: httpresponse.hpp:194
dodo::network::protocol::http::HTTPResponse::HTTPResponseLine::getHTTPVersion
const HTTPVersion & getHTTPVersion() const
The HTTPVersion of the response line.
Definition: httpresponse.hpp:188
dodo::network::protocol::http::HTTPResponse
HTTPResponse class.
Definition: httpresponse.hpp:40
dodo
A C++ platform interface to lean Linux services tailored for containerized deployment.
Definition: application.hpp:29
dodo::network::protocol::http::HTTPResponse::response_line_
HTTPResponseLine response_line_
The HTTPResponseLIne of the HTTPResponse.
Definition: httpresponse.hpp:236
dodo::network::protocol::http::HTTPResponse::HTTPCodeAsString
static std::string HTTPCodeAsString(HTTPCode code)
Return the (upper case) string representation of the http error code.
Definition: httpresponse.cpp:176
dodo::network::protocol::http::HTTPFragment::ParseResult
Used to convey parsing succces.
Definition: httpfragment.hpp:75
dodo::network::protocol::http::HTTPResponse::asString
virtual std::string asString() const
Return the HTTPResponse as a string.
Definition: httpresponse.cpp:71
dodo::network::protocol::http::HTTPResponse::parseBody
virtual ParseResult parseBody(VirtualReadBuffer &data)
Parse a body and resturn it as a single string.
Definition: httpresponse.cpp:143
dodo::network::protocol::http::HTTPMessage
Generic HTTPMessage, parent to HTTPRequest and HTTPResponse.
Definition: httpmessage.hpp:40
dodo::network::protocol::http::HTTPResponse::getResponseLine
const HTTPResponseLine & getResponseLine() const
Return a const reference to the HTTPResponseLine of this HTTPResponse.
Definition: httpresponse.hpp:207
dodo::network::protocol::http::HTTPResponse::HTTPResponseLine
A HTTP response line.
Definition: httpresponse.hpp:161
dodo::common::SystemError
Linux system error primitive to provide a consistent interface to Linux error codes.
Definition: systemerror.hpp:53
dodo::network::protocol::http::HTTPResponse::HTTPResponseLine::HTTPResponseLine
HTTPResponseLine(const HTTPVersion &version, HTTPCode http_code)
Explicit consructor.
Definition: httpresponse.hpp:174
dodo::network::protocol::http::HTTPResponse::send
virtual common::SystemError send(BaseSocket *socket)
Send this HTTPMessage to the socket.
Definition: httpresponse.cpp:84
dodo::network::protocol::http::HTTPResponse::HTTPResponseLine::parse
virtual ParseResult parse(VirtualReadBuffer &data)
Read a complete HTTPFragment from a VirtualReadBuffer.
Definition: httpresponse.cpp:111
dodo::network::protocol::http::HTTPResponse::hasBody
bool hasBody() const
Return true when the response has a body.
Definition: httpresponse.cpp:58
dodo::network::BaseSocket
Interface to and common implementation of concrete sockets (Socket, TLSSocket).
Definition: basesocket.hpp:36
dodo::network::protocol::http::HTTPResponse::HTTPResponseLine::HTTPResponseLine
HTTPResponseLine()
Default constructor.
Definition: httpresponse.hpp:167