From c579f7c7175b34426e8392e1edd3885c4b9cd185 Mon Sep 17 00:00:00 2001 From: garciay <yann.garcia@fscom.fr> Date: Wed, 15 May 2024 09:18:33 +0000 Subject: [PATCH] Add fragmentated message support for HTTP request decoding --- ccsrc/Protocols/Http/http_codec.cc | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/ccsrc/Protocols/Http/http_codec.cc b/ccsrc/Protocols/Http/http_codec.cc index c0fac4c..bd13632 100644 --- a/ccsrc/Protocols/Http/http_codec.cc +++ b/ccsrc/Protocols/Http/http_codec.cc @@ -117,7 +117,7 @@ int http_codec::decode(const OCTETSTRING &data, LibHttp__TypesAndValues::HttpMes loggers::get_instance().log("http_codec::decode: decoding_buffer.get_len()=%d", decoding_buffer.get_len()); if (_dc.length > decoding_buffer.get_len()) { // HTTP response is fragmented // Need to bufferize the first packet and wait for the other fragments tp be received - loggers::get_instance().warning("http_codec::decode: Need to bufferize the first packet and wait for the other fragments tp be received"); + loggers::get_instance().warning("http_codec::decode: Need to bufferize the first packet and wait for the other fragments to be received"); // Set counters loggers::get_instance().log("http_codec::decode: Set bufferization counters"); _initial_content_length = _dc.length; @@ -177,7 +177,30 @@ int http_codec::decode(const OCTETSTRING &data, LibHttp__TypesAndValues::HttpMes LibHttp__TypesAndValues::Headers headers; std::string content_type; decode_headers(decoding_buffer, headers, content_type); + loggers::get_instance().log("http_codec::decode (request): _dc.length=%d", _dc.length); + loggers::get_instance().log("http_codec::decode (request): decoding_buffer.get_len()=%d", decoding_buffer.get_len()); + if (_dc.length > decoding_buffer.get_len()) { // HTTP response is fragmented + // Need to bufferize the first packet and wait for the other fragments tp be received + loggers::get_instance().warning("http_codec::decode (request): Need to bufferize the first packet and wait for the other fragments to be received"); + // Set counters + loggers::get_instance().log("http_codec::decode (request): Set bufferization counters"); + _initial_content_length = _dc.length; + _current_content_length = 0; + _bufferized_buffers.push_back(data); + _current_content_length += data.lengthof(); + loggers::get_instance().log("http_codec::decode (request): _bufferized_buffers size: %d - _current_content_length: %d", _bufferized_buffers.size(), _current_content_length); + return -2; + } else { + // Force reset counters + loggers::get_instance().log("http_codec::decode (request): Force reset bufferization counters"); + _initial_content_length = 0; + _current_content_length = 0; + } + request.header() = headers; + loggers::get_instance().log_to_hexa("Before decoding Body (request): ", decoding_buffer); + loggers::get_instance().log("http_codec::decode (request): _initial_content_length = %d", _initial_content_length); + loggers::get_instance().log("http_codec::decode (request): headers().content_length (_dc.length) = %d - decoding_buffer.get_len() = %d", _dc.length, decoding_buffer.get_len()); OPTIONAL<LibHttp__MessageBodyTypes::HttpMessageBody> body; body.set_to_omit(); if (decode_body(decoding_buffer, body, content_type) == -1) { @@ -459,11 +482,11 @@ int http_codec::decode_header(CHARSTRING &header_line, LibHttp__TypesAndValues:: } // End of 'for' statement header.header__value() = OPTIONAL<LibHttp__TypesAndValues::charstring__list>(v); - if (m[1].str().compare("Content-Length") == 0) { + if ((m[1].str().compare("Content-Length") == 0) || (m[1].str().compare("content-length")) == 0) { // Save the the body length loggers::get_instance().log("http_codec::decode_header: decoded Content-Length %s", m[2].str().c_str()); _dc.length = std::stoi(m[2].str()); - } else if (m[1].str().compare("Transfer-Encoding") == 0) { + } else if ((m[1].str().compare("Transfer-Encoding") == 0) || (m[1].str().compare("transfer-encoding"))) { if (m[2].str().find("chunked") != std::string::npos) { _dc.chunked = true; loggers::get_instance().log("http_codec::decode_header: decoded Transfer-Encoding %x", _dc.chunked); -- GitLab