From 6cad2aba5ee9bf9569fe5f60a8ff08add5cd2b47 Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 22 Mar 2021 02:41:13 +0000 Subject: [PATCH] LibHTTP: Add support for the deflate content encoding --- Userland/Libraries/LibHTTP/Job.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Userland/Libraries/LibHTTP/Job.cpp b/Userland/Libraries/LibHTTP/Job.cpp index 062397071f..60a24d8a2a 100644 --- a/Userland/Libraries/LibHTTP/Job.cpp +++ b/Userland/Libraries/LibHTTP/Job.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -57,6 +58,32 @@ static ByteBuffer handle_content_encoding(const ByteBuffer& buf, const String& c dbgln(" Output size: {}", uncompressed.value().size()); } + return uncompressed.value(); + } else if (content_encoding == "deflate") { + dbgln_if(JOB_DEBUG, "Job::handle_content_encoding: buf is deflate compressed!"); + + // Even though the content encoding is "deflate", it's actually deflate with the zlib wrapper. + // https://tools.ietf.org/html/rfc7230#section-4.2.2 + auto uncompressed = Compress::Zlib::decompress_all(buf); + if (!uncompressed.has_value()) { + // From the RFC: + // "Note: Some non-conformant implementations send the "deflate" + // compressed data without the zlib wrapper." + dbgln_if(JOB_DEBUG, "Job::handle_content_encoding: Zlib::decompress_all() failed. Trying DeflateDecompressor::decompress_all()"); + uncompressed = Compress::DeflateDecompressor::decompress_all(buf); + + if (!uncompressed.has_value()) { + dbgln("Job::handle_content_encoding: DeflateDecompressor::decompress_all() failed, returning original buffer."); + return buf; + } + } + + if constexpr (JOB_DEBUG) { + dbgln("Job::handle_content_encoding: Deflate decompression successful."); + dbgln(" Input size: {}", buf.size()); + dbgln(" Output size: {}", uncompressed.value().size()); + } + return uncompressed.value(); }