Expand description
Response body encode/decode helpers shared by body-mutating plugins (response-rewrite, gzip/brotli compression, loggers that include response bodies).
Mirrors APISIX’s content-decode.lua semantics: upstream bodies are
decoded before text filters run, then optionally re-encoded on the way
out. HTTP deflate is the zlib-wrapped format (RFC 1950), not raw
DEFLATE — this module follows that convention.
§Body-mutation convention for plugin authors
After replacing a response body you MUST:
- remove the
content-lengthheader — the server layer recomputes it from the final body; and - remove
content-encoding(if you left the body decoded) or set it toContentEncoding::header_valueof the codec you re-encoded with.
Getting either wrong yields truncated or garbled responses at the client:
a stale content-length truncates or over-reads the stream, and a stale
content-encoding makes the client “decompress” plain bytes.
Enums§
- Content
Encoding - Content-Encoding values the gateway can decode/encode.
Constants§
- BROTLI_
BUFFER_ 🔒SIZE - Buffer size used for brotli’s streaming reader/writer, matching the 4 KiB chunks flate2 uses internally.
- BROTLI_
LGWIN 🔒 - Brotli window size (log2). 22 is the codec’s common default and what nginx’s brotli module ships with.
Functions§
- decode
- Decodes a body compressed with
encoding. Errors on corrupt or truncated data, with a message naming the codec. - encode
- Encodes a body with
encoding.levelis clamped to the codec’s valid range (gzip/deflate 0-9, brotli 0-11), so out-of-range config values degrade to maximum compression instead of failing.