wibble  1.1
http.h
Go to the documentation of this file.
1 #ifndef WIBBLE_NET_HTTP_H
2 #define WIBBLE_NET_HTTP_H
3 
4 /*
5  * net/http - HTTP server utilities
6  *
7  * Copyright (C) 2010 Enrico Zini <enrico@enricozini.org>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library 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 GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #include <string>
25 #include <map>
26 #include <wibble/regexp.h>
27 #include <wibble/net/mime.h>
28 #include <iosfwd>
29 #include <stdexcept>
30 
31 namespace wibble {
32 namespace net {
33 namespace http {
34 
35 struct Request;
36 
37 struct error : public std::exception
38 {
39  int code;
40  std::string desc;
41  std::string msg;
42 
43  error(int code, const std::string& desc)
44  : code(code), desc(desc) {}
45  error(int code, const std::string& desc, const std::string& msg)
46  : code(code), desc(desc), msg(msg) {}
47  virtual ~error() throw () {}
48 
49  virtual const char* what() const throw ();
50 
51  virtual void send(Request& req);
52 };
53 
54 struct error400 : public error
55 {
56  error400() : error(400, "Bad request") {}
57  error400(const std::string& msg) : error(400, "Bad request", msg) {}
58 };
59 
60 struct error404 : public error
61 {
62  error404() : error(404, "Not found") {}
63  error404(const std::string& msg) : error(404, "Not found", msg) {}
64 
65  virtual void send(Request& req);
66 };
67 
68 struct Request
69 {
70  // Request does not take ownership of the socket: it is up to the caller to
71  // close it
72  int sock;
73  std::string peer_hostname;
74  std::string peer_hostaddr;
75  std::string peer_port;
76  std::string server_name;
77  std::string server_port;
78  std::string script_name;
79  std::string path_info;
80  std::string query_string;
82  std::string server_software;
85 
86  std::string method;
87  std::string url;
88  std::string version;
89  std::map<std::string, std::string> headers;
91 
93 
94  std::map<std::string, std::string> extra_response_headers;
95 
96  Request();
97 
112  bool read_request();
113 
120  bool read_buf(std::string& res, size_t size);
121 
122  // Read HTTP method and its following empty line
123  bool read_method();
124 
131  bool read_headers();
132 
137  void set_cgi_env();
138 
140  void send(const std::string& buf);
141 
143  void send_status_line(int code, const std::string& msg, const std::string& version = "HTTP/1.0");
144 
146  void send_server_header();
147 
149  void send_date_header();
150 
152  void send_extra_response_headers();
153 
155  void send_result(const std::string& content, const std::string& content_type="text/html; charset=utf-8", const std::string& filename=std::string());
156 
158  void discard_input();
159 
166  std::string pop_path_info();
167 
173  std::string path_info_head();
174 };
175 
177 struct Param
178 {
179  virtual ~Param();
180 
187  virtual void parse(const std::string& str) = 0;
188 };
189 
191 struct ParamSingle : public std::string, public Param
192 {
193  virtual void parse(const std::string& str);
194 };
195 
197 struct ParamMulti : public std::vector<std::string>, public Param
198 {
199  virtual void parse(const std::string& str);
200 };
201 
205 struct FileParam
206 {
208  struct FileInfo
209  {
211  std::string fname;
213  std::string client_fname;
214 
218  bool read(net::mime::Reader& mime_reader,
219  std::map<std::string, std::string> headers,
220  const std::string& outdir,
221  const std::string& fname_blacklist,
222  const std::string& client_fname,
223  int sock,
224  const std::string& boundary,
225  size_t inputsize);
226  };
227 
228  virtual ~FileParam();
229 
233  virtual bool read(
234  net::mime::Reader& mime_reader,
235  std::map<std::string, std::string> headers,
236  const std::string& outdir,
237  const std::string& fname_blacklist,
238  const std::string& client_fname,
239  int sock,
240  const std::string& boundary,
241  size_t inputsize) = 0;
242 };
243 
247 struct FileParamSingle : public FileParam
248 {
250 
255  FileParamSingle(const std::string& fname=std::string());
256 
257  virtual bool read(
258  net::mime::Reader& mime_reader,
259  std::map<std::string, std::string> headers,
260  const std::string& outdir,
261  const std::string& fname_blacklist,
262  const std::string& client_fname,
263  int sock,
264  const std::string& boundary,
265  size_t inputsize);
266 };
267 
271 struct FileParamMulti : public FileParam
272 {
273  std::vector<FileInfo> files;
274 
275  virtual bool read(
276  net::mime::Reader& mime_reader,
277  std::map<std::string, std::string> headers,
278  const std::string& outdir,
279  const std::string& fname_blacklist,
280  const std::string& client_fname,
281  int sock,
282  const std::string& boundary,
283  size_t inputsize);
284 };
285 
292 struct Params : public std::map<std::string, Param*>
293 {
295  std::map<std::string, FileParam*> files;
296 
299 
302 
311 
320 
326  std::string conf_outdir;
327 
335  std::string conf_fname_blacklist;
336 
337 
338  Params();
339  ~Params();
340 
342  template<typename TYPE>
343  TYPE* add(const std::string& name)
344  {
345  TYPE* res = new TYPE;
346  add(name, res);
347  return res;
348  }
349 
351  void add(const std::string& name, Param* param);
352 
354  void add(const std::string& name, FileParam* param);
355 
361  Param* obtain_field(const std::string& name);
362 
368  FileParam* obtain_file_field(const std::string& name);
369 
371  Param* field(const std::string& name);
372 
374  FileParam* file_field(const std::string& name);
375 
377  void parse_get_or_post(net::http::Request& req);
378 
380  void parse_urlencoded(const std::string& qstring);
381 
383  void parse_multipart(net::http::Request& req, size_t inputsize, const std::string& content_type);
384 
386  void parse_post(net::http::Request& req);
387 };
388 
389 
390 }
391 }
392 }
393 
394 // vim:set ts=4 sw=4:
395 #endif
Infomation about one uploaded file.
Definition: http.h:208
Multiple file uploads with the same name.
Definition: http.h:271
error400(const std::string &msg)
Definition: http.h:57
virtual const char * what() const
Definition: http.cpp:38
Definition: http.h:37
std::string conf_fname_blacklist
String containing blacklist characters that are replaced with "_" in the file name.
Definition: http.h:335
TYPE * add(const std::string &name)
Universal, automatic add method.
Definition: http.h:343
size_t conf_max_field_size
Maximum size of field data for one non-file field.
Definition: http.h:301
std::string query_string
Definition: http.h:80
error(int code, const std::string &desc)
Definition: http.h:43
error404()
Definition: http.h:62
Definition: http.h:60
std::string peer_hostaddr
Definition: http.h:74
std::string script_name
Definition: http.h:78
std::string server_port
Definition: http.h:77
File upload parameter.
Definition: http.h:205
wibble::net::mime::Reader mime_reader
Definition: http.h:92
Single-valued parameter.
Definition: http.h:191
Definition: http.h:54
bool conf_accept_unknown_fields
Whether to accept unknown fields.
Definition: http.h:310
std::string url
Definition: http.h:87
std::map< std::string, std::string > extra_response_headers
Definition: http.h:94
std::string fname
File pathname on the local file system.
Definition: http.h:211
int code
Definition: http.h:39
Parse and store HTTP query parameters.
Definition: http.h:292
std::map< std::string, std::string > headers
Definition: http.h:89
virtual void send(Request &req)
Definition: http.cpp:44
error(int code, const std::string &desc, const std::string &msg)
Definition: http.h:45
Base interface for GET or POST parameters.
Definition: http.h:177
std::vector< FileInfo > files
Definition: http.h:273
std::string server_software
String to use as server software "NAME/version".
Definition: http.h:82
std::string desc
Definition: http.h:40
std::string path_info
Definition: http.h:79
Definition: http.h:68
std::string server_name
Definition: http.h:76
std::string version
Definition: http.h:88
size_t conf_max_input_size
Maximum size of POST input data.
Definition: http.h:298
std::string msg
Definition: http.h:41
virtual ~error()
Definition: http.h:47
size_t size(const std::string &file)
File size.
Definition: fs.cpp:287
wibble::Splitter space_splitter
Definition: http.h:90
Definition: amorph.h:17
Single file upload field.
Definition: http.h:247
FileInfo info
Definition: http.h:249
bool response_started
true if some response has already been sent to the client
Definition: http.h:84
std::string conf_outdir
Directory where we write uploaded files.
Definition: http.h:326
Definition: mime.h:33
std::string peer_hostname
Definition: http.h:73
Map< List, F > map(const List &l, const F &f)
Definition: list.h:381
error400()
Definition: http.h:56
std::map< std::string, FileParam * > files
File parameters.
Definition: http.h:295
std::string method
Definition: http.h:86
int sock
Definition: http.h:72
std::string peer_port
Definition: http.h:75
bool conf_accept_unknown_file_fields
Whether to accept unknown file upload fields.
Definition: http.h:319
Split a string using a regular expression to match the token separators.
Definition: regexp.h:145
Multi-valued parameter.
Definition: http.h:197
std::string client_fname
File pathname provided by the client.
Definition: http.h:213
error404(const std::string &msg)
Definition: http.h:63