nhttp_compress (nhttp_lib v1.1.1)
View SourceHTTP compression utilities for nhttp.
Supports gzip and deflate content encoding for request/response bodies. Compression is configurable and can be disabled.
Configuration options:
compression=> boolean() (default: true)compression_level=> 1..9 (default: 6)compression_threshold=> non_neg_integer() (default: 1024)compress_mime_types=> [binary()] (default: text/*, application/json, etc.)
Summary
Functions
Compress data using the specified encoding. Returns {ok, CompressedData} or {error, Reason}.
Decompress data using the specified encoding. Caps inflated output at
16 MiB to defend against decompression bombs. Use decompress/3 to
override the cap and to read the rule on trailing input.
Decompress data with an explicit cap on the inflated output. Returns
{error, max_output_exceeded} if decoding would produce more than Max
bytes; pass infinity to disable the cap.
Data must hold exactly one complete compressed stream and nothing else.
Any byte after the end of that stream gives {error, trailing_data}. This
refuses a gzip file that holds more than one member, which RFC 1952
Section 2.2 permits. The rule is deliberate. A decoder that drops the bytes
after the first stream derives a different body from the same octets than a
decoder that reads them all.
identity applies Max to the input itself, because the input is the
output.
Return default MIME types eligible for compression.
Convert encoding atom to header value.
Negotiate encoding based on Accept-Encoding header. Returns the best encoding the client accepts that we support.
Check if response should be compressed using the default threshold
(?DEFAULT_COMPRESSION_THRESHOLD). Equivalent to
should_compress(ContentType, Size, MimeTypes, ?DEFAULT_COMPRESSION_THRESHOLD).
Check if response should be compressed. Returns true when body size meets the threshold and Content-Type matches the compressible MIME types.
Types
-type compress_opts() :: #{compression => boolean(), compression_level => 1..9, compression_threshold => non_neg_integer(), compress_mime_types => [binary()]}.
-type encoding() :: gzip | deflate | identity.
-type zlib_error() :: data_error | buf_error | stream_error | badarg | enomem | max_output_exceeded | trailing_data | {unknown, term()}.
Functions
-spec compress(Data :: iodata(), Encoding :: gzip | deflate, Level :: 1..9) -> {ok, binary()} | {error, zlib_error()}.
Compress data using the specified encoding. Returns {ok, CompressedData} or {error, Reason}.
-spec decompress(Data :: binary(), Encoding :: encoding()) -> {ok, binary()} | {error, zlib_error()}.
Decompress data using the specified encoding. Caps inflated output at
16 MiB to defend against decompression bombs. Use decompress/3 to
override the cap and to read the rule on trailing input.
-spec decompress(Data :: binary(), Encoding :: encoding(), Max) -> {ok, binary()} | {error, zlib_error()} when Max :: pos_integer() | infinity.
Decompress data with an explicit cap on the inflated output. Returns
{error, max_output_exceeded} if decoding would produce more than Max
bytes; pass infinity to disable the cap.
Data must hold exactly one complete compressed stream and nothing else.
Any byte after the end of that stream gives {error, trailing_data}. This
refuses a gzip file that holds more than one member, which RFC 1952
Section 2.2 permits. The rule is deliberate. A decoder that drops the bytes
after the first stream derives a different body from the same octets than a
decoder that reads them all.
identity applies Max to the input itself, because the input is the
output.
-spec default_mime_types() -> [binary()].
Return default MIME types eligible for compression.
Convert encoding atom to header value.
Negotiate encoding based on Accept-Encoding header. Returns the best encoding the client accepts that we support.
-spec should_compress(ContentType :: binary() | undefined, Size :: non_neg_integer(), MimeTypes :: [binary()]) -> boolean().
Check if response should be compressed using the default threshold
(?DEFAULT_COMPRESSION_THRESHOLD). Equivalent to
should_compress(ContentType, Size, MimeTypes, ?DEFAULT_COMPRESSION_THRESHOLD).
-spec should_compress(ContentType :: binary() | undefined, Size :: non_neg_integer(), MimeTypes :: [binary()], Threshold :: non_neg_integer()) -> boolean().
Check if response should be compressed. Returns true when body size meets the threshold and Content-Type matches the compressible MIME types.