nhttp_compress (nhttp_lib v1.1.1)

View Source

HTTP 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

compress_opts()

-type compress_opts() ::
          #{compression => boolean(),
            compression_level => 1..9,
            compression_threshold => non_neg_integer(),
            compress_mime_types => [binary()]}.

encoding()

-type encoding() :: gzip | deflate | identity.

zlib_error()

-type zlib_error() ::
          data_error | buf_error | stream_error | badarg | enomem | max_output_exceeded |
          trailing_data |
          {unknown, term()}.

Functions

compress(Data, Encoding, Level)

-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}.

decompress(Data, Encoding)

-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.

decompress(Data, Encoding, Max)

-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.

default_mime_types()

-spec default_mime_types() -> [binary()].

Return default MIME types eligible for compression.

encoding_header/1

-spec encoding_header(encoding()) -> binary().

Convert encoding atom to header value.

negotiate_encoding(Headers)

-spec negotiate_encoding(Headers :: [{binary(), binary()}]) -> encoding().

Negotiate encoding based on Accept-Encoding header. Returns the best encoding the client accepts that we support.

should_compress(ContentType, Size, MimeTypes)

-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).

should_compress(ContentType, Size, MimeTypes, 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.