Kea 2.5.8
client.h
Go to the documentation of this file.
1// Copyright (C) 2018-2024 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>
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
22namespace isc {
23namespace http {
24
26class HttpClientError : public Exception {
27public:
28 HttpClientError(const char* file, size_t line, const char* what) :
29 isc::Exception(file, line, what) { };
30};
31
32class HttpClientImpl;
33
87public:
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
148 explicit HttpClient(const asiolink::IOServicePtr& io_service,
149 bool multi_threading_enabled,
150 size_t thread_pool_size = 0,
151 bool defer_thread_start = false);
152
154 ~HttpClient();
155
240 void asyncSendRequest(const Url& url,
241 const asiolink::TlsContextPtr& tls_context,
242 const HttpRequestPtr& request,
243 const HttpResponsePtr& response,
244 const RequestHandler& request_callback,
245 const RequestTimeout& request_timeout =
246 RequestTimeout(10000),
247 const ConnectHandler& connect_callback =
249 const HandshakeHandler& handshake_callback =
251 const CloseHandler& close_callback =
252 CloseHandler());
253
259 void checkPermissions();
260
262 void start();
263
268 void pause();
269
274 void resume();
275
281 void stop();
282
295 void closeIfOutOfBand(int socket_fd);
296
303
307 uint16_t getThreadPoolSize() const;
308
312 uint16_t getThreadCount() const;
313
318 bool isRunning();
319
324 bool isStopped();
325
330 bool isPaused();
331
332private:
333
335 boost::shared_ptr<HttpClientImpl> impl_;
336};
337
339typedef boost::shared_ptr<HttpClient> HttpClientPtr;
340
341} // end of namespace isc::http
342} // end of namespace isc
343
344#endif
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic error raised by the HttpClient class.
Definition: client.h:26
HttpClientError(const char *file, size_t line, const char *what)
Definition: client.h:28
HTTP client class.
Definition: client.h:86
uint16_t getThreadCount() const
Fetches the number of threads in the pool.
Definition: client.cc:2056
bool isRunning()
Indicates if the thread pool is running.
Definition: client.cc:2061
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
void stop()
Halts client-side IO activity.
Definition: client.cc:2041
bool isPaused()
Indicates if the thread pool is paused.
Definition: client.cc:2071
void pause()
Pauses the client's thread pool.
Definition: client.cc:2031
std::function< void(const int)> CloseHandler
Optional handler invoked when client closes the connection to the server.
Definition: client.h:132
const asiolink::IOServicePtr getThreadIOService() const
Fetches a pointer to the internal IOService used to drive the thread-pool in multi-threaded mode.
Definition: client.cc:2046
void start()
Starts running the client's thread pool, if multi-threaded.
Definition: client.cc:2021
std::function< bool(const boost::system::error_code &, const int)> ConnectHandler
Optional handler invoked when client connects to the server.
Definition: client.h:114
uint16_t getThreadPoolSize() const
Fetches the maximum size of the thread pool.
Definition: client.cc:2051
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
void closeIfOutOfBand(int socket_fd)
Closes a connection if it has an out-of-band socket event.
Definition: client.cc:2016
~HttpClient()
Destructor.
Definition: client.cc:1975
void resume()
Resumes running the client's thread pool.
Definition: client.cc:2036
void asyncSendRequest(const Url &url, const asiolink::TlsContextPtr &tls_context, const HttpRequestPtr &request, const HttpResponsePtr &response, const RequestHandler &request_callback, const RequestTimeout &request_timeout=RequestTimeout(10000), const ConnectHandler &connect_callback=ConnectHandler(), const HandshakeHandler &handshake_callback=HandshakeHandler(), const CloseHandler &close_callback=CloseHandler())
Queues new asynchronous HTTP request for a given URL.
Definition: client.cc:1980
bool isStopped()
Indicates if the thread pool is stopped.
Definition: client.cc:2066
void checkPermissions()
Check if the current thread can perform thread pool state transition.
Definition: client.cc:2026
Represents an URL.
Definition: url.h:20
boost::shared_ptr< HttpClient > HttpClientPtr
Defines a pointer to an HttpClient instance.
Definition: client.h:339
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition: response.h:81
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
Definition: request.h:30
Defines the logger used by the top-level component of kea-lfc.
HTTP request/response timeout value.
Definition: client.h:89
RequestTimeout(long value)
Constructor.
Definition: client.h:93
long value_
Timeout value specified.
Definition: client.h:96