Compress static file responses behind an opt-in (Fix #2545) (#2572)

* Drop the claim that small bodies skip compression

There is no size threshold anywhere in the compression path.
encoding_type() gates on the content type and Accept-Encoding only, and
apply_ranges() compresses whatever body it is given, so a two-byte
text/plain response comes back gzipped at 22 bytes.

Say what actually happens and leave the decision to the handler.

* Compress static file responses behind an opt-in (Fix #2545)

apply_ranges() runs the compressor inside the branch it takes when
res.body is non-empty. A response served from a file leaves res.body
empty and sets content_length_, so it took the other branch, which
writes Content-Length and returns; encoding_type() was computed before
the split and never consulted on that side. The same bytes handed to
set_content() came back gzipped, which left set_mount_point() and
Response::set_file_content() as the one path that missed out.

Add Server::set_static_file_compression(), off by default so nothing
about an existing server changes. When it is on, the file-backed
provider is run through the compressor into res.body ahead of the rest
of apply_ranges(), so the response is framed the way set_content()
already frames one: it keeps its Content-Length, and HEAD still reports
the size a GET would return.

Ranges are answered from the identity representation, since RFC 9110
applies Range after content coding and slicing a compressed body would
mean compressing the whole file first. The ETag carries the coding it
belongs to, so a client that cached the compressed form revalidates
against its own validator rather than the identity one. Both the ETag
and the body take their coding from static_file_encoding(), so the two
cannot disagree.

Providers registered with set_content_provider() are left alone. zlib
buffers until its window fills, so running one through a compressor
would hold back writes that a caller expects to reach the peer as they
are produced.

The compressed bytes stay in memory until the response has been
written, so the peak cost scales with requests in flight.
set_static_file_compression_max_length() bounds it, defaulting to 4MB.

* Add a minimum size for static file compression

Compressing a file that already fits in a single 1500-byte MTU does not
get it to the client any sooner, and a file of a few bytes comes back
larger than it went in once gzip's header and trailer are added. Every
other server draws this line: nginx's gzip_min_length, Caddy's
minimum_length, IIS's minFileSizeForComp, CloudFront's 1000-byte floor.

The note this replaces told callers to decide in the handler. A response
served through set_mount_point() has no handler to decide in, so the
floor has to live in the server. It defaults to 1400 bytes, the size
that fits inside one MTU with room for headers.

set_static_file_compression_min_length() moves it, and
CPPHTTPLIB_STATIC_FILE_COMPRESSION_MIN_LENGTH sets the default at
compile time. The empty-file case keeps its own early-out so that a zero
floor still cannot turn an empty body into a 20-byte gzip stream.

The two bounds now read as a pair, so the documentation says what each
one is for: the lower bound is about what is worth compressing, the
upper bound about what one request is allowed to cost.

Every file under test/www except 1MB.txt is below the default floor, so
the tests that need a small file compressed lower it explicitly.
This commit is contained in:
yhirose authored and GitHub committed 2026-08-27 17:19:43 -04:00
1 parent b4ec1bb1de
commit 139f30e0f1
5 files changed
+637 -24

No files matched your search

+321
View File
@@ -8858,6 +8858,327 @@ TEST_F(ServerTest, MultipartFormDataGzip) {
}
#endif
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
// Static file compression is opt-in, so these run their own server rather than
// flipping it on for the shared ServerTest fixture, which would silently
// rewrite what every other static file test is asserting.
class StaticFileCompressionTest : public ::testing::Test {
protected:
void TearDown() override {
if (t_.joinable()) {
svr_.stop();
t_.join();
}
}
int start(const std::function<void(Server &)> &configure = nullptr) {
svr_.set_mount_point("/", "./www");
svr_.Get("/file_content", [](const Request & /*req*/, Response &res) {
res.set_file_content("./www/dir/index.html", "text/html");
});
svr_.Get("/streamed", [](const Request & /*req*/, Response &res) {
res.set_content_provider(
6, "text/plain",
[](size_t offset, size_t /*length*/, DataSink &sink) {
sink.write(offset < 3 ? "aaa" : "bbb", 3);
return true;
});
});
svr_.Get("/slow-stream", [](const Request & /*req*/, Response &res) {
res.set_content_provider(
1000, "text/plain",
[](size_t offset, size_t /*length*/, DataSink &sink) {
if (offset < 100) {
std::string data(100, 'A');
sink.write(data.data(), data.size());
return true;
}
std::this_thread::sleep_for(std::chrono::seconds(2));
std::string data(900, 'B');
sink.write(data.data(), data.size());
return true;
});
});
if (configure) { configure(svr_); }
auto port = svr_.bind_to_any_port(HOST);
t_ = std::thread([&]() { svr_.listen_after_bind(); });
svr_.wait_until_ready();
return port;
}
static void enable(Server &svr) { svr.set_static_file_compression(true); }
// Every file under ./www except 1MB.txt is smaller than the default
// 1400-byte floor, so a test that needs a small file compressed has to lower
// it out of the way.
static void enable_without_floor(Server &svr) {
svr.set_static_file_compression(true);
svr.set_static_file_compression_min_length(0);
}
Server svr_;
std::thread t_;
};
TEST_F(StaticFileCompressionTest, DisabledByDefault) {
auto port = start();
Client cli(HOST, port);
auto res = cli.Get("/dir/1MB.txt", Headers{{"Accept-Encoding", "gzip"}});
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status);
EXPECT_FALSE(res->has_header("Content-Encoding"));
EXPECT_EQ("1048576", res->get_header_value("Content-Length"));
EXPECT_EQ(1048576U, res->body.size());
}
TEST_F(StaticFileCompressionTest, MountPoint) {
auto port = start(enable);
Client cli(HOST, port);
cli.set_decompress(false);
auto res = cli.Get("/dir/1MB.txt", Headers{{"Accept-Encoding", "gzip"}});
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status);
EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
EXPECT_EQ("Accept-Encoding", res->get_header_value("Vary"));
// The response stays self-delimiting: a length, not a chunked body.
EXPECT_FALSE(res->has_header("Transfer-Encoding"));
EXPECT_EQ(std::to_string(res->body.size()),
res->get_header_value("Content-Length"));
EXPECT_LT(res->body.size(), 1048576U);
EXPECT_FALSE(res->body.empty());
}
TEST_F(StaticFileCompressionTest, MountPointDecompressed) {
auto port = start(enable);
Client cli(HOST, port);
auto res = cli.Get("/dir/1MB.txt", Headers{{"Accept-Encoding", "gzip"}});
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status);
EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
EXPECT_EQ(1048576U, res->body.size());
}
TEST_F(StaticFileCompressionTest, FileContent) {
auto port = start(enable_without_floor);
Client cli(HOST, port);
auto res = cli.Get("/file_content", Headers{{"Accept-Encoding", "gzip"}});
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status);
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
EXPECT_EQ(104U, res->body.size());
}
TEST_F(StaticFileCompressionTest, Head) {
auto port = start(enable);
Client cli(HOST, port);
cli.set_decompress(false);
auto get = cli.Get("/dir/1MB.txt", Headers{{"Accept-Encoding", "gzip"}});
ASSERT_TRUE(get) << "Error: " << to_string(get.error());
auto res = cli.Head("/dir/1MB.txt", Headers{{"Accept-Encoding", "gzip"}});
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status);
EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
EXPECT_FALSE(res->has_header("Transfer-Encoding"));
// HEAD still reports the size a GET would return.
EXPECT_EQ(get->get_header_value("Content-Length"),
res->get_header_value("Content-Length"));
EXPECT_TRUE(res->body.empty());
}
TEST_F(StaticFileCompressionTest, RangeStaysIdentity) {
auto port = start(enable);
Client cli(HOST, port);
auto res = cli.Get("/dir/test.abcde", Headers{make_range_header({{2, 3}}),
{"Accept-Encoding", "gzip"}});
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_FALSE(res->has_header("Content-Encoding"));
EXPECT_EQ("2", res->get_header_value("Content-Length"));
EXPECT_EQ("bytes 2-3/5", res->get_header_value("Content-Range"));
EXPECT_EQ("cd", res->body);
}
TEST_F(StaticFileCompressionTest, NotCompressibleType) {
auto port = start(enable);
Client cli(HOST, port);
auto res = cli.Get("/file", Headers{{"Accept-Encoding", "gzip"}});
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status);
EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
EXPECT_FALSE(res->has_header("Content-Encoding"));
EXPECT_EQ("5", res->get_header_value("Content-Length"));
}
TEST_F(StaticFileCompressionTest, EmptyFile) {
auto port = start(enable);
Client cli(HOST, port);
auto res = cli.Get("/empty_file", Headers{{"Accept-Encoding", "gzip"}});
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status);
EXPECT_FALSE(res->has_header("Content-Encoding"));
EXPECT_EQ("0", res->get_header_value("Content-Length"));
EXPECT_TRUE(res->body.empty());
}
TEST_F(StaticFileCompressionTest, MinLength) {
auto port = start(enable);
Client cli(HOST, port);
// 104 bytes, well under the default floor. Compressing it would add the
// gzip header and trailer to a body that already fits in one packet.
auto res = cli.Get("/dir/index.html", Headers{{"Accept-Encoding", "gzip"}});
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status);
EXPECT_FALSE(res->has_header("Content-Encoding"));
EXPECT_EQ("104", res->get_header_value("Content-Length"));
}
TEST_F(StaticFileCompressionTest, MinLengthLowered) {
auto port = start([](Server &svr) {
svr.set_static_file_compression(true);
svr.set_static_file_compression_min_length(1);
});
Client cli(HOST, port);
cli.set_decompress(false);
auto res = cli.Get("/dir/test.html", Headers{{"Accept-Encoding", "gzip"}});
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
// A 9-byte file comes back larger than it went in, because gzip's header and
// trailer outweigh anything deflate can save. That is the end of the range
// the floor exists to keep out.
EXPECT_GT(res->body.size(), 9U);
}
TEST_F(StaticFileCompressionTest, MaxLength) {
auto port = start([](Server &svr) {
svr.set_static_file_compression(true);
svr.set_static_file_compression_min_length(0);
svr.set_static_file_compression_max_length(1024);
});
Client cli(HOST, port);
// Over the limit: served as is. Not named `big`/`small`: <rpcndr.h>
// defines `small` as a macro on Windows.
auto over = cli.Get("/dir/1MB.txt", Headers{{"Accept-Encoding", "gzip"}});
ASSERT_TRUE(over) << "Error: " << to_string(over.error());
EXPECT_FALSE(over->has_header("Content-Encoding"));
EXPECT_EQ("1048576", over->get_header_value("Content-Length"));
// Under it: compressed.
auto under = cli.Get("/dir/index.html", Headers{{"Accept-Encoding", "gzip"}});
ASSERT_TRUE(under) << "Error: " << to_string(under.error());
EXPECT_EQ("gzip", under->get_header_value("Content-Encoding"));
}
TEST_F(StaticFileCompressionTest, EtagPerEncoding) {
auto port = start(enable_without_floor);
Client cli(HOST, port);
auto identity = cli.Get("/dir/index.html", Headers{{"Accept-Encoding", ""}});
ASSERT_TRUE(identity) << "Error: " << to_string(identity.error());
EXPECT_FALSE(identity->has_header("Content-Encoding"));
auto identity_etag = identity->get_header_value("ETag");
ASSERT_FALSE(identity_etag.empty());
auto gzipped =
cli.Get("/dir/index.html", Headers{{"Accept-Encoding", "gzip"}});
ASSERT_TRUE(gzipped) << "Error: " << to_string(gzipped.error());
EXPECT_EQ("gzip", gzipped->get_header_value("Content-Encoding"));
auto gzip_etag = gzipped->get_header_value("ETag");
ASSERT_FALSE(gzip_etag.empty());
// Two representations, two validators.
EXPECT_NE(identity_etag, gzip_etag);
// Each one revalidates against the request that would produce it...
auto fresh =
cli.Get("/dir/index.html", Headers{{"Accept-Encoding", "gzip"},
{"If-None-Match", gzip_etag}});
ASSERT_TRUE(fresh) << "Error: " << to_string(fresh.error());
EXPECT_EQ(StatusCode::NotModified_304, fresh->status);
auto fresh_identity =
cli.Get("/dir/index.html", Headers{{"Accept-Encoding", ""},
{"If-None-Match", identity_etag}});
ASSERT_TRUE(fresh_identity) << "Error: " << to_string(fresh_identity.error());
EXPECT_EQ(StatusCode::NotModified_304, fresh_identity->status);
// ...and not against the other representation.
auto crossed =
cli.Get("/dir/index.html",
Headers{{"Accept-Encoding", ""}, {"If-None-Match", gzip_etag}});
ASSERT_TRUE(crossed) << "Error: " << to_string(crossed.error());
EXPECT_EQ(StatusCode::OK_200, crossed->status);
}
// The opt-in covers responses the server reads off disk itself. A caller's own
// known-length provider keeps its Content-Length and, more importantly, keeps
// delivering incrementally: routing it through a compressor would hold every
// write back until zlib's window filled.
TEST_F(StaticFileCompressionTest, KnownLengthProviderUnaffected) {
auto port = start(enable);
Client cli(HOST, port);
auto res = cli.Get("/streamed", Headers{{"Accept-Encoding", "gzip"}});
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status);
EXPECT_FALSE(res->has_header("Content-Encoding"));
EXPECT_EQ("6", res->get_header_value("Content-Length"));
EXPECT_EQ("aaabbb", res->body);
}
TEST_F(StaticFileCompressionTest, IncrementalProviderUnaffected) {
auto port = start(enable);
Client cli(HOST, port);
cli.set_read_timeout(1, 0);
auto handle = cli.open_stream("GET", "/slow-stream", Params{},
Headers{{"Accept-Encoding", "gzip"}});
ASSERT_TRUE(handle.is_valid());
// The provider writes 100 bytes and then stalls for longer than the read
// timeout. Those first bytes have to arrive anyway: run the response through
// a compressor and zlib holds them until its window fills, so the read would
// come back empty instead.
char buf[256];
auto n = handle.read(buf, sizeof(buf));
ASSERT_GT(n, 0) << "first read should return the bytes already written";
EXPECT_EQ(std::string(100, 'A'), std::string(buf, static_cast<size_t>(n)));
}
#endif
#ifdef CPPHTTPLIB_BROTLI_SUPPORT
TEST_F(ServerTest, Brotli) {
Headers headers;