9#ifndef CPPSERVER_HTTP_HTTP_CLIENT_H
10#define CPPSERVER_HTTP_HTTP_CLIENT_H
34 using TCPClient::TCPClient;
93 size_t SendRequestBody(std::string_view body,
const CppCommon::Timespan& timeout) {
return Send(body, timeout); }
101 size_t SendRequestBody(
const void* buffer,
size_t size,
const CppCommon::Timespan& timeout) {
return Send(buffer, size, timeout); }
130 void onReceived(
const void* buffer,
size_t size)
override;
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; }
207 std::future<HTTPResponse>
SendHeadRequest(std::string_view url,
const CppCommon::Timespan&
timeout = CppCommon::Timespan::minutes(1))
215 std::future<HTTPResponse>
SendGetRequest(std::string_view url,
const CppCommon::Timespan&
timeout = CppCommon::Timespan::minutes(1))
224 std::future<HTTPResponse>
SendPostRequest(std::string_view url, std::string_view content,
const CppCommon::Timespan&
timeout = CppCommon::Timespan::minutes(1))
233 std::future<HTTPResponse>
SendPutRequest(std::string_view url, std::string_view content,
const CppCommon::Timespan&
timeout = CppCommon::Timespan::minutes(1))
241 std::future<HTTPResponse>
SendDeleteRequest(std::string_view url,
const CppCommon::Timespan&
timeout = CppCommon::Timespan::minutes(1))
249 std::future<HTTPResponse>
SendOptionsRequest(std::string_view url,
const CppCommon::Timespan&
timeout = CppCommon::Timespan::minutes(1))
257 std::future<HTTPResponse>
SendTraceRequest(std::string_view url,
const CppCommon::Timespan&
timeout = CppCommon::Timespan::minutes(1))
267 std::shared_ptr<Asio::TCPResolver> _resolver;
268 std::shared_ptr<Asio::Timer> _timeout;
269 std::promise<HTTPResponse> _promise;
272 void SetPromiseError(
const std::string& error);
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).
HTTPClient(const HTTPClient &)=delete
void onReceivedResponse(const HTTPResponse &response) override
Handle HTTP response received notification.
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::shared_ptr< Asio::TCPResolver > & resolver() noexcept
Get the TCP resolver.
std::future< HTTPResponse > SendTraceRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send TRACE request.
const std::shared_ptr< Asio::Timer > & timeout() const noexcept
std::shared_ptr< Asio::Timer > & timeout() noexcept
Get the timeout check timer.
const std::shared_ptr< Asio::TCPResolver > & resolver() const noexcept
std::future< HTTPResponse > SendDeleteRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send DELETE request.
void onDisconnected() override
Handle client disconnected notification.
std::future< HTTPResponse > SendHeadRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send HEAD request.
void onConnected() override
Handle client connected notification.
std::future< HTTPResponse > SendRequest(const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send HTTP request.
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 > SendOptionsRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send OPTIONS request.
size_t SendRequest(const CppCommon::Timespan &timeout)
Send the current HTTP request with timeout (synchronous).
HTTPClient(const HTTPClient &)=delete
virtual ~HTTPClient()=default
bool SendRequestAsync(const HTTPRequest &request)
Send the HTTP request (asynchronous).
void onReceived(const void *buffer, size_t size) override
Handle buffer received notification.
HTTPResponse _response
HTTP response.
bool SendRequestBodyAsync(std::string_view body)
Send the HTTP request body (asynchronous).
HTTPRequest _request
HTTP request.
size_t SendRequest()
Send the current HTTP request (synchronous).
size_t SendRequest(const HTTPRequest &request, const CppCommon::Timespan &timeout)
Send the HTTP request with timeout (synchronous).
HTTPClient & operator=(const HTTPClient &)=delete
bool SendRequestBodyAsync(const void *buffer, size_t size)
Send the HTTP request body (asynchronous).
size_t SendRequest(const HTTPRequest &request)
Send the HTTP request (synchronous).
size_t SendRequestBody(const void *buffer, size_t size)
Send the HTTP request body (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.
size_t SendRequestBody(std::string_view body)
Send the HTTP request body (synchronous).
HTTPClient & operator=(HTTPClient &&)=delete
HTTPClient(HTTPClient &&)=delete
size_t SendRequestBody(std::string_view body, const CppCommon::Timespan &timeout)
Send the HTTP request body with timeout (synchronous).
size_t SendRequestBody(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
Send the HTTP request body with timeout (synchronous).
void onDisconnected() override
Handle client disconnected notification.
const HTTPRequest & request() const noexcept
HTTP response definition.
C++ Server project definitions.