From a7e7cb0e7032cc6e0bc7a12aee3e9bffa715c14c Mon Sep 17 00:00:00 2001 From: Karol Kosek Date: Sat, 16 Oct 2021 20:17:18 +0200 Subject: [PATCH] LibHTTP: Store Content-Length value in the HTTP Job class This way we can save some calculations, but more importantly this will also be needed in next commits. :P --- Userland/Libraries/LibHTTP/Job.cpp | 19 +++++++------------ Userland/Libraries/LibHTTP/Job.h | 1 + 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibHTTP/Job.cpp b/Userland/Libraries/LibHTTP/Job.cpp index aaed0d864a..8d7f2d1d42 100644 --- a/Userland/Libraries/LibHTTP/Job.cpp +++ b/Userland/Libraries/LibHTTP/Job.cpp @@ -221,6 +221,10 @@ void Job::on_socket_connected() // Assume that any content-encoding means that we can't decode it as a stream :( dbgln_if(JOB_DEBUG, "Content-Encoding {} detected, cannot stream output :(", value); m_can_stream_response = false; + } else if (name.equals_ignoring_case("Content-Length")) { + auto length = value.to_uint(); + if (length.has_value()) + m_content_length = length.value(); } dbgln_if(JOB_DEBUG, "Job: [{}] = '{}'", name, value); return; @@ -341,19 +345,10 @@ void Job::on_socket_connected() m_current_chunk_remaining_size = size; } - auto content_length_header = m_headers.get("Content-Length"); - Optional content_length {}; + deferred_invoke([this] { did_progress(m_content_length, m_received_size); }); - if (content_length_header.has_value()) { - auto length = content_length_header.value().to_uint(); - if (length.has_value()) - content_length = length.value(); - } - - deferred_invoke([this, content_length] { did_progress(content_length, m_received_size); }); - - if (content_length.has_value()) { - auto length = content_length.value(); + if (m_content_length.has_value()) { + auto length = m_content_length.value(); if (m_received_size >= length) { m_received_size = length; finish_up(); diff --git a/Userland/Libraries/LibHTTP/Job.h b/Userland/Libraries/LibHTTP/Job.h index 7eb85154df..cbb1b640b3 100644 --- a/Userland/Libraries/LibHTTP/Job.h +++ b/Userland/Libraries/LibHTTP/Job.h @@ -60,6 +60,7 @@ protected: size_t m_buffered_size { 0 }; size_t m_received_size { 0 }; bool m_sent_data { 0 }; + Optional m_content_length; Optional m_current_chunk_remaining_size; Optional m_current_chunk_total_size; bool m_can_stream_response { true };