diff options
Diffstat (limited to 'apps/http-proxy/src/http_session.cc')
-rw-r--r-- | apps/http-proxy/src/http_session.cc | 117 |
1 files changed, 51 insertions, 66 deletions
diff --git a/apps/http-proxy/src/http_session.cc b/apps/http-proxy/src/http_session.cc index 6b91c12c3..06a81dc27 100644 --- a/apps/http-proxy/src/http_session.cc +++ b/apps/http-proxy/src/http_session.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Cisco and/or its affiliates. + * Copyright (c) 2021-2022 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -13,30 +13,24 @@ * limitations under the License. */ +#include <hicn/apps/utils/logger.h> #include <hicn/http-proxy/http_proxy.h> #include <hicn/transport/utils/branch_prediction.h> -#include <hicn/transport/utils/log.h> #include <iostream> namespace transport { -HTTPSession::HTTPSession(asio::io_service &io_service, std::string &ip_address, - std::string &port, - ContentReceivedCallback receive_callback, - OnConnectionClosed on_connection_closed_callback, - bool client) +HTTPSession::HTTPSession( + asio::io_service &io_service, const std::string &ip_address, + const std::string &port, const ContentReceivedCallback &receive_callback, + const OnConnectionClosed &on_connection_closed_callback, bool client) : io_service_(io_service), socket_(io_service_), resolver_(io_service_), endpoint_iterator_(resolver_.resolve({ip_address, port})), timer_(io_service), reverse_(client), - is_reconnection_(false), - data_available_(false), - content_length_(0), - is_last_chunk_(false), - chunked_(false), receive_callback_(receive_callback), on_connection_closed_callback_(on_connection_closed_callback) { input_buffer_.prepare(buffer_size + 2048); @@ -51,10 +45,10 @@ HTTPSession::HTTPSession(asio::io_service &io_service, std::string &ip_address, doConnect(); } -HTTPSession::HTTPSession(asio::ip::tcp::socket socket, - ContentReceivedCallback receive_callback, - OnConnectionClosed on_connection_closed_callback, - bool client) +HTTPSession::HTTPSession( + asio::ip::tcp::socket socket, + const ContentReceivedCallback &receive_callback, + const OnConnectionClosed &on_connection_closed_callback, bool client) : #if ((ASIO_VERSION / 100 % 1000) < 12) io_service_(socket.get_io_service()), @@ -92,7 +86,7 @@ void HTTPSession::send(const uint8_t *packet, std::size_t len, io_service_.dispatch([this, packet, len, content_sent]() { asio::async_write(socket_, asio::buffer(packet, len), [content_sent = std::move(content_sent)]( - std::error_code ec, std::size_t /*length*/) { + const std::error_code &ec, std::size_t /*length*/) { if (!ec) { content_sent(); } @@ -111,7 +105,6 @@ void HTTPSession::send(utils::MemBuf *buffer, doWrite(); } } else { - TRANSPORT_LOGD("Tell the handle connect it has data to write"); data_available_ = true; } }); @@ -121,9 +114,7 @@ void HTTPSession::close() { if (state_ != ConnectorState::CLOSED) { state_ = ConnectorState::CLOSED; if (socket_.is_open()) { - // socket_.shutdown(asio::ip::tcp::socket::shutdown_type::shutdown_both); socket_.close(); - // on_disconnect_callback_(); } } } @@ -131,29 +122,26 @@ void HTTPSession::close() { void HTTPSession::doWrite() { auto &buffer = write_msgs_.front().first; - asio::async_write(socket_, asio::buffer(buffer->data(), buffer->length()), - [this](std::error_code ec, std::size_t length) { - if (TRANSPORT_EXPECT_FALSE(!ec)) { - TRANSPORT_LOGD("Content successfully sent! %zu", - length); - write_msgs_.front().second(); - write_msgs_.pop_front(); - if (!write_msgs_.empty()) { - doWrite(); - } - } else { - TRANSPORT_LOGD("Content NOT sent!"); - } - }); + asio::async_write( + socket_, asio::buffer(buffer->data(), buffer->length()), + [this](const std::error_code &ec, [[maybe_unused]] std::size_t length) { + if (TRANSPORT_EXPECT_FALSE(!ec)) { + write_msgs_.front().second(); + write_msgs_.pop_front(); + if (!write_msgs_.empty()) { + doWrite(); + } + } + }); } // namespace transport -void HTTPSession::handleRead(std::error_code ec, std::size_t length) { +void HTTPSession::handleRead(const std::error_code &ec, std::size_t length) { if (TRANSPORT_EXPECT_TRUE(!ec)) { content_length_ -= length; const uint8_t *buffer = asio::buffer_cast<const uint8_t *>(input_buffer_.data()); - bool is_last = chunked_ ? (is_last_chunk_ ? !content_length_ : false) - : !content_length_; + bool check = is_last_chunk_ ? !content_length_ : false; + bool is_last = chunked_ ? check : !content_length_; receive_callback_(buffer, input_buffer_.size(), is_last, false, nullptr); input_buffer_.consume(input_buffer_.size()); @@ -212,7 +200,7 @@ void HTTPSession::doReadBody(std::size_t body_size, void HTTPSession::doReadChunkedHeader() { asio::async_read_until( socket_, input_buffer_, "\r\n", - [this](std::error_code ec, std::size_t length) { + [this](const std::error_code &ec, std::size_t length) { if (TRANSPORT_EXPECT_TRUE(!ec)) { const uint8_t *buffer = asio::buffer_cast<const uint8_t *>(input_buffer_.data()); @@ -231,7 +219,7 @@ void HTTPSession::doReadChunkedHeader() { void HTTPSession::doReadHeader() { asio::async_read_until( socket_, input_buffer_, "\r\n\r\n", - [this](std::error_code ec, std::size_t length) { + [this](const std::error_code &ec, std::size_t length) { if (TRANSPORT_EXPECT_TRUE(!ec)) { const uint8_t *buffer = asio::buffer_cast<const uint8_t *>(input_buffer_.data()); @@ -274,12 +262,11 @@ void HTTPSession::doReadHeader() { void HTTPSession::tryReconnection() { if (on_connection_closed_callback_(socket_)) { if (state_ == ConnectorState::CONNECTED) { - TRANSPORT_LOGD("Connection lost. Trying to reconnect...\n"); + LoggerErr() << "Connection lost. Trying to reconnect..."; state_ = ConnectorState::CONNECTING; is_reconnection_ = true; io_service_.post([this]() { if (socket_.is_open()) { - // socket_.shutdown(asio::ip::tcp::socket::shutdown_type::shutdown_both); socket_.close(); } startConnectionTimer(); @@ -290,35 +277,33 @@ void HTTPSession::tryReconnection() { } void HTTPSession::doConnect() { - asio::async_connect(socket_, endpoint_iterator_, - [this](std::error_code ec, tcp::resolver::iterator) { - if (!ec) { - timer_.cancel(); - state_ = ConnectorState::CONNECTED; - - asio::ip::tcp::no_delay noDelayOption(true); - socket_.set_option(noDelayOption); + asio::async_connect( + socket_, endpoint_iterator_, + [this](const std::error_code &ec, tcp::resolver::iterator) { + if (!ec) { + timer_.cancel(); + state_ = ConnectorState::CONNECTED; - // on_reconnect_callback_(); + asio::ip::tcp::no_delay noDelayOption(true); + socket_.set_option(noDelayOption); - doReadHeader(); + doReadHeader(); - if (data_available_ && !write_msgs_.empty()) { - data_available_ = false; - doWrite(); - } + if (data_available_ && !write_msgs_.empty()) { + data_available_ = false; + doWrite(); + } - if (is_reconnection_) { - is_reconnection_ = false; - TRANSPORT_LOGD("Connection recovered!"); - } + if (is_reconnection_) { + is_reconnection_ = false; + LoggerInfo() << "Connection recovered!"; + } - } else { - TRANSPORT_LOGE("Impossible to reconnect: %s", - ec.message().c_str()); - close(); - } - }); + } else { + LoggerErr() << "Impossible to reconnect: " << ec.message(); + close(); + } + }); } bool HTTPSession::checkConnected() { @@ -335,7 +320,7 @@ void HTTPSession::handleDeadline(const std::error_code &ec) { if (!ec) { io_service_.post([this]() { socket_.close(); - TRANSPORT_LOGE("Error connecting. Is the server running?\n"); + LoggerErr() << "Error connecting. Is the server running?"; io_service_.stop(); }); } |