GPUdb C++ API  Version 7.0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  friend bool operator ==(const HttpUrl &lhs, const HttpUrl &rhs);
49  friend bool operator !=(const HttpUrl &lhs, const HttpUrl &rhs);
50 
51  private:
52  std::string m_url;
53  bool m_secure;
54  std::string m_host;
55  uint16_t m_port;
56  std::string m_path;
57  std::string m_query;
58 
59  void init(const std::string& url);
60  void createUrl();
61  };
62 
63  std::ostream& operator <<(std::ostream &stream, const HttpUrl &value);
64 
65 
66 
67  template<typename T>
69 
70  class HttpResponse;
71 
73  {
74  public:
75  static const std::string GET;
76  static const std::string HEAD;
77  static const std::string POST;
78 
79  HttpRequest();
80  HttpRequest(const HttpUrl& url);
81 
82  #ifndef GPUDB_NO_HTTPS
83  boost::asio::ssl::context* getSslContext();
84  #endif
85 
86  const HttpUrl& getUrl() const;
87  const std::string& getRequestMethod() const;
88  const std::string& getRequestHeader(const std::string& key) const;
89  std::map<std::string, std::string>& getRequestHeaders();
90  size_t getTimeout() const;
91 
92  #ifndef GPUDB_NO_HTTPS
93  void setSslContext(boost::asio::ssl::context* sslContext);
94  #endif
95 
96  void setUrl(const HttpUrl& url);
97  void setRequestMethod(const std::string& method);
98  void addRequestHeader(const std::string& key, const std::string& value);
99  void setTimeout(const size_t timeout);
100 
101  void send(HttpResponse& response);
102 
103  protected:
104  virtual void read(const void*& data, size_t& length) const;
105 
106  private:
107  template<typename T>
108  friend class HttpConnection;
109 
110  #ifndef GPUDB_NO_HTTPS
111  boost::asio::ssl::context* m_sslContext;
112  #endif
113 
114  HttpUrl m_url;
115  std::string m_requestMethod;
116  std::map<std::string, std::string> m_requestHeaders;
117  size_t m_timeout;
118  };
119 
121  {
122  public:
124  BinaryHttpRequest(const HttpUrl& url);
125 
126  const std::vector<uint8_t>* getRequestBody();
127 
128  void setRequestBody(const std::vector<uint8_t>* requestBody);
129 
130  protected:
131  virtual void read(const void*& data, size_t& length) const;
132 
133  private:
134  const std::vector<uint8_t>* m_requestBody;
135  };
136 
138  {
139  public:
141  StringHttpRequest(const HttpUrl& url);
142 
143  const std::string* getRequestBody();
144 
145  void setRequestBody(const std::string* requestBody);
146 
147  protected:
148  virtual void read(const void*& data, size_t& length) const;
149 
150  private:
151  const std::string* m_requestBody;
152  };
153 
155  {
156  public:
157  HttpResponse();
158  virtual ~HttpResponse();
159 
160  const std::string& getResponseVersion() const;
161  unsigned int getResponseCode() const;
162  const std::string& getResponseMessage() const;
163  const std::string& getResponseHeader(const std::string& key) const;
164  const std::map<std::string, std::string>& getResponseHeaders() const;
165 
166  protected:
167  virtual void write(const void* data, const size_t length);
168 
169  private:
170  template<typename T>
171  friend class HttpConnection;
172 
173  std::string m_responseVersion;
174  unsigned int m_responseCode;
175  std::string m_responseMessage;
176  std::map<std::string, std::string> m_responseHeaders;
177  };
178 
180  {
181  public:
182  const std::vector<uint8_t>& getResponseBody() const;
183 
184  protected:
185  virtual void write(const void* data, const size_t length);
186 
187  private:
188  std::vector<uint8_t> m_responseBody;
189  };
190 
192  {
193  public:
194  const std::string& getResponseBody() const;
195 
196  protected:
197  virtual void write(const void* data, const size_t length);
198 
199  private:
200  std::string m_responseBody;
201  };
202 }
203 
204 #endif
const std::string & getPath() const
const std::map< std::string, std::string > & getResponseHeaders() const
const std::vector< uint8_t > * getRequestBody()
const std::string & getProtocol() const
virtual void write(const void *data, const size_t length)
bool isSecure() const
friend bool operator==(const HttpUrl &lhs, const HttpUrl &rhs)
std::map< std::string, std::string > & getRequestHeaders()
void setRequestBody(const std::string *requestBody)
const std::string & getResponseHeader(const std::string &key) const
const std::string & getResponseMessage() const
boost::asio::ssl::context * getSslContext()
virtual void read(const void *&data, size_t &length) const
virtual void read(const void *&data, size_t &length) const
void setRequestMethod(const std::string &method)
std::string getAppendedUrl(const std::string &endpoint) const
Returns a string with the passed in endpoint appended to this URL.
static const std::string POST
Definition: Http.hpp:77
unsigned int getResponseCode() const
virtual void write(const void *data, const size_t length)
const std::string & getUrl() const
const std::vector< uint8_t > & getResponseBody() const
void setRequestBody(const std::vector< uint8_t > *requestBody)
const std::string & getRequestMethod() const
std::ostream & operator<<(std::ostream &stream, const HttpUrl &value)
void setTimeout(const size_t timeout)
void send(HttpResponse &response)
void setUrl(const HttpUrl &url)
friend bool operator!=(const HttpUrl &lhs, const HttpUrl &rhs)
static const std::string HTTPS
Definition: Http.hpp:18
static const std::string GET
Definition: Http.hpp:75
size_t getTimeout() const
const std::string & getResponseBody() const
static const std::string HTTP
Definition: Http.hpp:17
const std::string * getRequestBody()
virtual void write(const void *data, const size_t length)
void setSslContext(boost::asio::ssl::context *sslContext)
void addRequestHeader(const std::string &key, const std::string &value)
const std::string & getHost() const
const std::string & getQuery() const
const std::string & getRequestHeader(const std::string &key) const
uint16_t getPort() const
const std::string & getResponseVersion() const
static const std::string HEAD
Definition: Http.hpp:76
const HttpUrl & getUrl() const
virtual void read(const void *&data, size_t &length) const
virtual ~HttpResponse()