Kea  1.5.0
connection.h
Go to the documentation of this file.
1 // Copyright (C) 2017-2018 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/function.hpp>
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 <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 
38 class HttpConnection;
40 typedef boost::shared_ptr<HttpConnection> HttpConnectionPtr;
41 
43 class HttpConnection : public boost::enable_shared_from_this<HttpConnection> {
44 private:
45 
48  typedef boost::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 
82 public:
83 
98  HttpAcceptor& acceptor,
99  HttpConnectionPool& connection_pool,
100  const HttpResponseCreatorPtr& response_creator,
101  const HttpAcceptorCallback& callback,
102  const long request_timeout,
103  const long idle_timeout);
104 
108  ~HttpConnection();
109 
114  void asyncAccept();
115 
117  void close();
118 
127  void doRead();
128 
129 private:
130 
136  void doWrite();
137 
143  void asyncSendResponse(const ConstHttpResponsePtr& response);
144 
152  void acceptorCallback(const boost::system::error_code& ec);
153 
162  void socketReadCallback(boost::system::error_code ec,
163  size_t length);
164 
169  void socketWriteCallback(boost::system::error_code ec,
170  size_t length);
171 
178  void reinitProcessingState();
179 
181  void setupRequestTimer();
182 
184  void setupIdleTimer();
185 
190  void requestTimeoutCallback();
191 
192  void idleTimeoutCallback();
193 
195  void stopThisConnection();
196 
198  std::string getRemoteEndpointAddressAsText() const;
199 
201  asiolink::IntervalTimer request_timer_;
202 
204  long request_timeout_;
205 
208  long idle_timeout_;
209 
212 
214  HttpAcceptor& acceptor_;
215 
217  HttpConnectionPool& connection_pool_;
218 
221  HttpResponseCreatorPtr response_creator_;
222 
224  HttpRequestPtr request_;
225 
227  HttpRequestParserPtr parser_;
228 
230  HttpAcceptorCallback acceptor_callback_;
231 
233  std::array<char, 32768> buf_;
234 
236  std::string output_buf_;
237 };
238 
239 } // end of namespace isc::http
240 } // end of namespace isc
241 
242 #endif
isc::http::HttpRequestParserPtr
boost::shared_ptr< HttpRequestParser > HttpRequestParserPtr
Pointer to the HttpRequestParser.
Definition: request_parser.h:17
isc::http::HttpConnectionPool
Pool of active HTTP connections.
Definition: connection_pool.h:28
isc::http::HttpAcceptorCallback
boost::function< void(const boost::system::error_code &)> HttpAcceptorCallback
Type of the callback for the TCP acceptor used in this library.
Definition: http_acceptor.h:19
isc::http::HttpConnection::~HttpConnection
~HttpConnection()
Destructor.
Definition: connection.cc:61
isc::http::HttpRequestPtr
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
Definition: request.h:25
isc::http::ConstHttpResponsePtr
boost::shared_ptr< const HttpResponse > ConstHttpResponsePtr
Pointer to the const HttpResponse object.
Definition: response.h:84
io_service.h
isc::Exception
This is a base class for exceptions thrown from the DNS library module.
Definition: exceptions/exceptions.h:23
isc::http::HttpConnectionError::HttpConnectionError
HttpConnectionError(const char *file, size_t line, const char *what)
Definition: connection.h:28
http_acceptor.h
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition: exceptions/exceptions.cc:32
isc::http::HttpConnection
Accepts and handles a single HTTP connection.
Definition: connection.h:43
isc::http::HttpResponseCreatorPtr
boost::shared_ptr< HttpResponseCreator > HttpResponseCreatorPtr
Pointer to the HttpResponseCreator object.
Definition: response_creator.h:17
response_creator_factory.h
isc::http::HttpConnection::HttpConnection
HttpConnection(asiolink::IOService &io_service, HttpAcceptor &acceptor, HttpConnectionPool &connection_pool, const HttpResponseCreatorPtr &response_creator, const HttpAcceptorCallback &callback, const long request_timeout, const long idle_timeout)
Constructor.
Definition: connection.cc:39
isc::http::HttpConnectionError
Generic error reported within HttpConnection class.
Definition: connection.h:26
isc::http::HttpConnectionPtr
boost::shared_ptr< HttpConnection > HttpConnectionPtr
Pointer to the HttpConnection.
Definition: connection.h:38
isc::http::HttpConnection::doRead
void doRead()
Starts asynchronous read from the socket.
Definition: connection.cc:101
interval_timer.h
isc::http::HttpConnection::asyncAccept
void asyncAccept()
Asynchronously accepts new connection.
Definition: connection.cc:84
request_parser.h
isc::http::HttpConnection::close
void close()
Closes the socket.
Definition: connection.cc:66