Kea  1.9.9-git
connection.h
Go to the documentation of this file.
1 // Copyright (C) 2017-2021 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 
22 namespace isc {
23 namespace http {
24 
27 public:
28  HttpConnectionError(const char* file, size_t line, const char* what) :
29  isc::Exception(file, line, what) { };
30 };
31 
36 class HttpConnectionPool;
37 
40 typedef boost::shared_ptr<HttpConnection> HttpConnectionPtr;
41 
43 class HttpConnection : public boost::enable_shared_from_this<HttpConnection> {
44 private:
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 
81 protected:
82 
83  class Transaction;
84 
86  typedef boost::shared_ptr<Transaction> TransactionPtr;
87 
118  class Transaction {
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 
171  char* getInputBufData() {
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 
228 public:
229 
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 
272 
275  void doHandshake();
276 
289  void doRead(TransactionPtr transaction = TransactionPtr());
290 
291 protected:
292 
301  void doWrite(TransactionPtr transaction);
302 
309  void asyncSendResponse(const ConstHttpResponsePtr& response,
310  TransactionPtr transaction);
311 
320  void acceptorCallback(const boost::system::error_code& ec);
321 
328  void handshakeCallback(const boost::system::error_code& ec);
329 
340  void socketReadCallback(TransactionPtr transaction,
341  boost::system::error_code ec,
342  size_t length);
343 
350  virtual void socketWriteCallback(TransactionPtr transaction,
351  boost::system::error_code ec,
352  size_t length);
353 
361  void shutdownCallback(const boost::system::error_code& ec);
362 
366  void setupRequestTimer(TransactionPtr transaction = TransactionPtr());
367 
369  void setupIdleTimer();
370 
377  void requestTimeoutCallback(TransactionPtr transaction);
378 
379  void idleTimeoutCallback();
380 
384  void shutdownConnection();
385 
387  void stopThisConnection();
388 
390  std::string getRemoteEndpointAddressAsText() const;
391 
394 
397 
400 
404 
406  std::unique_ptr<asiolink::TCPSocket<SocketCallback> > tcp_socket_;
407 
409  std::unique_ptr<asiolink::TLSSocket<SocketCallback> > tls_socket_;
410 
413 
416 
420 
423 };
424 
425 } // end of namespace isc::http
426 } // end of namespace isc
427 
428 #endif
void asyncSendResponse(const ConstHttpResponsePtr &response, TransactionPtr transaction)
Sends HTTP response asynchronously.
Definition: connection.cc:292
boost::shared_ptr< const HttpResponse > ConstHttpResponsePtr
Pointer to the const HttpResponse object.
Definition: response.h:84
size_t getInputBufSize() const
Returns input buffer size.
Definition: connection.h:176
std::unique_ptr< asiolink::TCPSocket< SocketCallback > > tcp_socket_
TCP socket used by this connection.
Definition: connection.h:406
void handshakeCallback(const boost::system::error_code &ec)
Local callback invoked when TLS handshake is performed.
Definition: connection.cc:330
Accepts and handles a single HTTP connection.
Definition: connection.h:43
void doRead(TransactionPtr transaction=TransactionPtr())
Starts asynchronous read from the socket.
Definition: connection.cc:210
std::function< void()> callback_
The callback function.
Definition: io_service.cc:38
HttpAcceptorCallback acceptor_callback_
External TCP acceptor callback.
Definition: connection.h:422
boost::shared_ptr< Transaction > TransactionPtr
Shared pointer to the Transaction.
Definition: connection.h:83
void doWrite(TransactionPtr transaction)
Starts asynchronous write to the socket.
Definition: connection.cc:246
boost::shared_ptr< HttpAcceptor > HttpAcceptorPtr
Type of shared pointer to TCP acceptors.
Definition: http_acceptor.h:31
HttpAcceptorPtr acceptor_
Pointer to the TCP acceptor used to accept new connections.
Definition: connection.h:412
void shutdownCallback(const boost::system::error_code &ec)
Callback invoked when TLS shutdown is performed.
Definition: connection.cc:96
static TransactionPtr spawn(const HttpResponseCreatorPtr &response_creator, const TransactionPtr &transaction)
Creates new transaction from the current transaction.
Definition: connection.cc:47
void doHandshake()
Asynchronously performs TLS handshake.
Definition: connection.cc:187
void shutdown()
Shutdown the socket.
Definition: connection.cc:101
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
virtual ~HttpConnection()
Destructor.
Definition: connection.cc:91
asiolink::TlsContextPtr tls_context_
TLS context.
Definition: connection.h:399
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:440
void stopThisConnection()
Stops current connection.
Definition: connection.cc:147
Generic error reported within HttpConnection class.
Definition: connection.h:26
void shutdownConnection()
Shuts down current connection.
Definition: connection.cc:135
void setupIdleTimer()
Reset timer for detecting idle timeout in persistent connections.
Definition: connection.cc:497
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:346
bool outputDataAvail() const
Checks if the output buffer contains some data to be sent.
Definition: connection.h:185
std::unique_ptr< asiolink::TLSSocket< SocketCallback > > tls_socket_
TLS socket used by this connection.
Definition: connection.h:409
HttpConnectionPool & connection_pool_
Connection pool holding this connection.
Definition: connection.h:415
Represents a single exchange of the HTTP messages.
Definition: connection.h:118
HttpRequestPtr getRequest() const
Returns request instance associated with the transaction.
Definition: connection.h:161
HttpResponseCreatorPtr response_creator_
Pointer to the HttpResponseCreator object used to create HTTP responses.
Definition: connection.h:419
Transaction(const HttpResponseCreatorPtr &response_creator, const HttpRequestPtr &request=HttpRequestPtr())
Constructor.
Definition: connection.cc:32
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
HttpRequestParserPtr getParser() const
Returns parser instance associated with the transaction.
Definition: connection.h:166
size_t getOutputBufSize() const
Returns size of the output buffer.
Definition: connection.h:195
static TransactionPtr create(const HttpResponseCreatorPtr &response_creator)
Creates new transaction instance.
Definition: connection.cc:42
void setupRequestTimer(TransactionPtr transaction=TransactionPtr())
Reset timer for detecting request timeouts.
Definition: connection.cc:486
HttpConnection(asiolink::IOService &io_service, const HttpAcceptorPtr &acceptor, const asiolink::TlsContextPtr &tls_context, HttpConnectionPool &connection_pool, const HttpResponseCreatorPtr &response_creator, const HttpAcceptorCallback &callback, const long request_timeout, const long idle_timeout)
Constructor.
Definition: connection.cc:65
boost::shared_ptr< HttpResponseCreator > HttpResponseCreatorPtr
Pointer to the HttpResponseCreator object.
boost::shared_ptr< HttpRequestParser > HttpRequestParserPtr
Pointer to the HttpRequestParser.
const char * getOutputBufData() const
Returns pointer to the first byte of the output buffer.
Definition: connection.h:190
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-dhcp-ddns.
asiolink::IntervalTimer request_timer_
Timer used to detect Request Timeout.
Definition: connection.h:393
std::string getRemoteEndpointAddressAsText() const
returns remote address in textual form
Definition: connection.cc:551
void acceptorCallback(const boost::system::error_code &ec)
Local callback invoked when new connection is accepted.
Definition: connection.cc:300
void close()
Closes the socket.
Definition: connection.cc:120
void consumeOutputBuf(const size_t length)
Erases n bytes from the beginning of the output buffer.
Definition: connection.h:209
void asyncAccept()
Asynchronously accepts new connection.
Definition: connection.cc:159
void setOutputBuf(const std::string &response)
Replaces output buffer contents with new contents.
Definition: connection.h:202
boost::shared_ptr< HttpConnection > HttpConnectionPtr
Pointer to the HttpConnection.
Definition: connection.h:38
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
Definition: request.h:27
void requestTimeoutCallback(TransactionPtr transaction)
Callback invoked when the HTTP Request Timeout occurs.
Definition: connection.cc:504
char * getInputBufData()
Returns pointer to the first byte of the input buffer.
Definition: connection.h:171
Pool of active HTTP connections.
HttpConnectionError(const char *file, size_t line, const char *what)
Definition: connection.h:28
long idle_timeout_
Timeout after which the persistent HTTP connection is shut down by the server.
Definition: connection.h:403
long request_timeout_
Configured Request Timeout in milliseconds.
Definition: connection.h:396