15#include <boost/make_shared.hpp>
21namespace ph = std::placeholders;
28constexpr size_t MAX_LOGGED_MESSAGE_SIZE = 1024;
37 : request_(request ? request : response_creator->createNewHttpRequest()),
46 return (boost::make_shared<Transaction>(response_creator));
53 return (boost::make_shared<Transaction>(response_creator,
54 transaction->getRequest()));
56 return (create(response_creator));
61SocketCallback::operator()(boost::system::error_code ec,
size_t length) {
62 if (ec.value() == boost::asio::error::operation_aborted) {
65 callback_(ec, length);
74 const long request_timeout,
75 const long idle_timeout)
115 request->setTls(
true);
120 request->setSubject(
tls_socket_->getTlsStream().getSubject());
126 request->setIssuer(
tls_socket_->getTlsStream().getIssuer());
171 }
catch (
const std::exception& ex) {
185 }
catch (
const std::exception& ex) {
199 std::string watch_error;
240 if (connection_pool) {
241 connection_pool->shutdown(shared_from_this());
257 if (connection_pool) {
258 connection_pool->stop(shared_from_this());
277 boost::dynamic_pointer_cast<HttpsAcceptor>(
acceptor_);
289 }
catch (
const std::exception& ex) {
291 "connections: " << ex.what());
314 }
catch (
const std::exception& ex) {
341 tcp_socket_->asyncReceive(
static_cast<void*
>(transaction->getInputBufData()),
342 transaction->getInputBufSize(),
347 tls_socket_->asyncReceive(
static_cast<void*
>(transaction->getInputBufData()),
348 transaction->getInputBufSize(),
360 if (transaction->outputDataAvail()) {
370 tcp_socket_->asyncSend(transaction->getOutputBufData(),
371 transaction->getOutputBufSize(),
379 tls_socket_->asyncSend(transaction->getOutputBufData(),
380 transaction->getOutputBufSize(),
393 if (!transaction->getRequest()->isPersistent()) {
412 transaction->setOutputBuf(response->toString());
444 iface_mgr.addExternalSocket(
tcp_socket_->getNative(), 0);
447 iface_mgr.addExternalSocket(
tls_socket_->getNative(), 0);
450 iface_mgr.addExternalSocket(
watch_socket_->getSelectFd(), 0);
479 boost::system::error_code ec,
size_t length) {
483 if (ec.value() == boost::asio::error::operation_aborted) {
488 }
else if ((ec.value() != boost::asio::error::try_again) &&
489 (ec.value() != boost::asio::error::would_block)) {
510 transaction->getParser()->postBuffer(
static_cast<void*
>(transaction->getInputBufData()),
512 transaction->getParser()->poll();
515 if (transaction->getParser()->needData()) {
523 transaction->getRequest()->finalize();
532 .arg(transaction->getParser()->getBufferAsString(MAX_LOGGED_MESSAGE_SIZE));
534 }
catch (
const std::exception& ex) {
543 .arg(transaction->getParser()->getBufferAsString(MAX_LOGGED_MESSAGE_SIZE));
558 .arg(response->toBriefString())
565 MAX_LOGGED_MESSAGE_SIZE));
577 boost::system::error_code ec,
size_t length) {
584 if (ec.value() == boost::asio::error::operation_aborted) {
589 }
else if ((ec.value() != boost::asio::error::try_again) &&
590 (ec.value() != boost::asio::error::would_block)) {
607 if (length > transaction->getOutputBufSize()) {
608 length = transaction->getOutputBufSize();
611 if (length <= transaction->getOutputBufSize()) {
618 transaction->consumeOutputBuf(length);
655 auto request = spawned_transaction->getRequest();
662 if (request->context()->http_version_major_ == 0) {
694 return (
tcp_socket_->getASIOSocket().remote_endpoint().address().to_string());
698 return (
tls_socket_->getASIOSocket().remote_endpoint().address().to_string());
703 return (
"(unknown address)");
A generic exception that is thrown when an unexpected error condition occurs.
void cancel()
Cancel the timer.
void setup(const Callback &cbfunc, const long interval, const Mode &mode=REPEATING)
Register timer callback function and interval.
The TCPEndpoint class is a concrete derived class of IOEndpoint that represents an endpoint of a TCP ...
The TCPSocket class is a concrete derived class of IOAsioSocket that represents a TCP socket.
The TLSSocket class is a concrete derived class of IOAsioSocket that represents a TLS socket.
void deleteExternalSocket(int socketfd)
Deletes external socket.
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Generic error reported within HttpConnection class.
static TransactionPtr create(const HttpResponseCreatorPtr &response_creator)
Creates new transaction instance.
Transaction(const HttpResponseCreatorPtr &response_creator, const HttpRequestPtr &request=HttpRequestPtr())
Constructor.
static TransactionPtr spawn(const HttpResponseCreatorPtr &response_creator, const TransactionPtr &transaction)
Creates new transaction from the current 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.
asiolink::IOServicePtr io_service_
The IO service used to handle events.
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.
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.
boost::shared_ptr< Transaction > TransactionPtr
Shared pointer to the Transaction.
void doWrite(TransactionPtr transaction)
Starts asynchronous write to the socket.
bool defer_shutdown_
Flag which indicates if the connection shutdown should be deferred until the connection is no longer ...
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.
static std::string logFormatHttpMessage(const std::string &message, const size_t limit=0)
Formats provided HTTP message for logging.
A generic parser for HTTP requests.
Represents HTTP request message.
static bool recordIssuer_
Record issuer name.
static bool recordSubject_
Access control parameters: Flags which indicate what information to record.
Provides an IO "ready" semaphore for use with select() or poll() WatchSocket exposes a single open fi...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
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.
const isc::log::MessageID HTTP_CONNECTION_SHUTDOWN
const isc::log::MessageID HTTP_CONNECTION_WATCH_SOCKET_CLOSE_ERROR
const isc::log::MessageID HTTP_CONNECTION_HANDSHAKE_START
const isc::log::MessageID HTTP_CONNECTION_SHUTDOWN_FAILED
boost::shared_ptr< const HttpResponse > ConstHttpResponsePtr
Pointer to the const HttpResponse object.
const isc::log::MessageID HTTP_IDLE_CONNECTION_TIMEOUT_OCCURRED
const isc::log::MessageID HTTP_DATA_RECEIVED
const isc::log::MessageID HTTP_BAD_CLIENT_REQUEST_RECEIVED_DETAILS
const isc::log::MessageID HTTP_SERVER_RESPONSE_SEND_DETAILS
const isc::log::MessageID HTTP_CONNECTION_STOP_FAILED
isc::log::Logger http_logger("http")
Defines the logger used within libkea-http library.
const isc::log::MessageID HTTP_CLIENT_REQUEST_RECEIVED
const isc::log::MessageID HTTP_CLIENT_REQUEST_TIMEOUT_OCCURRED
const isc::log::MessageID HTTPS_REQUEST_RECEIVE_START
const isc::log::MessageID HTTP_CONNECTION_STOP
std::shared_ptr< HttpConnectionPool > HttpConnectionPoolPtr
Pointer to the HttpConnectionPool.
const isc::log::MessageID HTTP_CONNECTION_HANDSHAKE_FAILED
boost::shared_ptr< HttpResponseCreator > HttpResponseCreatorPtr
Pointer to the HttpResponseCreator object.
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
const isc::log::MessageID HTTP_REQUEST_RECEIVE_START
const isc::log::MessageID HTTP_CLIENT_REQUEST_RECEIVED_DETAILS
const isc::log::MessageID HTTP_CONNECTION_WATCH_SOCKET_CLEAR_ERROR
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< HttpsAcceptor > HttpsAcceptorPtr
Type of shared pointer to TLS acceptors.
const isc::log::MessageID HTTP_CONNECTION_WATCH_SOCKET_MARK_READY_ERROR
const isc::log::MessageID HTTP_BAD_CLIENT_REQUEST_RECEIVED
const isc::log::MessageID HTTP_SERVER_RESPONSE_SEND
const int DBGLVL_TRACE_BASIC
Trace basic operations.
const int DBGLVL_TRACE_DETAIL_DATA
Trace data associated with detailed operations.
const int DBGLVL_TRACE_BASIC_DATA
Trace data associated with the basic operations.
const int DBGLVL_TRACE_DETAIL
Trace detailed operations.
Defines the logger used by the top-level component of kea-lfc.
static const HttpVersion & HTTP_10()
HTTP version 1.0.