mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-09 04:43:48 +00:00
Don't compress a response whose handler already set Content-Encoding (#2575)
* Don't compress a response whose handler already set Content-Encoding * Don't compress a pre-encoded response served from a file The guard that stands down when a response already names a content coding covered the responses that settle their coding in `apply_ranges()`, but a file-backed one settles it in `static_file_encoding()`, which asked the content-type overload and so never saw the field. With static file compression enabled, a mount point naming the coding for a tree of build-time compressed assets, and a handler setting the field on a `set_file_content()` response, both had their stored bytes compressed a second time and a second `Content-Encoding` field line appended. A file-backed response has not been given a content type by the time its coding is decided, which is the only reason it could not go through `encoding_type()`. It takes the type as an argument now, so both paths share the one guard instead of carrying a copy each. `Response::content_encoding_` becomes `content_coding_`, after what it holds. It names the coding chosen for the body, which is what its own comment already called it, while the old name read as the value of the `Content-Encoding` field whose presence is exactly what forces the coding to `None`. README gains the behaviour, including the part that stays with the handler: `Vary` is added only to a coding the server chose, so a handler that picks a representation from `Accept-Encoding` has to add the field itself. --------- Co-authored-by: yhirose <yuji.hirose.bug@gmail.com>
This commit is contained in:
13
README.md
13
README.md
@@ -1474,6 +1474,19 @@ The server can apply compression to the following MIME type contents:
|
||||
- application/protobuf
|
||||
- application/xhtml+xml
|
||||
|
||||
A response that already carries `Content-Encoding` is sent as it is. A handler serving content it encoded itself, an asset compressed at build time for instance, keeps its own coding and its own bytes:
|
||||
|
||||
```c++
|
||||
svr.Get("/app.js", [](const Request & /*req*/, Response &res) {
|
||||
res.set_header("Content-Encoding", "gzip");
|
||||
res.set_content(gzipped_asset, "application/javascript");
|
||||
});
|
||||
```
|
||||
|
||||
This holds for every kind of response, including the file-backed ones below.
|
||||
|
||||
`Vary: Accept-Encoding` is added only to responses the server encoded itself. A handler that chooses between an encoded and an identity representation by reading `Accept-Encoding` should set the field itself, so that shared caches keep the two apart.
|
||||
|
||||
### Static file compression
|
||||
|
||||
Responses served from a file, whether through `set_mount_point()` or `Response::set_file_content()`, are sent as is by default. Turn compression on for them with:
|
||||
|
||||
Reference in New Issue
Block a user