From 3077f21549b4d4d2459cae1eaef07281edf63032 Mon Sep 17 00:00:00 2001 From: Chunting Gu Date: Wed, 7 Aug 2019 15:59:22 +0800 Subject: [PATCH] Move data decompression to StringBody. --- autotest/client_autotest.cc | 1 - examples/client_basics.cc | Bin 3252 -> 1565 bytes webcc/body.cc | 23 +++++++++++++++++++++++ webcc/body.h | 28 ++++++++++++++++++++++------ webcc/parser.cc | 24 ++++-------------------- webcc/request_builder.h | 4 ++-- webcc/response_builder.h | 4 ++-- 7 files changed, 53 insertions(+), 31 deletions(-) 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 abf280d921350ccbc0db4664c9379799cf0542ef..27b3f1b338e8cdf34e0675315ab7120bd17303d3 100644 GIT binary patch literal 1565 zcmcgs+iu!G5PkPoOpR0)iQkQsWLQ#{!dz4(7 zu!l-vOqov^$APaT@!XtmCJOD=$*M)G7z0274x!%+3`t51kFww2k7tpKT=#zdk@d3% zmg=uq-;*RcLEOZ&ee!oal)B!wD|6ey^Oka5+~nh)L3+}*rWcUDM8*FM!)s<_B@SbL zsDsHU@)(D^W+*q<>^Fy=+%((Jlq64~!8W;1{}kD_$BZ4gaoO7n9j=G@UFL zi)pqOer5FmK&FbF`8qg2^1mRsfuYBlf#XzWmjcsEtwAW>NwjClMC(Yu2F#9P5ds+U zTvxFOdcEQ$`^lvoA}GkDkrExkd3Jl|y3qWQQJd+^&Z`}F9@mnXvVZWMb+!z4c44K~ EPnhkjX8-^I literal 3252 zcmd^>U2hUW6o${WiT`1xiIk*5?Ulh|0!V7LsZ}&y5(-<3rC7IHwITd<^?7HOvP*!# z#u%fS49x7D@9&>KwrpS{>)Ft5tZ$(Ow$H6=*R0SYi|v`+w|lk-ZeX8*ih&BjxJ)1y zLl^8DP^ZxDS;g@ia36u)ah$|Y^hQ>;b1N+2nWZuW{v%6r&alK^I>qR6iCj8wTZ!A) zQ4KhKu$DEf$?DjVy|x4X5AB%!4%h-~2dJ6ERjb~?HGx02Hor$uDcMs;Ra`|mi_^re zQ$5?rIny$j(|Q$^w+{2lSw~(uaW)UoI*TPw+#>^!qNa=rIF%(BP} z$k{xc^Tmm{{d*qI*2F(kF;B=mU!T{lnd%e5$X{(5Q0ON$b8JO2U)??ARAbk9)4k}b zMv=2Ru-^7;7r*D{`x3cTXMB}=j}B+{#NGa%s-rpUjGRwTC@toVI`fL=jSiDXo0BY( zw#xcd>U!;-o1HHH{))Xt9FB-iZ^6J#7Modeg5`bh>oJoA#9q%ur1EKgwrkN(B3>hV z$eZROf6GpqvYN2_SiK5052q=6KZ7;>GK2NH43ime+S4K^s5?ez8ZftbAL(_q>}k5+ qogC_VzQcR@Hw9BK{W=+_7)~ 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(std::move(content_)); - message_->SetBody(body, false); - return true; - } + bool compressed = IsContentCompressed(); + auto body = std::make_shared(std::move(content_), compressed); #if WEBCC_ENABLE_GZIP - LOG_INFO("Decompress the HTTP content..."); - - std::string decompressed; - if (!gzip::Decompress(content_, &decompressed)) { + if (!body->Decompress()) { LOG_ERRO("Cannot decompress the HTTP content!"); return false; } - - auto body = std::make_shared(std::move(decompressed)); - message_->SetBody(body, false); - - return true; - #else - LOG_WARN("Compressed HTTP content remains untouched."); +#endif // WEBCC_ENABLE_GZIP - auto body = std::make_shared(std::move(content_)); message_->SetBody(body, false); - return true; - -#endif // WEBCC_ENABLE_GZIP } void Parser::AppendContent(const char* data, std::size_t count) { diff --git a/webcc/request_builder.h b/webcc/request_builder.h index f832c3c..244a8ce 100644 --- a/webcc/request_builder.h +++ b/webcc/request_builder.h @@ -86,12 +86,12 @@ public: } RequestBuilder& Body(const std::string& data) { - body_.reset(new StringBody{ data }); + body_.reset(new StringBody{ data, false }); return *this; } RequestBuilder& Body(std::string&& data) { - body_.reset(new StringBody{ std::move(data) }); + body_.reset(new StringBody{ std::move(data), false }); return *this; } diff --git a/webcc/response_builder.h b/webcc/response_builder.h index 320ed05..98913fd 100644 --- a/webcc/response_builder.h +++ b/webcc/response_builder.h @@ -87,12 +87,12 @@ public: } ResponseBuilder& Body(const std::string& data) { - body_.reset(new StringBody{ data }); + body_.reset(new StringBody{ data, false }); return *this; } ResponseBuilder& Body(std::string&& data) { - body_.reset(new StringBody{ std::move(data) }); + body_.reset(new StringBody{ std::move(data), false }); return *this; }