OpenAB  1.0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Http.hpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5  */
10 #ifndef HTTP_HPP_
11 #define HTTP_HPP_
12 
13 #include <curl/curl.h>
14 #include <string>
15 #include <vector>
16 
17 namespace OpenAB
18 {
19 
20 class HttpMessage;
21 
27 {
28  public:
32  HttpSession();
33 
37  virtual ~HttpSession();
38 
42  bool init();
43 
47  void cleanup();
48 
55  bool execute (HttpMessage* msg);
56 
57  void enableTrace(bool enabled);
58 
59  private:
63  HttpSession(HttpSession const &other);
64 
68  HttpSession& operator=(HttpSession const &other);
69 
70  void reset();
71 
72  CURL* curl;
73  curl_slist * internalHeader;
74 
75  std::string lastErrorStr;
76  std::string responseBuffer;
77  std::string responseHeaders;
78  const char* writeBuffer;
79  unsigned int currentWriteOffset;
80  bool traceEnabled;
81 
82  static size_t readResponse (void* ptr, size_t size, size_t nmemb, HttpSession* curl);
83  static size_t readResponseHeaders (void* ptr, size_t size, size_t nmemb, HttpSession* curl);
84  static size_t writeData (void* ptr, size_t size, size_t nmemb, HttpSession* curl);
85  static int writeSeek(HttpSession *curl, curl_off_t offset, int origin);
86 
87  static int printTrace (CURL* curl, curl_infotype type, char* data, size_t size, void* userp);
88 };
89 
90 
96 {
97  public:
101  HttpMessage();
102 
106  ~HttpMessage();
107 
112  {
113  POST = 0, //< POST request
114  GET, //< GET request
115  PUT, //< PUT request
116  CUSTOM //< Custom request
117  };
118 
120  {
121  OK = 200,
122  CREATED = 201,
123  ACCEPTED = 202,
124  NO_CONTENT = 204,
125  MULTISTATUS = 207,
127  BAD_REQUEST = 400,
129  FORBIDDEN = 403,
130  NOT_FOUND = 404,
132  };
133 
134  static std::string responseCodeDescription(long code);
135 
136 
142  void setRequestType(RequestType type);
143 
149  void setRequestType(const std::string& custom);
150 
155  RequestType getRequestType() const;
156 
161  std::string getCustomRequestType() const;
162 
163  typedef std::vector<std::pair<std::string, std::string> > Headers;
164 
170  void appendHeader(const std::string& key, const std::string& value);
171 
175  const Headers& getHeaders() const;
176 
182  void setData (const std::string& data);
183 
188  std::string getData() const;
189 
194  void setFollowRedirection (bool follow);
195 
200  bool followRedirection() const;
201 
206  void setURL (const std::string& url);
207 
212  std::string getURL() const;
213 
219  void setResponse(const std::string& response);
220 
225  std::string getResponse() const;
226 
231  void setResponseHeaders(const std::string& headers);
232 
237  Headers getResponseHeaders() const;
238 
243  void setResponseCode(long code);
244 
249  long getResponseCode() const;
250 
255  void setErrorString(const std::string& errorStr);
256 
261  std::string getErrorString() const;
262 
268  void setCredentials(const std::string& login, const std::string& password);
269 
275  void getCredentials(std::string& login, std::string& password);
276 
282  void enableBasicHttpAuthentication(bool enable);
283 
289 
295  void enableDigestHttpAuthentication(bool enable);
296 
302 
303  private:
307  HttpMessage(HttpMessage const &other);
308 
312  HttpMessage& operator=(HttpMessage const &other);
313 
314  RequestType requestType;
315  std::string customRequestType;
316  Headers headers;
317  std::string data;
318  std::string url;
319  bool redirectionEnabled;
320  long responseCode;
321  std::string response;
322  Headers responseHeaders;
323  std::string errorString;
324  bool basicAuthenticationEnabled;
325  bool digestAuthenticationEnabled;
326  std::string login;
327  std::string password;
328 };
329 
335 {
336  public:
340  virtual ~HttpAuthorizer() {};
341 
347  virtual bool authorizeMessage(HttpMessage* msg) = 0;
348 };
349 
350 } //namespace OpenAB
351 #endif // HTTP_HPP_
void setResponseHeaders(const std::string &headers)
Sets response headers data.
Definition: Http.cpp:150
bool digestHttpAuthenticationEnabled()
Checks if digest HTTP authentication is enabled.
Definition: Http.cpp:123
std::string getURL() const
Definition: Http.cpp:95
void enableDigestHttpAuthentication(bool enable)
Enables or disables digest HTTP authentication.
Definition: Http.cpp:114
Headers getResponseHeaders() const
Gets response headers.
Definition: Http.cpp:168
HttpSession class. Allows to send Http requests.
Definition: Http.hpp:26
void setResponseCode(long code)
Sets request response code.
Definition: Http.cpp:173
void setFollowRedirection(bool follow)
Should redirections be followed.
Definition: Http.cpp:80
void enableBasicHttpAuthentication(bool enable)
Enables or disables basic HTTP authentication.
Definition: Http.cpp:100
HttpAuthorizer interface. This should be used by classes implementing different Http authorization me...
Definition: Http.hpp:334
std::vector< std::pair< std::string, std::string > > Headers
Definition: Http.hpp:163
bool execute(HttpMessage *msg)
Executes given http message. After executing message object will be updated with response, response code etc.
Definition: Http.cpp:288
RequestType getRequestType() const
Returns currently set request type.
Definition: Http.cpp:49
Definition: Http.hpp:130
Definition: Http.hpp:116
Definition: Http.hpp:115
bool followRedirection() const
Returns status of redirections.
Definition: Http.cpp:85
Definition: Http.hpp:127
void setData(const std::string &data)
Sets body of message.
Definition: Http.cpp:70
std::string getData() const
Definition: Http.cpp:75
Definition: Http.hpp:121
void cleanup()
Cleans up http session.
Definition: Http.cpp:257
void setResponse(const std::string &response)
Sets response data.
Definition: Http.cpp:140
const Headers & getHeaders() const
Returns all headers set for message.
Definition: Http.cpp:65
void getCredentials(std::string &login, std::string &password)
Gets credential for HTTP authentication.
Definition: Http.cpp:134
~HttpMessage()
Destructor, virtual by default.
Definition: Http.cpp:30
Definition: Http.hpp:114
bool init()
Initializes http session.
Definition: Http.cpp:238
virtual bool authorizeMessage(HttpMessage *msg)=0
Authorizes message.
Definition: Http.hpp:122
std::string getResponse() const
Gets request response.
Definition: Http.cpp:145
void setRequestType(RequestType type)
Sets message request type.
Definition: Http.cpp:35
static std::string responseCodeDescription(long code)
Definition: Http.cpp:193
ResponseCode
Definition: Http.hpp:119
Definition: Http.hpp:129
void appendHeader(const std::string &key, const std::string &value)
Appends given header to message headers.
Definition: Http.cpp:59
HttpSession()
Constructor.
Definition: Http.cpp:224
long getResponseCode() const
Gets request response code.
Definition: Http.cpp:178
virtual ~HttpSession()
Destructor, virtual by default.
Definition: Http.cpp:233
Definition: Http.hpp:128
Definition: Http.hpp:123
Definition: Http.hpp:124
void setURL(const std::string &url)
Sets URL of message.
Definition: Http.cpp:90
std::string getCustomRequestType() const
Returns custom request type, if request type is set to RequestType::CUSTOM.
Definition: Http.cpp:54
bool basicHttpAuthenticationEnabled()
Checks if basic HTTP authentication is enabled.
Definition: Http.cpp:109
virtual ~HttpAuthorizer()
Destructor, virtual by default.
Definition: Http.hpp:340
Definition: Http.hpp:125
void setCredentials(const std::string &login, const std::string &password)
Sets credentials for HTTP authentication.
Definition: Http.cpp:128
HttpMessage()
Constructor.
Definition: Http.cpp:20
void setErrorString(const std::string &errorStr)
Sets error description.
Definition: Http.cpp:183
HttpMessage class. Represents Http request that can be executed using HttpSession object...
Definition: Http.hpp:95
void enableTrace(bool enabled)
Definition: Http.cpp:283
Definition: Http.hpp:113
RequestType
Definition: Http.hpp:111
std::string getErrorString() const
Gets error description.
Definition: Http.cpp:188