Fawkes API Fawkes Development Version
request.h
1
2/***************************************************************************
3 * request.h - Web request
4 *
5 * Created: Mon Jun 17 17:58:51 2013
6 * Copyright 2006-2018 Tim Niemueller [www.niemueller.de]
7 ****************************************************************************/
8
9/* This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Library General Public License for more details.
18 *
19 * Read the full text in the LICENSE.GPL file in the doc directory.
20 */
21
22#ifndef _LIBS_WEBVIEW_REQUEST_H_
23#define _LIBS_WEBVIEW_REQUEST_H_
24
25#include <arpa/inet.h>
26#include <utils/time/time.h>
27#include <webview/reply.h>
28
29#include <map>
30#include <string>
31
32extern "C" {
33struct MHD_Connection;
34struct MHD_PostProcessor;
35}
36
37namespace fawkes {
38
39class WebRequestDispatcher;
40
42{
44
45public:
46 /** HTTP transfer methods. */
47 typedef enum {
48 METHOD_CONNECT, ///< CONNECT
49 METHOD_DELETE, ///< DELETE
50 METHOD_GET, ///< GET
51 METHOD_HEAD, ///< HEAD
52 METHOD_OPTIONS, ///< OPTIONS
53 METHOD_POST, ///< POST
54 METHOD_PUT, ///< PUT
55 METHOD_TRACE, ///< TRACE
56 METHOD_PATCH ///< PATCH
58
59 /** HTTP version. */
60 typedef enum { HTTP_VERSION_1_0, HTTP_VERSION_1_1 } HttpVersion;
61
62 WebRequest(const char *uri);
64
65 /** Get URL.
66 * @return URL */
67 const std::string &
68 url() const
69 {
70 return url_;
71 }
72
73 /** Get URI.
74 * @return URI */
75 const std::string &
76 uri() const
77 {
78 return uri_;
79 }
80
81 /** Get HTTP transfer method.
82 * @return request's HTTP transfer method */
83 Method
84 method() const
85 {
86 return method_;
87 }
88 const char *method_str() const;
89
90 /** Get HTTP version.
91 * @return HTTP protocol version */
94 {
95 return http_version_;
96 }
97 const char *http_version_str() const;
98
99 /** Get request time.
100 * @return request time */
101 const Time &
102 time() const
103 {
104 return time_;
105 }
106
107 /** Get name of authenticated user (basic auth).
108 * @return name of authenticated user or empty if non-protected URL */
109 const std::string &
110 user() const
111 {
112 return user_;
113 }
114
115 /** Get client address as string.
116 * @return client address as string */
117 const std::string &
119 {
120 return client_addr_;
121 }
122
123 /** Get map of cookies.
124 * @return map of cookies. */
125 const std::map<std::string, std::string> &
126 cookies() const
127 {
128 return cookies_;
129 }
130 /** Get specific cookie.
131 * @param key key of the cookie
132 * @return value of cookie or empty string if not set
133 */
134 std::string
135 cookie(std::string &key) const
136 {
137 std::map<std::string, std::string>::const_iterator c = cookies_.find(key);
138 return (c != cookies_.end()) ? c->second : "";
139 }
140 /** Check if the named cookie has been received.
141 * @param key key of the requested cookie
142 * @return true if the cookie was set, false otherwise
143 */
144 bool
145 has_cookie(std::string key) const
146 {
147 return (cookies_.find(key) != cookies_.end());
148 }
149
150 /** Get map of POST values.
151 * @return map of POST values. */
152 const std::map<std::string, std::string> &
154 {
155 return post_values_;
156 }
157 /** Get specific POST value.
158 * @param key key of the post value
159 * @return value of post value or empty string if not set
160 */
161 std::string
162 post_value(std::string &key) const
163 {
164 std::map<std::string, std::string>::const_iterator p = post_values_.find(key);
165 return (p != post_values_.end()) ? p->second : "";
166 }
167 /** Get specific POST value.
168 * @param key key of the post value
169 * @return value of post value or empty string if not set
170 */
171 std::string
172 post_value(const char *key) const
173 {
174 std::map<std::string, std::string>::const_iterator p = post_values_.find(key);
175 return (p != post_values_.end()) ? p->second : "";
176 }
177 /** Check if the named post value has been received.
178 * @param key key of the post value
179 * @return true if the post value was received, false otherwise
180 */
181 bool
182 has_post_value(std::string key) const
183 {
184 return (post_values_.find(key) != post_values_.end());
185 }
186
187 /** Get map of GET values.
188 * @return map of GET values. */
189 const std::map<std::string, std::string> &
191 {
192 return get_values_;
193 }
194 /** Get specific GET value.
195 * @param key key of the get value
196 * @return value of get value or empty string if not set
197 */
198 std::string
199 get_value(std::string &key) const
200 {
201 std::map<std::string, std::string>::const_iterator p = get_values_.find(key);
202 return (p != get_values_.end()) ? p->second : "";
203 }
204 /** Get specific GET value.
205 * @param key key of the get value
206 * @return value of get value or empty string if not set
207 */
208 std::string
209 get_value(const char *key) const
210 {
211 std::map<std::string, std::string>::const_iterator p = get_values_.find(key);
212 return (p != get_values_.end()) ? p->second : "";
213 }
214 /** Check if the named get value has been received.
215 * @param key key of the requested get value
216 * @return true if the get value was received, false otherwise
217 */
218 bool
219 has_get_value(std::string key) const
220 {
221 return (get_values_.find(key) != get_values_.end());
222 }
223
224 /** Get map of header values.
225 * @return map of header values. */
226 const std::map<std::string, std::string> &
227 headers() const
228 {
229 return headers_;
230 }
231 /** Header specific header value.
232 * @param key key of the header value
233 * @return value of header value or empty string if not set
234 */
235 std::string
236 header(std::string &key) const
237 {
238 std::map<std::string, std::string>::const_iterator p = headers_.find(key);
239 return (p != headers_.end()) ? p->second : "";
240 }
241 /** Get specific header value.
242 * @param key key of the header value
243 * @return value of header value or empty string if not set
244 */
245 std::string
246 header(const char *key) const
247 {
248 std::map<std::string, std::string>::const_iterator p = headers_.find(key);
249 return (p != headers_.end()) ? p->second : "";
250 }
251 /** Check if the named header value has been received.
252 * @param key key of the requested header
253 * @return true if the header value was received, false otherwise
254 */
255 bool
256 has_header(std::string key) const
257 {
258 return (headers_.find(key) != headers_.end());
259 }
260
261 /** Set a cookie.
262 * @param key key of the cookie
263 * @param value value of the cookie
264 */
265 void
266 set_cookie(const std::string &key, const std::string &value)
267 {
268 cookies_[key] = value;
269 }
270
271 void set_post_value(const char *key, const char *data, size_t size);
272
273 /** Set a GET value.
274 * @param key key of the cookie
275 * @param value value of the GET argument
276 */
277 void
278 set_get_value(const std::string &key, const std::string &value)
279 {
280 get_values_[key] = value;
281 }
282
283 /** Set a header value.
284 * @param key key of the cookie
285 * @param value value of the header argument
286 */
287 void
288 set_header(const std::string &key, const std::string &value)
289 {
290 headers_[key] = value;
291 }
292
293 /** Get a path argument.
294 * Retrieves a named argument that was a token in the
295 * registration URL, e.g., retrieve "id" for "/item/{id}".
296 * @param what what to retrieve
297 * @return item passed in URL or empty string
298 */
299 std::string
300 path_arg(const std::string &what) const
301 {
302 const auto p = path_args_.find(what);
303 if (p != path_args_.end()) {
304 return p->second;
305 } else {
306 return "";
307 }
308 }
309
310 /** Set path arguments.
311 * @param args path arguments
312 */
313 void
314 set_path_args(std::map<std::string, std::string> &&args)
315 {
316 path_args_ = std::move(args);
317 }
318
319 /** Get body of request.
320 * @return The data that was received with the request. This is not
321 * set if we receive a form submission. The values will be available
322 * as POST values then. Note that this is not necesarily a printable
323 * string (or zero-terminated) */
324 const std::string &
325 body() const
326 {
327 return body_;
328 }
329
330 void increment_reply_size(size_t increment_by);
331 size_t reply_size() const;
334
335protected:
336 /** Set cookie map.
337 * @param cookies cookies map
338 */
339 void
340 set_cookies(const std::map<std::string, std::string> &cookies)
341 {
342 cookies_ = cookies;
343 }
344 void set_body(const char *data, size_t data_size);
345 void addto_body(const char *data, size_t data_size);
346 void finish_body();
347
348private:
349 bool
350 is_setup()
351 {
352 return is_setup_;
353 }
354 void setup(const char *url, const char *method, const char *version, MHD_Connection *connection);
355
356private:
357 MHD_PostProcessor *pp_;
358 bool is_setup_;
359
360 std::string uri_;
361 std::string url_;
362 std::string user_;
363 std::string client_addr_;
364 Method method_;
365 HttpVersion http_version_;
366 Time time_;
367 size_t reply_size_;
368 WebReply::Code reply_code_;
369 std::map<std::string, std::string> cookies_;
370 std::map<std::string, std::string> post_values_;
371 std::string body_;
372 std::map<std::string, std::string> get_values_;
373 std::map<std::string, std::string> headers_;
374 std::map<std::string, std::string> path_args_;
375};
376
377} // end namespace fawkes
378
379#endif
A class for handling time.
Definition: time.h:93
Code
HTTP response code.
Definition: reply.h:37
Web request dispatcher.
Web request meta data carrier.
Definition: request.h:42
const std::map< std::string, std::string > & post_values() const
Get map of POST values.
Definition: request.h:153
std::string get_value(const char *key) const
Get specific GET value.
Definition: request.h:209
HttpVersion http_version() const
Get HTTP version.
Definition: request.h:93
const std::map< std::string, std::string > & cookies() const
Get map of cookies.
Definition: request.h:126
const std::map< std::string, std::string > & headers() const
Get map of header values.
Definition: request.h:227
WebRequest(const char *uri)
Constructor.
Definition: request.cpp:79
const std::string & url() const
Get URL.
Definition: request.h:68
std::string post_value(const char *key) const
Get specific POST value.
Definition: request.h:172
const char * http_version_str() const
Get HTTP version as string.
Definition: request.cpp:269
bool has_header(std::string key) const
Check if the named header value has been received.
Definition: request.h:256
void set_cookies(const std::map< std::string, std::string > &cookies)
Set cookie map.
Definition: request.h:340
void addto_body(const char *data, size_t data_size)
Add to request body.
Definition: request.cpp:210
const char * method_str() const
Get method as string.
Definition: request.cpp:250
void finish_body()
Finalize body handling.
Definition: request.cpp:219
bool has_post_value(std::string key) const
Check if the named post value has been received.
Definition: request.h:182
bool has_cookie(std::string key) const
Check if the named cookie has been received.
Definition: request.h:145
Method method() const
Get HTTP transfer method.
Definition: request.h:84
void set_post_value(const char *key, const char *data, size_t size)
Set a POST value.
Definition: request.cpp:181
std::string post_value(std::string &key) const
Get specific POST value.
Definition: request.h:162
std::string header(std::string &key) const
Header specific header value.
Definition: request.h:236
const std::string & client_addr() const
Get client address as string.
Definition: request.h:118
const std::string & body() const
Get body of request.
Definition: request.h:325
void set_reply_code(WebReply::Code code)
Set HTTP code of the final reply.
Definition: request.cpp:282
Method
HTTP transfer methods.
Definition: request.h:47
@ METHOD_HEAD
HEAD.
Definition: request.h:51
@ METHOD_DELETE
DELETE.
Definition: request.h:49
@ METHOD_GET
GET.
Definition: request.h:50
@ METHOD_POST
POST.
Definition: request.h:53
@ METHOD_PATCH
PATCH.
Definition: request.h:56
@ METHOD_OPTIONS
OPTIONS.
Definition: request.h:52
@ METHOD_PUT
PUT.
Definition: request.h:54
@ METHOD_TRACE
TRACE.
Definition: request.h:55
@ METHOD_CONNECT
CONNECT.
Definition: request.h:48
~WebRequest()
Destructor.
Definition: request.cpp:167
size_t reply_size() const
Get number of bytes actually sent out so far.
Definition: request.cpp:241
std::string get_value(std::string &key) const
Get specific GET value.
Definition: request.h:199
std::string path_arg(const std::string &what) const
Get a path argument.
Definition: request.h:300
const Time & time() const
Get request time.
Definition: request.h:102
void set_cookie(const std::string &key, const std::string &value)
Set a cookie.
Definition: request.h:266
const std::string & uri() const
Get URI.
Definition: request.h:76
const std::string & user() const
Get name of authenticated user (basic auth).
Definition: request.h:110
std::string cookie(std::string &key) const
Get specific cookie.
Definition: request.h:135
void set_header(const std::string &key, const std::string &value)
Set a header value.
Definition: request.h:288
bool has_get_value(std::string key) const
Check if the named get value has been received.
Definition: request.h:219
void increment_reply_size(size_t increment_by)
Increment reply bytes counter.
Definition: request.cpp:232
const std::map< std::string, std::string > & get_values() const
Get map of GET values.
Definition: request.h:190
HttpVersion
HTTP version.
Definition: request.h:60
std::string header(const char *key) const
Get specific header value.
Definition: request.h:246
void set_path_args(std::map< std::string, std::string > &&args)
Set path arguments.
Definition: request.h:314
WebReply::Code reply_code() const
Get HTTP code of reply.
Definition: request.cpp:291
void set_body(const char *data, size_t data_size)
Set request body.
Definition: request.cpp:198
void set_get_value(const std::string &key, const std::string &value)
Set a GET value.
Definition: request.h:278
Fawkes library namespace.