summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcel Schneider <marcelschneider5@outlook.de>2019-11-09 18:33:37 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-11-10 12:52:23 +0100
commitc40935e79f6a404e053b1c5908d8acf557cfaa91 (patch)
tree5b28d619f062ebbf55b8b35881db6c04b8a537fe
parent4fe5503b17d5cf8a99d69e8ad95a82e67c975d86 (diff)
downloadserenity-c40935e79f6a404e053b1c5908d8acf557cfaa91.zip
LibCore: Add Content-Encoding handling to CHttpJob
-rw-r--r--Libraries/LibCore/CHttpJob.cpp35
1 files changed, 34 insertions, 1 deletions
diff --git a/Libraries/LibCore/CHttpJob.cpp b/Libraries/LibCore/CHttpJob.cpp
index d75cdbd0bc..ace3fe26df 100644
--- a/Libraries/LibCore/CHttpJob.cpp
+++ b/Libraries/LibCore/CHttpJob.cpp
@@ -4,8 +4,37 @@
#include <stdio.h>
#include <unistd.h>
+#include <CGzip.h>
+
#define CHTTPJOB_DEBUG
+static ByteBuffer handle_content_encoding(const ByteBuffer& buf, const String& content_encoding)
+{
+ dbg() << "CHttpJob::handle_content_encoding: buf has content_encoding = " << content_encoding;
+
+ if (content_encoding == "gzip") {
+ if (!Gzip::is_compressed(buf)) {
+ dbg() << "CHttpJob::handle_content_encoding: buf is not gzip compressed!";
+ }
+
+ dbg() << "CHttpJob::handle_content_encoding: buf is gzip compressed!";
+
+ auto uncompressed = Gzip::decompress(buf);
+ if (!uncompressed.has_value()) {
+ dbg() << "CHttpJob::handle_content_encoding: Gzip::decompress() failed. Returning original buffer.";
+ return buf;
+ }
+
+ dbg() << "CHttpJob::handle_content_encoding: Gzip::decompress() successful.\n"
+ << " Input size = " << buf.size() << "\n"
+ << " Output size = " << uncompressed.value().size();
+
+ return uncompressed.value();
+ }
+
+ return buf;
+}
+
CHttpJob::CHttpJob(const CHttpRequest& request)
: m_request(request)
{
@@ -113,6 +142,11 @@ void CHttpJob::finish_up()
}
m_received_buffers.clear();
+ auto content_encoding = m_headers.get("Content-Encoding");
+ if (content_encoding.has_value()) {
+ flattened_buffer = handle_content_encoding(flattened_buffer, content_encoding.value());
+ }
+
auto response = CHttpResponse::create(m_code, move(m_headers), move(flattened_buffer));
deferred_invoke([this, response](auto&) {
did_finish(move(response));
@@ -146,4 +180,3 @@ void CHttpJob::shutdown()
remove_child(*m_socket);
m_socket = nullptr;
}
-