Skip to content

Commit

Permalink
Track bytes read and bytes parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
mjjbell committed Oct 13, 2022
1 parent d143de5 commit 5bb7b33
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/server/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Connection::Connection(boost::asio::io_context &io_context, RequestHandler &hand
request_handler(handler), http_request_parser(std::make_optional<RequestParser>())
{
http_request_parser->header_limit(std::numeric_limits<std::uint32_t>::max());
incoming_data_buffer.resize(CHUNK_SIZE, 0);
}

boost::asio::ip::tcp::socket &Connection::socket() { return TCP_socket; }
Expand Down Expand Up @@ -66,7 +67,7 @@ void Connection::start()
}

void Connection::handle_read(const boost::system::error_code &error,
std::size_t /*bytes_transferred*/)
std::size_t bytes_transferred)
{
if (error)
{
Expand All @@ -86,14 +87,20 @@ void Connection::handle_read(const boost::system::error_code &error,
}

boost::beast::error_code ec;
http_request_parser->put(boost::asio::buffer(incoming_data_buffer), ec);

std::size_t bytes_available = incoming_data_buffer.size() + bytes_transferred - CHUNK_SIZE;
std::size_t bytes_used = http_request_parser->put(boost::asio::buffer(incoming_data_buffer, bytes_available), ec);
// no error detected, let's parse the request
http::compression_type compression_type(http::no_compression);

if (ec)
{
if (ec == boost::beast::http::error::need_more)
{
if (bytes_used != bytes_available) {
std::rotate(incoming_data_buffer.begin(), incoming_data_buffer.begin() + bytes_used, incoming_data_buffer.end());
incoming_data_buffer.resize(bytes_available - bytes_used);
}
const auto current_size = incoming_data_buffer.size();
incoming_data_buffer.resize(incoming_data_buffer.size() + CHUNK_SIZE, 0);
// we don't have a result yet, so continue reading
Expand Down

0 comments on commit 5bb7b33

Please sign in to comment.