CppServer  1.0.4.0
C++ Server Library
http_client.h
Go to the documentation of this file.
1 
9 #ifndef CPPSERVER_HTTP_HTTP_CLIENT_H
10 #define CPPSERVER_HTTP_HTTP_CLIENT_H
11 
12 #include "http_request.h"
13 #include "http_response.h"
14 
15 #include "server/asio/tcp_client.h"
16 #include "server/asio/timer.h"
17 
18 #include <future>
19 
20 namespace CppServer {
21 namespace HTTP {
22 
24 
32 {
33 public:
34  using TCPClient::TCPClient;
35 
36  HTTPClient(const HTTPClient&) = delete;
37  HTTPClient(HTTPClient&&) = delete;
38  virtual ~HTTPClient() = default;
39 
40  HTTPClient& operator=(const HTTPClient&) = delete;
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 
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 
129 protected:
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 
161 protected:
166 };
167 
169 
175 class HTTPClientEx : public HTTPClient
176 {
177 public:
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); }
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); }
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); }
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); }
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); }
244 
249  std::future<HTTPResponse> SendOptionsRequest(std::string_view url, const CppCommon::Timespan& timeout = CppCommon::Timespan::minutes(1))
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 
260 protected:
261  void onConnected() override;
262  void onDisconnected() override;
263  void onReceivedResponse(const HTTPResponse& response) override;
264  void onReceivedResponseError(const HTTPResponse& response, const std::string& error) override;
265 
266 private:
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 
277 } // namespace HTTP
278 } // namespace CppServer
279 
280 #endif // CPPSERVER_HTTP_HTTP_CLIENT_H
virtual size_t Send(const void *buffer, size_t size)
Send data to the server (synchronous)
Definition: tcp_client.cpp:505
virtual bool SendAsync(const void *buffer, size_t size)
Send data to the server (asynchronous)
Definition: tcp_client.cpp:603
HTTP extended client.
Definition: http_client.h:176
std::future< HTTPResponse > SendRequest(const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send HTTP request.
Definition: http_client.h:192
void onReceivedResponse(const HTTPResponse &response) override
Handle HTTP response received notification.
std::future< HTTPResponse > SendTraceRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send TRACE request.
Definition: http_client.h:257
const std::shared_ptr< Asio::Timer > & timeout() const noexcept
Definition: http_client.h:185
std::future< HTTPResponse > SendGetRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send GET request.
Definition: http_client.h:215
std::future< HTTPResponse > SendDeleteRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send DELETE request.
Definition: http_client.h:241
void onDisconnected() override
Handle client disconnected notification.
std::future< HTTPResponse > SendPutRequest(std::string_view url, std::string_view content, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send PUT request.
Definition: http_client.h:233
void onConnected() override
Handle client connected notification.
std::future< HTTPResponse > SendPostRequest(std::string_view url, std::string_view content, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send POST request.
Definition: http_client.h:224
std::shared_ptr< Asio::Timer > & timeout() noexcept
Get the timeout check timer.
Definition: http_client.h:184
std::future< HTTPResponse > SendOptionsRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send OPTIONS request.
Definition: http_client.h:249
void onReceivedResponseError(const HTTPResponse &response, const std::string &error) override
Handle HTTP response error notification.
std::future< HTTPResponse > SendHeadRequest(std::string_view url, const CppCommon::Timespan &timeout=CppCommon::Timespan::minutes(1))
Send HEAD request.
Definition: http_client.h:207
std::shared_ptr< Asio::TCPResolver > & resolver() noexcept
Get the TCP resolver.
Definition: http_client.h:181
const std::shared_ptr< Asio::TCPResolver > & resolver() const noexcept
Definition: http_client.h:182
size_t SendRequest(const CppCommon::Timespan &timeout)
Send the current HTTP request with timeout (synchronous)
Definition: http_client.h:78
HTTPClient(const HTTPClient &)=delete
virtual ~HTTPClient()=default
bool SendRequestAsync(const HTTPRequest &request)
Send the HTTP request (asynchronous)
Definition: http_client.h:113
void onReceived(const void *buffer, size_t size) override
Handle buffer received notification.
Definition: http_client.cpp:14
HTTPResponse _response
HTTP response.
Definition: http_client.h:165
bool SendRequestBodyAsync(std::string_view body)
Send the HTTP request body (asynchronous)
Definition: http_client.h:120
HTTPRequest _request
HTTP request.
Definition: http_client.h:163
HTTPRequest & request() noexcept
Get the HTTP request.
Definition: http_client.h:44
HTTPClient & operator=(HTTPClient &&)=delete
HTTPClient & operator=(const HTTPClient &)=delete
size_t SendRequest()
Send the current HTTP request (synchronous)
Definition: http_client.h:51
size_t SendRequest(const HTTPRequest &request, const CppCommon::Timespan &timeout)
Send the HTTP request with timeout (synchronous)
Definition: http_client.h:85
bool SendRequestBodyAsync(const void *buffer, size_t size)
Send the HTTP request body (asynchronous)
Definition: http_client.h:127
size_t SendRequest(const HTTPRequest &request)
Send the HTTP request (synchronous)
Definition: http_client.h:57
size_t SendRequestBody(const void *buffer, size_t size)
Send the HTTP request body (synchronous)
Definition: http_client.h:71
bool SendRequestAsync()
Send the current HTTP request (asynchronous)
Definition: http_client.h:107
virtual void onReceivedResponse(const HTTPResponse &response)
Handle HTTP response received notification.
Definition: http_client.h:149
virtual void onReceivedResponseHeader(const HTTPResponse &response)
Handle HTTP response header received notification.
Definition: http_client.h:140
virtual void onReceivedResponseError(const HTTPResponse &response, const std::string &error)
Handle HTTP response error notification.
Definition: http_client.h:159
size_t SendRequestBody(std::string_view body)
Send the HTTP request body (synchronous)
Definition: http_client.h:64
HTTPClient(HTTPClient &&)=delete
const HTTPRequest & request() const noexcept
Definition: http_client.h:45
size_t SendRequestBody(std::string_view body, const CppCommon::Timespan &timeout)
Send the HTTP request body with timeout (synchronous)
Definition: http_client.h:93
size_t SendRequestBody(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
Send the HTTP request body with timeout (synchronous)
Definition: http_client.h:101
void onDisconnected() override
Handle client disconnected notification.
Definition: http_client.cpp:52
const std::string & cache() const noexcept
Get the HTTP request cache content.
Definition: http_request.h:77
HTTPRequest & MakePutRequest(std::string_view url, std::string_view content, std::string_view content_type="text/plain; charset=UTF-8")
Make PUT request.
HTTPRequest & MakeHeadRequest(std::string_view url)
Make HEAD request.
HTTPRequest & MakeDeleteRequest(std::string_view url)
Make DELETE request.
HTTPRequest & MakePostRequest(std::string_view url, std::string_view content, std::string_view content_type="text/plain; charset=UTF-8")
Make POST request.
HTTPRequest & MakeOptionsRequest(std::string_view url)
Make OPTIONS request.
HTTPRequest & MakeGetRequest(std::string_view url)
Make GET request.
HTTPRequest & MakeTraceRequest(std::string_view url)
Make TRACE request.
HTTP request definition.
HTTP response definition.
C++ Server project definitions.
Definition: asio.h:56
TCP client definition.
Timer definition.