Port to verion 1.39.0 of Boost
[Arachnida.git] / lib / Damon / Request.h
blobd5c8649ff3295e14289439fe2bbfc51af468ddac
1 #ifndef _damon_request_h
2 #define _damon_request_h
4 #include "Details/prologue.h"
5 #include <string>
6 #include "Response.h"
7 #include "Details/Header.h"
9 namespace Damon
11 class Request;
12 class Session;
14 /** A request to be sent to a server.
15 * To construct a complex request, try this
16 * \code
17 * Request req("url");
18 * req("header-name", "value")
19 * ("another-header-name", "another value")
20 * (some_serializable_object)
21 * ;
22 * \endcode */
23 class DAMON_API Request
25 public :
26 //! Type of the method to be used in the request
27 enum Method {
28 //! send the request with the OPTIONS method
29 options__,
30 //! send the request with the GET method
31 get__,
32 //! send the request with the HEAD method
33 head__,
34 //! send the request with the POST method
35 post__,
36 //! send the request with the PUT method
37 put__,
38 //! send the request with the DELETE method
39 delete__,
40 //! send the request with the TRACE method
41 trace__,
42 //! send the request with the CONNECT method
43 connect__
46 //! Authorization method to use for user and password in url
47 enum AuthorizationMethod {
48 //! no authorization
49 none__,
50 //! use basic authorization
51 basic__,
54 /** Construct a request.
55 * \param url the URL of the resource you're requesting. Should normally
56 * at least include http:// or https://, and the server name. The
57 * URL will be parsed to extract the server name and port number,
58 * as well as whether or not HTTPS should be used to secure the
59 * connection.
60 * \param method the method that should be used with the request, GET by
61 * default.*/
62 Request(const std::string & url, Method method = get__, AuthorizationMethod auth_method = none__);
63 ~Request();
65 /** Add a header to the response.
66 * \param name name of the header to be added
67 * \param value value of the header to be added */
68 void addHeader(const std::string & name, const std::string & value) { header_fields_.push_back(Details::Header(name, value)); }
69 /** Add a header to the response.
70 * \param name name of the header to be added
71 * \param value value of the header to be added */
72 Request & operator()(const std::string & name, const std::string & value) { addHeader(name, value); return *this; }
74 //! Set the body of the response
75 void setBody(const std::vector< char > & body) { body_ = body; }
76 //! Set the body of the response
77 Request & operator()(const std::vector< char > & body) { setBody(body); return *this; }
78 //! Set the body of the response
79 Request & operator()(const std::string & body) { setBody(std::vector< char >(body.begin(), body.end())); return *this; }
80 //! Set the body of the response
81 Request & operator()(const char * body) { return (*this)(std::string(body)); }
82 //! Set the body of the response
83 template < typename T >
84 Request & operator()(const T & body) { return (*this)(serialize(body)); }
86 private :
87 std::string url_;
88 Method method_;
89 AuthorizationMethod auth_method_;
90 Details::HeaderFields header_fields_; ///< all header fields found while parsing the request
91 std::vector< char > body_;
93 friend DAMON_API Response send(Session & /*session*/, const Request &/* request*/);
94 friend DAMON_API std::vector< Response > send(Session & /*session*/, const std::vector< Request > & requests);
95 friend DAMON_API std::string serialize(Request /* request*/);
98 DAMON_API Response send(Session & session, const Request & request);
99 DAMON_API Response send(const Request & request);
100 DAMON_API std::vector< Response > send(Session & session, const std::vector< Request > & requests);
101 DAMON_API std::vector< Response > send(const std::vector< Request > & requests);
102 DAMON_API std::string serialize(Request request);
105 #endif