mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-26 12:24:46 +00:00
Honor set_decompress(false) in response streams (#2640)
open_stream_() created a decoder whatever set_decompress() said, so a response stream decoded the body when decompression was disabled, and failed with UnsupportedContentEncoding in a build without that decoder. The buffered path already checks the option; the streaming path now does too. The new test covers open_stream() and stream::Get(). Fixes #2639.
This commit is contained in:
1 parent
d23d1b6029
commit
76f84a5bfd
2 files changed
+32
-1
No files matched your search
@@ -16020,7 +16020,7 @@ inline ClientImpl::StreamHandle ClientImpl::open_stream_(Request &req) {
|
|||||||
|
|
||||||
auto content_encoding = detail::get_combined_header_value(
|
auto content_encoding = detail::get_combined_header_value(
|
||||||
handle.response->headers, "Content-Encoding");
|
handle.response->headers, "Content-Encoding");
|
||||||
if (!content_encoding.empty()) {
|
if (decompress_ && !content_encoding.empty()) {
|
||||||
// Same policy as prepare_content_receiver(): reject a coding we know about
|
// Same policy as prepare_content_receiver(): reject a coding we know about
|
||||||
// but were not built with, pass an unrecognized one through as-is.
|
// but were not built with, pass an unrecognized one through as-is.
|
||||||
handle.decompressor_ = detail::create_decompressor(content_encoding);
|
handle.decompressor_ = detail::create_decompressor(content_encoding);
|
||||||
|
|||||||
@@ -23779,6 +23779,37 @@ static void with_single_response(const std::string &response, Fn fn) {
|
|||||||
fn(cli);
|
fn(cli);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set_decompress(false) hands out the encoded bytes through the streaming APIs
|
||||||
|
// too, also in a build without the decoder for the advertised coding.
|
||||||
|
TEST(StreamDecompressionTest, DisabledPreservesEncodedBody) {
|
||||||
|
const std::string gzipped(GZIPPED_HELLO_WORLD, sizeof(GZIPPED_HELLO_WORLD));
|
||||||
|
const auto response = "HTTP/1.1 200 OK\r\n"
|
||||||
|
"Content-Encoding: gzip\r\n"
|
||||||
|
"Content-Length: " +
|
||||||
|
std::to_string(gzipped.size()) +
|
||||||
|
"\r\n"
|
||||||
|
"Connection: close\r\n\r\n" +
|
||||||
|
gzipped;
|
||||||
|
|
||||||
|
with_single_response(response, [&](Client &cli) {
|
||||||
|
cli.set_decompress(false);
|
||||||
|
auto handle = cli.open_stream("GET", "/");
|
||||||
|
ASSERT_TRUE(handle.is_valid()) << to_string(handle.error);
|
||||||
|
EXPECT_EQ("gzip", handle.response->get_header_value("Content-Encoding"));
|
||||||
|
EXPECT_EQ(gzipped, read_all(handle));
|
||||||
|
EXPECT_FALSE(handle.has_read_error());
|
||||||
|
});
|
||||||
|
|
||||||
|
with_single_response(response, [&](Client &cli) {
|
||||||
|
cli.set_decompress(false);
|
||||||
|
auto res = stream::Get(cli, "/", 7);
|
||||||
|
ASSERT_TRUE(res) << to_string(res.error());
|
||||||
|
EXPECT_EQ("gzip", res.get_header_value("Content-Encoding"));
|
||||||
|
EXPECT_EQ(gzipped, res.read_all());
|
||||||
|
EXPECT_FALSE(res.has_read_error());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
TEST(StreamBodyEndTest, BodyEndingAtConnectionClose) {
|
TEST(StreamBodyEndTest, BodyEndingAtConnectionClose) {
|
||||||
// Neither Content-Length nor chunked: the body ends at the close
|
// Neither Content-Length nor chunked: the body ends at the close
|
||||||
const char *close_delimited = "HTTP/1.1 200 OK\r\n"
|
const char *close_delimited = "HTTP/1.1 200 OK\r\n"
|
||||||
|
|||||||
Reference in new issue
Block a user