diff --git a/src/server/connection.cpp b/src/server/connection.cpp index 980d2fcdf77..73a6e07d370 100644 --- a/src/server/connection.cpp +++ b/src/server/connection.cpp @@ -22,6 +22,7 @@ Connection::Connection(boost::asio::io_context &io_context, RequestHandler &hand request_handler(handler), http_request_parser(std::make_optional()) { http_request_parser->header_limit(std::numeric_limits::max()); + incoming_data_buffer.resize(CHUNK_SIZE, 0); } boost::asio::ip::tcp::socket &Connection::socket() { return TCP_socket; } @@ -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) { @@ -86,7 +87,9 @@ 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); @@ -94,6 +97,10 @@ void Connection::handle_read(const boost::system::error_code &error, { 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