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

Protected Types

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

Protected Member Functions

void acceptorCallback (const boost::system::error_code &ec)
 Local callback invoked when new connection is accepted.
 
void asyncSendResponse (const ConstHttpResponsePtr &response, TransactionPtr transaction)
 Sends HTTP response asynchronously.
 
void clearWatchSocket ()
 Clear the watch socket's ready marker.
 
void closeWatchSocket ()
 Close the watch socket.
 
void doWrite (TransactionPtr transaction)
 Starts asynchronous write to the socket.
 
std::string getRemoteEndpointAddressAsText () const
 Returns remote address in textual form.
 
void handshakeCallback (const boost::system::error_code &ec)
 Local callback invoked when TLS handshake is performed.
 
void idleTimeoutCallback ()
 
void markWatchSocketReady ()
 Mark the watch socket as ready.
 
void requestTimeoutCallback (TransactionPtr transaction)
 Callback invoked when the HTTP Request Timeout occurs.
 
void setupIdleTimer ()
 Reset timer for detecting idle timeout in persistent connections.
 
void setupRequestTimer (TransactionPtr transaction=TransactionPtr())
 Reset timer for detecting request timeouts.
 
void shutdownCallback (const boost::system::error_code &ec)
 Callback invoked when TLS shutdown is performed.
 
void shutdownConnection ()
 Shuts down current connection.
 
void socketReadCallback (TransactionPtr transaction, boost::system::error_code ec, size_t length)
 Callback invoked when new data is received over the socket.
 
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.
 

Protected Attributes

HttpAcceptorPtr acceptor_
 Pointer to the TCP acceptor used to accept new connections.
 
HttpAcceptorCallback acceptor_callback_
 External TCP acceptor callback.
 
std::weak_ptr< HttpConnectionPoolconnection_pool_
 Connection pool holding this connection.
 
long idle_timeout_
 Timeout after which the persistent HTTP connection is shut down by the server.
 
long request_timeout_
 Configured Request Timeout in milliseconds.
 
asiolink::IntervalTimer request_timer_
 Timer used to detect Request Timeout.
 
HttpResponseCreatorPtr response_creator_
 Pointer to the HttpResponseCreator object used to create HTTP responses.
 
std::unique_ptr< asiolink::TCPSocket< SocketCallback > > tcp_socket_
 TCP socket used by this connection.
 
asiolink::TlsContextPtr tls_context_
 TLS context.
 
std::unique_ptr< asiolink::TLSSocket< SocketCallback > > tls_socket_
 TLS socket used by this connection.
 
bool use_external_
 Use external sockets flag.
 
util::WatchSocketPtr watch_socket_
 Pointer to watch socket instance used to signal that the socket is ready for read or write when use external sockets is true.
 

Detailed Description

Accepts and handles a single HTTP connection.

Definition at line 44 of file connection.h.

Member Typedef Documentation

◆ TransactionPtr

Shared pointer to the Transaction.

Definition at line 87 of file connection.h.

Constructor & Destructor Documentation

◆ HttpConnection()

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

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 68 of file connection.cc.

References tcp_socket_, and tls_socket_.

◆ ~HttpConnection()

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

Destructor.

Closes current connection.

Definition at line 96 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 420 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, isc::dhcp::IfaceMgr::instance(), LOG_DEBUG, request_timeout_, setupRequestTimer(), stopThisConnection(), tcp_socket_, tls_context_, tls_socket_, use_external_, and watch_socket_.

Referenced by asyncAccept().

+ Here is the call graph for this function:

◆ addExternalSockets()

void isc::http::HttpConnection::addExternalSockets ( bool use_external = false)

Use external sockets flag.

Add sockets as external sockets of the interface manager so available I/O on them makes a waiting select to return.

Parameters
use_externalTrue add external sockets (default false).

Definition at line 101 of file connection.cc.

References use_external_.

◆ 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 270 of file connection.cc.

References acceptor_, acceptorCallback(), isc_throw, tcp_socket_, and tls_socket_.

+ 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 412 of file connection.cc.

References doWrite().

Referenced by requestTimeoutCallback(), and socketReadCallback().

+ Here is the call graph for this function:

◆ clearWatchSocket()

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

Clear the watch socket's ready marker.

Clear the watch socket so as the future send operation can mark it again to interrupt the synchronous select() call.

Should not happen...

Definition at line 184 of file connection.cc.

References isc::http::HTTP_CONNECTION_WATCH_SOCKET_CLEAR_ERROR, isc::http::http_logger, LOG_ERROR, and watch_socket_.

Referenced by handshakeCallback(), and socketWriteCallback().

◆ close()

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

Closes the socket.

Definition at line 213 of file connection.cc.

References isc::asiolink::IntervalTimer::cancel(), closeWatchSocket(), isc::dhcp::IfaceMgr::deleteExternalSocket(), isc::dhcp::IfaceMgr::instance(), isc_throw, request_timer_, tcp_socket_, tls_socket_, and use_external_.

Referenced by ~HttpConnection(), and stopThisConnection().

+ Here is the call graph for this function:

◆ closeWatchSocket()

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

Close the watch socket.

Should not happen...

Definition at line 198 of file connection.cc.

References isc::dhcp::IfaceMgr::deleteExternalSocket(), isc::http::HTTP_CONNECTION_WATCH_SOCKET_CLOSE_ERROR, isc::http::http_logger, isc::dhcp::IfaceMgr::instance(), LOG_ERROR, and watch_socket_.

Referenced by close(), shutdown(), and shutdownCallback().

+ 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 298 of file connection.cc.

References doRead(), handshakeCallback(), isc_throw, markWatchSocketReady(), tls_socket_, and use_external_.

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 323 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 360 of file connection.cc.

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

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 462 of file connection.cc.

References clearWatchSocket(), 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, stopThisConnection(), and use_external_.

Referenced by doHandshake().

+ Here is the call graph for this function:

◆ idleTimeoutCallback()

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

Definition at line 678 of file connection.cc.

References isc::log::DBGLVL_TRACE_DETAIL, getRemoteEndpointAddressAsText(), isc::http::HTTP_IDLE_CONNECTION_TIMEOUT_OCCURRED, isc::http::http_logger, LOG_DEBUG, and stopThisConnection().

Referenced by setupIdleTimer().

+ Here is the call graph for this function:

◆ markWatchSocketReady()

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

Mark the watch socket as ready.

Asynchronous handshake or send has been scheduled and we need to indicate this to break the synchronous select(). The handler should clear this status when invoked.

Should not happen...

Definition at line 170 of file connection.cc.

References isc::http::HTTP_CONNECTION_WATCH_SOCKET_MARK_READY_ERROR, isc::http::http_logger, LOG_ERROR, and watch_socket_.

Referenced by doHandshake(), and doWrite().

◆ 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 106 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 642 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 635 of file connection.cc.

References idle_timeout_, idleTimeoutCallback(), isc::asiolink::IntervalTimer::ONE_SHOT, 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 624 of file connection.cc.

References isc::asiolink::IntervalTimer::ONE_SHOT, 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 147 of file connection.cc.

References isc::asiolink::IntervalTimer::cancel(), closeWatchSocket(), isc::dhcp::IfaceMgr::deleteExternalSocket(), isc::dhcp::IfaceMgr::instance(), isc_throw, request_timer_, shutdownCallback(), tcp_socket_, tls_socket_, and use_external_.

Referenced by shutdownConnection().

+ 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 137 of file connection.cc.

References closeWatchSocket(), isc::dhcp::IfaceMgr::deleteExternalSocket(), isc::dhcp::IfaceMgr::instance(), tls_socket_, and use_external_.

Referenced by shutdown().

+ Here is the call graph for this function:

◆ shutdownConnection()

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

Shuts down current connection.

Copied from the next method stopThisConnection

Definition at line 236 of file connection.cc.

References connection_pool_, isc::log::DBGLVL_TRACE_BASIC, getRemoteEndpointAddressAsText(), isc::http::HTTP_CONNECTION_SHUTDOWN, isc::http::HTTP_CONNECTION_SHUTDOWN_FAILED, isc::http::http_logger, LOG_DEBUG, LOG_ERROR, and shutdown().

+ Here is the call graph for this function:

◆ 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 481 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 575 of file connection.cc.

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

Referenced by doWrite().

+ Here is the call graph for this function:

◆ stopThisConnection()

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

Stops current connection.

Definition at line 253 of file connection.cc.

References close(), connection_pool_, isc::log::DBGLVL_TRACE_BASIC, getRemoteEndpointAddressAsText(), isc::http::HTTP_CONNECTION_STOP, isc::http::HTTP_CONNECTION_STOP_FAILED, isc::http::http_logger, LOG_DEBUG, and LOG_ERROR.

Referenced by acceptorCallback(), doRead(), doWrite(), handshakeCallback(), idleTimeoutCallback(), socketReadCallback(), and socketWriteCallback().

+ Here is the call graph for this function:

Member Data Documentation

◆ acceptor_

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

Pointer to the TCP acceptor used to accept new connections.

Definition at line 441 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 451 of file connection.h.

Referenced by acceptorCallback().

◆ connection_pool_

std::weak_ptr<HttpConnectionPool> isc::http::HttpConnection::connection_pool_
protected

Connection pool holding this connection.

Definition at line 444 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 432 of file connection.h.

Referenced by setupIdleTimer().

◆ request_timeout_

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

Configured Request Timeout in milliseconds.

Definition at line 425 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 422 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 448 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 435 of file connection.h.

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

◆ tls_context_

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

TLS context.

Definition at line 428 of file connection.h.

Referenced by acceptorCallback().

◆ tls_socket_

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

◆ use_external_

bool isc::http::HttpConnection::use_external_
protected

◆ watch_socket_

util::WatchSocketPtr isc::http::HttpConnection::watch_socket_
protected

Pointer to watch socket instance used to signal that the socket is ready for read or write when use external sockets is true.

Definition at line 458 of file connection.h.

Referenced by acceptorCallback(), clearWatchSocket(), closeWatchSocket(), and markWatchSocketReady().


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