19 if (
_response.ReceiveHeader(buffer, size))
67 _resolver = std::make_shared<Asio::TCPResolver>(
service());
70 _timeout = std::make_shared<Asio::Timer>(
service());
72 _promise = std::promise<HTTPResponse>();
78 SetResponseError(
"Invalid HTTP request!");
79 return _promise.get_future();
87 SetResponseError(
"Connection failed!");
88 return _promise.get_future();
96 SetResponseError(
"Failed to send HTTP request!");
97 return _promise.get_future();
102 auto self(this->shared_from_this());
103 auto timeout_handler = [
this, self](
bool canceled)
113 if (!_timeout->Setup(timeout_handler,
timeout) || !_timeout->WaitAsync())
115 SetResponseError(
"Failed to setup timeout timer!");
116 return _promise.get_future();
119 return _promise.get_future();
129 SetResponseError(
"Failed to send HTTP request!");
139 SetResponseError(
"Disconnected!");
151 SetResponseError(message.empty() ?
"Socket error!" : message);
162 SetResponseValue(response);
171 SetResponseError(error);
174void HTTPSClientEx::SetResponseValue(
const HTTPResponse& response)
179 _promise.set_value(response);
186void HTTPSClientEx::SetResponseError(
const std::string& error)
191 _promise.set_exception(std::make_exception_ptr(std::runtime_error(error)));
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
bool IsHandshaked() const noexcept
Is the session handshaked?
virtual void onHandshaked()
Handle session handshaked notification.
virtual bool ConnectAsync()
Connect the client (asynchronous).
virtual bool DisconnectAsync()
Disconnect the client (asynchronous).
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
HTTPRequest & Clear()
Clear the HTTP request cache.
void onDisconnected() override
Handle client disconnected notification.
void onError(int error, const std::string &category, const std::string &message) override
Handle error 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.
C++ Server project definitions.