Kea 2.7.6
listener_impl.cc
Go to the documentation of this file.
1// Copyright (C) 2019-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#include <config.h>
9#include <dhcp/iface_mgr.h>
11#include <http/listener.h>
12#include <http/listener_impl.h>
13
14using namespace isc::asiolink;
15using namespace isc::dhcp;
16namespace ph = std::placeholders;
17
18namespace isc {
19namespace http {
20
22 const asiolink::IOAddress& server_address,
23 const unsigned short server_port,
24 const TlsContextPtr& tls_context,
25 const HttpResponseCreatorFactoryPtr& creator_factory,
26 const long request_timeout,
27 const long idle_timeout)
28 : io_service_(io_service), tls_context_(tls_context), acceptor_(),
29 endpoint_(), connections_(new HttpConnectionPool()),
30 creator_factory_(creator_factory),
31 request_timeout_(request_timeout), idle_timeout_(idle_timeout),
32 use_external_(false) {
33 // Create the TCP or TLS acceptor.
34 if (!tls_context) {
35 acceptor_.reset(new HttpAcceptor(io_service));
36 } else {
37 acceptor_.reset(new HttpsAcceptor(io_service));
38 }
39
40 // Try creating an endpoint. This may cause exceptions.
41 try {
42 endpoint_.reset(new TCPEndpoint(server_address, server_port));
43
44 } catch (...) {
45 isc_throw(HttpListenerError, "unable to create TCP endpoint for "
46 << server_address << ":" << server_port);
47 }
48
49 // The factory must not be null.
50 if (!creator_factory_) {
51 isc_throw(HttpListenerError, "HttpResponseCreatorFactory must not"
52 " be null");
53 }
54
55 // Request timeout is signed and must be greater than 0.
56 if (request_timeout_ <= 0) {
57 isc_throw(HttpListenerError, "Invalid desired HTTP request timeout "
59 }
60
61 // Idle persistent connection timeout is signed and must be greater than 0.
62 if (idle_timeout_ <= 0) {
63 isc_throw(HttpListenerError, "Invalid desired HTTP idle persistent connection"
64 " timeout " << idle_timeout_);
65 }
66}
67
68const TCPEndpoint&
70 return (*endpoint_);
71}
72
73const TlsContextPtr&
77
78int
80 return (acceptor_ ? acceptor_->getNative() : -1);
81}
82
83void
85 use_external_ = use_external;
86}
87
88void
90 try {
91 acceptor_->open(*endpoint_);
92 if (use_external_) {
94 }
95 acceptor_->setOption(HttpAcceptor::ReuseAddress(true));
96 acceptor_->bind(*endpoint_);
97 acceptor_->listen();
98
99 } catch (const boost::system::system_error& ex) {
100 stop();
101 isc_throw(HttpListenerError, "unable to setup TCP acceptor for "
102 "listening to the incoming HTTP requests: " << ex.what());
103 }
104
105 accept();
106}
107
108void
110 connections_->stopAll();
111 if (use_external_) {
113 }
114 acceptor_->close();
115}
116
117void
119 // In some cases we may need HttpResponseCreator instance per connection.
120 // But, the factory may also return the same instance each time. It
121 // depends on the use case.
122 HttpResponseCreatorPtr response_creator = creator_factory_->create();
123 HttpAcceptorCallback acceptor_callback =
124 std::bind(&HttpListenerImpl::acceptHandler, shared_from_this(), ph::_1);
125 HttpConnectionPtr conn = createConnection(response_creator,
126 acceptor_callback);
127 // Transmit the use external sockets flag.
128 if (use_external_) {
129 conn->addExternalSockets(true);
130 }
131 // Add this new connection to the pool.
132 connections_->start(conn);
133}
134
135void
136HttpListenerImpl::acceptHandler(const boost::system::error_code&) {
137 // The new connection has arrived. Set the acceptor to continue
138 // accepting new connections.
139 accept();
140}
141
144 const HttpAcceptorCallback& callback) {
147 connections_, response_creator, callback,
149 return (conn);
150}
151
152} // end of namespace isc::http
153} // end of namespace isc
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
void deleteExternalSocket(int socketfd)
Deletes external socket.
Definition iface_mgr.cc:352
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Definition iface_mgr.cc:54
void addExternalSocket(int socketfd, SocketCallback callback)
Adds external socket and a callback.
Definition iface_mgr.cc:329
Pool of active HTTP connections.
Accepts and handles a single HTTP connection.
Definition connection.h:44
A generic error raised by the HttpListener class.
Definition listener.h:22
bool use_external_
Use external sockets flag.
HttpListenerImpl(const asiolink::IOServicePtr &io_service, const asiolink::IOAddress &server_address, const unsigned short server_port, const asiolink::TlsContextPtr &tls_context, const HttpResponseCreatorFactoryPtr &creator_factory, const long request_timeout, const long idle_timeout)
Constructor.
long request_timeout_
Timeout for HTTP Request Timeout desired.
void start()
Starts accepting new connections.
boost::scoped_ptr< asiolink::TCPEndpoint > endpoint_
Pointer to the endpoint representing IP address and port on which the service is running.
long idle_timeout_
Timeout after which idle persistent connection is closed by the server.
HttpResponseCreatorFactoryPtr creator_factory_
Pointer to the HttpResponseCreatorFactory.
asiolink::IOServicePtr io_service_
Pointer to the IO service.
const asiolink::TCPEndpoint & getEndpoint() const
Returns reference to the current listener endpoint.
void accept()
Creates HttpConnection instance and adds it to the pool of active connections.
void addExternalSockets(bool use_external)
Use external sockets flag.
const asiolink::TlsContextPtr & getTlsContext() const
Returns reference to the current TLS context.
HttpAcceptorPtr acceptor_
Acceptor instance.
int getNative() const
file descriptor of the underlying acceptor socket.
asiolink::TlsContextPtr tls_context_
TLS context.
virtual HttpConnectionPtr createConnection(const HttpResponseCreatorPtr &response_creator, const HttpAcceptorCallback &callback)
Creates an instance of the HttpConnection.
void acceptHandler(const boost::system::error_code &ec)
Callback invoked when the new connection is accepted.
HttpConnectionPoolPtr connections_
Pool of active connections.
void stop()
Stops all active connections and shuts down the service.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< HttpResponseCreator > HttpResponseCreatorPtr
Pointer to the HttpResponseCreator object.
boost::shared_ptr< HttpResponseCreatorFactory > HttpResponseCreatorFactoryPtr
Pointer to the HttpResponseCreatorFactory.
asiolink::TLSAcceptor< HttpAcceptorCallback > HttpsAcceptor
Type of the TLS acceptor used in this library.
std::function< void(const boost::system::error_code &)> HttpAcceptorCallback
Type of the callback for the TCP acceptor used in this library.
boost::shared_ptr< HttpConnection > HttpConnectionPtr
Pointer to the HttpConnection.
Definition connection.h:41
asiolink::TCPAcceptor< HttpAcceptorCallback > HttpAcceptor
Type of the TCP acceptor used in this library.
Defines the logger used by the top-level component of kea-lfc.