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-2014 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 <webview/reply.h>
26 #include <utils/time/time.h>
27 
28 #include <map>
29 #include <string>
30 #include <arpa/inet.h>
31 
32 extern "C" {
33  struct MHD_Connection;
34  struct MHD_PostProcessor;
35 }
36 
37 namespace fawkes {
38 #if 0 /* just to make Emacs auto-indent happy */
39 }
40 #endif
41 
42 class WebRequest {
43  friend class 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;
57 
58  /** HTTP version. */
59  typedef enum {
60  HTTP_VERSION_1_0,
61  HTTP_VERSION_1_1
62  } HttpVersion;
63 
64  WebRequest(const char *uri);
65  ~WebRequest();
66 
67  /** Get URL.
68  * @return URL */
69  const std::string & url() const { return url_; }
70 
71  /** Get URI.
72  * @return URI */
73  const std::string & uri() const { return uri_; }
74 
75  /** Get HTTP transfer method.
76  * @return request's HTTP transfer method */
77  Method method() const { return method_; }
78  const char * method_str() const;
79 
80  /** Get HTTP version.
81  * @return HTTP protocol version */
82  HttpVersion http_version() const { return http_version_; }
83  const char * http_version_str() const;
84 
85  /** Get request time.
86  * @return request time */
87  const Time & time() const { return time_; }
88 
89  /** Get name of authenticated user (basic auth).
90  * @return name of authenticated user or empty if non-protected URL */
91  const std::string & user() const { return user_; }
92 
93  /** Get client address as string.
94  * @return client address as string */
95  const std::string & client_addr() const { return client_addr_; }
96 
97  /** Get map of cookies.
98  * @return map of cookies. */
99  const std::map<std::string, std::string> & cookies() const { return cookies_; }
100  /** Get specific cookie.
101  * @param key key of the cookie
102  * @return value of cookie or empty string if not set
103  */
104  std::string cookie(std::string &key) const
105  {
106  std::map<std::string, std::string>::const_iterator c = cookies_.find(key);
107  return (c != cookies_.end()) ? c->second : "";
108  }
109  /** Check if the named cookie has been received.
110  * @param key key of the requested cookie
111  * @return true if the cookie was set, false otherwise
112  */
113  bool has_cookie(std::string key) const
114  { return (cookies_.find(key) != cookies_.end()); }
115 
116  /** Get map of POST values.
117  * @return map of POST values. */
118  const std::map<std::string, std::string> & post_values() const { return post_values_; }
119  /** Get specific POST value.
120  * @param key key of the post value
121  * @return value of post value or empty string if not set
122  */
123  std::string post_value(std::string &key) const
124  {
125  std::map<std::string, std::string>::const_iterator p = post_values_.find(key);
126  return (p != post_values_.end()) ? p->second : "";
127  }
128  /** Get specific POST value.
129  * @param key key of the post value
130  * @return value of post value or empty string if not set
131  */
132  std::string post_value(const char *key) const
133  {
134  std::map<std::string, std::string>::const_iterator p = post_values_.find(key);
135  return (p != post_values_.end()) ? p->second : "";
136  }
137  /** Check if the named post value has been received.
138  * @param key key of the post value
139  * @return true if the post value was received, false otherwise
140  */
141  bool has_post_value(std::string key) const
142  { return (post_values_.find(key) != post_values_.end()); }
143 
144  /** Get map of GET values.
145  * @return map of GET values. */
146  const std::map<std::string, std::string> & get_values() const { return get_values_; }
147  /** Get specific GET value.
148  * @param key key of the get value
149  * @return value of get value or empty string if not set
150  */
151  std::string get_value(std::string &key) const
152  {
153  std::map<std::string, std::string>::const_iterator p = get_values_.find(key);
154  return (p != get_values_.end()) ? p->second : "";
155  }
156  /** Get specific GET value.
157  * @param key key of the get value
158  * @return value of get value or empty string if not set
159  */
160  std::string get_value(const char *key) const
161  {
162  std::map<std::string, std::string>::const_iterator p = get_values_.find(key);
163  return (p != get_values_.end()) ? p->second : "";
164  }
165  /** Check if the named get value has been received.
166  * @param key key of the requested get value
167  * @return true if the get value was received, false otherwise
168  */
169  bool has_get_value(std::string key) const
170  { return (get_values_.find(key) != get_values_.end()); }
171 
172 
173  /** Get map of header values.
174  * @return map of header values. */
175  const std::map<std::string, std::string> & headers() const { return headers_; }
176  /** Header specific header value.
177  * @param key key of the header value
178  * @return value of header value or empty string if not set
179  */
180  std::string header(std::string &key) const
181  {
182  std::map<std::string, std::string>::const_iterator p = headers_.find(key);
183  return (p != headers_.end()) ? p->second : "";
184  }
185  /** Get specific header value.
186  * @param key key of the header value
187  * @return value of header value or empty string if not set
188  */
189  std::string header(const char *key) const
190  {
191  std::map<std::string, std::string>::const_iterator p = headers_.find(key);
192  return (p != headers_.end()) ? p->second : "";
193  }
194  /** Check if the named header value has been received.
195  * @param key key of the requested header
196  * @return true if the header value was received, false otherwise
197  */
198  bool has_header(std::string key) const
199  { return (headers_.find(key) != headers_.end()); }
200 
201  /** Set a cookie.
202  * @param key key of the cookie
203  * @param value value of the cookie
204  */
205  void set_cookie(const std::string &key, const std::string &value) { cookies_[key] = value; }
206 
207  /** Set a POST value.
208  * @param key key of the cookie
209  * @param data incoming data
210  * @param size size in bytes of @p data
211  */
212  void set_post_value(const char *key, const char *data, size_t size);
213 
214  /** Set a GET value.
215  * @param key key of the cookie
216  * @param value value of the GET argument
217  */
218  void set_get_value(const std::string &key, const std::string &value) { get_values_[key] = value; }
219 
220  /** Set a header value.
221  * @param key key of the cookie
222  * @param value value of the header argument
223  */
224  void set_header(const std::string &key, const std::string &value)
225  { headers_[key] = value; }
226 
227  /** Get raw post data.
228  * @return raw port data or empty string if none. Note that this is not necesarily
229  * a printable string (or zero-terminated) */
230  const std::string & raw_post_data() const { return post_raw_data_; }
231 
232  void increment_reply_size(size_t increment_by);
233  size_t reply_size() const;
234  WebReply::Code reply_code() const;
235  void set_reply_code(WebReply::Code code);
236 
237  protected:
238  /** Set cookie map.
239  * @param cookies cookies map
240  */
241  void set_cookies(const std::map<std::string, std::string> &cookies) { cookies_ = cookies; }
242  void set_raw_post_data(const char *data, size_t data_size);
243 
244  private:
245  bool is_setup() { return is_setup_; }
246  void setup(const char *url, const char *method,
247  const char *version, MHD_Connection *connection);
248 
249  private:
250  MHD_PostProcessor *pp_;
251  bool is_setup_;
252 
253  std::string uri_;
254  std::string url_;
255  std::string user_;
256  std::string client_addr_;
257  Method method_;
258  HttpVersion http_version_;
259  Time time_;
260  size_t reply_size_;
261  WebReply::Code reply_code_;
262  std::map<std::string, std::string> cookies_;
263  std::map<std::string, std::string> post_values_;
264  std::string post_raw_data_;
265  std::map<std::string, std::string> get_values_;
266  std::map<std::string, std::string> headers_;
267 };
268 
269 
270 } // end namespace fawkes
271 
272 
273 
274 #endif
Web request dispatcher.
const std::map< std::string, std::string > & get_values() const
Get map of GET values.
Definition: request.h:146
void set_reply_code(WebReply::Code code)
Set HTTP code of the final reply.
Definition: request.cpp:249
void set_header(const std::string &key, const std::string &value)
Set a header value.
Definition: request.h:224
const std::string & raw_post_data() const
Get raw post data.
Definition: request.h:230
std::string header(std::string &key) const
Header specific header value.
Definition: request.h:180
const std::string & client_addr() const
Get client address as string.
Definition: request.h:95
Fawkes library namespace.
WebRequest(const char *uri)
Constructor.
Definition: request.cpp:80
const Time & time() const
Get request time.
Definition: request.h:87
void set_cookie(const std::string &key, const std::string &value)
Set a cookie.
Definition: request.h:205
bool has_post_value(std::string key) const
Check if the named post value has been received.
Definition: request.h:141
size_t reply_size() const
Get number of bytes actually sent out so far.
Definition: request.cpp:205
A class for handling time.
Definition: time.h:91
bool has_get_value(std::string key) const
Check if the named get value has been received.
Definition: request.h:169
const std::map< std::string, std::string > & cookies() const
Get map of cookies.
Definition: request.h:99
std::string post_value(std::string &key) const
Get specific POST value.
Definition: request.h:123
Method method() const
Get HTTP transfer method.
Definition: request.h:77
std::string cookie(std::string &key) const
Get specific cookie.
Definition: request.h:104
std::string post_value(const char *key) const
Get specific POST value.
Definition: request.h:132
const std::string & uri() const
Get URI.
Definition: request.h:73
void set_post_value(const char *key, const char *data, size_t size)
Set a POST value.
Definition: request.cpp:169
std::string header(const char *key) const
Get specific header value.
Definition: request.h:189
bool has_header(std::string key) const
Check if the named header value has been received.
Definition: request.h:198
std::string get_value(std::string &key) const
Get specific GET value.
Definition: request.h:151
WebReply::Code reply_code() const
Get HTTP code of reply.
Definition: request.cpp:259
const char * http_version_str() const
Get HTTP version as string.
Definition: request.cpp:235
Web request meta data carrier.
Definition: request.h:42
const char * method_str() const
Get method as string.
Definition: request.cpp:215
HttpVersion http_version() const
Get HTTP version.
Definition: request.h:82
void increment_reply_size(size_t increment_by)
Increment reply bytes counter.
Definition: request.cpp:196
const std::map< std::string, std::string > & headers() const
Get map of header values.
Definition: request.h:175
bool has_cookie(std::string key) const
Check if the named cookie has been received.
Definition: request.h:113
~WebRequest()
Destructor.
Definition: request.cpp:154
std::string get_value(const char *key) const
Get specific GET value.
Definition: request.h:160
void set_raw_post_data(const char *data, size_t data_size)
Set raw post data.
Definition: request.cpp:187
void set_get_value(const std::string &key, const std::string &value)
Set a GET value.
Definition: request.h:218
const std::string & url() const
Get URL.
Definition: request.h:69
Code
HTTP response code.
Definition: reply.h:40
Method
HTTP transfer methods.
Definition: request.h:47
const std::string & user() const
Get name of authenticated user (basic auth).
Definition: request.h:91
const std::map< std::string, std::string > & post_values() const
Get map of POST values.
Definition: request.h:118
HttpVersion
HTTP version.
Definition: request.h:59
void set_cookies(const std::map< std::string, std::string > &cookies)
Set cookie map.
Definition: request.h:241