aboutsummaryrefslogtreecommitdiffstats
path: root/apps/http-proxy/src/http_session.cc
diff options
context:
space:
mode:
Diffstat (limited to 'apps/http-proxy/src/http_session.cc')
-rw-r--r--apps/http-proxy/src/http_session.cc74
1 files changed, 32 insertions, 42 deletions
diff --git a/apps/http-proxy/src/http_session.cc b/apps/http-proxy/src/http_session.cc
index 84c814cbd..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();
}
@@ -120,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_();
}
}
}
@@ -130,25 +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)) {
- write_msgs_.front().second();
- write_msgs_.pop_front();
- if (!write_msgs_.empty()) {
- doWrite();
- }
- }
- });
+ 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());
@@ -207,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());
@@ -226,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());
@@ -269,12 +262,11 @@ void HTTPSession::doReadHeader() {
void HTTPSession::tryReconnection() {
if (on_connection_closed_callback_(socket_)) {
if (state_ == ConnectorState::CONNECTED) {
- TRANSPORT_LOG_ERROR << "Connection lost. Trying to reconnect...";
+ 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();
@@ -287,7 +279,7 @@ void HTTPSession::tryReconnection() {
void HTTPSession::doConnect() {
asio::async_connect(
socket_, endpoint_iterator_,
- [this](std::error_code ec, tcp::resolver::iterator) {
+ [this](const std::error_code &ec, tcp::resolver::iterator) {
if (!ec) {
timer_.cancel();
state_ = ConnectorState::CONNECTED;
@@ -295,8 +287,6 @@ void HTTPSession::doConnect() {
asio::ip::tcp::no_delay noDelayOption(true);
socket_.set_option(noDelayOption);
- // on_reconnect_callback_();
-
doReadHeader();
if (data_available_ && !write_msgs_.empty()) {
@@ -306,11 +296,11 @@ void HTTPSession::doConnect() {
if (is_reconnection_) {
is_reconnection_ = false;
- TRANSPORT_LOG_INFO << "Connection recovered!";
+ LoggerInfo() << "Connection recovered!";
}
} else {
- TRANSPORT_LOG_ERROR << "Impossible to reconnect: " << ec.message();
+ LoggerErr() << "Impossible to reconnect: " << ec.message();
close();
}
});
@@ -330,7 +320,7 @@ void HTTPSession::handleDeadline(const std::error_code &ec) {
if (!ec) {
io_service_.post([this]() {
socket_.close();
- TRANSPORT_LOG_ERROR << "Error connecting. Is the server running?";
+ LoggerErr() << "Error connecting. Is the server running?";
io_service_.stop();
});
}