From 89c87ff7b9eaf88bccf7e63419c4be79d0812184 Mon Sep 17 00:00:00 2001 From: Karol Kosek Date: Sat, 16 Oct 2021 20:17:25 +0200 Subject: [PATCH] LibHTTP: Trim the last packet if it exceeded the Content-Length value Used these commands to test it: printf 'HTTP/1.0 200 OK\r\n%s\r\n\r\n%s' 'Content-Length: 4' \ 'well hello friends!' | nc -lN 0.0.0.0 8000 pro http://0.0.0.0:8000 --- Userland/Libraries/LibHTTP/Job.cpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibHTTP/Job.cpp b/Userland/Libraries/LibHTTP/Job.cpp index 8d7f2d1d42..c690bd067d 100644 --- a/Userland/Libraries/LibHTTP/Job.cpp +++ b/Userland/Libraries/LibHTTP/Job.cpp @@ -316,11 +316,27 @@ void Job::on_socket_connected() } } + bool read_everything = false; + if (m_content_length.has_value()) { + auto length = m_content_length.value(); + if (m_received_size + payload.size() >= length) { + payload.resize(length - m_buffered_size); + read_everything = true; + } + } + m_received_buffers.append(payload); m_buffered_size += payload.size(); m_received_size += payload.size(); flush_received_buffers(); + deferred_invoke([this] { did_progress(m_content_length, m_received_size); }); + + if (read_everything) { + finish_up(); + return IterationDecision::Break; + } + if (m_current_chunk_remaining_size.has_value()) { auto size = m_current_chunk_remaining_size.value() - payload.size(); @@ -345,16 +361,6 @@ void Job::on_socket_connected() m_current_chunk_remaining_size = size; } - deferred_invoke([this] { did_progress(m_content_length, m_received_size); }); - - if (m_content_length.has_value()) { - auto length = m_content_length.value(); - if (m_received_size >= length) { - m_received_size = length; - finish_up(); - return IterationDecision::Break; - } - } return IterationDecision::Continue; });