Kea 2.5.9
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 <boost/enable_shared_from_this.hpp>
16#include <boost/system/error_code.hpp>
17#include <boost/shared_ptr.hpp>
18#include <array>
19#include <functional>
20#include <string>
21
22namespace isc {
23namespace http {
24
27public:
28 HttpConnectionError(const char* file, size_t line, const char* what) :
29 isc::Exception(file, line, what) { };
30};
31
36class HttpConnectionPool;
37
38class HttpConnection;
40typedef boost::shared_ptr<HttpConnection> HttpConnectionPtr;
41
43class HttpConnection : public boost::enable_shared_from_this<HttpConnection> {
44private:
45
48 typedef std::function<void(boost::system::error_code ec, size_t length)>
49 SocketCallbackFunction;
50
54 class SocketCallback {
55 public:
56
61 SocketCallback(SocketCallbackFunction socket_callback)
62 : callback_(socket_callback) {
63 }
64
74 void operator()(boost::system::error_code ec, size_t length = 0);
75
76 private:
78 SocketCallbackFunction callback_;
79 };
80
81protected:
82
83 class Transaction;
84
86 typedef boost::shared_ptr<Transaction> TransactionPtr;
87
119 public:
120
128 Transaction(const HttpResponseCreatorPtr& response_creator,
129 const HttpRequestPtr& request = HttpRequestPtr());
130
140 static TransactionPtr create(const HttpResponseCreatorPtr& response_creator);
141
157 static TransactionPtr spawn(const HttpResponseCreatorPtr& response_creator,
158 const TransactionPtr& transaction);
159
162 return (request_);
163 }
164
167 return (parser_);
168 }
169
172 return (input_buf_.data());
173 }
174
176 size_t getInputBufSize() const {
177 return (input_buf_.size());
178 }
179
185 bool outputDataAvail() const {
186 return (!output_buf_.empty());
187 }
188
190 const char* getOutputBufData() const {
191 return (output_buf_.data());
192 }
193
195 size_t getOutputBufSize() const {
196 return (output_buf_.size());
197 }
198
202 void setOutputBuf(const std::string& response) {
203 output_buf_ = response;
204 }
205
209 void consumeOutputBuf(const size_t length) {
210 output_buf_.erase(0, length);
211 }
212
213 private:
214
216 HttpRequestPtr request_;
217
219 HttpRequestParserPtr parser_;
220
222 std::array<char, 32768> input_buf_;
223
225 std::string output_buf_;
226 };
227
228public:
229
244 HttpConnection(const asiolink::IOServicePtr& io_service,
245 const HttpAcceptorPtr& acceptor,
246 const asiolink::TlsContextPtr& tls_context,
247 HttpConnectionPool& connection_pool,
248 const HttpResponseCreatorPtr& response_creator,
249 const HttpAcceptorCallback& callback,
250 const long request_timeout,
251 const long idle_timeout);
252
256 virtual ~HttpConnection();
257
262 void asyncAccept();
263
265 void shutdown();
266
268 void close();
269
273 void recordParameters(const HttpRequestPtr& request) const;
274
279 void doHandshake();
280
293 void doRead(TransactionPtr transaction = TransactionPtr());
294
295protected:
296
305 void doWrite(TransactionPtr transaction);
306
313 void asyncSendResponse(const ConstHttpResponsePtr& response,
314 TransactionPtr transaction);
315
324 void acceptorCallback(const boost::system::error_code& ec);
325
332 void handshakeCallback(const boost::system::error_code& ec);
333
344 void socketReadCallback(TransactionPtr transaction,
345 boost::system::error_code ec,
346 size_t length);
347
354 virtual void socketWriteCallback(TransactionPtr transaction,
355 boost::system::error_code ec,
356 size_t length);
357
365 void shutdownCallback(const boost::system::error_code& ec);
366
370 void setupRequestTimer(TransactionPtr transaction = TransactionPtr());
371
373 void setupIdleTimer();
374
381 void requestTimeoutCallback(TransactionPtr transaction);
382
383 void idleTimeoutCallback();
384
388 void shutdownConnection();
389
391 void stopThisConnection();
392
394 std::string getRemoteEndpointAddressAsText() const;
395
398
401
404
408
410 std::unique_ptr<asiolink::TCPSocket<SocketCallback> > tcp_socket_;
411
413 std::unique_ptr<asiolink::TLSSocket<SocketCallback> > tls_socket_;
414
417
420
424
427};
428
429} // end of namespace isc::http
430} // end of namespace isc
431
432#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:26
HttpConnectionError(const char *file, size_t line, const char *what)
Definition: connection.h:28
Pool of active HTTP connections.
Represents a single exchange of the HTTP messages.
Definition: connection.h:118
void setOutputBuf(const std::string &response)
Replaces output buffer contents with new contents.
Definition: connection.h:202
static TransactionPtr create(const HttpResponseCreatorPtr &response_creator)
Creates new transaction instance.
Definition: connection.cc:42
HttpRequestPtr getRequest() const
Returns request instance associated with the transaction.
Definition: connection.h:161
bool outputDataAvail() const
Checks if the output buffer contains some data to be sent.
Definition: connection.h:185
static TransactionPtr spawn(const HttpResponseCreatorPtr &response_creator, const TransactionPtr &transaction)
Creates new transaction from the current transaction.
Definition: connection.cc:47
size_t getInputBufSize() const
Returns input buffer size.
Definition: connection.h:176
char * getInputBufData()
Returns pointer to the first byte of the input buffer.
Definition: connection.h:171
const char * getOutputBufData() const
Returns pointer to the first byte of the output buffer.
Definition: connection.h:190
void consumeOutputBuf(const size_t length)
Erases n bytes from the beginning of the output buffer.
Definition: connection.h:209
size_t getOutputBufSize() const
Returns size of the output buffer.
Definition: connection.h:195
HttpRequestParserPtr getParser() const
Returns parser instance associated with the transaction.
Definition: connection.h:166
Accepts and handles a single HTTP connection.
Definition: connection.h:43
HttpConnectionPool & connection_pool_
Connection pool holding this connection.
Definition: connection.h:419
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:378
asiolink::TlsContextPtr tls_context_
TLS context.
Definition: connection.h:403
void recordParameters(const HttpRequestPtr &request) const
Records connection parameters into the HTTP request.
Definition: connection.cc:96
void acceptorCallback(const boost::system::error_code &ec)
Local callback invoked when new connection is accepted.
Definition: connection.cc:332
boost::shared_ptr< Transaction > TransactionPtr
Shared pointer to the Transaction.
Definition: connection.h:86
void setupIdleTimer()
Reset timer for detecting idle timeout in persistent connections.
Definition: connection.cc:529
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:472
void doHandshake()
Asynchronously performs TLS handshake.
Definition: connection.cc:218
void asyncSendResponse(const ConstHttpResponsePtr &response, TransactionPtr transaction)
Sends HTTP response asynchronously.
Definition: connection.cc:324
void doRead(TransactionPtr transaction=TransactionPtr())
Starts asynchronous read from the socket.
Definition: connection.cc:241
HttpAcceptorPtr acceptor_
Pointer to the TCP acceptor used to accept new connections.
Definition: connection.h:416
std::unique_ptr< asiolink::TLSSocket< SocketCallback > > tls_socket_
TLS socket used by this connection.
Definition: connection.h:413
void doWrite(TransactionPtr transaction)
Starts asynchronous write to the socket.
Definition: connection.cc:278
void setupRequestTimer(TransactionPtr transaction=TransactionPtr())
Reset timer for detecting request timeouts.
Definition: connection.cc:518
void shutdown()
Shutdown the socket.
Definition: connection.cc:132
void shutdownCallback(const boost::system::error_code &ec)
Callback invoked when TLS shutdown is performed.
Definition: connection.cc:127
void close()
Closes the socket.
Definition: connection.cc:151
void stopThisConnection()
Stops current connection.
Definition: connection.cc:178
std::string getRemoteEndpointAddressAsText() const
returns remote address in textual form
Definition: connection.cc:583
void handshakeCallback(const boost::system::error_code &ec)
Local callback invoked when TLS handshake is performed.
Definition: connection.cc:362
HttpResponseCreatorPtr response_creator_
Pointer to the HttpResponseCreator object used to create HTTP responses.
Definition: connection.h:423
long idle_timeout_
Timeout after which the persistent HTTP connection is shut down by the server.
Definition: connection.h:407
void asyncAccept()
Asynchronously accepts new connection.
Definition: connection.cc:190
std::unique_ptr< asiolink::TCPSocket< SocketCallback > > tcp_socket_
TCP socket used by this connection.
Definition: connection.h:410
void requestTimeoutCallback(TransactionPtr transaction)
Callback invoked when the HTTP Request Timeout occurs.
Definition: connection.cc:536
asiolink::IntervalTimer request_timer_
Timer used to detect Request Timeout.
Definition: connection.h:397
HttpAcceptorCallback acceptor_callback_
External TCP acceptor callback.
Definition: connection.h:426
long request_timeout_
Configured Request Timeout in milliseconds.
Definition: connection.h:400
void shutdownConnection()
Shuts down current connection.
Definition: connection.cc:166
virtual ~HttpConnection()
Destructor.
Definition: connection.cc:91
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.
Definition: http_acceptor.h:20
boost::shared_ptr< HttpConnection > HttpConnectionPtr
Pointer to the HttpConnection.
Definition: connection.h:40
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.
Definition: http_acceptor.h:31
Defines the logger used by the top-level component of kea-lfc.