Current section

Files

Jump to
multipartkit src multipartkit@limit.erl
Raw

src/multipartkit@limit.erl

-module(multipartkit@limit).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/multipartkit/limit.gleam").
-export([default_limits/0, new/4, max_body_bytes/1, max_part_bytes/1, max_parts/1, max_header_bytes/1]).
-export_type([limits/0, limit_config_error/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-opaque limits() :: {limits, integer(), integer(), integer(), integer()}.
-type limit_config_error() :: {non_positive_limit, binary(), integer()}.
-file("src/multipartkit/limit.gleam", 36).
?DOC(
" Conservative defaults used by `parse` and `parse_stream` when no explicit\n"
" limits are supplied.\n"
).
-spec default_limits() -> limits().
default_limits() ->
{limits, 10000000, 5000000, 100, 16384}.
-file("src/multipartkit/limit.gleam", 84).
-spec check_positive(binary(), integer()) -> {ok, nil} |
{error, limit_config_error()}.
check_positive(Field, Value) ->
gleam@bool:guard(
Value < 1,
{error, {non_positive_limit, Field, Value}},
fun() -> {ok, nil} end
).
-file("src/multipartkit/limit.gleam", 66).
?DOC(
" Construct a `Limits` value with validation. Each field must be `>= 1`;\n"
" otherwise the returned `Error` names the first failing field.\n"
"\n"
" Use this in any path where the limit values come from configuration,\n"
" CLI flags, request input, or other dynamic input. For trusted\n"
" constants the direct `Limits(...)` constructor is also fine, but\n"
" callers that want a stable surface ahead of the `Limits` type being\n"
" closed (see the upcoming opaque-type work) should prefer `new`.\n"
"\n"
" Example:\n"
" import multipartkit/limit\n"
"\n"
" case limit.new(\n"
" max_body_bytes: 5_000_000,\n"
" max_part_bytes: 2_000_000,\n"
" max_parts: 50,\n"
" max_header_bytes: 8_192,\n"
" ) {\n"
" Ok(limits) -> ...\n"
" Error(limit.NonPositiveLimit(field: f, given: g)) -> ...\n"
" }\n"
).
-spec new(integer(), integer(), integer(), integer()) -> {ok, limits()} |
{error, limit_config_error()}.
new(Max_body_bytes, Max_part_bytes, Max_parts, Max_header_bytes) ->
gleam@result:'try'(
check_positive(<<"max_body_bytes"/utf8>>, Max_body_bytes),
fun(_) ->
gleam@result:'try'(
check_positive(<<"max_part_bytes"/utf8>>, Max_part_bytes),
fun(_) ->
gleam@result:'try'(
check_positive(<<"max_parts"/utf8>>, Max_parts),
fun(_) ->
gleam@result:'try'(
check_positive(
<<"max_header_bytes"/utf8>>,
Max_header_bytes
),
fun(_) ->
{ok,
{limits,
Max_body_bytes,
Max_part_bytes,
Max_parts,
Max_header_bytes}}
end
)
end
)
end
)
end
).
-file("src/multipartkit/limit.gleam", 93).
?DOC(" Total-input byte budget. See `Limits.max_body_bytes`.\n").
-spec max_body_bytes(limits()) -> integer().
max_body_bytes(Limits) ->
erlang:element(2, Limits).
-file("src/multipartkit/limit.gleam", 98).
?DOC(" Per-part body byte budget. See `Limits.max_part_bytes`.\n").
-spec max_part_bytes(limits()) -> integer().
max_part_bytes(Limits) ->
erlang:element(3, Limits).
-file("src/multipartkit/limit.gleam", 103).
?DOC(" Maximum number of parts. See `Limits.max_parts`.\n").
-spec max_parts(limits()) -> integer().
max_parts(Limits) ->
erlang:element(4, Limits).
-file("src/multipartkit/limit.gleam", 108).
?DOC(" Per-part header-block byte budget. See `Limits.max_header_bytes`.\n").
-spec max_header_bytes(limits()) -> integer().
max_header_bytes(Limits) ->
erlang:element(5, Limits).