CppServer 1.0.6.0
C++ Server Library
Loading...
Searching...
No Matches
https_client.cpp
Go to the documentation of this file.
1
8
10
11namespace CppServer {
12namespace HTTP {
13
14void HTTPSClient::onReceived(const void* buffer, size_t size)
15{
16 // Receive HTTP response header
17 if (_response.IsPendingHeader())
18 {
19 if (_response.ReceiveHeader(buffer, size))
21
22 size = 0;
23 }
24
25 // Check for HTTP response error
26 if (_response.error())
27 {
28 onReceivedResponseError(_response, "Invalid HTTP response!");
29 _response.Clear();
31 return;
32 }
33
34 // Receive HTTP response body
35 if (_response.ReceiveBody(buffer, size))
36 {
38 _response.Clear();
39 return;
40 }
41
42 // Check for HTTP response error
43 if (_response.error())
44 {
45 onReceivedResponseError(_response, "Invalid HTTP response!");
46 _response.Clear();
48 return;
49 }
50}
51
53{
54 // Receive HTTP response body
55 if (_response.IsPendingBody())
56 {
58 _response.Clear();
59 return;
60 }
61}
62
63std::future<HTTPResponse> HTTPSClientEx::SendRequest(const HTTPRequest& request, const CppCommon::Timespan& timeout)
64{
65 // Create TCP resolver if the current one is empty
66 if (!_resolver)
67 _resolver = std::make_shared<Asio::TCPResolver>(service());
68 // Create timeout check timer if the current one is empty
69 if (!_timeout)
70 _timeout = std::make_shared<Asio::Timer>(service());
71
72 _promise = std::promise<HTTPResponse>();
74
75 // Check if the HTTP request is valid
76 if (_request.empty() || _request.error())
77 {
78 SetPromiseError("Invalid HTTP request!");
79 return _promise.get_future();
80 }
81
82 if (!IsHandshaked())
83 {
84 // Connect to the Web server
85 if (!ConnectAsync(_resolver))
86 {
87 SetPromiseError("Connection failed!");
88 return _promise.get_future();
89 }
90 }
91 else
92 {
93 // Send prepared HTTP request
94 if (!SendRequestAsync())
95 {
96 SetPromiseError("Failed to send HTTP request!");
97 return _promise.get_future();
98 }
99 }
100
101 // Setup timeout check timer
102 auto self(this->shared_from_this());
103 auto timeout_handler = [this, self](bool canceled)
104 {
105 if (canceled)
106 return;
107
108 // Disconnect on timeout
110 _response.Clear();
112 };
113 if (!_timeout->Setup(timeout_handler, timeout) || !_timeout->WaitAsync())
114 {
115 SetPromiseError("Failed to setup timeout timer!");
116 return _promise.get_future();
117 }
118
119 return _promise.get_future();
120}
121
123{
125
126 // Send prepared HTTP request on connect
127 if (!_request.empty() && !_request.error())
128 if (!SendRequestAsync())
129 SetPromiseError("Failed to send HTTP request!");
130}
131
133{
134 // Cancel timeout check timer
135 if (_timeout)
136 _timeout->Cancel();
137
139}
140
142{
143 // Cancel timeout check timer
144 if (_timeout)
145 _timeout->Cancel();
146
147 SetPromiseValue(response);
148}
149
150void HTTPSClientEx::onReceivedResponseError(const HTTPResponse& response, const std::string& error)
151{
152 // Cancel timeout check timer
153 if (_timeout)
154 _timeout->Cancel();
155
156 SetPromiseError(error);
157}
158
159void HTTPSClientEx::SetPromiseValue(const HTTPResponse& response)
160{
161 _promise.set_value(response);
162 _request.Clear();
163}
164
165void HTTPSClientEx::SetPromiseError(const std::string& error)
166{
167 _promise.set_exception(std::make_exception_ptr(std::runtime_error(error)));
168 _request.Clear();
169}
170
171} // namespace HTTP
172} // namespace CppServer
bool IsHandshaked() const noexcept
Is the session handshaked?
Definition ssl_client.h:111
virtual void onHandshaked()
Handle session handshaked notification.
Definition ssl_client.h:295
virtual bool ConnectAsync()
Connect the client (asynchronous).
virtual bool DisconnectAsync()
Disconnect the client (asynchronous).
Definition ssl_client.h:156
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
Definition ssl_client.h:67
HTTPRequest & Clear()
Clear the HTTP request cache.
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.
void onHandshaked() override
Handle session handshaked notification.
HTTPResponse _response
HTTP response.
virtual void onReceivedResponseError(const HTTPResponse &response, const std::string &error)
Handle HTTP response error notification.
HTTPRequest _request
HTTP request.
void onDisconnected() override
Handle client disconnected notification.
bool SendRequestAsync()
Send the current HTTP request (asynchronous).
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).
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.
HTTPS client definition.
HTTP definitions.
C++ Server project definitions.
Definition asio.h:56