Kea 2.7.5
|
Accepts and handles a single HTTP connection. More...
#include <connection.h>
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< Transaction > | TransactionPtr |
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< HttpConnectionPool > | connection_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. | |
Accepts and handles a single HTTP connection.
Definition at line 44 of file connection.h.
|
protected |
Shared pointer to the Transaction
.
Definition at line 87 of file connection.h.
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.
io_service | IO service to be used by the connection. |
acceptor | Pointer to the TCP acceptor object used to listen for new HTTP connections. |
tls_context | TLS context. |
connection_pool | Connection pool in which this connection is stored. |
response_creator | Pointer to the response creator object used to create HTTP response from the HTTP request received. |
callback | Callback invoked when new connection is accepted. |
request_timeout | Configured timeout for a HTTP request. |
idle_timeout | Timeout after which persistent HTTP connection is closed by the server. |
Definition at line 68 of file connection.cc.
References tcp_socket_, and tls_socket_.
|
virtual |
Destructor.
Closes current connection.
Definition at line 96 of file connection.cc.
References close().
|
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.
ec | Error 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().
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.
use_external | True add external sockets (default false). |
Definition at line 101 of file connection.cc.
References use_external_.
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_, tls_socket_, and isc::Exception::what().
|
protected |
Sends HTTP response asynchronously.
Internally it calls HttpConnection::doWrite to send the data.
response | Pointer to the HTTP response to be sent. |
transaction | Pointer to the transaction. |
Definition at line 412 of file connection.cc.
References doWrite().
Referenced by requestTimeoutCallback(), and socketReadCallback().
|
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().
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::instance(), isc_throw, request_timer_, tcp_socket_, tls_socket_, and use_external_.
Referenced by ~HttpConnection(), and stopThisConnection().
|
protected |
Close the watch socket.
Should not happen...
Definition at line 198 of file connection.cc.
References 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().
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_, use_external_, and isc::Exception::what().
Referenced by acceptorCallback().
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.
transaction | Pointer 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().
|
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.
transaction | Pointer 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().
|
protected |
Returns remote address in textual form.
Definition at line 689 of file connection.cc.
References tcp_socket_, and tls_socket_.
Referenced by acceptorCallback(), handshakeCallback(), idleTimeoutCallback(), recordParameters(), requestTimeoutCallback(), shutdownConnection(), socketReadCallback(), and stopThisConnection().
|
protected |
Local callback invoked when TLS handshake is performed.
If the handshake is performed successfully the HttpConnection::doRead is called.
ec | Error 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().
|
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().
|
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().
void isc::http::HttpConnection::recordParameters | ( | const HttpRequestPtr & | request | ) | const |
Records connection parameters into the HTTP request.
request | Pointer 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().
|
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.
transaction | Pointer 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().
|
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().
|
protected |
Reset timer for detecting request timeouts.
transaction | Pointer 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().
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::instance(), isc_throw, request_timer_, shutdownCallback(), tcp_socket_, tls_socket_, and use_external_.
Referenced by shutdownConnection().
|
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.
ec | Error code (ignored). |
Definition at line 137 of file connection.cc.
References closeWatchSocket(), isc::dhcp::IfaceMgr::instance(), tls_socket_, and use_external_.
Referenced by shutdown().
|
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().
|
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.
transaction | Pointer to the transaction for which the callback is invoked. |
ec | Error code. |
length | Length 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().
|
protectedvirtual |
Callback invoked when data is sent over the socket.
transaction | Pointer to the transaction for which the callback is invoked. |
ec | Error code. |
length | Length of the data sent. |
Definition at line 575 of file connection.cc.
References clearWatchSocket(), doWrite(), setupRequestTimer(), stopThisConnection(), and use_external_.
Referenced by doWrite().
|
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().
|
protected |
Pointer to the TCP acceptor used to accept new connections.
Definition at line 441 of file connection.h.
Referenced by acceptorCallback(), and asyncAccept().
|
protected |
External TCP acceptor callback.
Definition at line 451 of file connection.h.
Referenced by acceptorCallback().
|
protected |
Connection pool holding this connection.
Definition at line 444 of file connection.h.
Referenced by shutdownConnection(), and stopThisConnection().
|
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().
|
protected |
Configured Request Timeout in milliseconds.
Definition at line 425 of file connection.h.
Referenced by acceptorCallback(), and setupRequestTimer().
|
protected |
Timer used to detect Request Timeout.
Definition at line 422 of file connection.h.
Referenced by close(), setupIdleTimer(), setupRequestTimer(), shutdown(), and socketReadCallback().
|
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().
|
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().
|
protected |
|
protected |
TLS socket used by this connection.
Definition at line 438 of file connection.h.
Referenced by HttpConnection(), acceptorCallback(), asyncAccept(), close(), doHandshake(), doRead(), doWrite(), getRemoteEndpointAddressAsText(), recordParameters(), shutdown(), and shutdownCallback().
|
protected |
Use external sockets flag.
Definition at line 454 of file connection.h.
Referenced by acceptorCallback(), addExternalSockets(), close(), doHandshake(), doWrite(), handshakeCallback(), shutdown(), shutdownCallback(), and socketWriteCallback().
|
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().