Kea  2.3.5-git
isc::http::HttpConnection Class Reference

Accepts and handles a single HTTP connection. More...

#include <connection.h>

+ Inheritance diagram for isc::http::HttpConnection:

Classes

class  Transaction
 Represents a single exchange of the HTTP messages. More...
 

Public Member Functions

 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. More...
 
virtual ~HttpConnection ()
 Destructor. More...
 
void asyncAccept ()
 Asynchronously accepts new connection. More...
 
void close ()
 Closes the socket. More...
 
void doHandshake ()
 Asynchronously performs TLS handshake. More...
 
void doRead (TransactionPtr transaction=TransactionPtr())
 Starts asynchronous read from the socket. More...
 
void recordParameters (const HttpRequestPtr &request) const
 Records connection parameters into the HTTP request. More...
 
void shutdown ()
 Shutdown the socket. More...
 

Protected Types

typedef boost::shared_ptr< TransactionTransactionPtr
 Shared pointer to the Transaction. More...
 

Protected Member Functions

void acceptorCallback (const boost::system::error_code &ec)
 Local callback invoked when new connection is accepted. More...
 
void asyncSendResponse (const ConstHttpResponsePtr &response, TransactionPtr transaction)
 Sends HTTP response asynchronously. More...
 
void doWrite (TransactionPtr transaction)
 Starts asynchronous write to the socket. More...
 
std::string getRemoteEndpointAddressAsText () const
 returns remote address in textual form More...
 
void handshakeCallback (const boost::system::error_code &ec)
 Local callback invoked when TLS handshake is performed. More...
 
void idleTimeoutCallback ()
 
void requestTimeoutCallback (TransactionPtr transaction)
 Callback invoked when the HTTP Request Timeout occurs. More...
 
void setupIdleTimer ()
 Reset timer for detecting idle timeout in persistent connections. More...
 
void setupRequestTimer (TransactionPtr transaction=TransactionPtr())
 Reset timer for detecting request timeouts. More...
 
void shutdownCallback (const boost::system::error_code &ec)
 Callback invoked when TLS shutdown is performed. More...
 
void shutdownConnection ()
 Shuts down current connection. More...
 
void socketReadCallback (TransactionPtr transaction, boost::system::error_code ec, size_t length)
 Callback invoked when new data is received over the socket. More...
 
virtual void socketWriteCallback (TransactionPtr transaction, boost::system::error_code ec, size_t length)
 Callback invoked when data is sent over the socket. More...
 
void stopThisConnection ()
 Stops current connection. More...
 

Protected Attributes

HttpAcceptorPtr acceptor_
 Pointer to the TCP acceptor used to accept new connections. More...
 
HttpAcceptorCallback acceptor_callback_
 External TCP acceptor callback. More...
 
HttpConnectionPoolconnection_pool_
 Connection pool holding this connection. More...
 
long idle_timeout_
 Timeout after which the persistent HTTP connection is shut down by the server. More...
 
long request_timeout_
 Configured Request Timeout in milliseconds. More...
 
asiolink::IntervalTimer request_timer_
 Timer used to detect Request Timeout. More...
 
HttpResponseCreatorPtr response_creator_
 Pointer to the HttpResponseCreator object used to create HTTP responses. More...
 
std::unique_ptr< asiolink::TCPSocket< SocketCallback > > tcp_socket_
 TCP socket used by this connection. More...
 
asiolink::TlsContextPtr tls_context_
 TLS context. More...
 
std::unique_ptr< asiolink::TLSSocket< SocketCallback > > tls_socket_
 TLS socket used by this connection. More...
 

Detailed Description

Accepts and handles a single HTTP connection.

Definition at line 43 of file connection.h.

Member Typedef Documentation

◆ TransactionPtr

typedef boost::shared_ptr<Transaction> isc::http::HttpConnection::TransactionPtr
protected

Shared pointer to the Transaction.

Definition at line 83 of file connection.h.

Constructor & Destructor Documentation

◆ HttpConnection()

isc::http::HttpConnection::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.

Parameters
io_serviceIO service to be used by the connection.
acceptorPointer to the TCP acceptor object used to listen for new HTTP connections.
tls_contextTLS context.
connection_poolConnection pool in which this connection is stored.
response_creatorPointer to the response creator object used to create HTTP response from the HTTP request received.
callbackCallback invoked when new connection is accepted.
request_timeoutConfigured timeout for a HTTP request.
idle_timeoutTimeout after which persistent HTTP connection is closed by the server.

Definition at line 65 of file connection.cc.

References tcp_socket_, and tls_socket_.

◆ ~HttpConnection()

isc::http::HttpConnection::~HttpConnection ( )
virtual

Destructor.

Closes current connection.

Definition at line 91 of file connection.cc.

References close().

+ Here is the call graph for this function:

Member Function Documentation

◆ acceptorCallback()

void isc::http::HttpConnection::acceptorCallback ( const boost::system::error_code &  ec)
protected

Local callback invoked when new connection is accepted.

It invokes external (supplied via constructor) acceptor callback. If the acceptor is not opened it returns immediately. If the connection is accepted successfully the HttpConnection::doRead or HttpConnection::doHandshake is called.

Parameters
ecError code.

Definition at line 332 of file connection.cc.

References acceptor_, acceptor_callback_, isc::log::DBGLVL_TRACE_DETAIL, doHandshake(), getRemoteEndpointAddressAsText(), isc::http::HTTP_CONNECTION_HANDSHAKE_START, isc::http::http_logger, isc::http::HTTP_REQUEST_RECEIVE_START, LOG_DEBUG, request_timeout_, setupRequestTimer(), stopThisConnection(), and tls_context_.

Referenced by asyncAccept().

+ Here is the call graph for this function:

◆ asyncAccept()

void isc::http::HttpConnection::asyncAccept ( )

Asynchronously accepts new connection.

When the connection is established successfully, the timeout timer is setup and the asynchronous handshake with client is performed.

Definition at line 190 of file connection.cc.

References acceptor_, acceptorCallback(), isc_throw, tcp_socket_, tls_socket_, and isc::Exception::what().

+ Here is the call graph for this function:

◆ asyncSendResponse()

void isc::http::HttpConnection::asyncSendResponse ( const ConstHttpResponsePtr response,
TransactionPtr  transaction 
)
protected

Sends HTTP response asynchronously.

Internally it calls HttpConnection::doWrite to send the data.

Parameters
responsePointer to the HTTP response to be sent.
transactionPointer to the transaction.

Definition at line 324 of file connection.cc.

References doWrite().

Referenced by requestTimeoutCallback(), and socketReadCallback().

+ Here is the call graph for this function:

◆ close()

void isc::http::HttpConnection::close ( )

Closes the socket.

Definition at line 151 of file connection.cc.

References isc::asiolink::IntervalTimer::cancel(), isc_throw, request_timer_, tcp_socket_, and tls_socket_.

Referenced by ~HttpConnection().

+ Here is the call graph for this function:

◆ doHandshake()

void isc::http::HttpConnection::doHandshake ( )

Asynchronously performs TLS handshake.

When the handshake is performed successfully or skipped because TLS was not enabled, the asynchronous read from the socket is started.

Definition at line 218 of file connection.cc.

References doRead(), handshakeCallback(), isc_throw, tls_socket_, and isc::Exception::what().

Referenced by acceptorCallback().

+ Here is the call graph for this function:

◆ doRead()

void isc::http::HttpConnection::doRead ( TransactionPtr  transaction = TransactionPtr())

Starts asynchronous read from the socket.

The data received over the socket are supplied to the HTTP parser until the parser signals that the entire request has been received or until the parser signals an error. In the former case the server creates an HTTP response using supplied response creator object.

In case of error the connection is stopped.

Parameters
transactionPointer to the transaction for which the read operation should be performed. It defaults to null pointer which indicates that this function should create new transaction.

Definition at line 241 of file connection.cc.

References isc::http::HttpConnection::Transaction::create(), recordParameters(), response_creator_, socketReadCallback(), stopThisConnection(), tcp_socket_, and tls_socket_.

Referenced by doHandshake(), doWrite(), handshakeCallback(), and socketReadCallback().

+ Here is the call graph for this function:

◆ doWrite()

void isc::http::HttpConnection::doWrite ( HttpConnection::TransactionPtr  transaction)
protected

Starts asynchronous write to the socket.

The output_buf_ must contain the data to be sent.

In case of error the connection is stopped.

Parameters
transactionPointer to the transaction for which the write operation should be performed.

Definition at line 278 of file connection.cc.

References doRead(), setupIdleTimer(), socketWriteCallback(), stopThisConnection(), tcp_socket_, and tls_socket_.

Referenced by asyncSendResponse(), and socketWriteCallback().

+ Here is the call graph for this function:

◆ getRemoteEndpointAddressAsText()

std::string isc::http::HttpConnection::getRemoteEndpointAddressAsText ( ) const
protected

◆ handshakeCallback()

void isc::http::HttpConnection::handshakeCallback ( const boost::system::error_code &  ec)
protected

Local callback invoked when TLS handshake is performed.

If the handshake is performed successfully the HttpConnection::doRead is called.

Parameters
ecError code.

Definition at line 362 of file connection.cc.

References isc::log::DBGLVL_TRACE_DETAIL, doRead(), getRemoteEndpointAddressAsText(), isc::http::HTTP_CONNECTION_HANDSHAKE_FAILED, isc::http::http_logger, isc::http::HTTPS_REQUEST_RECEIVE_START, LOG_DEBUG, LOG_INFO, and stopThisConnection().

Referenced by doHandshake().

+ Here is the call graph for this function:

◆ idleTimeoutCallback()

void isc::http::HttpConnection::idleTimeoutCallback ( )
protected

◆ recordParameters()

void isc::http::HttpConnection::recordParameters ( const HttpRequestPtr request) const

Records connection parameters into the HTTP request.

Parameters
requestPointer to the HTTP request.

Definition at line 96 of file connection.cc.

References getRemoteEndpointAddressAsText(), isc::http::HttpRequest::recordIssuer_, isc::http::HttpRequest::recordSubject_, and tls_socket_.

Referenced by doRead().

+ Here is the call graph for this function:

◆ requestTimeoutCallback()

void isc::http::HttpConnection::requestTimeoutCallback ( TransactionPtr  transaction)
protected

Callback invoked when the HTTP Request Timeout occurs.

This callback creates HTTP response with Request Timeout error code and sends it to the client.

Parameters
transactionPointer to the transaction for which timeout occurs.

Definition at line 536 of file connection.cc.

References asyncSendResponse(), isc::log::DBGLVL_TRACE_DETAIL, getRemoteEndpointAddressAsText(), isc::http::HttpVersion::HTTP_10(), isc::http::HTTP_CLIENT_REQUEST_TIMEOUT_OCCURRED, isc::http::http_logger, isc::http::HttpRequest::HTTP_POST, LOG_DEBUG, isc::http::REQUEST_TIMEOUT, response_creator_, and isc::http::HttpConnection::Transaction::spawn().

Referenced by setupRequestTimer().

+ Here is the call graph for this function:

◆ setupIdleTimer()

void isc::http::HttpConnection::setupIdleTimer ( )
protected

Reset timer for detecting idle timeout in persistent connections.

Definition at line 529 of file connection.cc.

References idle_timeout_, idleTimeoutCallback(), request_timer_, and isc::asiolink::IntervalTimer::setup().

Referenced by doWrite().

+ Here is the call graph for this function:

◆ setupRequestTimer()

void isc::http::HttpConnection::setupRequestTimer ( TransactionPtr  transaction = TransactionPtr())
protected

Reset timer for detecting request timeouts.

Parameters
transactionPointer to the transaction to be guarded by the timeout.

Definition at line 518 of file connection.cc.

References request_timeout_, request_timer_, requestTimeoutCallback(), and isc::asiolink::IntervalTimer::setup().

Referenced by acceptorCallback(), socketReadCallback(), and socketWriteCallback().

+ Here is the call graph for this function:

◆ shutdown()

void isc::http::HttpConnection::shutdown ( )

Shutdown the socket.

Definition at line 132 of file connection.cc.

References isc::asiolink::IntervalTimer::cancel(), isc_throw, request_timer_, shutdownCallback(), tcp_socket_, and tls_socket_.

+ Here is the call graph for this function:

◆ shutdownCallback()

void isc::http::HttpConnection::shutdownCallback ( const boost::system::error_code &  ec)
protected

Callback invoked when TLS shutdown is performed.

The TLS socket is unconditionally closed but the callback is called only when the peer has answered so the connection should be explicitly closed in all cases, i.e. do not rely on this handler.

Parameters
ecError code (ignored).

Definition at line 127 of file connection.cc.

References tls_socket_.

Referenced by shutdown().

◆ shutdownConnection()

void isc::http::HttpConnection::shutdownConnection ( )
protected

◆ socketReadCallback()

void isc::http::HttpConnection::socketReadCallback ( HttpConnection::TransactionPtr  transaction,
boost::system::error_code  ec,
size_t  length 
)
protected

Callback invoked when new data is received over the socket.

This callback supplies the data to the HTTP parser and continues parsing. When the parser signals end of the HTTP request the callback prepares a response and starts asynchronous send over the socket.

Parameters
transactionPointer to the transaction for which the callback is invoked.
ecError code.
lengthLength of the received data.

Definition at line 378 of file connection.cc.

References asyncSendResponse(), isc::asiolink::IntervalTimer::cancel(), isc::log::DBGLVL_TRACE_BASIC, isc::log::DBGLVL_TRACE_BASIC_DATA, isc::log::DBGLVL_TRACE_DETAIL_DATA, doRead(), getRemoteEndpointAddressAsText(), isc::http::HTTP_BAD_CLIENT_REQUEST_RECEIVED, isc::http::HTTP_BAD_CLIENT_REQUEST_RECEIVED_DETAILS, isc::http::HTTP_CLIENT_REQUEST_RECEIVED, isc::http::HTTP_CLIENT_REQUEST_RECEIVED_DETAILS, isc::http::HTTP_DATA_RECEIVED, isc::http::http_logger, isc::http::HTTP_SERVER_RESPONSE_SEND, isc::http::HTTP_SERVER_RESPONSE_SEND_DETAILS, LOG_DEBUG, isc::http::HttpMessageParserBase::logFormatHttpMessage(), request_timer_, response_creator_, setupRequestTimer(), and stopThisConnection().

Referenced by doRead().

+ Here is the call graph for this function:

◆ socketWriteCallback()

void isc::http::HttpConnection::socketWriteCallback ( HttpConnection::TransactionPtr  transaction,
boost::system::error_code  ec,
size_t  length 
)
protectedvirtual

Callback invoked when data is sent over the socket.

Parameters
transactionPointer to the transaction for which the callback is invoked.
ecError code.
lengthLength of the data sent.

Definition at line 472 of file connection.cc.

References doWrite(), setupRequestTimer(), and stopThisConnection().

Referenced by doWrite().

+ Here is the call graph for this function:

◆ stopThisConnection()

Member Data Documentation

◆ acceptor_

HttpAcceptorPtr isc::http::HttpConnection::acceptor_
protected

Pointer to the TCP acceptor used to accept new connections.

Definition at line 416 of file connection.h.

Referenced by acceptorCallback(), and asyncAccept().

◆ acceptor_callback_

HttpAcceptorCallback isc::http::HttpConnection::acceptor_callback_
protected

External TCP acceptor callback.

Definition at line 426 of file connection.h.

Referenced by acceptorCallback().

◆ connection_pool_

HttpConnectionPool& isc::http::HttpConnection::connection_pool_
protected

Connection pool holding this connection.

Definition at line 419 of file connection.h.

Referenced by shutdownConnection(), and stopThisConnection().

◆ idle_timeout_

long isc::http::HttpConnection::idle_timeout_
protected

Timeout after which the persistent HTTP connection is shut down by the server.

Definition at line 407 of file connection.h.

Referenced by setupIdleTimer().

◆ request_timeout_

long isc::http::HttpConnection::request_timeout_
protected

Configured Request Timeout in milliseconds.

Definition at line 400 of file connection.h.

Referenced by acceptorCallback(), and setupRequestTimer().

◆ request_timer_

asiolink::IntervalTimer isc::http::HttpConnection::request_timer_
protected

Timer used to detect Request Timeout.

Definition at line 397 of file connection.h.

Referenced by close(), setupIdleTimer(), setupRequestTimer(), shutdown(), and socketReadCallback().

◆ response_creator_

HttpResponseCreatorPtr isc::http::HttpConnection::response_creator_
protected

Pointer to the HttpResponseCreator object used to create HTTP responses.

Definition at line 423 of file connection.h.

Referenced by doRead(), requestTimeoutCallback(), and socketReadCallback().

◆ tcp_socket_

std::unique_ptr<asiolink::TCPSocket<SocketCallback> > isc::http::HttpConnection::tcp_socket_
protected

TCP socket used by this connection.

Definition at line 410 of file connection.h.

Referenced by asyncAccept(), close(), doRead(), doWrite(), getRemoteEndpointAddressAsText(), HttpConnection(), and shutdown().

◆ tls_context_

asiolink::TlsContextPtr isc::http::HttpConnection::tls_context_
protected

TLS context.

Definition at line 403 of file connection.h.

Referenced by acceptorCallback().

◆ tls_socket_

std::unique_ptr<asiolink::TLSSocket<SocketCallback> > isc::http::HttpConnection::tls_socket_
protected

The documentation for this class was generated from the following files: