Decode every member of a gzip body (#2710)

RFC 1952 2.2: a gzip body is a series of members. gzip_decompressor
stopped at the end of the first one and ignored the rest, so a body
made of several members came back cut short with no error.

After a member ends, bytes that start with ID1 ID2 begin the next
member; anything else is still ignored, as before.
This commit is contained in:
Faris Basha TM authored and GitHub committed 2026-12-04 21:41:17 -05:00
1 parent 4e6226d7af
commit da4a0a6f33
2 files changed
+88

No files matched your search

+44
View File
@@ -9996,6 +9996,50 @@ TEST(GzipDecompressor, DeflateDecompressionTrailingBytes) {
ASSERT_EQ(original_text, decompressed_data);
}
TEST(GzipDecompressor, MultipleMembers) {
auto gzip = [](const std::string &data) {
std::string out;
httplib::detail::gzip_compressor compressor;
compressor.compress(data.data(), data.size(), /*last=*/true,
[&](const char *chunk, size_t n) {
out.append(chunk, n);
return true;
});
return out;
};
// Decompresses `data` handed over in two calls, the first `split` bytes long
auto decompress = [](const std::string &data, size_t split,
std::string &out) {
httplib::detail::gzip_decompressor decompressor;
auto callback = [&](const char *chunk, size_t n) {
out.append(chunk, n);
return true;
};
return decompressor.decompress(data.data(), split, callback) &&
decompressor.decompress(data.data() + split, data.size() - split,
callback);
};
// RFC 1952 2.2: a gzip body is a series of members, an empty one included.
// Bytes after the last member that start no other one are still ignored.
const auto members = gzip("Hello, ") + gzip("") + gzip("World!");
for (const auto &body : {members, members + std::string(8, '\0')}) {
for (size_t split = 0; split <= body.size(); split++) {
std::string out;
ASSERT_TRUE(decompress(body, split, out)) << "split at " << split;
EXPECT_EQ("Hello, World!", out) << "split at " << split;
}
}
// A broken later member fails like a broken first one
auto broken = gzip("Hello, ") + gzip("World!");
auto crc_pos = broken.size() - 8;
broken[crc_pos] = static_cast<char>(broken[crc_pos] ^ 0x01);
std::string out;
EXPECT_FALSE(decompress(broken, broken.size(), out));
}
#endif
#ifdef CPPHTTPLIB_BROTLI_SUPPORT