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;
269 boost::dynamic_pointer_cast<HttpsAcceptor>(
acceptor_);
281 }
catch (
const std::exception& ex) {
283 "connections: " << ex.
what());
306 }
catch (
const std::exception& ex) {
333 tcp_socket_->asyncReceive(
static_cast<void*
>(transaction->getInputBufData()),
334 transaction->getInputBufSize(),
339 tls_socket_->asyncReceive(
static_cast<void*
>(transaction->getInputBufData()),
340 transaction->getInputBufSize(),
352 if (transaction->outputDataAvail()) {
362 tcp_socket_->asyncSend(transaction->getOutputBufData(),
363 transaction->getOutputBufSize(),
371 tls_socket_->asyncSend(transaction->getOutputBufData(),
372 transaction->getOutputBufSize(),
385 if (!transaction->getRequest()->isPersistent()) {
404 transaction->setOutputBuf(response->toString());
437 iface_mgr.addExternalSocket(
tcp_socket_->getNative(), 0);
440 iface_mgr.addExternalSocket(
tls_socket_->getNative(), 0);
443 iface_mgr.addExternalSocket(
watch_socket_->getSelectFd(), 0);
472 boost::system::error_code ec,
size_t length) {
476 if (ec.value() == boost::asio::error::operation_aborted) {
481 }
else if ((ec.value() != boost::asio::error::try_again) &&
482 (ec.value() != boost::asio::error::would_block)) {
503 transaction->getParser()->postBuffer(
static_cast<void*
>(transaction->getInputBufData()),
505 transaction->getParser()->poll();
508 if (transaction->getParser()->needData()) {
516 transaction->getRequest()->finalize();
525 .arg(transaction->getParser()->getBufferAsString(MAX_LOGGED_MESSAGE_SIZE));
527 }
catch (
const std::exception& ex) {
536 .arg(transaction->getParser()->getBufferAsString(MAX_LOGGED_MESSAGE_SIZE));
547 .arg(response->toBriefString())
554 MAX_LOGGED_MESSAGE_SIZE));
566 boost::system::error_code ec,
size_t length) {
573 if (ec.value() == boost::asio::error::operation_aborted) {
578 }
else if ((ec.value() != boost::asio::error::try_again) &&
579 (ec.value() != boost::asio::error::would_block)) {
596 if (length > transaction->getOutputBufSize()) {
597 length = transaction->getOutputBufSize();
600 if (length <= transaction->getOutputBufSize()) {
607 transaction->consumeOutputBuf(length);
644 auto request = spawned_transaction->getRequest();
651 if (request->context()->http_version_major_ == 0) {
683 return (
tcp_socket_->getASIOSocket().remote_endpoint().address().to_string());
687 return (
tls_socket_->getASIOSocket().remote_endpoint().address().to_string());
692 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.
Pool of active HTTP connections.
void stop(const HttpConnectionPtr &connection)
Removes a connection from the pool and stops it.
void shutdown(const HttpConnectionPtr &connection)
Removes a connection from the pool and shutdown it.
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.
HttpConnectionPool & connection_pool_
Connection pool holding this connection.
void socketReadCallback(TransactionPtr transaction, boost::system::error_code ec, size_t length)
Callback invoked when new data is received over the socket.
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.
HttpConnection(const asiolink::IOServicePtr &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.
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.
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
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.