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)
121 request->setTls(
true);
126 request->setSubject(
tls_socket_->getTlsStream().getSubject());
132 request->setIssuer(
tls_socket_->getTlsStream().getIssuer());
177 }
catch (
const std::exception& ex) {
191 }
catch (
const std::exception& ex) {
205 std::string watch_error;
242 if (connection_pool) {
243 connection_pool->shutdown(shared_from_this());
259 if (connection_pool) {
260 connection_pool->stop(shared_from_this());
279 boost::dynamic_pointer_cast<HttpsAcceptor>(
acceptor_);
291 }
catch (
const std::exception& ex) {
293 "connections: " << ex.
what());
316 }
catch (
const std::exception& ex) {
343 tcp_socket_->asyncReceive(
static_cast<void*
>(transaction->getInputBufData()),
344 transaction->getInputBufSize(),
349 tls_socket_->asyncReceive(
static_cast<void*
>(transaction->getInputBufData()),
350 transaction->getInputBufSize(),
362 if (transaction->outputDataAvail()) {
372 tcp_socket_->asyncSend(transaction->getOutputBufData(),
373 transaction->getOutputBufSize(),
381 tls_socket_->asyncSend(transaction->getOutputBufData(),
382 transaction->getOutputBufSize(),
395 if (!transaction->getRequest()->isPersistent()) {
414 transaction->setOutputBuf(response->toString());
447 iface_mgr.addExternalSocket(
tcp_socket_->getNative(), 0);
450 iface_mgr.addExternalSocket(
tls_socket_->getNative(), 0);
453 iface_mgr.addExternalSocket(
watch_socket_->getSelectFd(), 0);
482 boost::system::error_code ec,
size_t length) {
486 if (ec.value() == boost::asio::error::operation_aborted) {
491 }
else if ((ec.value() != boost::asio::error::try_again) &&
492 (ec.value() != boost::asio::error::would_block)) {
513 transaction->getParser()->postBuffer(
static_cast<void*
>(transaction->getInputBufData()),
515 transaction->getParser()->poll();
518 if (transaction->getParser()->needData()) {
526 transaction->getRequest()->finalize();
535 .arg(transaction->getParser()->getBufferAsString(MAX_LOGGED_MESSAGE_SIZE));
537 }
catch (
const std::exception& ex) {
546 .arg(transaction->getParser()->getBufferAsString(MAX_LOGGED_MESSAGE_SIZE));
557 .arg(response->toBriefString())
564 MAX_LOGGED_MESSAGE_SIZE));
576 boost::system::error_code ec,
size_t length) {
583 if (ec.value() == boost::asio::error::operation_aborted) {
588 }
else if ((ec.value() != boost::asio::error::try_again) &&
589 (ec.value() != boost::asio::error::would_block)) {
606 if (length > transaction->getOutputBufSize()) {
607 length = transaction->getOutputBufSize();
610 if (length <= transaction->getOutputBufSize()) {
617 transaction->consumeOutputBuf(length);
654 auto request = spawned_transaction->getRequest();
661 if (request->context()->http_version_major_ == 0) {
693 return (
tcp_socket_->getASIOSocket().remote_endpoint().address().to_string());
697 return (
tls_socket_->getASIOSocket().remote_endpoint().address().to_string());
702 return (
"(unknown address)");
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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.
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.
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.
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.
boost::shared_ptr< Transaction > TransactionPtr
Shared pointer to the Transaction.
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.
void doWrite(TransactionPtr transaction)
Starts asynchronous write to the socket.
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
std::function< void(const boost::system::error_code &) HttpAcceptorCallback)
Type of the callback for the TCP acceptor used in this library.
const isc::log::MessageID HTTP_CLIENT_REQUEST_RECEIVED_DETAILS
const isc::log::MessageID HTTP_CONNECTION_WATCH_SOCKET_CLEAR_ERROR
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.