Kea  2.3.5-git
connection.h
Go to the documentation of this file.
1 // Copyright (C) 2017-2022 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 
273  void recordParameters(const HttpRequestPtr& request) const;
274 
279  void doHandshake();
280 
293  void doRead(TransactionPtr transaction = TransactionPtr());
294 
295 protected:
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
boost::shared_ptr< const HttpResponse > ConstHttpResponsePtr
Pointer to the const HttpResponse object.
Definition: response.h:84
std::unique_ptr< asiolink::TCPSocket< SocketCallback > > tcp_socket_
TCP socket used by this connection.
Definition: connection.h:410
Accepts and handles a single HTTP connection.
Definition: connection.h:43
HttpAcceptorCallback acceptor_callback_
External TCP acceptor callback.
Definition: connection.h:426
boost::shared_ptr< Transaction > TransactionPtr
Shared pointer to the Transaction.
Definition: connection.h:83
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:416
const char * getOutputBufData() const
Returns pointer to the first byte of the output buffer.
Definition: connection.h:190
size_t getOutputBufSize() const
Returns size of the output buffer.
Definition: connection.h:195
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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
HttpRequestPtr getRequest() const
Returns request instance associated with the transaction.
Definition: connection.h:161
asiolink::TlsContextPtr tls_context_
TLS context.
Definition: connection.h:403
bool outputDataAvail() const
Checks if the output buffer contains some data to be sent.
Definition: connection.h:185
Generic error reported within HttpConnection class.
Definition: connection.h:26
std::unique_ptr< asiolink::TLSSocket< SocketCallback > > tls_socket_
TLS socket used by this connection.
Definition: connection.h:413
HttpConnectionPool & connection_pool_
Connection pool holding this connection.
Definition: connection.h:419
Represents a single exchange of the HTTP messages.
Definition: connection.h:118
HttpResponseCreatorPtr response_creator_
Pointer to the HttpResponseCreator object used to create HTTP responses.
Definition: connection.h:423
boost::shared_ptr< HttpResponseCreator > HttpResponseCreatorPtr
Pointer to the HttpResponseCreator object.
boost::shared_ptr< HttpRequestParser > HttpRequestParserPtr
Pointer to the HttpRequestParser.
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-lfc.
HttpRequestParserPtr getParser() const
Returns parser instance associated with the transaction.
Definition: connection.h:166
asiolink::IntervalTimer request_timer_
Timer used to detect Request Timeout.
Definition: connection.h:397
void consumeOutputBuf(const size_t length)
Erases n bytes from the beginning of the output buffer.
Definition: connection.h:209
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
size_t getInputBufSize() const
Returns input buffer size.
Definition: connection.h:176
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
Definition: request.h:27
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:407
long request_timeout_
Configured Request Timeout in milliseconds.
Definition: connection.h:400