14 #include <boost/make_shared.hpp>
18 namespace ph = std::placeholders;
25 constexpr
size_t MAX_LOGGED_MESSAGE_SIZE = 1024;
34 : request_(request ? request : response_creator->createNewHttpRequest()),
43 return (boost::make_shared<Transaction>(response_creator));
50 return (boost::make_shared<Transaction>(response_creator,
51 transaction->getRequest()));
53 return (create(response_creator));
58 SocketCallback::operator()(boost::system::error_code ec,
size_t length) {
59 if (ec.value() == boost::asio::error::operation_aborted) {
71 const long request_timeout,
72 const long idle_timeout)
180 }
catch (
const std::exception& ex) {
182 "connections: " << ex.
what());
203 }
catch (
const std::exception& ex) {
229 tcp_socket_->asyncReceive(static_cast<void*>(transaction->getInputBufData()),
230 transaction->getInputBufSize(),
235 tls_socket_->asyncReceive(static_cast<void*>(transaction->getInputBufData()),
236 transaction->getInputBufSize(),
248 if (transaction->outputDataAvail()) {
258 tcp_socket_->asyncSend(transaction->getOutputBufData(),
259 transaction->getOutputBufSize(),
264 tls_socket_->asyncSend(transaction->getOutputBufData(),
265 transaction->getOutputBufSize(),
275 if (!transaction->getRequest()->isPersistent()) {
294 transaction->setOutputBuf(response->toString());
347 boost::system::error_code ec,
size_t length) {
351 if (ec.value() == boost::asio::error::operation_aborted) {
356 }
else if ((ec.value() != boost::asio::error::try_again) &&
357 (ec.value() != boost::asio::error::would_block)) {
378 transaction->getParser()->postBuffer(static_cast<void*>(transaction->getInputBufData()),
380 transaction->getParser()->poll();
383 if (transaction->getParser()->needData()) {
391 transaction->getRequest()->finalize();
400 .arg(transaction->getParser()->getBufferAsString(MAX_LOGGED_MESSAGE_SIZE));
402 }
catch (
const std::exception& ex) {
411 .arg(transaction->getParser()->getBufferAsString(MAX_LOGGED_MESSAGE_SIZE));
422 .arg(response->toBriefString())
429 MAX_LOGGED_MESSAGE_SIZE));
441 boost::system::error_code ec,
size_t length) {
445 if (ec.value() == boost::asio::error::operation_aborted) {
450 }
else if ((ec.value() != boost::asio::error::try_again) &&
451 (ec.value() != boost::asio::error::would_block)) {
468 if (length > transaction->getOutputBufSize()) {
469 length = transaction->getOutputBufSize();
472 if (length <= transaction->getOutputBufSize()) {
479 transaction->consumeOutputBuf(length);
516 auto request = spawned_transaction->getRequest();
523 if (request->context()->http_version_major_ == 0) {
555 return (
tcp_socket_->getASIOSocket().remote_endpoint().address().to_string());
559 return (
tls_socket_->getASIOSocket().remote_endpoint().address().to_string());
564 return (
"(unknown address)");
void asyncSendResponse(const ConstHttpResponsePtr &response, TransactionPtr transaction)
Sends HTTP response asynchronously.
boost::shared_ptr< const HttpResponse > ConstHttpResponsePtr
Pointer to the const HttpResponse object.
const isc::log::MessageID HTTP_CONNECTION_STOP
std::unique_ptr< asiolink::TCPSocket< SocketCallback > > tcp_socket_
TCP socket used by this connection.
void handshakeCallback(const boost::system::error_code &ec)
Local callback invoked when TLS handshake is performed.
const int DBGLVL_TRACE_BASIC
Trace basic operations.
static std::string logFormatHttpMessage(const std::string &message, const size_t limit=0)
Formats provided HTTP message for logging.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
void doRead(TransactionPtr transaction=TransactionPtr())
Starts asynchronous read from the socket.
std::function< void()> callback_
The callback function.
HttpAcceptorCallback acceptor_callback_
External TCP acceptor callback.
boost::shared_ptr< Transaction > TransactionPtr
Shared pointer to the Transaction.
void doWrite(TransactionPtr transaction)
Starts asynchronous write to the socket.
boost::shared_ptr< HttpAcceptor > HttpAcceptorPtr
Type of shared pointer to TCP acceptors.
const isc::log::MessageID HTTP_CLIENT_REQUEST_TIMEOUT_OCCURRED
HttpAcceptorPtr acceptor_
Pointer to the TCP acceptor used to accept new connections.
void cancel()
Cancel the timer.
void shutdownCallback(const boost::system::error_code &ec)
Callback invoked when TLS shutdown is performed.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
static TransactionPtr spawn(const HttpResponseCreatorPtr &response_creator, const TransactionPtr &transaction)
Creates new transaction from the current transaction.
void doHandshake()
Asynchronously performs TLS handshake.
const isc::log::MessageID HTTP_CONNECTION_SHUTDOWN_FAILED
A generic parser for HTTP requests.
const isc::log::MessageID HTTP_CONNECTION_HANDSHAKE_START
void shutdown()
Shutdown the socket.
const isc::log::MessageID HTTP_CLIENT_REQUEST_RECEIVED_DETAILS
std::function< void(const boost::system::error_code &)> HttpAcceptorCallback
Type of the callback for the TCP acceptor used in this library.
The IOService class is a wrapper for the ASIO io_service class.
virtual ~HttpConnection()
Destructor.
boost::shared_ptr< TlsContext > TlsContextPtr
The type of shared pointers to TlsContext objects.
asiolink::TlsContextPtr tls_context_
TLS context.
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.
Generic error reported within HttpConnection class.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
The TCPEndpoint class is a concrete derived class of IOEndpoint that represents an endpoint of a TCP ...
const isc::log::MessageID HTTP_BAD_CLIENT_REQUEST_RECEIVED_DETAILS
void shutdownConnection()
Shuts down current connection.
void setupIdleTimer()
Reset timer for detecting idle timeout in persistent connections.
void socketReadCallback(TransactionPtr transaction, boost::system::error_code ec, size_t length)
Callback invoked when new data is received over the socket.
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
std::unique_ptr< asiolink::TLSSocket< SocketCallback > > tls_socket_
TLS socket used by this connection.
A generic exception that is thrown when an unexpected error condition occurs.
const int DBGLVL_TRACE_DETAIL_DATA
Trace data associated with detailed operations.
boost::shared_ptr< HttpsAcceptor > HttpsAcceptorPtr
Type of shared pointer to TLS acceptors.
HttpConnectionPool & connection_pool_
Connection pool holding this connection.
void idleTimeoutCallback()
void stop(const HttpConnectionPtr &connection)
Removes a connection from the pool and stops it.
HttpResponseCreatorPtr response_creator_
Pointer to the HttpResponseCreator object used to create HTTP responses.
const isc::log::MessageID HTTP_DATA_RECEIVED
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static TransactionPtr create(const HttpResponseCreatorPtr &response_creator)
Creates new transaction instance.
void setupRequestTimer(TransactionPtr transaction=TransactionPtr())
Reset timer for detecting request timeouts.
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.
boost::shared_ptr< HttpResponseCreator > HttpResponseCreatorPtr
Pointer to the HttpResponseCreator object.
Defines the logger used by the top-level component of kea-dhcp-ddns.
const isc::log::MessageID HTTPS_REQUEST_RECEIVE_START
const isc::log::MessageID HTTP_CLIENT_REQUEST_RECEIVED
asiolink::IntervalTimer request_timer_
Timer used to detect Request Timeout.
std::string getRemoteEndpointAddressAsText() const
returns remote address in textual form
void acceptorCallback(const boost::system::error_code &ec)
Local callback invoked when new connection is accepted.
void close()
Closes the socket.
const isc::log::MessageID HTTP_SERVER_RESPONSE_SEND_DETAILS
void asyncAccept()
Asynchronously accepts new connection.
static const HttpVersion & HTTP_10()
HTTP version 1.0.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
const isc::log::MessageID HTTP_CONNECTION_HANDSHAKE_FAILED
Represents HTTP request message.
const isc::log::MessageID HTTP_BAD_CLIENT_REQUEST_RECEIVED
const isc::log::MessageID HTTP_CONNECTION_SHUTDOWN
isc::log::Logger http_logger("http")
Defines the logger used within libkea-http library.
void shutdown(const HttpConnectionPtr &connection)
Removes a connection from the pool and shutdown it.
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
Provides a service for accepting new TLS connections.
const isc::log::MessageID HTTP_IDLE_CONNECTION_TIMEOUT_OCCURRED
const isc::log::MessageID HTTP_REQUEST_RECEIVE_START
const int DBGLVL_TRACE_DETAIL
Trace detailed operations.
const isc::log::MessageID HTTP_SERVER_RESPONSE_SEND
void requestTimeoutCallback(TransactionPtr transaction)
Callback invoked when the HTTP Request Timeout occurs.
const isc::log::MessageID HTTP_CONNECTION_STOP_FAILED
const int DBGLVL_TRACE_BASIC_DATA
Trace data associated with the basic operations.
Pool of active HTTP connections.
void setup(const Callback &cbfunc, const long interval, const Mode &mode=REPEATING)
Register timer callback function and interval.
long idle_timeout_
Timeout after which the persistent HTTP connection is shut down by the server.
long request_timeout_
Configured Request Timeout in milliseconds.