Kea 3.3.3
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
11#include <asiolink/io_service.h>
12#include <http/http_acceptor.h>
13#include <http/request_parser.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
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;
41typedef boost::shared_ptr<HttpConnection> HttpConnectionPtr;
42
44class 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
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
163 return (request_);
164 }
165
168 return (parser_);
169 }
170
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 (position_ < output_buf_.size());
188 }
189
194 const char* getOutputBufData() const {
195 return (&output_buf_[position_]);
196 }
197
199 size_t getOutputBufSize() const {
200 return (output_buf_.size() - position_);
201 }
202
206 void setOutputBuf(const std::string& response) {
207 if (response.empty()) {
208 isc_throw(Unexpected, "empty response");
209 }
210 output_buf_ = response;
211 }
212
216 void consumeOutputBuf(const size_t length) {
217 if (length >= output_buf_.size() - position_) {
218 position_ = output_buf_.size();
219 } else {
220 position_ += length;
221 }
222 }
223
224 private:
225
227 HttpRequestPtr request_;
228
230 HttpRequestParserPtr parser_;
231
233 std::array<char, 32768> input_buf_;
234
236 std::string output_buf_;
237
239 size_t position_;
240 };
241
242public:
243
258 HttpConnection(const asiolink::IOServicePtr& io_service,
259 const HttpAcceptorPtr& acceptor,
260 const asiolink::TlsContextPtr& tls_context,
261 std::shared_ptr<HttpConnectionPool> connection_pool,
262 const HttpResponseCreatorPtr& response_creator,
263 const HttpAcceptorCallback& callback,
264 const long request_timeout,
265 const long idle_timeout);
266
270 virtual ~HttpConnection();
271
278 void addExternalSockets(bool use_external = false);
279
284 void asyncAccept();
285
287 void shutdown();
288
290 void close();
291
295 void recordParameters(const HttpRequestPtr& request) const;
296
301 void doHandshake();
302
315 virtual void doRead(TransactionPtr transaction = TransactionPtr());
316
317protected:
318
327 virtual void doWrite(TransactionPtr transaction);
328
335 void asyncSendResponse(const ConstHttpResponsePtr& response,
336 TransactionPtr transaction);
337
346 void acceptorCallback(const boost::system::error_code& ec);
347
354 void handshakeCallback(const boost::system::error_code& ec);
355
366 virtual void socketReadCallback(TransactionPtr transaction,
367 boost::system::error_code ec,
368 size_t length);
369
376 virtual void socketWriteCallback(TransactionPtr transaction,
377 boost::system::error_code ec,
378 size_t length);
379
387 void shutdownCallback(const boost::system::error_code& ec);
388
392 void setupRequestTimer(TransactionPtr transaction = TransactionPtr());
393
395 void setupIdleTimer();
396
403 void requestTimeoutCallback(TransactionPtr transaction);
404
405 void idleTimeoutCallback();
406
410 void shutdownConnection();
411
413 void stopThisConnection();
414
416 std::string getRemoteEndpointAddressAsText() const;
417
424
429 void clearWatchSocket();
430
432 void closeWatchSocket();
433
436
439
442
445
449
451 std::unique_ptr<asiolink::TCPSocket<SocketCallback> > tcp_socket_;
452
454 std::unique_ptr<asiolink::TLSSocket<SocketCallback> > tls_socket_;
455
458
460 std::weak_ptr<HttpConnectionPool> connection_pool_;
461
465
468
471
475
479
483
487};
488
489} // end of namespace isc::http
490} // end of namespace isc
491
492#endif
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown when an unexpected error condition occurs.
HttpConnectionError(const char *file, size_t line, const char *what)
Definition connection.h:29
Represents a single exchange of the HTTP messages.
Definition connection.h:119
void setOutputBuf(const std::string &response)
Replaces output buffer contents with new contents.
Definition connection.h:206
static TransactionPtr create(const HttpResponseCreatorPtr &response_creator)
Creates new transaction instance.
Definition connection.cc:46
HttpRequestPtr getRequest() const
Returns request instance associated with the transaction.
Definition connection.h:162
Transaction(const HttpResponseCreatorPtr &response_creator, const HttpRequestPtr &request=HttpRequestPtr())
Constructor.
Definition connection.cc:35
bool outputDataAvail() const
Checks if the output buffer contains some data to be sent.
Definition connection.h:186
static TransactionPtr spawn(const HttpResponseCreatorPtr &response_creator, const TransactionPtr &transaction)
Creates new transaction from the current transaction.
Definition connection.cc:51
size_t getInputBufSize() const
Returns input buffer size.
Definition connection.h:177
char * getInputBufData()
Returns pointer to the first byte of the input buffer.
Definition connection.h:172
const char * getOutputBufData() const
Returns pointer to the first byte of the output buffer.
Definition connection.h:194
void consumeOutputBuf(const size_t length)
Erases n bytes from the beginning of the output buffer.
Definition connection.h:216
size_t getOutputBufSize() const
Returns remaining size of the output buffer.
Definition connection.h:199
HttpRequestParserPtr getParser() const
Returns parser instance associated with the transaction.
Definition connection.h:167
void closeWatchSocket()
Close the watch socket.
virtual void socketReadCallback(TransactionPtr transaction, boost::system::error_code ec, size_t length)
Callback invoked when new data is received over the socket.
std::weak_ptr< HttpConnectionPool > connection_pool_
Connection pool holding this connection.
Definition connection.h:460
bool closed_
Flag which indicates if the connection was already closed to avoid multiple close calls.
Definition connection.h:482
asiolink::TlsContextPtr tls_context_
TLS context.
Definition connection.h:444
asiolink::IOServicePtr io_service_
The IO service used to handle events.
Definition connection.h:435
void markWatchSocketReady()
Mark the watch socket as ready.
void recordParameters(const HttpRequestPtr &request) const
Records connection parameters into the HTTP request.
void acceptorCallback(const boost::system::error_code &ec)
Local callback invoked when new connection is accepted.
boost::shared_ptr< Transaction > TransactionPtr
Shared pointer to the Transaction.
Definition connection.h:87
void setupIdleTimer()
Reset timer for detecting idle timeout in persistent connections.
void clearWatchSocket()
Clear the watch socket's ready marker.
bool registered_
Flag which indicates if the connection file descriptor was registered as an external socket.
Definition connection.h:486
virtual void socketWriteCallback(TransactionPtr transaction, boost::system::error_code ec, size_t length)
Callback invoked when data is sent over the socket.
void doHandshake()
Asynchronously performs TLS handshake.
void asyncSendResponse(const ConstHttpResponsePtr &response, TransactionPtr transaction)
Sends HTTP response asynchronously.
virtual void doRead(TransactionPtr transaction=TransactionPtr())
Starts asynchronous read from the socket.
HttpAcceptorPtr acceptor_
Pointer to the TCP acceptor used to accept new connections.
Definition connection.h:457
std::unique_ptr< asiolink::TLSSocket< SocketCallback > > tls_socket_
TLS socket used by this connection.
Definition connection.h:454
virtual void doWrite(TransactionPtr transaction)
Starts asynchronous write to the socket.
bool defer_shutdown_
Flag which indicates if the connection shutdown should be deferred until the connection is no longer ...
Definition connection.h:478
void setupRequestTimer(TransactionPtr transaction=TransactionPtr())
Reset timer for detecting request timeouts.
void shutdown()
Shutdown the socket.
void shutdownCallback(const boost::system::error_code &ec)
Callback invoked when TLS shutdown is performed.
void close()
Closes the socket.
void stopThisConnection()
Stops current connection.
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:69
std::string getRemoteEndpointAddressAsText() const
Returns remote address in textual form.
bool use_external_
Use external sockets flag.
Definition connection.h:470
void handshakeCallback(const boost::system::error_code &ec)
Local callback invoked when TLS handshake is performed.
HttpResponseCreatorPtr response_creator_
Pointer to the HttpResponseCreator object used to create HTTP responses.
Definition connection.h:464
long idle_timeout_
Timeout after which the persistent HTTP connection is shut down by the server.
Definition connection.h:448
void asyncAccept()
Asynchronously accepts new connection.
std::unique_ptr< asiolink::TCPSocket< SocketCallback > > tcp_socket_
TCP socket used by this connection.
Definition connection.h:451
void requestTimeoutCallback(TransactionPtr transaction)
Callback invoked when the HTTP Request Timeout occurs.
void addExternalSockets(bool use_external=false)
Use external sockets flag.
Definition connection.cc:97
asiolink::IntervalTimer request_timer_
Timer used to detect Request Timeout.
Definition connection.h:438
HttpAcceptorCallback acceptor_callback_
External TCP acceptor callback.
Definition connection.h:467
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:474
long request_timeout_
Configured Request Timeout in milliseconds.
Definition connection.h:441
void shutdownConnection()
Shuts down current connection.
virtual ~HttpConnection()
Destructor.
Definition connection.cc:92
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const HttpResponse > ConstHttpResponsePtr
Pointer to the const HttpResponse object.
Definition response.h:85
boost::shared_ptr< HttpRequestParser > HttpRequestParserPtr
Pointer to the HttpRequestParser.
boost::shared_ptr< HttpResponseCreator > HttpResponseCreatorPtr
Pointer to the HttpResponseCreator object.
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
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
Definition request.h:30
boost::shared_ptr< HttpAcceptor > HttpAcceptorPtr
Type of shared pointer to TCP acceptors.
boost::shared_ptr< WatchSocket > WatchSocketPtr
Defines a smart pointer to an instance of a WatchSocket.
Defines the logger used by the top-level component of kea-lfc.
Defines the class, WatchSocket.