Kea: connection.h Source File

| Kea 3.2.0-git

connection.h
Go to the documentation of this file.
1// Copyright (C) 2017-2026 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_CONNECTION_H
8#define HTTP_CONNECTION_H
9
10#include <asiolink/interval_timer.h>
11#include <asiolink/io_service.h>
12#include <http/http_acceptor.h>
13#include <http/request_parser.h>
14#include <http/response_creator_factory.h>
15#include <util/watch_socket.h>
16#include <boost/enable_shared_from_this.hpp>
17#include <boost/system/error_code.hpp>
18#include <boost/shared_ptr.hpp>
19#include <array>
20#include <functional>
21#include <string>
22
23namespace isc {
24namespace http {
25
27 class HttpConnectionError : public Exception {
28public:
29 HttpConnectionError(const char* file, size_t line, const char* what) :
30 isc::Exception(file, line, what) { }
31};
32
37class HttpConnectionPool;
38
39class HttpConnection;
41 typedef boost::shared_ptr<HttpConnection> HttpConnectionPtr;
42
44 class HttpConnection : public boost::enable_shared_from_this<HttpConnection> {
45private:
46
49 typedef std::function<void(boost::system::error_code ec, size_t length)>
50 SocketCallbackFunction;
51
55 class SocketCallback {
56 public:
57
62 SocketCallback(SocketCallbackFunction socket_callback)
63 : callback_(socket_callback) {
64 }
65
75 void operator()(boost::system::error_code ec, size_t length = 0);
76
77 private:
79 SocketCallbackFunction callback_;
80 };
81
82protected:
83
84 class Transaction;
85
87 typedef boost::shared_ptr<Transaction> TransactionPtr;
88
119 class Transaction {
120 public:
121
129 Transaction(const HttpResponseCreatorPtr& response_creator,
130 const HttpRequestPtr& request = HttpRequestPtr());
131
141 static TransactionPtr create(const HttpResponseCreatorPtr& response_creator);
142
158 static TransactionPtr spawn(const HttpResponseCreatorPtr& response_creator,
159 const TransactionPtr& transaction);
160
162 HttpRequestPtr getRequest() const {
163 return (request_);
164 }
165
167 HttpRequestParserPtr getParser() const {
168 return (parser_);
169 }
170
172 char* getInputBufData() {
173 return (input_buf_.data());
174 }
175
177 size_t getInputBufSize() const {
178 return (input_buf_.size());
179 }
180
186 bool outputDataAvail() const {
187 return (!output_buf_.empty());
188 }
189
191 const char* getOutputBufData() const {
192 return (output_buf_.data());
193 }
194
196 size_t getOutputBufSize() const {
197 return (output_buf_.size());
198 }
199
203 void setOutputBuf(const std::string& response) {
204 output_buf_ = response;
205 }
206
210 void consumeOutputBuf(const size_t length) {
211 output_buf_.erase(0, length);
212 }
213
214 private:
215
217 HttpRequestPtr request_;
218
220 HttpRequestParserPtr parser_;
221
223 std::array<char, 32768> input_buf_;
224
226 std::string output_buf_;
227 };
228
229public:
230
245 HttpConnection(const asiolink::IOServicePtr& io_service,
246 const HttpAcceptorPtr& acceptor,
247 const asiolink::TlsContextPtr& tls_context,
248 std::shared_ptr<HttpConnectionPool> connection_pool,
249 const HttpResponseCreatorPtr& response_creator,
250 const HttpAcceptorCallback& callback,
251 const long request_timeout,
252 const long idle_timeout);
253
257 virtual ~HttpConnection();
258
265 void addExternalSockets(bool use_external = false);
266
271 void asyncAccept();
272
274 void shutdown();
275
277 void close();
278
282 void recordParameters(const HttpRequestPtr& request) const;
283
288 void doHandshake();
289
302 void doRead(TransactionPtr transaction = TransactionPtr());
303
304protected:
305
314 void doWrite(TransactionPtr transaction);
315
322 void asyncSendResponse(const ConstHttpResponsePtr& response,
323 TransactionPtr transaction);
324
333 void acceptorCallback(const boost::system::error_code& ec);
334
341 void handshakeCallback(const boost::system::error_code& ec);
342
353 void socketReadCallback(TransactionPtr transaction,
354 boost::system::error_code ec,
355 size_t length);
356
363 virtual void socketWriteCallback(TransactionPtr transaction,
364 boost::system::error_code ec,
365 size_t length);
366
374 void shutdownCallback(const boost::system::error_code& ec);
375
379 void setupRequestTimer(TransactionPtr transaction = TransactionPtr());
380
382 void setupIdleTimer();
383
390 void requestTimeoutCallback(TransactionPtr transaction);
391
392 void idleTimeoutCallback();
393
397 void shutdownConnection();
398
400 void stopThisConnection();
401
403 std::string getRemoteEndpointAddressAsText() const;
404
410 void markWatchSocketReady();
411
416 void clearWatchSocket();
417
419 void closeWatchSocket();
420
422 asiolink::IOServicePtr io_service_;
423
425 asiolink::IntervalTimer request_timer_;
426
428 long request_timeout_;
429
431 asiolink::TlsContextPtr tls_context_;
432
435 long idle_timeout_;
436
438 std::unique_ptr<asiolink::TCPSocket<SocketCallback> > tcp_socket_;
439
441 std::unique_ptr<asiolink::TLSSocket<SocketCallback> > tls_socket_;
442
444 HttpAcceptorPtr acceptor_;
445
447 std::weak_ptr<HttpConnectionPool> connection_pool_;
448
451 HttpResponseCreatorPtr response_creator_;
452
454 HttpAcceptorCallback acceptor_callback_;
455
457 bool use_external_;
458
461 util::WatchSocketPtr watch_socket_;
462
465 bool defer_shutdown_;
466
469 bool closed_;
470};
471
472} // end of namespace isc::http
473} // end of namespace isc
474
475#endif
isc::Exception::Exception
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
Definition exceptions/exceptions.cc:17
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::asiolink::IntervalTimer
The IntervalTimer class is a wrapper for the ASIO boost::asio::system_timer class.
Definition interval_timer.h:61
isc::http::HttpConnectionError::HttpConnectionError
HttpConnectionError(const char *file, size_t line, const char *what)
Definition connection.h:29
isc::http::HttpConnection::Transaction
Represents a single exchange of the HTTP messages.
Definition connection.h:119
isc::http::HttpConnection::Transaction::setOutputBuf
void setOutputBuf(const std::string &response)
Replaces output buffer contents with new contents.
Definition connection.h:203
isc::http::HttpConnection::Transaction::create
static TransactionPtr create(const HttpResponseCreatorPtr &response_creator)
Creates new transaction instance.
Definition connection.cc:45
isc::http::HttpConnection::Transaction::getRequest
HttpRequestPtr getRequest() const
Returns request instance associated with the transaction.
Definition connection.h:162
isc::http::HttpConnection::Transaction::Transaction
Transaction(const HttpResponseCreatorPtr &response_creator, const HttpRequestPtr &request=HttpRequestPtr())
Constructor.
Definition connection.cc:35
isc::http::HttpConnection::Transaction::outputDataAvail
bool outputDataAvail() const
Checks if the output buffer contains some data to be sent.
Definition connection.h:186
isc::http::HttpConnection::Transaction::spawn
static TransactionPtr spawn(const HttpResponseCreatorPtr &response_creator, const TransactionPtr &transaction)
Creates new transaction from the current transaction.
Definition connection.cc:50
isc::http::HttpConnection::Transaction::getInputBufSize
size_t getInputBufSize() const
Returns input buffer size.
Definition connection.h:177
isc::http::HttpConnection::Transaction::getInputBufData
char * getInputBufData()
Returns pointer to the first byte of the input buffer.
Definition connection.h:172
isc::http::HttpConnection::Transaction::getOutputBufData
const char * getOutputBufData() const
Returns pointer to the first byte of the output buffer.
Definition connection.h:191
isc::http::HttpConnection::Transaction::consumeOutputBuf
void consumeOutputBuf(const size_t length)
Erases n bytes from the beginning of the output buffer.
Definition connection.h:210
isc::http::HttpConnection::Transaction::getOutputBufSize
size_t getOutputBufSize() const
Returns size of the output buffer.
Definition connection.h:196
isc::http::HttpConnection::Transaction::getParser
HttpRequestParserPtr getParser() const
Returns parser instance associated with the transaction.
Definition connection.h:167
isc::http::HttpConnection::closeWatchSocket
void closeWatchSocket()
Close the watch socket.
Definition connection.cc:203
isc::http::HttpConnection::socketReadCallback
void socketReadCallback(TransactionPtr transaction, boost::system::error_code ec, size_t length)
Callback invoked when new data is received over the socket.
Definition connection.cc:499
isc::http::HttpConnection::connection_pool_
std::weak_ptr< HttpConnectionPool > connection_pool_
Connection pool holding this connection.
Definition connection.h:447
isc::http::HttpConnection::closed_
bool closed_
Flag which indicates if the connection was already closed to avoid multiple close calls.
Definition connection.h:469
isc::http::HttpConnection::tls_context_
asiolink::TlsContextPtr tls_context_
TLS context.
Definition connection.h:431
isc::http::HttpConnection::io_service_
asiolink::IOServicePtr io_service_
The IO service used to handle events.
Definition connection.h:422
isc::http::HttpConnection::markWatchSocketReady
void markWatchSocketReady()
Mark the watch socket as ready.
Definition connection.cc:175
isc::http::HttpConnection::recordParameters
void recordParameters(const HttpRequestPtr &request) const
Records connection parameters into the HTTP request.
Definition connection.cc:101
isc::http::HttpConnection::acceptorCallback
void acceptorCallback(const boost::system::error_code &ec)
Local callback invoked when new connection is accepted.
Definition connection.cc:438
isc::http::HttpConnection::TransactionPtr
boost::shared_ptr< Transaction > TransactionPtr
Shared pointer to the Transaction.
Definition connection.h:87
isc::http::HttpConnection::setupIdleTimer
void setupIdleTimer()
Reset timer for detecting idle timeout in persistent connections.
Definition connection.cc:659
isc::http::HttpConnection::clearWatchSocket
void clearWatchSocket()
Clear the watch socket's ready marker.
Definition connection.cc:189
isc::http::HttpConnection::socketWriteCallback
virtual void socketWriteCallback(TransactionPtr transaction, boost::system::error_code ec, size_t length)
Callback invoked when data is sent over the socket.
Definition connection.cc:599
isc::http::HttpConnection::doHandshake
void doHandshake()
Asynchronously performs TLS handshake.
Definition connection.cc:317
isc::http::HttpConnection::asyncSendResponse
void asyncSendResponse(const ConstHttpResponsePtr &response, TransactionPtr transaction)
Sends HTTP response asynchronously.
Definition connection.cc:431
isc::http::HttpConnection::doRead
void doRead(TransactionPtr transaction=TransactionPtr())
Starts asynchronous read from the socket.
Definition connection.cc:342
isc::http::HttpConnection::acceptor_
HttpAcceptorPtr acceptor_
Pointer to the TCP acceptor used to accept new connections.
Definition connection.h:444
isc::http::HttpConnection::tls_socket_
std::unique_ptr< asiolink::TLSSocket< SocketCallback > > tls_socket_
TLS socket used by this connection.
Definition connection.h:441
isc::http::HttpConnection::doWrite
void doWrite(TransactionPtr transaction)
Starts asynchronous write to the socket.
Definition connection.cc:379
isc::http::HttpConnection::defer_shutdown_
bool defer_shutdown_
Flag which indicates if the connection shutdown should be deferred until the connection is no longer ...
Definition connection.h:465
isc::http::HttpConnection::setupRequestTimer
void setupRequestTimer(TransactionPtr transaction=TransactionPtr())
Reset timer for detecting request timeouts.
Definition connection.cc:648
isc::http::HttpConnection::shutdown
void shutdown()
Shutdown the socket.
Definition connection.cc:147
isc::http::HttpConnection::shutdownCallback
void shutdownCallback(const boost::system::error_code &ec)
Callback invoked when TLS shutdown is performed.
Definition connection.cc:132
isc::http::HttpConnection::close
void close()
Closes the socket.
Definition connection.cc:218
isc::http::HttpConnection::stopThisConnection
void stopThisConnection()
Stops current connection.
Definition connection.cc:269
isc::http::HttpConnection::HttpConnection
HttpConnection(const asiolink::IOServicePtr &io_service, const HttpAcceptorPtr &acceptor, const asiolink::TlsContextPtr &tls_context, std::shared_ptr< HttpConnectionPool > connection_pool, const HttpResponseCreatorPtr &response_creator, const HttpAcceptorCallback &callback, const long request_timeout, const long idle_timeout)
Constructor.
Definition connection.cc:68
isc::http::HttpConnection::getRemoteEndpointAddressAsText
std::string getRemoteEndpointAddressAsText() const
Returns remote address in textual form.
Definition connection.cc:713
isc::http::HttpConnection::use_external_
bool use_external_
Use external sockets flag.
Definition connection.h:457
isc::http::HttpConnection::handshakeCallback
void handshakeCallback(const boost::system::error_code &ec)
Local callback invoked when TLS handshake is performed.
Definition connection.cc:480
isc::http::HttpConnection::response_creator_
HttpResponseCreatorPtr response_creator_
Pointer to the HttpResponseCreator object used to create HTTP responses.
Definition connection.h:451
isc::http::HttpConnection::idleTimeoutCallback
void idleTimeoutCallback()
Definition connection.cc:702
isc::http::HttpConnection::idle_timeout_
long idle_timeout_
Timeout after which the persistent HTTP connection is shut down by the server.
Definition connection.h:435
isc::http::HttpConnection::asyncAccept
void asyncAccept()
Asynchronously accepts new connection.
Definition connection.cc:289
isc::http::HttpConnection::tcp_socket_
std::unique_ptr< asiolink::TCPSocket< SocketCallback > > tcp_socket_
TCP socket used by this connection.
Definition connection.h:438
isc::http::HttpConnection::requestTimeoutCallback
void requestTimeoutCallback(TransactionPtr transaction)
Callback invoked when the HTTP Request Timeout occurs.
Definition connection.cc:666
isc::http::HttpConnection::addExternalSockets
void addExternalSockets(bool use_external=false)
Use external sockets flag.
Definition connection.cc:96
isc::http::HttpConnection::request_timer_
asiolink::IntervalTimer request_timer_
Timer used to detect Request Timeout.
Definition connection.h:425
isc::http::HttpConnection::acceptor_callback_
HttpAcceptorCallback acceptor_callback_
External TCP acceptor callback.
Definition connection.h:454
isc::http::HttpConnection::watch_socket_
util::WatchSocketPtr watch_socket_
Pointer to watch socket instance used to signal that the socket is ready for read or write when use e...
Definition connection.h:461
isc::http::HttpConnection::request_timeout_
long request_timeout_
Configured Request Timeout in milliseconds.
Definition connection.h:428
isc::http::HttpConnection::shutdownConnection
void shutdownConnection()
Shuts down current connection.
Definition connection.cc:252
isc::http::HttpConnection::~HttpConnection
virtual ~HttpConnection()
Destructor.
Definition connection.cc:91
http_acceptor.h
interval_timer.h
io_service.h
isc::asiolink::TlsContextPtr
boost::shared_ptr< TlsContext > TlsContextPtr
The type of shared pointers to TlsContext objects.
Definition common_tls.h:37
isc::asiolink::IOServicePtr
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
Definition io_service.h:35
isc::http
Definition auth_config.h:19
isc::http::ConstHttpResponsePtr
boost::shared_ptr< const HttpResponse > ConstHttpResponsePtr
Pointer to the const HttpResponse object.
Definition response.h:84
isc::http::HttpRequestParserPtr
boost::shared_ptr< HttpRequestParser > HttpRequestParserPtr
Pointer to the HttpRequestParser.
Definition request_parser.h:20
isc::http::HttpResponseCreatorPtr
boost::shared_ptr< HttpResponseCreator > HttpResponseCreatorPtr
Pointer to the HttpResponseCreator object.
Definition response_creator.h:20
isc::http::HttpAcceptorCallback
std::function< void(const boost::system::error_code &)> HttpAcceptorCallback
Type of the callback for the TCP acceptor used in this library.
Definition http_acceptor.h:20
isc::http::HttpConnectionPtr
boost::shared_ptr< HttpConnection > HttpConnectionPtr
Pointer to the HttpConnection.
Definition connection.h:41
isc::http::HttpRequestPtr
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
Definition request.h:30
isc::http::HttpAcceptorPtr
boost::shared_ptr< HttpAcceptor > HttpAcceptorPtr
Type of shared pointer to TCP acceptors.
Definition http_acceptor.h:31
isc::util::WatchSocketPtr
boost::shared_ptr< WatchSocket > WatchSocketPtr
Defines a smart pointer to an instance of a WatchSocket.
Definition watch_socket.h:139
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
request_parser.h
response_creator_factory.h
watch_socket.h
Defines the class, WatchSocket.