Skip to content

Static file responses are never compressed, even when the client accepts gzip #2545

Closed

Description

@yhirose

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:

  1. 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.
  2. 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.
  3. 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.
  4. Default or opt-in. Turning this on changes long-standing behavior. Nine existing tests assert the uncompressed Content-Length for 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

  1. added a commit that references this issue on Aug 27, 2026
    94f50ae
  2. added a commit that references this issue on Aug 30, 2026
    139f30e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions