Kea 2.7.4
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_(),
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
73int
75 return (acceptor_ ? acceptor_->getNative() : -1);
76}
77
78void
80 use_external_ = use_external;
81}
82
83void
85 try {
86 acceptor_->open(*endpoint_);
87 if (use_external_) {
88 IfaceMgr::instance().addExternalSocket(acceptor_->getNative(), 0);
89 }
90 acceptor_->setOption(HttpAcceptor::ReuseAddress(true));
91 acceptor_->bind(*endpoint_);
92 acceptor_->listen();
93
94 } catch (const boost::system::system_error& ex) {
95 stop();
96 isc_throw(HttpListenerError, "unable to setup TCP acceptor for "
97 "listening to the incoming HTTP requests: " << ex.what());
98 }
99
100 accept();
101}
102
103void
106 if (use_external_) {
107 IfaceMgr::instance().deleteExternalSocket(acceptor_->getNative());
108 }
109 acceptor_->close();
110}
111
112void
114 // In some cases we may need HttpResponseCreator instance per connection.
115 // But, the factory may also return the same instance each time. It
116 // depends on the use case.
117 HttpResponseCreatorPtr response_creator = creator_factory_->create();
118 HttpAcceptorCallback acceptor_callback =
119 std::bind(&HttpListenerImpl::acceptHandler, this, ph::_1);
120 HttpConnectionPtr conn = createConnection(response_creator,
121 acceptor_callback);
122 // Transmit the use external sockets flag.
123 if (use_external_) {
124 conn->addExternalSockets(true);
125 }
126 // Add this new connection to the pool.
127 connections_.start(conn);
128}
129
130void
131HttpListenerImpl::acceptHandler(const boost::system::error_code&) {
132 // The new connection has arrived. Set the acceptor to continue
133 // accepting new connections.
134 accept();
135}
136
139 const HttpAcceptorCallback& callback) {
142 connections_, response_creator, callback,
144 return (conn);
145}
146
147} // end of namespace isc::http
148} // end of namespace isc
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Definition iface_mgr.cc:54
void stopAll()
Stops all connections and removes them from the pool.
void start(const HttpConnectionPtr &connection)
Start new connection.
Accepts and handles a single HTTP connection.
Definition connection.h:44
A generic error raised by the HttpListener class.
Definition listener.h:22
HttpConnectionPool connections_
Pool of active connections.
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.
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.
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.