MICO Platform  1.0.0
 All Classes Namespaces Functions Variables Friends
http_client.hpp
1 #ifndef HAVE_HTTP_CLIENT_H
2 #define HAVE_HTTP_CLIENT_H 1
3 
4 #include <string>
5 #include <map>
6 #include <iostream>
7 
8 
9 struct curl_slist;
10 
11 namespace mico {
12  namespace http {
13 
17  enum Method {
18  GET, POST, PUT, DELETE
19  };
20 
21 
22  class Body {
23 
24  friend class Request;
25  friend class HTTPClient;
26  friend size_t write_callback(void *ptr, size_t size, size_t nmemm, void *response);
27  friend size_t read_callback(void *ptr, size_t size, size_t nmemb, void *request);
28 
29  private:
30  char* ptr;
31  size_t length;
32  std::string type;
33  bool managed;
34  size_t pos; // internal use by read_callback
35 
36  Body() : ptr(NULL), length(0), managed(false) {};
37 
38  public:
39 
40  Body(char* ptr, size_t length, const std::string& content_type) : ptr(ptr), length(length), type(content_type), managed(false), pos(0) {};
41  Body(const std::string& data, const std::string& content_type);
42  ~Body();
43 
44 
45  const char* getContent() const { return ptr; };
46 
50  size_t getContentLength() const { return length; };
51 
55  const std::string& getContentType() const { return type; };
56 
57  };
58 
59 
60  class Message {
61  friend class HTTPClient;
62  friend std::ostream& operator<<(std::ostream& os, const Message& req);
63  friend std::istream& operator>>(std::istream& is, const Message& req);
64 
65  typedef std::map<std::string,std::string> header_map;
66 
67  protected:
68 
69  // headers
70  header_map headers;
71 
72  // optional request body
73  Body* body;
74 
75  public:
76 
77  Message() : body(NULL) { headers["Expect"]=""; };
78  virtual ~Message();
79 
83  inline const std::string& getHeader(const std::string hdr) const { return headers.at(hdr); };
84 
85 
89  inline const Body* getBody() const { return body; };
90 
91 
92 
96  curl_slist* getCurlHeaders();
97 
101  void setCurlHeaders(curl_slist* hdr);
102 
103  };
104 
105 
106 
107  class Request : public Message{
108 
109  friend std::ostream& operator<<(std::ostream& os, const Request& req);
110  friend std::istream& operator>>(std::istream& is, const Request& req);
111 
112  friend size_t read_callback(void *ptr, size_t size, size_t nmemb, void *request);
113 
114  private:
115  // request type to send
116  Method type;
117 
118  // request URL
119  std::string url;
120 
121 
122  public:
123 
124  Request(Method type, std::string url) : Message(), type(type), url(url) {};
125 
129  inline Method getMethod() const { return type; };
130 
134  inline const std::string& getURL() const { return url; }
135 
140  inline Request& setHeader(const std::string hdr, const std::string value) { headers[hdr] = value; return *this; };
141 
142 
146  inline Request& delHeader(const std::string hdr) { headers.erase(hdr); return *this; };
147 
154  Request& setBody(char *ptr, size_t length, const std::string content_type);
155 
156 
162  Request& setBody(const std::string data, const std::string content_type);
163 
164 
169  inline Request& setBody(Body& body) { return setBody(body.ptr, body.length, body.type); };
170 
171 
172 
173  };
174 
175 
176 
177  class Response : public Message {
178  friend size_t header_callback(char *buffer, size_t size, size_t nitems, void *response);
179  friend size_t write_callback(void *ptr, size_t size, size_t nmemm, void *response);
180 
181  friend class HTTPClient;
182 
183  private:
184  long status;
185 
186  public:
187  Response() : Message() {};
188 
189 
190  long getStatus() const { return status; };
191 
192  };
193 
194 
198  class HTTPClient {
199 
200  public:
201  HTTPClient();
202  ~HTTPClient();
203 
204 
205  Response* execute(Request& req);
206 
207  };
208 
209 
210  std::ostream& operator<<(std::ostream& os, const Message& req);
211  std::ostream& operator<<(std::ostream& os, const Request& req);
212  std::ostream& operator<<(std::ostream& os, const Response& resp);
213  std::istream& operator>>(std::istream& is, const Message& req);
214  std::istream& operator>>(std::istream& is, const Request& req);
215 
216 
217  }
218 }
219 
220 
221 #endif
size_t getContentLength() const
Get the length of the content of this request.
Definition: http_client.hpp:50
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in c...
Definition: http_client.cpp:23
HTTPClient()
Initialise cURL HTTP client; carries out main cURL initialisation and registers callbacks.
Definition: http_client.cpp:172
const Body * getBody() const
Return the body of the message.
Definition: http_client.hpp:89
Request & setBody(char *ptr, size_t length, const std::string content_type)
Set the request body for the request, starting at the given memory location and counting length bytes...
Definition: http_client.hpp:177
const std::string & getContentType() const
Get the type of the content of this request.
Definition: http_client.hpp:55
Request & setHeader(const std::string hdr, const std::string value)
Add an HTTP header to the request.
Definition: http_client.hpp:140
curl_slist * getCurlHeaders()
Return the headers in CURL format.
Definition: http_client.cpp:50
Request & delHeader(const std::string hdr)
Remove an HTTP header from the request.
Definition: http_client.hpp:146
Definition: http_client.hpp:22
Request & setBody(Body &body)
Set the request body for the request, using the given body object.
Definition: http_client.hpp:169
A simple HTTP client.
Definition: http_client.hpp:198
Method getMethod() const
Return the request method.
Definition: http_client.hpp:129
void setCurlHeaders(curl_slist *hdr)
Set the headers in CURL format.
Definition: http_client.cpp:62
Definition: http_client.hpp:60
const std::string & getHeader(const std::string hdr) const
Return the header with the given name.
Definition: http_client.hpp:83
Definition: http_client.hpp:107
const std::string & getURL() const
Return the request URL.
Definition: http_client.hpp:134
~HTTPClient()
Clean up cURL HTTP client; carries out main cURL shutdown.
Definition: http_client.cpp:180