Repository navigation
Description
Summary
A response whose body comes from a known-length content source is never compressed, even when the client sends Accept-Encoding: gzip and the content type is one we already classify as compressible. That covers set_content_provider(length, ...) and, through it, set_mount_point() and Response::set_file_content().
The same bytes served through set_content() are compressed. So static file serving, which is where compression pays off most, is the one path that does not get it.
Measurements
0.53.1, built with CPPHTTPLIB_ZLIB_SUPPORT, client sending Accept-Encoding: gzip.
| request | content source | Content-Encoding | bytes on wire |
|---|---|---|---|
/static/index.html |
set_mount_point() |
none | 31,868 |
/static/style.css |
set_mount_point() |
none | 25,690 |
/static/app.js |
set_mount_point() |
none | 24,080 |
/static/data.json |
set_mount_point() |
none | 16,981 |
/fc (the same index.html) |
set_file_content() |
none | 31,868 |
/inline (the same index.html) |
set_content() |
gzip | 2,581 |
The last two rows serve byte-identical content. Compression ratios for the rest (gzip -9): style.css 27.6x, app.js 14.5x, data.json 8.5x.
Which sources compress today
| content source | compressed |
|---|---|
set_content() (owned body) |
yes |
set_chunked_content_provider() |
yes |
set_content_provider(length, ...) |
no |
The inconsistency is not obvious from the API, and the workaround is to give up mmap and read the file into a std::string so it lands in res.body.
Cause
In Server::apply_ranges(), the compression block sits inside the branch taken when res.body is non-empty. A known-length provider has an empty res.body and a non-zero content_length_, so it takes the other branch, which only sets Content-Length and returns. detail::encoding_type() is computed before the split but is never consulted on that side.
Notes for a fix
A few things to decide before changing anything:
- Range requests. RFC 9110 applies Range to the representation after content coding, so a compressed 206 means compressing the whole file and then slicing it. Serving ranges uncompressed is the usual way out.
- Cost per request. Compressing the same file on every hit is wasteful. Pre-compressed sidecar files (nginx
gzip_static) or an in-memory cache are the usual answers, but both are a bigger feature than flipping the branch. - Zero-copy. Compressing an mmap'd file gives up the zero-copy path.
can_compress_content_type()already returns false for images, video, and archives, so only text-ish files would pay it. - Default or opt-in. Turning this on changes long-standing behavior. Nine existing tests assert the uncompressed
Content-Lengthfor static responses:GetFileContent,GetFileContentWithRange,GetFileContentWithContentType,HeadMethod200Static,StaticFileRange,StaticFileRangeHead,StaticFileRangeBigFile,StaticFileRangeBigFile2,StaticFileBigFile.
I confirmed (4) by making the known-length branch compress: those nine fail, and nothing else does.
Activity
Compress static file responses behind an opt-in (Fix #2545)
Compress static file responses behind an opt-in (Fix yhirose#2545) (y…