dodo  0.0.1
A C++ library to create containerized Linux services
httprequest.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 httprequest.hpp
21  * Defines the dodo::network::protocol::http::HTTPRequest class.
22  */
23 
24 #ifndef dodo_network_protocol_http_httprequest_hpp
25 #define dodo_network_protocol_http_httprequest_hpp
26 
29 #include <string>
30 
31 namespace dodo {
32 
33  namespace network::protocol::http {
34 
35  /**
36  * HTTPRequest class represents a HTTP request. A HTTPRequest contains
37  * - A HTTPRequestLine
38  * - An optional body of type common::Bytes.
39  *
40  * @include examples/http-client/http-client.cpp
41  */
42  class HTTPRequest : public HTTPMessage {
43  public:
44 
45  /**
46  * The HTTP request method.
47  */
48  enum Method {
49  meGET,
50  meHEAD,
51  mePOST,
52  mePUT,
53  mePATCH,
54  meDELETE,
55  meCONNECT,
56  meOPTIONS,
57  meTRACE,
58  meINVALID, /**< The parser was presented an invalid method. */
59  };
60 
61  /**
62  * HTTPRequestLine class. Contains
63  * - A request method HTTPRequest::Method.
64  * - A request 'uri' (it is not an uri buth rather a path that could be an URI).
65  * - A HTTP version
66  */
67  class HTTPRequestLine : public HTTPFragment {
68  public:
69 
70  /**
71  * Construct default HTTPRequestLine.
72  */
74  request_uri_("\"*\""),
76  {};
77 
78  /**
79  * Convert the HTTPRequestLine to a HTTP string.
80  * @return The HTTP string.
81  */
82  virtual std::string asString() const;
83 
84  /**
85  * Parses a HTTPMessage
86  * @param data The VirtualReadBuffer to read from.
87  * @return The ParseResult.
88  */
89  virtual ParseResult parse( VirtualReadBuffer &data );
90 
91  /**
92  * Get the HTTPRequest::Method.
93  * @return the HTTPRequest::Method.
94  */
95  HTTPRequest::Method getMethod() const { return method_; };
96 
97  /**
98  * Set the HTTPRequest::Method.
99  * @param method The HTTPRequest::Method.
100  */
101  void setMethod( HTTPRequest::Method method ) { method_ = method; };
102 
103  /**
104  * Get the request uri.
105  * @return the request uri.
106  */
107  std::string getRequestURI() const { return request_uri_; };
108 
109  /**
110  * Set the request uri.
111  * @param uri The request uri.
112  */
113  void setRequestURI( const std::string &uri ) { request_uri_ = uri; };
114 
115  /**
116  * Get the HTTP version.
117  * @return The HTTP version.
118  */
120 
121  /**
122  * Set the HTTP version.
123  * @param version The HTTP version.
124  */
125  void setHTTPVersion( const HTTPVersion &version ) { http_version_ = version; };
126 
127  protected:
128 
129  /**
130  * The request method.
131  */
133 
134  /**
135  * The request 'uri'.
136  */
137  std::string request_uri_;
138 
139  /**
140  * The HTTP version.
141  */
143  };
144 
145  HTTPRequest() : request_line_() {};
146 
147  /**
148  * Read the HTTPRequest from the socket.
149  * @param data The VirtualReadBuffer to read from.
150  * @return The ParseResult.
151  */
152  virtual ParseResult parse( VirtualReadBuffer& data );
153 
154  virtual ParseResult parseBody( VirtualReadBuffer &data );
155 
156  virtual common::SystemError send( BaseSocket* socket );
157 
158  /**
159  * Write the HTTPRequest to the socket.
160  * @param socket The BaseSocket to write to.
161  * @return The SystemError.
162  */
163  common::SystemError write( BaseSocket* socket ) const;
164 
165 
166  /**
167  * Get the HTTPRequestLine.
168  * @return The HTTPRequestLine.
169  */
171 
172 
173  /**
174  * Set the HTTPRequestLine.
175  * @param req The HTTPRequestLine to set.
176  */
177  void setRequestLine( const HTTPRequestLine& req );
178 
179  /**
180  * Return the HTTPRequest as a string.
181  * @return the HTTPRequest as a string.
182  */
183  virtual std::string asString() const;
184 
185  /**
186  * Translate a Method enum to a human readable string.
187  * @param method The method to translate.
188  * @return the string.
189  */
190  static std::string methodAsString( Method method );
191 
192  /**
193  * Translate an uppercase string ('GET', "POST', ..) into a Method enum.
194  * @param s The uppercase string to translate.
195  * @return the Method (and meINVALID if the string is not a Method).
196  */
197  static Method methodFromString( const std::string& s );
198 
199  /**
200  * Return true if the Method allows a body in the message.
201  * @param method the Method to check.
202  * @return if the Method allows a body.
203  */
204  static bool methodAllowsBody( Method method );
205 
206  protected:
207  /** The HTTPRequestLine of the HTTPRequest. */
209 
210  };
211 
212  }
213 
214 }
215 
216 #endif
dodo::network::protocol::http::HTTPRequest::parse
virtual ParseResult parse(VirtualReadBuffer &data)
Read the HTTPRequest from the socket.
Definition: httprequest.cpp:114
dodo::network::protocol::http::HTTPRequest::meINVALID
@ meINVALID
The parser was presented an invalid method.
Definition: httprequest.hpp:58
dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::getHTTPVersion
HTTPVersion getHTTPVersion() const
Get the HTTP version.
Definition: httprequest.hpp:119
dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::method_
HTTPRequest::Method method_
The request method.
Definition: httprequest.hpp:125
httpmessage.hpp
dodo::network::protocol::http::HTTPRequest
HTTPRequest class represents a HTTP request.
Definition: httprequest.hpp:42
dodo::network::protocol::http::HTTPRequest::HTTPRequestLine
HTTPRequestLine class.
Definition: httprequest.hpp:67
dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::getRequestURI
std::string getRequestURI() const
Get the request uri.
Definition: httprequest.hpp:107
dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::setMethod
void setMethod(HTTPRequest::Method method)
Set the HTTPRequest::Method.
Definition: httprequest.hpp:101
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::HTTPRequest::methodFromString
static Method methodFromString(const std::string &s)
Translate an uppercase string ('GET', "POST', ..) into a Method enum.
Definition: httprequest.cpp:47
dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::asString
virtual std::string asString() const
Convert the HTTPRequestLine to a HTTP string.
Definition: httprequest.cpp:77
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::HTTPRequest::HTTPRequestLine::getMethod
HTTPRequest::Method getMethod() const
Get the HTTPRequest::Method.
Definition: httprequest.hpp:95
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::HTTPRequest::send
virtual common::SystemError send(BaseSocket *socket)
Send this HTTPMessage to the socket.
Definition: httprequest.cpp:99
dodo::network::protocol::http::HTTPRequest::Method
Method
The HTTP request method.
Definition: httprequest.hpp:48
httpversion.hpp
dodo
A C++ platform interface to lean Linux services tailored for containerized deployment.
Definition: application.hpp:29
dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::HTTPRequestLine
HTTPRequestLine()
Construct default HTTPRequestLine.
Definition: httprequest.hpp:73
dodo::network::protocol::http::HTTPRequest::getRequestLine
HTTPRequestLine & getRequestLine()
Get the HTTPRequestLine.
Definition: httprequest.hpp:170
dodo::network::protocol::http::HTTPFragment::ParseResult
Used to convey parsing succces.
Definition: httpfragment.hpp:75
dodo::network::protocol::http::HTTPRequest::methodAllowsBody
static bool methodAllowsBody(Method method)
Return true if the Method allows a body in the message.
Definition: httprequest.cpp:60
dodo::network::protocol::http::HTTPMessage
Generic HTTPMessage, parent to HTTPRequest and HTTPResponse.
Definition: httpmessage.hpp:40
dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::request_uri_
std::string request_uri_
The request 'uri'.
Definition: httprequest.hpp:137
dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::setHTTPVersion
void setHTTPVersion(const HTTPVersion &version)
Set the HTTP version.
Definition: httprequest.hpp:125
dodo::network::protocol::http::HTTPRequest::methodAsString
static std::string methodAsString(Method method)
Translate a Method enum to a human readable string.
Definition: httprequest.cpp:32
dodo::common::SystemError
Linux system error primitive to provide a consistent interface to Linux error codes.
Definition: systemerror.hpp:53
dodo::network::protocol::http::HTTPRequest::write
common::SystemError write(BaseSocket *socket) const
Write the HTTPRequest to the socket.
Definition: httprequest.cpp:249
dodo::network::protocol::http::HTTPRequest::request_line_
HTTPRequestLine request_line_
The HTTPRequestLine of the HTTPRequest.
Definition: httprequest.hpp:208
dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::setRequestURI
void setRequestURI(const std::string &uri)
Set the request uri.
Definition: httprequest.hpp:113
dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::http_version_
HTTPVersion http_version_
The HTTP version.
Definition: httprequest.hpp:142
dodo::network::protocol::http::HTTPRequest::asString
virtual std::string asString() const
Return the HTTPRequest as a string.
Definition: httprequest.cpp:86
dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::parse
virtual ParseResult parse(VirtualReadBuffer &data)
Parses a HTTPMessage.
Definition: httprequest.cpp:162
dodo::network::protocol::http::HTTPRequest::parseBody
virtual ParseResult parseBody(VirtualReadBuffer &data)
Parse a body and resturn it as a single string.
Definition: httprequest.cpp:139
dodo::network::BaseSocket
Interface to and common implementation of concrete sockets (Socket, TLSSocket).
Definition: basesocket.hpp:36
dodo::network::protocol::http::HTTPRequest::setRequestLine
void setRequestLine(const HTTPRequestLine &req)
Set the HTTPRequestLine.