Kea 2.7.6
connection.h
Go to the documentation of this file.
1// Copyright (C) 2017-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_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 (!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
411
416 void clearWatchSocket();
417
419 void closeWatchSocket();
420
423
426
429
433
435 std::unique_ptr<asiolink::TCPSocket<SocketCallback> > tcp_socket_;
436
438 std::unique_ptr<asiolink::TLSSocket<SocketCallback> > tls_socket_;
439
442
444 std::weak_ptr<HttpConnectionPool> connection_pool_;
445
449
452
455
459};
460
461} // end of namespace isc::http
462} // end of namespace isc
463
464#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.
Generic error reported within HttpConnection class.
Definition connection.h:27
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:203
static TransactionPtr create(const HttpResponseCreatorPtr &response_creator)
Creates new transaction instance.
Definition connection.cc:45
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:50
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:191
void consumeOutputBuf(const size_t length)
Erases n bytes from the beginning of the output buffer.
Definition connection.h:210
size_t getOutputBufSize() const
Returns size of the output buffer.
Definition connection.h:196
HttpRequestParserPtr getParser() const
Returns parser instance associated with the transaction.
Definition connection.h:167
Accepts and handles a single HTTP connection.
Definition connection.h:44
void closeWatchSocket()
Close the watch socket.
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:444
asiolink::TlsContextPtr tls_context_
TLS context.
Definition connection.h:428
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.
void setupIdleTimer()
Reset timer for detecting idle timeout in persistent connections.
void clearWatchSocket()
Clear the watch socket's ready marker.
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.
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:441
std::unique_ptr< asiolink::TLSSocket< SocketCallback > > tls_socket_
TLS socket used by this connection.
Definition connection.h:438
boost::shared_ptr< Transaction > TransactionPtr
Shared pointer to the Transaction.
Definition connection.h:87
void doWrite(TransactionPtr transaction)
Starts asynchronous write to the socket.
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:68
std::string getRemoteEndpointAddressAsText() const
Returns remote address in textual form.
bool use_external_
Use external sockets flag.
Definition connection.h:454
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:448
long idle_timeout_
Timeout after which the persistent HTTP connection is shut down by the server.
Definition connection.h:432
void asyncAccept()
Asynchronously accepts new connection.
std::unique_ptr< asiolink::TCPSocket< SocketCallback > > tcp_socket_
TCP socket used by this connection.
Definition connection.h:435
void requestTimeoutCallback(TransactionPtr transaction)
Callback invoked when the HTTP Request Timeout occurs.
void addExternalSockets(bool use_external=false)
Use external sockets flag.
asiolink::IntervalTimer request_timer_
Timer used to detect Request Timeout.
Definition connection.h:422
HttpAcceptorCallback acceptor_callback_
External TCP acceptor callback.
Definition connection.h:451
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:458
long request_timeout_
Configured Request Timeout in milliseconds.
Definition connection.h:425
void shutdownConnection()
Shuts down current connection.
virtual ~HttpConnection()
Destructor.
Definition connection.cc:96
boost::shared_ptr< const HttpResponse > ConstHttpResponsePtr
Pointer to the const HttpResponse object.
Definition response.h:84
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.