Repository navigation
Description
Goal
Add HTTP body compression for responses and decoding for requests. One codec on the wire in phase 1 (gzip), one engine (zlib-ng), shared across H1/H2/H3 since Content-Encoding is a representation-level mechanism (RFC 9110 §8.4) — independent of transport. HPACK/QPACK handle header compression in H2/H3 already and are out of scope.
Future codecs (Brotli, zstd) are deferred to phase 2; pre-compressed static assets to phase 3. The encoder API is shaped as a vtable from day one so phase 2 plugs in without touching the response pipeline.
Scope (phase 1)
- gzip response encoding via zlib-ng (fallback to system zlib if zlib-ng absent)
- gzip request body decoding with anti-bomb size cap
Accept-Encodingnegotiation: q-values,identity;q=0,*, 406 when unsatisfiable- Skip rules: status 1xx/204/304, HEAD, Range, already-encoded, body below threshold, MIME not in whitelist
Vary: Accept-Encodingon compressed responses;Content-Lengthdropped on streaming, kept on buffered small-body fast path- Per-response opt-out (
\$response->setNoCompression()) for BREACH-sensitive endpoints - Common path above H1 chunked / H2 DATA / H3 DATA — single implementation, three transports
HttpServerConfig knobs
All editable via setters on HttpServerConfig, frozen after HttpServer::__construct (matches existing setWs* pattern):
| Setter | Default | Validation |
|---|---|---|
setCompressionEnabled(bool) |
true |
— |
setCompressionLevel(int) |
6 |
1..9 |
setCompressionMinSize(int) |
1024 |
≥ 0 |
setCompressionMimeTypes(array) |
whitelist below | non-empty strings, lowercased + trimmed |
setRequestMaxDecompressedSize(int) |
10 MiB |
≥ 0 (0 = disabled) |
getCompressionMimeTypes() returns the materialized list so introspection reflects the actual policy.
Default MIME whitelist
Lives in a dedicated TU (src/http/compression/http_compression_defaults.c) exporting extern const char *const http_compression_default_mime_types[] (NULL-terminated, lowercase, sorted). Materialized into the config HashTable at init; setCompressionMimeTypes() replaces wholesale (nginx semantics).
```
application/javascript
application/json
application/xml
image/svg+xml
text/css
text/html
text/javascript
text/plain
text/xml
```
Build
--enable-http-compressioninconfig.m4, equivalent CMake flag- pkg-config probe:
zlib-ngpreferred,zlibfallback; print chosen engine in build banner - Compression module isolated under
src/http/compression/so phase-2 codec backends drop in alongside
Implementation plan (commits)
- build + skeleton — feature flag, pkg-config,
http_encoder_tvtable, gzip-backend stub. No call sites yet. - HttpServerConfig knobs — five setters above + freeze + validation; phpt for setter validation and immutability after bind.
- Accept-Encoding parser + negotiation — q-values, identity,
*, MIME match (param-stripped), skip heuristics; unit tests intests/unit/. - gzip encoder via zlib-ng (streaming) — vtable impl, window=15+16 (gzip wrap), correct teardown on early disconnect / write-error; standalone unit tests.
- Wire into response pipeline — single hook above H1 chunked / H2 DATA / H3 DATA;
Content-Encoding,Vary,Content-Lengthhandling; per-responsesetNoCompression()opt-out; phpt covering H1 + H2. - Request body decoding — transparent gzip decoding in request body pipeline, anti-bomb cap → 413, unknown coding → 415; phpt for golden path, bomb limit, unknown coding.
- Docs + CHANGELOG.
phpt coverage
Response (H1 + H2 where feasible):
- identity → not compressed
gzipin Accept-Encoding → compressed + Vary setgzip;q=0→ not compressed- below threshold → not compressed
- non-whitelist MIME (e.g.
image/png) → not compressed - HEAD → not compressed
- Range response → not compressed
setNoCompression()→ not compressed- handler already set
Content-Encoding→ no double-encode
Request:
- gzipped POST → handler sees decoded body
- decompressed size exceeds limit → 413
- unknown
Content-Encoding→ 415
H3 cross-version test gated on whether CI has an lsquic client harness; otherwise tracked as a follow-up.
Security
- BREACH mitigation policy:
setNoCompression()documented for endpoints reflecting user input next to secrets. - Anti-bomb cap on request decoding is mandatory (default 10 MiB); 0 = disabled (must be explicit).
- Whitelist semantics on MIME (not blacklist) — refuse to compress unknown content rather than risk corrupting binaries with mistaken MIME.
Out of scope (phase 2+)
- Brotli (libbrotli), zstd (libzstd) — same vtable, additional backends
- Threadpool offload for large bodies — measure first, optimize on evidence
- Pre-compressed static assets (
*.gz,*.bron disk served via sendfile) deflatecodec (historical raw-vs-zlib-wrapped ambiguity — skip)
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
feat(compression): build flag + encoder vtable skeleton (#8)
feat(compression): HttpServerConfig knobs + default MIME whitelist (#8)
feat(compression): Accept-Encoding parser, codec selector, MIME norma…
feat(compression): real gzip encoder via zlib-ng (streaming) (#8)
feat(compression): H1 response pipeline — buffered + streaming + opt-…
feat(compression): wire response pipeline for H2 + H3 (#8)
feat(compression): inbound request body decoding + cached conn->config (
perf(compression): tighten allocations + branch hints in hot path (#8)
docs(compression): CHANGELOG entry, COMPRESSION.md, README mention (#8)
feat(compression): Brotli + zstd backends (issue #9)
chore(static): yellow cleanups (#8/#9/#12/#13/#15)