7 #ifndef HTTP_CONNECTION_H
8 #define HTTP_CONNECTION_H
15 #include <boost/enable_shared_from_this.hpp>
16 #include <boost/system/error_code.hpp>
17 #include <boost/shared_ptr.hpp>
36 class HttpConnectionPool;
43 class HttpConnection :
public boost::enable_shared_from_this<HttpConnection> {
48 typedef std::function<void(boost::system::error_code ec, size_t length)>
49 SocketCallbackFunction;
54 class SocketCallback {
61 SocketCallback(SocketCallbackFunction socket_callback)
74 void operator()(boost::system::error_code ec,
size_t length = 0);
158 const TransactionPtr& transaction);
172 return (input_buf_.data());
177 return (input_buf_.size());
186 return (!output_buf_.empty());
191 return (output_buf_.data());
196 return (output_buf_.size());
203 output_buf_ = response;
210 output_buf_.erase(0, length);
222 std::array<char, 32768> input_buf_;
225 std::string output_buf_;
250 const long request_timeout,
251 const long idle_timeout);
301 void doWrite(TransactionPtr transaction);
310 TransactionPtr transaction);
341 boost::system::error_code ec,
351 boost::system::error_code ec,
406 std::unique_ptr<asiolink::TCPSocket<SocketCallback> >
tcp_socket_;
409 std::unique_ptr<asiolink::TLSSocket<SocketCallback> >
tls_socket_;
void asyncSendResponse(const ConstHttpResponsePtr &response, TransactionPtr transaction)
Sends HTTP response asynchronously.
boost::shared_ptr< const HttpResponse > ConstHttpResponsePtr
Pointer to the const HttpResponse object.
size_t getInputBufSize() const
Returns input buffer size.
std::unique_ptr< asiolink::TCPSocket< SocketCallback > > tcp_socket_
TCP socket used by this connection.
void handshakeCallback(const boost::system::error_code &ec)
Local callback invoked when TLS handshake is performed.
Accepts and handles a single HTTP connection.
void doRead(TransactionPtr transaction=TransactionPtr())
Starts asynchronous read from the socket.
std::function< void()> callback_
The callback function.
HttpAcceptorCallback acceptor_callback_
External TCP acceptor callback.
boost::shared_ptr< Transaction > TransactionPtr
Shared pointer to the Transaction.
void doWrite(TransactionPtr transaction)
Starts asynchronous write to the socket.
boost::shared_ptr< HttpAcceptor > HttpAcceptorPtr
Type of shared pointer to TCP acceptors.
HttpAcceptorPtr acceptor_
Pointer to the TCP acceptor used to accept new connections.
void shutdownCallback(const boost::system::error_code &ec)
Callback invoked when TLS shutdown is performed.
static TransactionPtr spawn(const HttpResponseCreatorPtr &response_creator, const TransactionPtr &transaction)
Creates new transaction from the current transaction.
void doHandshake()
Asynchronously performs TLS handshake.
void shutdown()
Shutdown the socket.
std::function< void(const boost::system::error_code &)> HttpAcceptorCallback
Type of the callback for the TCP acceptor used in this library.
The IOService class is a wrapper for the ASIO io_service class.
virtual ~HttpConnection()
Destructor.
boost::shared_ptr< TlsContext > TlsContextPtr
The type of shared pointers to TlsContext objects.
asiolink::TlsContextPtr tls_context_
TLS context.
virtual void socketWriteCallback(TransactionPtr transaction, boost::system::error_code ec, size_t length)
Callback invoked when data is sent over the socket.
void stopThisConnection()
Stops current connection.
Generic error reported within HttpConnection class.
The IntervalTimer class is a wrapper for the ASIO boost::asio::deadline_timer class.
void shutdownConnection()
Shuts down current connection.
void setupIdleTimer()
Reset timer for detecting idle timeout in persistent connections.
void socketReadCallback(TransactionPtr transaction, boost::system::error_code ec, size_t length)
Callback invoked when new data is received over the socket.
bool outputDataAvail() const
Checks if the output buffer contains some data to be sent.
std::unique_ptr< asiolink::TLSSocket< SocketCallback > > tls_socket_
TLS socket used by this connection.
HttpConnectionPool & connection_pool_
Connection pool holding this connection.
Represents a single exchange of the HTTP messages.
void idleTimeoutCallback()
HttpRequestPtr getRequest() const
Returns request instance associated with the transaction.
HttpResponseCreatorPtr response_creator_
Pointer to the HttpResponseCreator object used to create HTTP responses.
Transaction(const HttpResponseCreatorPtr &response_creator, const HttpRequestPtr &request=HttpRequestPtr())
Constructor.
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.
size_t getOutputBufSize() const
Returns size of the output buffer.
static TransactionPtr create(const HttpResponseCreatorPtr &response_creator)
Creates new transaction instance.
void setupRequestTimer(TransactionPtr transaction=TransactionPtr())
Reset timer for detecting request timeouts.
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.
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.
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.
std::string getRemoteEndpointAddressAsText() const
returns remote address in textual form
void acceptorCallback(const boost::system::error_code &ec)
Local callback invoked when new connection is accepted.
void close()
Closes the socket.
void consumeOutputBuf(const size_t length)
Erases n bytes from the beginning of the output buffer.
void asyncAccept()
Asynchronously accepts new connection.
void setOutputBuf(const std::string &response)
Replaces output buffer contents with new contents.
boost::shared_ptr< HttpConnection > HttpConnectionPtr
Pointer to the HttpConnection.
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
void requestTimeoutCallback(TransactionPtr transaction)
Callback invoked when the HTTP Request Timeout occurs.
char * getInputBufData()
Returns pointer to the first byte of the input buffer.
Pool of active HTTP connections.
HttpConnectionError(const char *file, size_t line, const char *what)
long idle_timeout_
Timeout after which the persistent HTTP connection is shut down by the server.
long request_timeout_
Configured Request Timeout in milliseconds.