nhttp_compress (nhttp_lib v1.0.0)
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
?DEFAULT_MAX_DECOMPRESSED_SIZE (16 MiB) to defend against decompression
bombs. Use decompress/3 to override.
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.
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 | {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
?DEFAULT_MAX_DECOMPRESSED_SIZE (16 MiB) to defend against decompression
bombs. Use decompress/3 to override.
-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.
-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.