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 
32 extern "C" {
33 struct MHD_Connection;
34 struct MHD_PostProcessor;
35 }
36 
37 namespace fawkes {
38 
39 class WebRequestDispatcher;
40 
42 {
43  friend WebRequestDispatcher;
44 
45 public:
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
57  } Method;
58 
59  /** HTTP version. */
60  typedef enum { HTTP_VERSION_1_0, HTTP_VERSION_1_1 } HttpVersion;
61 
62  WebRequest(const char *uri);
63  ~WebRequest();
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 */
93  http_version() const
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 &
118  client_addr() const
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> &
153  post_values() const
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> &
190  get_values() const
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  /** Set a POST value.
272  * @param key key of the cookie
273  * @param data incoming data
274  * @param size size in bytes of @p data
275  */
276  void set_post_value(const char *key, const char *data, size_t size);
277 
278  /** Set a GET value.
279  * @param key key of the cookie
280  * @param value value of the GET argument
281  */
282  void
283  set_get_value(const std::string &key, const std::string &value)
284  {
285  get_values_[key] = value;
286  }
287 
288  /** Set a header value.
289  * @param key key of the cookie
290  * @param value value of the header argument
291  */
292  void
293  set_header(const std::string &key, const std::string &value)
294  {
295  headers_[key] = value;
296  }
297 
298  /** Get a path argument.
299  * Retrieves a named argument that was a token in the
300  * registration URL, e.g., retrieve "id" for "/item/{id}".
301  * @param what what to retrieve
302  * @return item passed in URL or empty string
303  */
304  std::string
305  path_arg(const std::string &what) const
306  {
307  const auto p = path_args_.find(what);
308  if (p != path_args_.end()) {
309  return p->second;
310  } else {
311  return "";
312  }
313  }
314 
315  /** Set path arguments.
316  * @param args path arguments
317  */
318  void
319  set_path_args(std::map<std::string, std::string> &&args)
320  {
321  path_args_ = std::move(args);
322  }
323 
324  /** Get body of request.
325  * @return The data that was received with the request. This is not
326  * set if we receive a form submission. The values will be available
327  * as POST values then. Note that this is not necesarily a printable
328  * string (or zero-terminated) */
329  const std::string &
330  body() const
331  {
332  return body_;
333  }
334 
335  void increment_reply_size(size_t increment_by);
336  size_t reply_size() const;
337  WebReply::Code reply_code() const;
338  void set_reply_code(WebReply::Code code);
339 
340 protected:
341  /** Set cookie map.
342  * @param cookies cookies map
343  */
344  void
345  set_cookies(const std::map<std::string, std::string> &cookies)
346  {
347  cookies_ = cookies;
348  }
349  void set_body(const char *data, size_t data_size);
350  void addto_body(const char *data, size_t data_size);
351  void finish_body();
352 
353 private:
354  bool
355  is_setup()
356  {
357  return is_setup_;
358  }
359  void setup(const char *url, const char *method, const char *version, MHD_Connection *connection);
360 
361 private:
362  MHD_PostProcessor *pp_;
363  bool is_setup_;
364 
365  std::string uri_;
366  std::string url_;
367  std::string user_;
368  std::string client_addr_;
369  Method method_;
370  HttpVersion http_version_;
371  Time time_;
372  size_t reply_size_;
373  WebReply::Code reply_code_;
374  std::map<std::string, std::string> cookies_;
375  std::map<std::string, std::string> post_values_;
376  std::string body_;
377  std::map<std::string, std::string> get_values_;
378  std::map<std::string, std::string> headers_;
379  std::map<std::string, std::string> path_args_;
380 };
381 
382 } // end namespace fawkes
383 
384 #endif
std::string path_arg(const std::string &what) const
Get a path argument.
Definition: request.h:305
Web request dispatcher.
const std::map< std::string, std::string > & get_values() const
Get map of GET values.
Definition: request.h:190
void set_reply_code(WebReply::Code code)
Set HTTP code of the final reply.
Definition: request.cpp:281
void set_header(const std::string &key, const std::string &value)
Set a header value.
Definition: request.h:293
void set_body(const char *data, size_t data_size)
Set request body.
Definition: request.cpp:197
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
Fawkes library namespace.
WebRequest(const char *uri)
Constructor.
Definition: request.cpp:78
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
bool has_post_value(std::string key) const
Check if the named post value has been received.
Definition: request.h:182
size_t reply_size() const
Get number of bytes actually sent out so far.
Definition: request.cpp:240
A class for handling time.
Definition: time.h:92
bool has_get_value(std::string key) const
Check if the named get value has been received.
Definition: request.h:219
const std::map< std::string, std::string > & cookies() const
Get map of cookies.
Definition: request.h:126
std::string post_value(std::string &key) const
Get specific POST value.
Definition: request.h:162
Method method() const
Get HTTP transfer method.
Definition: request.h:84
std::string cookie(std::string &key) const
Get specific cookie.
Definition: request.h:135
const std::string & body() const
Get body of request.
Definition: request.h:330
void set_path_args(std::map< std::string, std::string > &&args)
Set path arguments.
Definition: request.h:319
std::string post_value(const char *key) const
Get specific POST value.
Definition: request.h:172
const std::string & uri() const
Get URI.
Definition: request.h:76
void set_post_value(const char *key, const char *data, size_t size)
Set a POST value.
Definition: request.cpp:180
std::string header(const char *key) const
Get specific header value.
Definition: request.h:246
bool has_header(std::string key) const
Check if the named header value has been received.
Definition: request.h:256
std::string get_value(std::string &key) const
Get specific GET value.
Definition: request.h:199
WebReply::Code reply_code() const
Get HTTP code of reply.
Definition: request.cpp:290
const char * http_version_str() const
Get HTTP version as string.
Definition: request.cpp:268
Web request meta data carrier.
Definition: request.h:41
const char * method_str() const
Get method as string.
Definition: request.cpp:249
HttpVersion http_version() const
Get HTTP version.
Definition: request.h:93
void increment_reply_size(size_t increment_by)
Increment reply bytes counter.
Definition: request.cpp:231
const std::map< std::string, std::string > & headers() const
Get map of header values.
Definition: request.h:227
void finish_body()
Finalize body handling.
Definition: request.cpp:218
bool has_cookie(std::string key) const
Check if the named cookie has been received.
Definition: request.h:145
~WebRequest()
Destructor.
Definition: request.cpp:166
std::string get_value(const char *key) const
Get specific GET value.
Definition: request.h:209
void set_get_value(const std::string &key, const std::string &value)
Set a GET value.
Definition: request.h:283
const std::string & url() const
Get URL.
Definition: request.h:68
Code
HTTP response code.
Definition: reply.h:37
Method
HTTP transfer methods.
Definition: request.h:47
const std::string & user() const
Get name of authenticated user (basic auth).
Definition: request.h:110
const std::map< std::string, std::string > & post_values() const
Get map of POST values.
Definition: request.h:153
void addto_body(const char *data, size_t data_size)
Add to request body.
Definition: request.cpp:209
HttpVersion
HTTP version.
Definition: request.h:60
void set_cookies(const std::map< std::string, std::string > &cookies)
Set cookie map.
Definition: request.h:345