Skip to main content

Module content_codec

Module content_codec 

Source
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:

  1. remove the content-length header — the server layer recomputes it from the final body; and
  2. remove content-encoding (if you left the body decoded) or set it to ContentEncoding::header_value of 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§

ContentEncoding
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. level is 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.