Kea
1.9.9-git
|
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 (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 | shutdown () |
Shutdown the socket. More... | |
Protected Types | |
typedef boost::shared_ptr< Transaction > | TransactionPtr |
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... | |
HttpConnectionPool & | connection_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... | |
Accepts and handles a single HTTP connection.
Definition at line 43 of file connection.h.
|
protected |
Shared pointer to the Transaction
.
Definition at line 83 of file connection.h.
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.
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 65 of file connection.cc.
References tcp_socket_, and tls_socket_.
|
virtual |
Destructor.
Closes current connection.
Definition at line 91 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 300 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().
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 159 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 292 of file connection.cc.
References doWrite().
Referenced by requestTimeoutCallback(), and socketReadCallback().
void isc::http::HttpConnection::close | ( | ) |
Closes the socket.
Definition at line 120 of file connection.cc.
References isc::asiolink::IntervalTimer::cancel(), isc_throw, request_timer_, tcp_socket_, and tls_socket_.
Referenced by ~HttpConnection().
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 187 of file connection.cc.
References doRead(), handshakeCallback(), isc_throw, tls_socket_, 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 210 of file connection.cc.
References isc::http::HttpConnection::Transaction::create(), 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 246 of file connection.cc.
References doRead(), setupIdleTimer(), socketWriteCallback(), stopThisConnection(), tcp_socket_, and tls_socket_.
Referenced by asyncSendResponse(), and socketWriteCallback().
|
protected |
returns remote address in textual form
Definition at line 551 of file connection.cc.
References tcp_socket_, and tls_socket_.
Referenced by acceptorCallback(), handshakeCallback(), idleTimeoutCallback(), 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 330 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().
|
protected |
Definition at line 540 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 |
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 504 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 497 of file connection.cc.
References idle_timeout_, idleTimeoutCallback(), 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 486 of file connection.cc.
References 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 101 of file connection.cc.
References isc::asiolink::IntervalTimer::cancel(), isc_throw, request_timer_, shutdownCallback(), tcp_socket_, and tls_socket_.
|
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 96 of file connection.cc.
References tls_socket_.
Referenced by shutdown().
|
protected |
Shuts down current connection.
Copied from the next method stopThisConnection
Definition at line 135 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 isc::http::HttpConnectionPool::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 346 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 440 of file connection.cc.
References doWrite(), setupRequestTimer(), and stopThisConnection().
Referenced by doWrite().
|
protected |
Stops current connection.
Definition at line 147 of file connection.cc.
References 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, LOG_ERROR, and isc::http::HttpConnectionPool::stop().
Referenced by acceptorCallback(), doRead(), doWrite(), handshakeCallback(), idleTimeoutCallback(), socketReadCallback(), and socketWriteCallback().
|
protected |
Pointer to the TCP acceptor used to accept new connections.
Definition at line 412 of file connection.h.
Referenced by acceptorCallback(), and asyncAccept().
|
protected |
External TCP acceptor callback.
Definition at line 422 of file connection.h.
Referenced by acceptorCallback().
|
protected |
Connection pool holding this connection.
Definition at line 415 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 403 of file connection.h.
Referenced by setupIdleTimer().
|
protected |
Configured Request Timeout in milliseconds.
Definition at line 396 of file connection.h.
Referenced by acceptorCallback(), and setupRequestTimer().
|
protected |
Timer used to detect Request Timeout.
Definition at line 393 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 419 of file connection.h.
Referenced by doRead(), requestTimeoutCallback(), and socketReadCallback().
|
protected |
TCP socket used by this connection.
Definition at line 406 of file connection.h.
Referenced by asyncAccept(), close(), doRead(), doWrite(), getRemoteEndpointAddressAsText(), HttpConnection(), and shutdown().
|
protected |
|
protected |
TLS socket used by this connection.
Definition at line 409 of file connection.h.
Referenced by asyncAccept(), close(), doHandshake(), doRead(), doWrite(), getRemoteEndpointAddressAsText(), HttpConnection(), shutdown(), and shutdownCallback().