http.h
Go to the documentation of this file.
#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;
std::string server_software;
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();
bool read_request();
bool read_buf(std::string& res, size_t size);
// Read HTTP method and its following empty line
bool read_method();
bool read_headers();
void set_cgi_env();
void send(const std::string& buf);
void send_status_line(int code, const std::string& msg, const std::string& version = "HTTP/1.0");
void send_server_header();
void send_date_header();
void send_extra_response_headers();
void send_result(const std::string& content, const std::string& content_type="text/html; charset=utf-8", const std::string& filename=std::string());
void discard_input();
std::string pop_path_info();
std::string path_info_head();
};
struct Param
{
virtual ~Param();
virtual void parse(const std::string& str) = 0;
};
struct ParamSingle : public std::string, public Param
{
virtual void parse(const std::string& str);
};
struct ParamMulti : public std::vector<std::string>, public Param
{
virtual void parse(const std::string& str);
};
struct FileParam
{
struct FileInfo
{
std::string fname;
std::string client_fname;
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();
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;
};
struct FileParamSingle : public FileParam
{
FileInfo info;
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);
};
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);
};
struct Params : public std::map<std::string, Param*>
{
std::map<std::string, FileParam*> files;
size_t conf_max_input_size;
size_t conf_max_field_size;
bool conf_accept_unknown_fields;
bool conf_accept_unknown_file_fields;
std::string conf_outdir;
std::string conf_fname_blacklist;
Params();
~Params();
template<typename TYPE>
TYPE* add(const std::string& name)
{
TYPE* res = new TYPE;
add(name, res);
return res;
}
void add(const std::string& name, Param* param);
void add(const std::string& name, FileParam* param);
Param* obtain_field(const std::string& name);
FileParam* obtain_file_field(const std::string& name);
Param* field(const std::string& name);
FileParam* file_field(const std::string& name);
void parse_get_or_post(net::http::Request& req);
void parse_urlencoded(const std::string& qstring);
void parse_multipart(net::http::Request& req, size_t inputsize, const std::string& content_type);
void parse_post(net::http::Request& req);
};
}
}
}
// vim:set ts=4 sw=4:
#endif
wibble::Splitter
Split a string using a regular expression to match the token separators.
Definition: regexp.h:146
mime.h
wibble::list::map
Map< List, F > map(const List &l, const F &f)
Definition: list.h:381
wibble::sys::fs::size
size_t size(const std::string &file)
File size.
Definition: fs.cpp:287
wibble
Definition: amorph.h:17
regexp.h
wibble::net::http::FileParamMulti
Multiple file uploads with the same name.
Definition: http.h:272
wibble::net::http::FileParamMulti::files
std::vector< FileInfo > files
Definition: http.h:273
wibble::net::http::FileParamSingle
Single file upload field.
Definition: http.h:248
wibble::net::http::FileParamSingle::info
FileInfo info
Definition: http.h:249
wibble::net::http::FileParam::FileInfo
Infomation about one uploaded file.
Definition: http.h:209
wibble::net::http::FileParam::FileInfo::fname
std::string fname
File pathname on the local file system.
Definition: http.h:211
wibble::net::http::FileParam::FileInfo::client_fname
std::string client_fname
File pathname provided by the client.
Definition: http.h:213
wibble::net::http::FileParam
File upload parameter.
Definition: http.h:206
wibble::net::http::FileParam::read
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
Handle a file upload from a multipart/form-data file upload part.
wibble::net::http::ParamMulti
Multi-valued parameter.
Definition: http.h:198
wibble::net::http::ParamSingle
Single-valued parameter.
Definition: http.h:192
wibble::net::http::Param
Base interface for GET or POST parameters.
Definition: http.h:178
wibble::net::http::Param::parse
virtual void parse(const std::string &str)=0
Parse the value of this parameter from the given unescaped string value.
wibble::net::http::Params
Parse and store HTTP query parameters.
Definition: http.h:293
wibble::net::http::Params::conf_max_field_size
size_t conf_max_field_size
Maximum size of field data for one non-file field.
Definition: http.h:301
wibble::net::http::Params::conf_fname_blacklist
std::string conf_fname_blacklist
String containing blacklist characters that are replaced with "_" in the file name.
Definition: http.h:335
wibble::net::http::Params::conf_max_input_size
size_t conf_max_input_size
Maximum size of POST input data.
Definition: http.h:298
wibble::net::http::Params::conf_accept_unknown_fields
bool conf_accept_unknown_fields
Whether to accept unknown fields.
Definition: http.h:310
wibble::net::http::Params::add
TYPE * add(const std::string &name)
Universal, automatic add method.
Definition: http.h:343
wibble::net::http::Params::files
std::map< std::string, FileParam * > files
File parameters.
Definition: http.h:295
wibble::net::http::Params::conf_outdir
std::string conf_outdir
Directory where we write uploaded files.
Definition: http.h:326
wibble::net::http::Params::conf_accept_unknown_file_fields
bool conf_accept_unknown_file_fields
Whether to accept unknown file upload fields.
Definition: http.h:319
wibble::net::http::Request
Definition: http.h:69
wibble::net::http::Request::server_name
std::string server_name
Definition: http.h:76
wibble::net::http::Request::peer_hostname
std::string peer_hostname
Definition: http.h:73
wibble::net::http::Request::query_string
std::string query_string
Definition: http.h:80
wibble::net::http::Request::headers
std::map< std::string, std::string > headers
Definition: http.h:89
wibble::net::http::Request::method
std::string method
Definition: http.h:86
wibble::net::http::Request::version
std::string version
Definition: http.h:88
wibble::net::http::Request::url
std::string url
Definition: http.h:87
wibble::net::http::Request::mime_reader
wibble::net::mime::Reader mime_reader
Definition: http.h:92
wibble::net::http::Request::path_info
std::string path_info
Definition: http.h:79
wibble::net::http::Request::response_started
bool response_started
true if some response has already been sent to the client
Definition: http.h:84
wibble::net::http::Request::server_port
std::string server_port
Definition: http.h:77
wibble::net::http::Request::extra_response_headers
std::map< std::string, std::string > extra_response_headers
Definition: http.h:94
wibble::net::http::Request::server_software
std::string server_software
String to use as server software "NAME/version".
Definition: http.h:82
wibble::net::http::Request::script_name
std::string script_name
Definition: http.h:78
wibble::net::http::Request::space_splitter
wibble::Splitter space_splitter
Definition: http.h:90
wibble::net::http::Request::sock
int sock
Definition: http.h:72
wibble::net::http::Request::peer_port
std::string peer_port
Definition: http.h:75
wibble::net::http::Request::peer_hostaddr
std::string peer_hostaddr
Definition: http.h:74
wibble::net::http::error400
Definition: http.h:55
wibble::net::http::error400::error400
error400(const std::string &msg)
Definition: http.h:57
wibble::net::http::error400::error400
error400()
Definition: http.h:56
wibble::net::http::error404
Definition: http.h:61
wibble::net::http::error404::error404
error404(const std::string &msg)
Definition: http.h:63
wibble::net::http::error404::error404
error404()
Definition: http.h:62
wibble::net::http::error
Definition: http.h:38
wibble::net::http::error::error
error(int code, const std::string &desc, const std::string &msg)
Definition: http.h:45
wibble::net::http::error::msg
std::string msg
Definition: http.h:41
wibble::net::http::error::error
error(int code, const std::string &desc)
Definition: http.h:43
wibble::net::http::error::send
virtual void send(Request &req)
Definition: http.cpp:44
wibble::net::http::error::code
int code
Definition: http.h:39
wibble::net::http::error::desc
std::string desc
Definition: http.h:40
wibble::net::http::error::what
virtual const char * what() const
Definition: http.cpp:38
wibble::net::http::error::~error
virtual ~error()
Definition: http.h:47
wibble::net::mime::Reader
Definition: mime.h:34