From c438858d9290b9873e9d16303a4158d7a54a6193 Mon Sep 17 00:00:00 2001 From: Perfloop Agent Date: Fri, 4 Dec 2026 21:58:00 -0500 Subject: [PATCH] Leave ranged set_content responses uncompressed (#2709) A set_content() response to a request with Range plus Accept-Encoding was sliced before compression, leaving Content-Range describing uncompressed bytes. Serve ranged 206 responses from the identity representation, as static files already are, by deciding no content coding for a partial response. Close #2709 --- httplib.h | 5 +++-- test/test.cc | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 7cbafbd7..d8db1dca 100644 --- a/httplib.h +++ b/httplib.h @@ -16100,8 +16100,9 @@ inline void Server::apply_ranges(const Request &req, Response &res, "multipart/byteranges; boundary=" + boundary); } - auto type = - compress_ ? detail::encoding_type(req, res) : detail::EncodingType::None; + auto type = compress_ && !detail::is_partial_content(req, res) + ? detail::encoding_type(req, res) + : detail::EncodingType::None; if (res.body.empty()) { if (res.content_length_ > 0 && req.ranges.size() == 1 && diff --git a/test/test.cc b/test/test.cc index 2ef76738..c04991ea 100644 --- a/test/test.cc +++ b/test/test.cc @@ -9392,6 +9392,20 @@ TEST_F(ServerTest, GetWithRange5) { EXPECT_EQ(std::string("abcdef"), res->body); } +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +TEST_F(ServerTest, GetWithRangeAcceptEncoding) { + cli_.set_decompress(false); + auto res = cli_.Get("/with-range", Headers{{"Range", "bytes=1-3"}, + {"Accept-Encoding", "gzip"}}); + ASSERT_TRUE(res) << "Error: " << to_string(res.error()); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); + EXPECT_EQ("bytes 1-3/7", res->get_header_value("Content-Range")); + EXPECT_EQ("3", res->get_header_value("Content-Length")); + EXPECT_EQ("bcd", res->body); + EXPECT_FALSE(res->has_header("Content-Encoding")); +} +#endif + TEST_F(ServerTest, GetWithRangeOffsetGreaterThanContent) { auto res = cli_.Get("/with-range", Headers{{make_range_header({{10000, 20000}})}});