Kea  2.3.5-git
client.h
Go to the documentation of this file.
1 // Copyright (C) 2018-2022 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef HTTP_CLIENT_H
8 #define HTTP_CLIENT_H
9 
10 #include <asiolink/io_service.h>
11 #include <asiolink/tls_socket.h>
12 #include <exceptions/exceptions.h>
13 #include <http/url.h>
14 #include <http/request.h>
15 #include <http/response.h>
16 #include <boost/shared_ptr.hpp>
17 #include <functional>
18 #include <string>
19 #include <thread>
20 #include <vector>
21 
22 namespace isc {
23 namespace http {
24 
26 class HttpClientError : public Exception {
27 public:
28  HttpClientError(const char* file, size_t line, const char* what) :
29  isc::Exception(file, line, what) { };
30 };
31 
32 class HttpClientImpl;
33 
86 class HttpClient {
87 public:
89  struct RequestTimeout {
93  explicit RequestTimeout(long value)
94  : value_(value) {
95  }
96  long value_;
97  };
98 
100  typedef std::function<void(const boost::system::error_code&,
101  const HttpResponsePtr&,
102  const std::string&)> RequestHandler;
103 
114  typedef std::function<bool(const boost::system::error_code&, const int)> ConnectHandler;
115 
127  typedef std::function<bool(const boost::system::error_code&, const int)> HandshakeHandler;
128 
132  typedef std::function<void(const int)> CloseHandler;
133 
147  explicit HttpClient(asiolink::IOService& io_service, size_t thread_pool_size = 0,
148  bool defer_thread_start = false);
149 
151  ~HttpClient();
152 
237  void asyncSendRequest(const Url& url,
238  const asiolink::TlsContextPtr& tls_context,
239  const HttpRequestPtr& request,
240  const HttpResponsePtr& response,
241  const RequestHandler& request_callback,
242  const RequestTimeout& request_timeout =
243  RequestTimeout(10000),
244  const ConnectHandler& connect_callback =
245  ConnectHandler(),
246  const HandshakeHandler& handshake_callback =
247  HandshakeHandler(),
248  const CloseHandler& close_callback =
249  CloseHandler());
250 
256  void checkPermissions();
257 
259  void start();
260 
265  void pause();
266 
271  void resume();
272 
278  void stop();
279 
292  void closeIfOutOfBand(int socket_fd);
293 
299  const asiolink::IOServicePtr getThreadIOService() const;
300 
304  uint16_t getThreadPoolSize() const;
305 
309  uint16_t getThreadCount() const;
310 
315  bool isRunning();
316 
321  bool isStopped();
322 
327  bool isPaused();
328 
329 private:
330 
332  boost::shared_ptr<HttpClientImpl> impl_;
333 };
334 
336 typedef boost::shared_ptr<HttpClient> HttpClientPtr;
337 
338 } // end of namespace isc::http
339 } // end of namespace isc
340 
341 #endif
HttpClientError(const char *file, size_t line, const char *what)
Definition: client.h:28
std::function< bool(const boost::system::error_code &, const int)> ConnectHandler
Optional handler invoked when client connects to the server.
Definition: client.h:114
long value_
Timeout value specified.
Definition: client.h:96
std::function< void(const boost::system::error_code &, const HttpResponsePtr &, const std::string &)> RequestHandler
Callback type used in call to HttpClient::asyncSendRequest.
Definition: client.h:102
HTTP request/response timeout value.
Definition: client.h:89
RequestTimeout(long value)
Constructor.
Definition: client.h:93
std::function< void(const int)> CloseHandler
Optional handler invoked when client closes the connection to the server.
Definition: client.h:132
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Represents an URL.
Definition: url.h:20
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition: response.h:78
HTTP client class.
Definition: client.h:86
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-lfc.
std::function< bool(const boost::system::error_code &, const int)> HandshakeHandler
Optional handler invoked when client performs the TLS handshake with the server.
Definition: client.h:127
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
Definition: request.h:27
A generic error raised by the HttpClient class.
Definition: client.h:26
HttpClient implementation.
Definition: client.cc:1761
boost::shared_ptr< HttpClient > HttpClientPtr
Defines a pointer to an HttpClient instance.
Definition: client.h:336