CppServer  1.0.4.0
C++ Server Library
https_client.h
Go to the documentation of this file.
1 
9 #ifndef CPPSERVER_HTTP_HTTPS_CLIENT_H
10 #define CPPSERVER_HTTP_HTTPS_CLIENT_H
11 
12 #include "http_request.h"
13 #include "http_response.h"
14 
15 #include "server/asio/ssl_client.h"
16 #include "server/asio/timer.h"
17 
18 #include <future>
19 
20 namespace CppServer {
21 namespace HTTP {
22 
24 
32 {
33 public:
34  using SSLClient::SSLClient;
35 
36  HTTPSClient(const HTTPSClient&) = delete;
37  HTTPSClient(HTTPSClient&&) = delete;
38  virtual ~HTTPSClient() = default;
39 
40  HTTPSClient& operator=(const HTTPSClient&) = delete;
42 
44  HTTPRequest& request() noexcept { return _request; }
45  const HTTPRequest& request() const noexcept { return _request; }
46 
48 
51  size_t SendRequest() { return SendRequest(_request); }
53 
57  size_t SendRequest(const HTTPRequest& request) { return Send(request.cache()); }
58 
60 
64  size_t SendRequestBody(std::string_view body) { return Send(body); }
66 
71  size_t SendRequestBody(const void* buffer, size_t size) { return Send(buffer, size); }
72 
74 
78  size_t SendRequest(const CppCommon::Timespan& timeout) { return SendRequest(_request, timeout); }
80 
85  size_t SendRequest(const HTTPRequest& request, const CppCommon::Timespan& timeout) { return Send(request.cache(), timeout); }
86 
88 
93  size_t SendRequestBody(std::string_view body, const CppCommon::Timespan& timeout) { return Send(body, timeout); }
95 
101  size_t SendRequestBody(const void* buffer, size_t size, const CppCommon::Timespan& timeout) { return Send(buffer, size, timeout); }
102 
104 
109 
114 
116 
120  bool SendRequestBodyAsync(std::string_view body) { return SendAsync(body); }
122 
127  bool SendRequestBodyAsync(const void* buffer, size_t size) { return SendAsync(buffer, size); }
128 
129 protected:
130  void onReceived(const void* buffer, size_t size) override;
131  void onDisconnected() override;
132 
134 
140  virtual void onReceivedResponseHeader(const HTTPResponse& response) {}
141 
143 
149  virtual void onReceivedResponse(const HTTPResponse& response) {}
150 
152 
159  virtual void onReceivedResponseError(const HTTPResponse& response, const std::string& error) {}
160 
161 protected:
166 };
167 
169 
176 {
177 public:
179 
181  std::shared_ptr<Asio::TCPResolver>& resolver() noexcept { return _resolver; }
182  const std::shared_ptr<Asio::TCPResolver>& resolver() const noexcept { return _resolver; }
184  std::shared_ptr<Asio::Timer>& timeout() noexcept { return _timeout; }
185  const std::shared_ptr<Asio::Timer>& timeout() const noexcept { return _timeout; }
186 
188 
192  std::future<HTTPResponse> SendRequest(const CppCommon::Timespan& timeout = CppCommon::Timespan::minutes(1)) { return SendRequest(_request, timeout); }
194 
199  std::future<HTTPResponse> SendRequest(const HTTPRequest& request, const CppCommon::Timespan& timeout = CppCommon::Timespan::minutes(1));
200 
202 
207  std::future<HTTPResponse> SendHeadRequest(std::string_view url, const CppCommon::Timespan& timeout = CppCommon::Timespan::minutes(1))
208  { return SendRequest(_request.MakeHeadRequest(url), timeout); }
210 
215  std::future<HTTPResponse> SendGetRequest(std::string_view url, const CppCommon::Timespan& timeout = CppCommon::Timespan::minutes(1))
216  { return SendRequest(_request.MakeGetRequest(url), timeout); }
218 
224  std::future<HTTPResponse> SendPostRequest(std::string_view url, std::string_view content, const CppCommon::Timespan& timeout = CppCommon::Timespan::minutes(1))
225  { return SendRequest(_request.MakePostRequest(url, content), timeout); }
227 
233  std::future<HTTPResponse> SendPutRequest(std::string_view url, std::string_view content, const CppCommon::Timespan& timeout = CppCommon::Timespan::minutes(1))
234  { return SendRequest(_request.MakePutRequest(url, content), timeout); }
236 
241  std::future<HTTPResponse> SendDeleteRequest(std::string_view url, const CppCommon::Timespan& timeout = CppCommon::Timespan::minutes(1))
242  { return SendRequest(_request.MakeDeleteRequest(url), timeout); }
244 
249  std::future<HTTPResponse> SendOptionsRequest(std::string_view url, const CppCommon::Timespan& timeout = CppCommon::Timespan::minutes(1))
252 
257  std::future<HTTPResponse> SendTraceRequest(std::string_view url, const CppCommon::Timespan& timeout = CppCommon::Timespan::minutes(1))
258  { return SendRequest(_request.MakeTraceRequest(url), timeout); }
259 
260 protected:
261  void onHandshaked() override;
262  void onDisconnected() override;
263  void onReceivedResponse(const HTTPResponse& response) override;
264  void onReceivedResponseError(const HTTPResponse& response, const std::string& error) override;
265 
266 private:
267  std::shared_ptr<Asio::TCPResolver> _resolver;
268  std::shared_ptr<Asio::Timer> _timeout;
269  std::promise<HTTPResponse> _promise;
270 
271  void SetPromiseValue(const HTTPResponse& response);
272  void SetPromiseError(const std::string& error);
273 };
274 
277 } // namespace HTTP
278 } // namespace CppServer
279 
280 #endif // CPPSERVER_HTTP_HTTPS_CLIENT_H
virtual size_t Send(const void *buffer, size_t size)
Send data to the server (synchronous)
Definition: ssl_client.cpp:655
virtual bool SendAsync(const void *buffer, size_t size)
Send data to the server (asynchronous)
Definition: ssl_client.cpp:753
const std::string & cache() const noexcept
Get the HTTP request cache content.
Definition: http_request.h:77
HTTPRequest & MakePutRequest(std::string_view url, std::string_view content, std::string_view content_type="text/plain; charset=UTF-8")
Make PUT request.
HTTPRequest & MakeHeadRequest(std::string_view url)
Make HEAD request.
HTTPRequest & MakeDeleteRequest(std::string_view url)
Make DELETE request.
HTTPRequest & MakePostRequest(std::string_view url, std::string_view content, std::string_view content_type="text/plain; charset=UTF-8")
Make POST request.
HTTPRequest & MakeOptionsRequest(std::string_view url)
Make OPTIONS request.
HTTPRequest & MakeGetRequest(std::string_view url)
Make GET request.
HTTPRequest & MakeTraceRequest(std::string_view url)
Make TRACE request.
HTTPS extended client.
Definition: https_client.h:176
void onDisconnected() override
Handle client disconnected notification.
const std::shared_ptr< Asio::Timer > & timeout() const noexcept
Definition: https_client.h:185
const std::shared_ptr< Asio::TCPResolver > & resolver() const noexcept
Definition: https_client.h:182
void onReceivedResponse(const HTTPResponse &response) override
Handle HTTP response received notification.
void onReceivedResponseError(const HTTPResponse &response, const std::string &error) override
Handle HTTP response error notification.
std::future< HTTPResponse > SendHeadRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send HEAD request.
Definition: https_client.h:207
std::future< HTTPResponse > SendGetRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send GET request.
Definition: https_client.h:215
std::shared_ptr< Asio::Timer > & timeout() noexcept
Get the timeout check timer.
Definition: https_client.h:184
std::future< HTTPResponse > SendDeleteRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send DELETE request.
Definition: https_client.h:241
std::future< HTTPResponse > SendRequest(const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send HTTP request.
Definition: https_client.h:192
std::future< HTTPResponse > SendOptionsRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send OPTIONS request.
Definition: https_client.h:249
std::future< HTTPResponse > SendPutRequest(std::string_view url, std::string_view content, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send PUT request.
Definition: https_client.h:233
std::future< HTTPResponse > SendTraceRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send TRACE request.
Definition: https_client.h:257
void onHandshaked() override
Handle session handshaked notification.
std::future< HTTPResponse > SendPostRequest(std::string_view url, std::string_view content, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send POST request.
Definition: https_client.h:224
std::shared_ptr< Asio::TCPResolver > & resolver() noexcept
Get the TCP resolver.
Definition: https_client.h:181
HTTPSClient(HTTPSClient &&)=delete
HTTPSClient(const HTTPSClient &)=delete
HTTPResponse _response
HTTP response.
Definition: https_client.h:165
size_t SendRequestBody(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
Send the HTTP request body with timeout (synchronous)
Definition: https_client.h:101
virtual void onReceivedResponseError(const HTTPResponse &response, const std::string &error)
Handle HTTP response error notification.
Definition: https_client.h:159
HTTPRequest _request
HTTP request.
Definition: https_client.h:163
size_t SendRequestBody(std::string_view body)
Send the HTTP request body (synchronous)
Definition: https_client.h:64
size_t SendRequest(const HTTPRequest &request)
Send the HTTP request (synchronous)
Definition: https_client.h:57
void onDisconnected() override
Handle client disconnected notification.
size_t SendRequest(const HTTPRequest &request, const CppCommon::Timespan &timeout)
Send the HTTP request with timeout (synchronous)
Definition: https_client.h:85
bool SendRequestAsync()
Send the current HTTP request (asynchronous)
Definition: https_client.h:107
virtual ~HTTPSClient()=default
HTTPSClient & operator=(const HTTPSClient &)=delete
virtual void onReceivedResponse(const HTTPResponse &response)
Handle HTTP response received notification.
Definition: https_client.h:149
size_t SendRequest()
Send the current HTTP request (synchronous)
Definition: https_client.h:51
HTTPRequest & request() noexcept
Get the HTTP request.
Definition: https_client.h:44
size_t SendRequestBody(std::string_view body, const CppCommon::Timespan &timeout)
Send the HTTP request body with timeout (synchronous)
Definition: https_client.h:93
const HTTPRequest & request() const noexcept
Definition: https_client.h:45
HTTPSClient & operator=(HTTPSClient &&)=delete
bool SendRequestAsync(const HTTPRequest &request)
Send the HTTP request (asynchronous)
Definition: https_client.h:113
bool SendRequestBodyAsync(std::string_view body)
Send the HTTP request body (asynchronous)
Definition: https_client.h:120
bool SendRequestBodyAsync(const void *buffer, size_t size)
Send the HTTP request body (asynchronous)
Definition: https_client.h:127
virtual void onReceivedResponseHeader(const HTTPResponse &response)
Handle HTTP response header received notification.
Definition: https_client.h:140
void onReceived(const void *buffer, size_t size) override
Handle buffer received notification.
size_t SendRequest(const CppCommon::Timespan &timeout)
Send the current HTTP request with timeout (synchronous)
Definition: https_client.h:78
size_t SendRequestBody(const void *buffer, size_t size)
Send the HTTP request body (synchronous)
Definition: https_client.h:71
HTTP request definition.
HTTP response definition.
C++ Server project definitions.
Definition: asio.h:56
SSL client definition.
Timer definition.