1 #ifndef _damon_request_h
2 #define _damon_request_h
4 #include "Details/prologue.h"
7 #include "Details/Header.h"
14 /** A request to be sent to a server.
15 * To construct a complex request, try this
18 * req("header-name", "value")
19 * ("another-header-name", "another value")
20 * (some_serializable_object)
23 class DAMON_API Request
26 //! Type of the method to be used in the request
28 //! send the request with the OPTIONS method
30 //! send the request with the GET method
32 //! send the request with the HEAD method
34 //! send the request with the POST method
36 //! send the request with the PUT method
38 //! send the request with the DELETE method
40 //! send the request with the TRACE method
42 //! send the request with the CONNECT method
46 //! Authorization method to use for user and password in url
47 enum AuthorizationMethod
{
50 //! use basic authorization
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
60 * \param method the method that should be used with the request, GET by
62 Request(const std::string
& url
, Method method
= get__
, AuthorizationMethod auth_method
= none__
);
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
)); }
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
);