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