diff --git a/autotest/client_autotest.cc b/autotest/client_autotest.cc index 7ad17cb..8b01f61 100644 --- a/autotest/client_autotest.cc +++ b/autotest/client_autotest.cc @@ -284,7 +284,6 @@ TEST(ClientTest, KeepAlive) { TEST(ClientTest, GetImageJpeg) { webcc::ClientSession session; - std::string url = "http://httpbin.org/get"; try { auto r = session.Get("http://httpbin.org/image/jpeg"); diff --git a/examples/client_basics.cc b/examples/client_basics.cc index abf280d..27b3f1b 100644 Binary files a/examples/client_basics.cc and b/examples/client_basics.cc differ diff --git a/webcc/body.cc b/webcc/body.cc index 3a7a30b..a473129 100644 --- a/webcc/body.cc +++ b/webcc/body.cc @@ -17,7 +17,12 @@ namespace webcc { // ----------------------------------------------------------------------------- #if WEBCC_ENABLE_GZIP + bool StringBody::Compress() { + if (compressed_) { + return true; // Already compressed. + } + if (data_.size() <= kGzipThreshold) { return false; } @@ -25,12 +30,30 @@ bool StringBody::Compress() { std::string compressed; if (gzip::Compress(data_, &compressed)) { data_ = std::move(compressed); + compressed_ = true; return true; } LOG_WARN("Failed to compress the body data!"); return false; } + +bool StringBody::Decompress() { + if (!compressed_) { + return true; // Already decompressed. + } + + std::string decompressed; + if (gzip::Decompress(data_, &decompressed)) { + data_ = std::move(decompressed); + compressed_ = false; + return true; + } + + LOG_WARN("Failed to decompress the body data!"); + return false; +} + #endif // WEBCC_ENABLE_GZIP void StringBody::InitPayload() { diff --git a/webcc/body.h b/webcc/body.h index fb7d220..414abb0 100644 --- a/webcc/body.h +++ b/webcc/body.h @@ -28,12 +28,19 @@ public: } #if WEBCC_ENABLE_GZIP - // Compress with Gzip. - // If the body size <= the threshold (1400 bytes), no compression will be done - // and just return false. + + // Compress the data with Gzip. + // If data size <= threshold (1400 bytes), no compression will be taken + // and false will be simply returned. virtual bool Compress() { return false; } + + // Decompress the data. + virtual bool Decompress() { + return false; + } + #endif // WEBCC_ENABLE_GZIP // Initialize the payload for iteration. @@ -61,10 +68,12 @@ using BodyPtr = std::shared_ptr
; class StringBody : public Body { public: - explicit StringBody(const std::string& data) : data_(data) { + explicit StringBody(const std::string& data, bool compressed) + : data_(data), compressed_(compressed) { } - explicit StringBody(std::string&& data) : data_(std::move(data)) { + explicit StringBody(std::string&& data, bool compressed) + : data_(std::move(data)), compressed_(compressed) { } std::size_t GetSize() const override { @@ -76,8 +85,12 @@ public: } #if WEBCC_ENABLE_GZIP + bool Compress() override; -#endif + + bool Decompress() override; + +#endif // WEBCC_ENABLE_GZIP void InitPayload() override; @@ -88,6 +101,9 @@ public: private: std::string data_; + // Is the data compressed? + bool compressed_; + // Index for (not really) iterating the payload. std::size_t index_ = 0; }; diff --git a/webcc/parser.cc b/webcc/parser.cc index 50d4011..a4b7092 100644 --- a/webcc/parser.cc +++ b/webcc/parser.cc @@ -305,37 +305,21 @@ bool Parser::Finish() { // Could be kInvalidLength when chunked. message_->set_content_length(content_length_); - if (!IsContentCompressed()) { - auto body = std::make_shared