CppServer 1.0.6.0
C++ Server Library
Loading...
Searching...
No Matches
https_client.h
Go to the documentation of this file.
1
8
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
16#include "server/asio/timer.h"
17
18#include <future>
19
20namespace CppServer {
21namespace HTTP {
22
24
32{
33public:
34 using SSLClient::SSLClient;
35
36 HTTPSClient(const HTTPSClient&) = delete;
38 virtual ~HTTPSClient() = default;
39
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
113 bool SendRequestAsync(const HTTPRequest& request) { return SendAsync(request.cache()); }
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
129protected:
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
161protected:
166};
167
169
176{
177public:
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); }
209
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); }
217
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); }
226
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); }
235
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); }
243
244
249 std::future<HTTPResponse> SendOptionsRequest(std::string_view url, const CppCommon::Timespan& timeout = CppCommon::Timespan::minutes(1))
250 { return SendRequest(_request.MakeOptionsRequest(url), timeout); }
251
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
260protected:
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
266private:
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
276
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).
virtual bool SendAsync(const void *buffer, size_t size)
Send data to the server (asynchronous).
HTTPS extended client.
HTTPSClient(const HTTPSClient &)=delete
std::future< HTTPResponse > SendDeleteRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send DELETE request.
std::future< HTTPResponse > SendHeadRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send HEAD request.
const std::shared_ptr< Asio::Timer > & timeout() const noexcept
void onDisconnected() override
Handle client disconnected notification.
std::shared_ptr< Asio::Timer > & timeout() noexcept
Get the timeout check timer.
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 > SendGetRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send GET request.
std::future< HTTPResponse > SendPostRequest(std::string_view url, std::string_view content, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send POST request.
std::future< HTTPResponse > SendPutRequest(std::string_view url, std::string_view content, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send PUT request.
std::future< HTTPResponse > SendOptionsRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send OPTIONS request.
std::future< HTTPResponse > SendRequest(const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send HTTP request.
std::shared_ptr< Asio::TCPResolver > & resolver() noexcept
Get the TCP resolver.
const std::shared_ptr< Asio::TCPResolver > & resolver() const noexcept
void onHandshaked() override
Handle session handshaked notification.
std::future< HTTPResponse > SendTraceRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send TRACE request.
HTTPSClient(HTTPSClient &&)=delete
HTTPSClient(const HTTPSClient &)=delete
HTTPResponse _response
HTTP response.
size_t SendRequestBody(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
Send the HTTP request body with timeout (synchronous).
virtual void onReceivedResponseError(const HTTPResponse &response, const std::string &error)
Handle HTTP response error notification.
HTTPRequest _request
HTTP request.
size_t SendRequestBody(std::string_view body)
Send the HTTP request body (synchronous).
size_t SendRequest(const HTTPRequest &request)
Send the HTTP request (synchronous).
const HTTPRequest & request() const noexcept
void onDisconnected() override
Handle client disconnected notification.
size_t SendRequest(const HTTPRequest &request, const CppCommon::Timespan &timeout)
Send the HTTP request with timeout (synchronous).
bool SendRequestAsync()
Send the current HTTP request (asynchronous).
virtual ~HTTPSClient()=default
HTTPRequest & request() noexcept
Get the HTTP request.
virtual void onReceivedResponse(const HTTPResponse &response)
Handle HTTP response received notification.
size_t SendRequest()
Send the current HTTP request (synchronous).
size_t SendRequestBody(std::string_view body, const CppCommon::Timespan &timeout)
Send the HTTP request body with timeout (synchronous).
HTTPSClient & operator=(const HTTPSClient &)=delete
bool SendRequestAsync(const HTTPRequest &request)
Send the HTTP request (asynchronous).
bool SendRequestBodyAsync(std::string_view body)
Send the HTTP request body (asynchronous).
bool SendRequestBodyAsync(const void *buffer, size_t size)
Send the HTTP request body (asynchronous).
HTTPSClient & operator=(HTTPSClient &&)=delete
virtual void onReceivedResponseHeader(const HTTPResponse &response)
Handle HTTP response header received notification.
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).
size_t SendRequestBody(const void *buffer, size_t size)
Send the HTTP request body (synchronous).
HTTP request definition.
HTTP response definition.
HTTP definitions.
C++ Server project definitions.
Definition asio.h:56
SSL client definition.
Timer definition.