accept-encoding default to identity even if gzip is enabled.
This commit is contained in:
+42
-32
@@ -19,6 +19,40 @@ void ClientSession::Accept(const std::string& content_types) {
|
||||
}
|
||||
}
|
||||
|
||||
#if WEBCC_ENABLE_GZIP
|
||||
|
||||
// Content-Encoding Tokens:
|
||||
// (https://en.wikipedia.org/wiki/HTTP_compression)
|
||||
//
|
||||
// * compress ¨C UNIX "compress" program method (historic; deprecated in most
|
||||
// applications and replaced by gzip or deflate);
|
||||
// * deflate ¨C compression based on the deflate algorithm, a combination of
|
||||
// the LZ77 algorithm and Huffman coding, wrapped inside the
|
||||
// zlib data format;
|
||||
// * gzip ¨C GNU zip format. Uses the deflate algorithm for compression,
|
||||
// but the data format and the checksum algorithm differ from
|
||||
// the "deflate" content-encoding. This method is the most
|
||||
// broadly supported as of March 2011.
|
||||
// * identity ¨C No transformation is used. This is the default value for
|
||||
// content coding.
|
||||
//
|
||||
// A note about "deflate":
|
||||
// "gzip" is the gzip format, and "deflate" is the zlib format. They should
|
||||
// probably have called the second one "zlib" instead to avoid confusion with
|
||||
// the raw deflate compressed data format.
|
||||
// Simply put, "deflate" is not recommended for HTTP 1.1 encoding.
|
||||
// (https://www.zlib.net/zlib_faq.html#faq39)
|
||||
|
||||
void ClientSession::AcceptGzip(bool gzip) {
|
||||
if (gzip) {
|
||||
headers_.Set(headers::kAcceptEncoding, "gzip, deflate");
|
||||
} else {
|
||||
headers_.Set(headers::kAcceptEncoding, "identity");
|
||||
}
|
||||
}
|
||||
|
||||
#endif // WEBCC_ENABLE_GZIP
|
||||
|
||||
void ClientSession::Auth(const std::string& type,
|
||||
const std::string& credentials) {
|
||||
headers_.Set(headers::kAuthorization, type + " " + credentials);
|
||||
@@ -54,41 +88,15 @@ ResponsePtr ClientSession::Send(RequestPtr request, bool stream) {
|
||||
}
|
||||
|
||||
void ClientSession::InitHeaders() {
|
||||
using namespace headers;
|
||||
headers_.Set(headers::kUserAgent, utility::UserAgent());
|
||||
|
||||
headers_.Set(kUserAgent, utility::UserAgent());
|
||||
headers_.Set(headers::kAccept, "*/*");
|
||||
|
||||
// Content-Encoding Tokens:
|
||||
// (https://en.wikipedia.org/wiki/HTTP_compression)
|
||||
//
|
||||
// * compress ¨C UNIX "compress" program method (historic; deprecated in most
|
||||
// applications and replaced by gzip or deflate);
|
||||
// * deflate ¨C compression based on the deflate algorithm, a combination of
|
||||
// the LZ77 algorithm and Huffman coding, wrapped inside the
|
||||
// zlib data format;
|
||||
// * gzip ¨C GNU zip format. Uses the deflate algorithm for compression,
|
||||
// but the data format and the checksum algorithm differ from
|
||||
// the "deflate" content-encoding. This method is the most
|
||||
// broadly supported as of March 2011.
|
||||
// * identity ¨C No transformation is used. This is the default value for
|
||||
// content coding.
|
||||
//
|
||||
// A note about "deflate":
|
||||
// "gzip" is the gzip format, and "deflate" is the zlib format. They should
|
||||
// probably have called the second one "zlib" instead to avoid confusion with
|
||||
// the raw deflate compressed data format.
|
||||
// Simply put, "deflate" is not recommended for HTTP 1.1 encoding.
|
||||
// (https://www.zlib.net/zlib_faq.html#faq39)
|
||||
// Accept-Encoding is always default to "identity", even if GZIP is enabled.
|
||||
// Please overwrite with AcceptGzip().
|
||||
headers_.Set(headers::kAcceptEncoding, "identity");
|
||||
|
||||
headers_.Set(kAccept, "*/*");
|
||||
|
||||
#if WEBCC_ENABLE_GZIP
|
||||
headers_.Set(kAcceptEncoding, "gzip, deflate");
|
||||
#else
|
||||
headers_.Set(kAcceptEncoding, "identity");
|
||||
#endif // WEBCC_ENABLE_GZIP
|
||||
|
||||
headers_.Set(kConnection, "Keep-Alive");
|
||||
headers_.Set(headers::kConnection, "Keep-Alive");
|
||||
}
|
||||
|
||||
ResponsePtr ClientSession::DoSend(RequestPtr request, bool stream) {
|
||||
@@ -141,9 +149,11 @@ ResponsePtr ClientSession::DoSend(RequestPtr request, bool stream) {
|
||||
}
|
||||
|
||||
auto response = client->response();
|
||||
|
||||
// The client object might be cached in the pool.
|
||||
// Reset to make sure it won't keep a reference to the response object.
|
||||
client->Reset();
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
@@ -51,6 +51,13 @@ public:
|
||||
// Set content types to accept.
|
||||
void Accept(const std::string& content_types);
|
||||
|
||||
#if WEBCC_ENABLE_GZIP
|
||||
|
||||
// Accept Gzip compressed response data or not.
|
||||
void AcceptGzip(bool gzip = true);
|
||||
|
||||
#endif // WEBCC_ENABLE_GZIP
|
||||
|
||||
// Set authorization.
|
||||
void Auth(const std::string& type, const std::string& credentials);
|
||||
|
||||
|
||||
@@ -54,6 +54,19 @@ RequestPtr RequestBuilder::operator()() {
|
||||
return request;
|
||||
}
|
||||
|
||||
#if WEBCC_ENABLE_GZIP
|
||||
|
||||
// Accept Gzip compressed response data or not.
|
||||
RequestBuilder& RequestBuilder::AcceptGzip(bool gzip) {
|
||||
if (gzip) {
|
||||
return Header(headers::kAcceptEncoding, "gzip, deflate");
|
||||
} else {
|
||||
return Header(headers::kAcceptEncoding, "identity");
|
||||
}
|
||||
}
|
||||
|
||||
#endif // WEBCC_ENABLE_GZIP
|
||||
|
||||
RequestBuilder& RequestBuilder::File(const bfs::path& path,
|
||||
bool infer_media_type,
|
||||
std::size_t chunk_size) {
|
||||
|
||||
+14
-5
@@ -124,6 +124,13 @@ public:
|
||||
return Header(headers::kAccept, content_types);
|
||||
}
|
||||
|
||||
#if WEBCC_ENABLE_GZIP
|
||||
|
||||
// Accept Gzip compressed response data or not.
|
||||
RequestBuilder& AcceptGzip(bool gzip = true);
|
||||
|
||||
#endif // WEBCC_ENABLE_GZIP
|
||||
|
||||
RequestBuilder& Body(const std::string& data) {
|
||||
body_.reset(new StringBody{ data, false });
|
||||
return *this;
|
||||
@@ -173,10 +180,17 @@ public:
|
||||
RequestBuilder& Date();
|
||||
|
||||
#if WEBCC_ENABLE_GZIP
|
||||
|
||||
// Compress the body data (only for string body).
|
||||
// NOTE:
|
||||
// Most servers don't support compressed requests.
|
||||
// Even the requests module from Python doesn't have a built-in support.
|
||||
// See: https://github.com/kennethreitz/requests/issues/1753
|
||||
RequestBuilder& Gzip(bool gzip = true) {
|
||||
gzip_ = gzip;
|
||||
return *this;
|
||||
}
|
||||
|
||||
#endif // WEBCC_ENABLE_GZIP
|
||||
|
||||
private:
|
||||
@@ -207,11 +221,6 @@ private:
|
||||
bool keep_alive_ = true;
|
||||
|
||||
#if WEBCC_ENABLE_GZIP
|
||||
// Compress the body data (only for string body).
|
||||
// NOTE:
|
||||
// Most servers don't support compressed requests.
|
||||
// Even the requests module from Python doesn't have a built-in support.
|
||||
// See: https://github.com/kennethreitz/requests/issues/1753
|
||||
bool gzip_ = false;
|
||||
#endif // WEBCC_ENABLE_GZIP
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user