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 HTTPClientEx::SetResponseValue(
const HTTPResponse& response)
179 _promise.set_value(response);
186void HTTPClientEx::SetResponseError(
const std::string& error)
191 _promise.set_exception(std::make_exception_ptr(std::runtime_error(error)));
bool IsConnected() const noexcept
Is the client connected?
virtual void onConnected()
Handle client connected notification.
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
virtual bool ConnectAsync()
Connect the client (asynchronous).
virtual bool DisconnectAsync()
Disconnect the client (asynchronous).
void onReceivedResponse(const HTTPResponse &response) override
Handle HTTP response received 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 onDisconnected() override
Handle client disconnected notification.
void onConnected() override
Handle client connected notification.
void onReceivedResponseError(const HTTPResponse &response, const std::string &error) override
Handle HTTP response error notification.
void onReceived(const void *buffer, size_t size) override
Handle buffer received notification.
HTTPResponse _response
HTTP response.
HTTPRequest _request
HTTP request.
size_t SendRequest()
Send the current HTTP request (synchronous).
HTTPRequest & request() noexcept
Get the HTTP request.
bool SendRequestAsync()
Send the current HTTP request (asynchronous).
virtual void onReceivedResponse(const HTTPResponse &response)
Handle HTTP response received notification.
virtual void onReceivedResponseHeader(const HTTPResponse &response)
Handle HTTP response header received notification.
virtual void onReceivedResponseError(const HTTPResponse &response, const std::string &error)
Handle HTTP response error notification.
void onDisconnected() override
Handle client disconnected notification.
HTTPRequest & Clear()
Clear the HTTP request cache.
C++ Server project definitions.