7#ifndef HTTP_CONNECTION_H
8#define HTTP_CONNECTION_H
16#include <boost/enable_shared_from_this.hpp>
17#include <boost/system/error_code.hpp>
18#include <boost/shared_ptr.hpp>
37class HttpConnectionPool;
44class HttpConnection :
public boost::enable_shared_from_this<HttpConnection> {
49 typedef std::function<void(boost::system::error_code ec,
size_t length)>
50 SocketCallbackFunction;
55 class SocketCallback {
62 SocketCallback(SocketCallbackFunction socket_callback)
63 : callback_(socket_callback) {
75 void operator()(boost::system::error_code ec,
size_t length = 0);
79 SocketCallbackFunction callback_;
173 return (input_buf_.data());
178 return (input_buf_.size());
187 return (!output_buf_.empty());
192 return (output_buf_.data());
197 return (output_buf_.size());
204 output_buf_ = response;
211 output_buf_.erase(0, length);
223 std::array<char, 32768> input_buf_;
226 std::string output_buf_;
248 std::shared_ptr<HttpConnectionPool> connection_pool,
251 const long request_timeout,
252 const long idle_timeout);
354 boost::system::error_code ec,
364 boost::system::error_code ec,
435 std::unique_ptr<asiolink::TCPSocket<SocketCallback> >
tcp_socket_;
438 std::unique_ptr<asiolink::TLSSocket<SocketCallback> >
tls_socket_;
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.
The IntervalTimer class is a wrapper for the ASIO boost::asio::deadline_timer class.
Generic error reported within HttpConnection class.
HttpConnectionError(const char *file, size_t line, const char *what)
Represents a single exchange of the HTTP messages.
void setOutputBuf(const std::string &response)
Replaces output buffer contents with new contents.
static TransactionPtr create(const HttpResponseCreatorPtr &response_creator)
Creates new transaction instance.
HttpRequestPtr getRequest() const
Returns request instance associated with the transaction.
Transaction(const HttpResponseCreatorPtr &response_creator, const HttpRequestPtr &request=HttpRequestPtr())
Constructor.
bool outputDataAvail() const
Checks if the output buffer contains some data to be sent.
static TransactionPtr spawn(const HttpResponseCreatorPtr &response_creator, const TransactionPtr &transaction)
Creates new transaction from the current transaction.
size_t getInputBufSize() const
Returns input buffer size.
char * getInputBufData()
Returns pointer to the first byte of the input buffer.
const char * getOutputBufData() const
Returns pointer to the first byte of the output buffer.
void consumeOutputBuf(const size_t length)
Erases n bytes from the beginning of the output buffer.
size_t getOutputBufSize() const
Returns size of the output buffer.
HttpRequestParserPtr getParser() const
Returns parser instance associated with the transaction.
Accepts and handles a single HTTP connection.
void closeWatchSocket()
Close the watch socket.
void socketReadCallback(TransactionPtr transaction, boost::system::error_code ec, size_t length)
Callback invoked when new data is received over the socket.
std::weak_ptr< HttpConnectionPool > connection_pool_
Connection pool holding this connection.
asiolink::TlsContextPtr tls_context_
TLS context.
void markWatchSocketReady()
Mark the watch socket as ready.
void recordParameters(const HttpRequestPtr &request) const
Records connection parameters into the HTTP request.
void acceptorCallback(const boost::system::error_code &ec)
Local callback invoked when new connection is accepted.
boost::shared_ptr< Transaction > TransactionPtr
Shared pointer to the Transaction.
void setupIdleTimer()
Reset timer for detecting idle timeout in persistent connections.
void clearWatchSocket()
Clear the watch socket's ready marker.
virtual void socketWriteCallback(TransactionPtr transaction, boost::system::error_code ec, size_t length)
Callback invoked when data is sent over the socket.
void doHandshake()
Asynchronously performs TLS handshake.
void asyncSendResponse(const ConstHttpResponsePtr &response, TransactionPtr transaction)
Sends HTTP response asynchronously.
void doRead(TransactionPtr transaction=TransactionPtr())
Starts asynchronous read from the socket.
HttpAcceptorPtr acceptor_
Pointer to the TCP acceptor used to accept new connections.
std::unique_ptr< asiolink::TLSSocket< SocketCallback > > tls_socket_
TLS socket used by this connection.
void doWrite(TransactionPtr transaction)
Starts asynchronous write to the socket.
void setupRequestTimer(TransactionPtr transaction=TransactionPtr())
Reset timer for detecting request timeouts.
void shutdown()
Shutdown the socket.
void shutdownCallback(const boost::system::error_code &ec)
Callback invoked when TLS shutdown is performed.
void close()
Closes the socket.
void stopThisConnection()
Stops current connection.
HttpConnection(const asiolink::IOServicePtr &io_service, const HttpAcceptorPtr &acceptor, const asiolink::TlsContextPtr &tls_context, std::shared_ptr< HttpConnectionPool > connection_pool, const HttpResponseCreatorPtr &response_creator, const HttpAcceptorCallback &callback, const long request_timeout, const long idle_timeout)
Constructor.
std::string getRemoteEndpointAddressAsText() const
Returns remote address in textual form.
bool use_external_
Use external sockets flag.
void handshakeCallback(const boost::system::error_code &ec)
Local callback invoked when TLS handshake is performed.
HttpResponseCreatorPtr response_creator_
Pointer to the HttpResponseCreator object used to create HTTP responses.
void idleTimeoutCallback()
long idle_timeout_
Timeout after which the persistent HTTP connection is shut down by the server.
void asyncAccept()
Asynchronously accepts new connection.
std::unique_ptr< asiolink::TCPSocket< SocketCallback > > tcp_socket_
TCP socket used by this connection.
void requestTimeoutCallback(TransactionPtr transaction)
Callback invoked when the HTTP Request Timeout occurs.
void addExternalSockets(bool use_external=false)
Use external sockets flag.
asiolink::IntervalTimer request_timer_
Timer used to detect Request Timeout.
HttpAcceptorCallback acceptor_callback_
External TCP acceptor callback.
util::WatchSocketPtr watch_socket_
Pointer to watch socket instance used to signal that the socket is ready for read or write when use e...
long request_timeout_
Configured Request Timeout in milliseconds.
void shutdownConnection()
Shuts down current connection.
virtual ~HttpConnection()
Destructor.
boost::shared_ptr< TlsContext > TlsContextPtr
The type of shared pointers to TlsContext objects.
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
boost::shared_ptr< const HttpResponse > ConstHttpResponsePtr
Pointer to the const HttpResponse object.
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.
boost::shared_ptr< HttpConnection > HttpConnectionPtr
Pointer to the HttpConnection.
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
boost::shared_ptr< HttpAcceptor > HttpAcceptorPtr
Type of shared pointer to TCP acceptors.
boost::shared_ptr< WatchSocket > WatchSocketPtr
Defines a smart pointer to an instance of a WatchSocket.
Defines the logger used by the top-level component of kea-lfc.
Defines the class, WatchSocket.