/usr/include/wibble/net/http.h is in libwibble-dev 0.1.28-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | #ifndef WIBBLE_NET_HTTP_H
#define WIBBLE_NET_HTTP_H
/*
* net/http - HTTP server utilities
*
* Copyright (C) 2010 Enrico Zini <enrico@enricozini.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <string>
#include <map>
#include <wibble/regexp.h>
#include <wibble/net/mime.h>
#include <iosfwd>
#include <stdexcept>
namespace wibble {
namespace net {
namespace http {
struct Request;
struct error : public std::exception
{
int code;
std::string desc;
std::string msg;
error(int code, const std::string& desc)
: code(code), desc(desc) {}
error(int code, const std::string& desc, const std::string& msg)
: code(code), desc(desc), msg(msg) {}
virtual ~error() throw () {}
virtual const char* what() const throw ();
virtual void send(Request& req);
};
struct error400 : public error
{
error400() : error(400, "Bad request") {}
error400(const std::string& msg) : error(400, "Bad request", msg) {}
};
struct error404 : public error
{
error404() : error(404, "Not found") {}
error404(const std::string& msg) : error(404, "Not found", msg) {}
virtual void send(Request& req);
};
struct Request
{
// Request does not take ownership of the socket: it is up to the caller to
// close it
int sock;
std::string peer_hostname;
std::string peer_hostaddr;
std::string peer_port;
std::string server_name;
std::string server_port;
std::string script_name;
std::string path_info;
std::string query_string;
/// String to use as server software "NAME/version"
std::string server_software;
/// true if some response has already been sent to the client
bool response_started;
std::string method;
std::string url;
std::string version;
std::map<std::string, std::string> headers;
wibble::Splitter space_splitter;
wibble::net::mime::Reader mime_reader;
std::map<std::string, std::string> extra_response_headers;
Request();
/**
* Read request method and headers from sock
*
* Sock will be positioned at the beginning of the request body, after the
* empty line that follows the header.
*
* The request URL will be parsed in script_name, path_info and
* query_string. At the start, script_name is always / and path_info is the
* rest of the path in the url. One can move path components from path_info
* to script_name as the request is processed.
*
* @returns true if the request has been read, false if EOF was found
* before the end of the headers.
*/
bool read_request();
/**
* Read a fixed amount of data from the file descriptor
*
* @returns true if all the data were read, false if EOF was encountered
* before the end of the buffer
*/
bool read_buf(std::string& res, size_t size);
// Read HTTP method and its following empty line
bool read_method();
/**
* Read HTTP headers
*
* @return true if there still data to read and headers are terminated
* by an empty line, false if headers are terminated by EOF
*/
bool read_headers();
/**
* Set the CGI environment variables for the current process using this
* request
*/
void set_cgi_env();
/// Send the content of buf, verbatim, to the client
void send(const std::string& buf);
/// Send the HTTP status line
void send_status_line(int code, const std::string& msg, const std::string& version = "HTTP/1.0");
/// Send the HTTP server header
void send_server_header();
/// Send the HTTP date header
void send_date_header();
/// Send headers in extra_response_headers
void send_extra_response_headers();
/// Send a string as result
void send_result(const std::string& content, const std::string& content_type="text/html; charset=utf-8", const std::string& filename=std::string());
/// Discard all input from the socket
void discard_input();
/**
* Remove the first component from path_info, append it to script_name and
* return it.
*
* If path_info if empty or only consisting of '/', returns the empty string.
*/
std::string pop_path_info();
/**
* Return the first component from path_info
*
* If path_info if empty or only consisting of '/', returns the empty string.
*/
std::string path_info_head();
};
/// Base interface for GET or POST parameters
struct Param
{
virtual ~Param();
/**
* Parse the value of this parameter from the given unescaped string value.
*
* This can be called more than once, if the value is found multiple
* times. It can also never be called, if the value is never found.
*/
virtual void parse(const std::string& str) = 0;
};
/// Single-valued parameter
struct ParamSingle : public std::string, public Param
{
virtual void parse(const std::string& str);
};
/// Multi-valued parameter
struct ParamMulti : public std::vector<std::string>, public Param
{
virtual void parse(const std::string& str);
};
/**
* File upload parameter
*/
struct FileParam
{
/// Infomation about one uploaded file
struct FileInfo
{
/// File pathname on the local file system
std::string fname;
/// File pathname provided by the client
std::string client_fname;
/**
* Handle a file upload from a multipart/form-data file upload part
*/
bool read(net::mime::Reader& mime_reader,
std::map<std::string, std::string> headers,
const std::string& outdir,
const std::string& fname_blacklist,
const std::string& client_fname,
int sock,
const std::string& boundary,
size_t inputsize);
};
virtual ~FileParam();
/**
* Handle a file upload from a multipart/form-data file upload part
*/
virtual bool read(
net::mime::Reader& mime_reader,
std::map<std::string, std::string> headers,
const std::string& outdir,
const std::string& fname_blacklist,
const std::string& client_fname,
int sock,
const std::string& boundary,
size_t inputsize) = 0;
};
/**
* Single file upload field
*/
struct FileParamSingle : public FileParam
{
FileInfo info;
/**
* If a file name is given, use its base name for storing the file;
* else, use the file name given by the client, without path
*/
FileParamSingle(const std::string& fname=std::string());
virtual bool read(
net::mime::Reader& mime_reader,
std::map<std::string, std::string> headers,
const std::string& outdir,
const std::string& fname_blacklist,
const std::string& client_fname,
int sock,
const std::string& boundary,
size_t inputsize);
};
/**
* Multiple file uploads with the same name
*/
struct FileParamMulti : public FileParam
{
std::vector<FileInfo> files;
virtual bool read(
net::mime::Reader& mime_reader,
std::map<std::string, std::string> headers,
const std::string& outdir,
const std::string& fname_blacklist,
const std::string& client_fname,
int sock,
const std::string& boundary,
size_t inputsize);
};
/**
* Parse and store HTTP query parameters
*
* It is preconfigured by manipulating the various conf_* fields and using the
* add() methods, before calling one of the parse_* methods.
*/
struct Params : public std::map<std::string, Param*>
{
/// File parameters
std::map<std::string, FileParam*> files;
/// Maximum size of POST input data
size_t conf_max_input_size;
/// Maximum size of field data for one non-file field
size_t conf_max_field_size;
/**
* Whether to accept unknown fields.
*
* If true, unkown fields are stored as ParamMulti
*
* If false, unknown fields are ignored.
*/
bool conf_accept_unknown_fields;
/**
* Whether to accept unknown file upload fields.
*
* If true, unkown fields are stored as FileParamMulti
*
* If false, unknown file upload fields are ignored.
*/
bool conf_accept_unknown_file_fields;
/**
* Directory where we write uploaded files
*
* @warning: if it is not set to anything, it ignores all file uploads
*/
std::string conf_outdir;
/**
* String containing blacklist characters that are replaced with "_" in
* the file name. If empty, nothing is replaced.
*
* This only applies to the basename: the pathname is ignored when
* building the local file name.
*/
std::string conf_fname_blacklist;
Params();
~Params();
/// Universal, automatic add method
template<typename TYPE>
TYPE* add(const std::string& name)
{
TYPE* res = new TYPE;
add(name, res);
return res;
}
/// Add a normal parameter to be parsed from the request
void add(const std::string& name, Param* param);
/// Add a file upload parameter to be parsed from the request
void add(const std::string& name, FileParam* param);
/**
* Get a normal fileld during form parsing. Depending on the value of
* conf_accept_unknown_fields, when handling a field that has not been
* added before it will either create it if missing, or just return NULL.
*/
Param* obtain_field(const std::string& name);
/**
* Get a normal fileld during form parsing. Depending on the value of
* conf_accept_unknown_file_fields, when handling a field that has not been
* added before it will either create it if missing, or just return NULL.
*/
FileParam* obtain_file_field(const std::string& name);
/// Get a field by name
Param* field(const std::string& name);
/// Get a file field by name
FileParam* file_field(const std::string& name);
/// Parse parameters as GET or POST according to request method
void parse_get_or_post(net::http::Request& req);
/// Parse parameters from urlencoded form data
void parse_urlencoded(const std::string& qstring);
/// Parse parameters from multipart/form-data
void parse_multipart(net::http::Request& req, size_t inputsize, const std::string& content_type);
/// Parse parameters from HTTP POST input
void parse_post(net::http::Request& req);
};
}
}
}
// vim:set ts=4 sw=4:
#endif
|