GPUdb C++ API  Version 6.2.0.3
Http.hpp
Go to the documentation of this file.
1 #ifndef __GPUDB__HTTP_HPP__
2 #define __GPUDB__HTTP_HPP__
3 
4 #ifndef GPUDB_NO_HTTPS
5 #include <boost/asio/ssl.hpp>
6 #endif
7 
8 #include <stdint.h>
9 #include <map>
10 #include <string>
11 #include <vector>
12 
13 namespace gpudb {
14  class HttpUrl
15  {
16  public:
17  static const std::string HTTP;
18  static const std::string HTTPS;
19 
20  HttpUrl();
21  HttpUrl(const char* url);
22  HttpUrl(const std::string& url);
23  HttpUrl(const std::string& protocol,
24  const std::string& host,
25  uint16_t port,
26  const std::string& path,
27  const std::string& query = std::string());
28  HttpUrl(const HttpUrl& baseUrl,
29  const std::string& path,
30  const std::string& query = std::string());
31 
32  const std::string& getUrl() const;
33  const std::string& getProtocol() const;
34  bool isSecure() const;
35  const std::string& getHost() const;
36  uint16_t getPort() const;
37  const std::string& getPath() const;
38  const std::string& getQuery() const;
39 
44  std::string getAppendedUrl( const std::string& endpoint ) const;
45 
46  operator std::string() const;
47 
48  private:
49  std::string m_url;
50  bool m_secure;
51  std::string m_host;
52  uint16_t m_port;
53  std::string m_path;
54  std::string m_query;
55 
56  void init(const std::string& url);
57  void createUrl();
58  };
59 
60  std::ostream& operator <<(std::ostream& stream, const HttpUrl& value);
61 
62  template<typename T>
64 
65  class HttpResponse;
66 
68  {
69  public:
70  static const std::string GET;
71  static const std::string HEAD;
72  static const std::string POST;
73 
74  HttpRequest();
75  HttpRequest(const HttpUrl& url);
76 
77  #ifndef GPUDB_NO_HTTPS
78  boost::asio::ssl::context* getSslContext();
79  #endif
80 
81  const HttpUrl& getUrl() const;
82  const std::string& getRequestMethod() const;
83  const std::string& getRequestHeader(const std::string& key) const;
84  std::map<std::string, std::string>& getRequestHeaders();
85  size_t getTimeout() const;
86 
87  #ifndef GPUDB_NO_HTTPS
88  void setSslContext(boost::asio::ssl::context* sslContext);
89  #endif
90 
91  void setUrl(const HttpUrl& url);
92  void setRequestMethod(const std::string& method);
93  void addRequestHeader(const std::string& key, const std::string& value);
94  void setTimeout(const size_t timeout);
95 
96  void send(HttpResponse& response);
97 
98  protected:
99  virtual void read(const void*& data, size_t& length) const;
100 
101  private:
102  template<typename T>
103  friend class HttpConnection;
104 
105  #ifndef GPUDB_NO_HTTPS
106  boost::asio::ssl::context* m_sslContext;
107  #endif
108 
109  HttpUrl m_url;
110  std::string m_requestMethod;
111  std::map<std::string, std::string> m_requestHeaders;
112  size_t m_timeout;
113  };
114 
116  {
117  public:
119  BinaryHttpRequest(const HttpUrl& url);
120 
121  const std::vector<uint8_t>* getRequestBody();
122 
123  void setRequestBody(const std::vector<uint8_t>* requestBody);
124 
125  protected:
126  virtual void read(const void*& data, size_t& length) const;
127 
128  private:
129  const std::vector<uint8_t>* m_requestBody;
130  };
131 
133  {
134  public:
136  StringHttpRequest(const HttpUrl& url);
137 
138  const std::string* getRequestBody();
139 
140  void setRequestBody(const std::string* requestBody);
141 
142  protected:
143  virtual void read(const void*& data, size_t& length) const;
144 
145  private:
146  const std::string* m_requestBody;
147  };
148 
150  {
151  public:
152  HttpResponse();
153  virtual ~HttpResponse();
154 
155  const std::string& getResponseVersion() const;
156  unsigned int getResponseCode() const;
157  const std::string& getResponseMessage() const;
158  const std::string& getResponseHeader(const std::string& key) const;
159  const std::map<std::string, std::string>& getResponseHeaders() const;
160 
161  protected:
162  virtual void write(const void* data, const size_t length);
163 
164  private:
165  template<typename T>
166  friend class HttpConnection;
167 
168  std::string m_responseVersion;
169  unsigned int m_responseCode;
170  std::string m_responseMessage;
171  std::map<std::string, std::string> m_responseHeaders;
172  };
173 
175  {
176  public:
177  const std::vector<uint8_t>& getResponseBody() const;
178 
179  protected:
180  virtual void write(const void* data, const size_t length);
181 
182  private:
183  std::vector<uint8_t> m_responseBody;
184  };
185 
187  {
188  public:
189  const std::string& getResponseBody() const;
190 
191  protected:
192  virtual void write(const void* data, const size_t length);
193 
194  private:
195  std::string m_responseBody;
196  };
197 }
198 
199 #endif
bool isSecure() const
const std::string & getProtocol() const
static const std::string POST
Definition: Http.hpp:72
std::ostream & operator<<(std::ostream &stream, const HttpUrl &value)
static const std::string HTTPS
Definition: Http.hpp:18
uint16_t getPort() const
static const std::string GET
Definition: Http.hpp:70
static const std::string HTTP
Definition: Http.hpp:17
const std::string & getHost() const
const std::string & getQuery() const
std::string getAppendedUrl(const std::string &endpoint) const
Returns a string with the passed in endpoint appended to this URL.
const std::string & getUrl() const
static const std::string HEAD
Definition: Http.hpp:71
const std::string & getPath() const