| 1 |
%% Copyright (c) Loïc Hoguin <[email protected]> |
| 2 |
%% |
| 3 |
%% Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
%% purpose with or without fee is hereby granted, provided that the above |
| 5 |
%% copyright notice and this permission notice appear in all copies. |
| 6 |
%% |
| 7 |
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 |
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 |
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 |
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 |
|
| 15 |
-module(cowboy_constraints). |
| 16 |
|
| 17 |
-export([validate/2]). |
| 18 |
-export([reverse/2]). |
| 19 |
-export([format_error/1]). |
| 20 |
-export([from_fun/1]). |
| 21 |
|
| 22 |
-type constraint() :: int | nonempty | fun(). |
| 23 |
-export_type([constraint/0]). |
| 24 |
|
| 25 |
-type reason() :: {constraint(), any(), any()}. |
| 26 |
-export_type([reason/0]). |
| 27 |
|
| 28 |
-spec validate(binary(), constraint() | [constraint()]) |
| 29 |
-> {ok, any()} | {error, reason()}. |
| 30 |
validate(Value, Constraints) when is_list(Constraints) -> |
| 31 |
apply_list(forward, Value, Constraints); |
| 32 |
validate(Value, Constraint) -> |
| 33 |
apply_list(forward, Value, [Constraint]). |
| 34 |
|
| 35 |
-spec reverse(any(), constraint() | [constraint()]) |
| 36 |
-> {ok, binary()} | {error, reason()}. |
| 37 |
reverse(Value, Constraints) when is_list(Constraints) -> |
| 38 |
apply_list(reverse, Value, Constraints); |
| 39 |
reverse(Value, Constraint) -> |
| 40 |
apply_list(reverse, Value, [Constraint]). |
| 41 |
|
| 42 |
-spec format_error(reason()) -> iodata(). |
| 43 |
format_error({Constraint, Reason, Value}) -> |
| 44 |
apply_constraint(format_error, {Reason, Value}, Constraint). |
| 45 |
|
| 46 |
apply_list(_, Value, []) -> |
| 47 |
{ok, Value}; |
| 48 |
apply_list(Type, Value0, [Constraint|Tail]) -> |
| 49 |
case apply_constraint(Type, Value0, Constraint) of |
| 50 |
{ok, Value} -> |
| 51 |
apply_list(Type, Value, Tail); |
| 52 |
{error, Reason} -> |
| 53 |
{error, {Constraint, Reason, Value0}} |
| 54 |
end. |
| 55 |
|
| 56 |
%% @todo {int, From, To}, etc. |
| 57 |
apply_constraint(Type, Value, int) -> |
| 58 |
int(Type, Value); |
| 59 |
apply_constraint(Type, Value, nonempty) -> |
| 60 |
nonempty(Type, Value); |
| 61 |
apply_constraint(Type, Value, F) when is_function(F, 2) -> |
| 62 |
F(Type, Value). |
| 63 |
|
| 64 |
%% Constraint functions. |
| 65 |
|
| 66 |
%% Same bound as cow_http_hd (?MAX_DIGITS): enough for 64-bit |
| 67 |
%% integers without allowing arbitrary-precision bignum DoS. |
| 68 |
-define(MAX_DIGITS, 17). |
| 69 |
|
| 70 |
int(forward, <<$+,R/bits>>) when byte_size(R) > ?MAX_DIGITS -> |
| 71 |
{error, limit_reached}; |
| 72 |
int(forward, <<$-,R/bits>>) when byte_size(R) > ?MAX_DIGITS -> |
| 73 |
{error, limit_reached}; |
| 74 |
int(forward, Value = <<C,_/bits>>) |
| 75 |
when C =/= $+, C =/= $-, byte_size(Value) > ?MAX_DIGITS -> |
| 76 |
{error, limit_reached}; |
| 77 |
int(forward, Value) -> |
| 78 |
try |
| 79 |
{ok, binary_to_integer(Value)} |
| 80 |
catch _:_ -> |
| 81 |
{error, not_an_integer} |
| 82 |
end; |
| 83 |
int(reverse, Value) -> |
| 84 |
try |
| 85 |
{ok, integer_to_binary(Value)} |
| 86 |
catch _:_ -> |
| 87 |
{error, not_an_integer} |
| 88 |
end; |
| 89 |
int(format_error, {not_an_integer, Value}) -> |
| 90 |
io_lib:format("The value ~p is not an integer.", [Value]); |
| 91 |
int(format_error, {limit_reached, _}) -> |
| 92 |
"The value is larger than the constraint is willing to accept.". |
| 93 |
|
| 94 |
nonempty(Type, <<>>) when Type =/= format_error -> |
| 95 |
{error, empty}; |
| 96 |
nonempty(Type, Value) when Type =/= format_error, is_binary(Value) -> |
| 97 |
{ok, Value}; |
| 98 |
nonempty(format_error, {empty, Value}) -> |
| 99 |
io_lib:format("The value ~p is empty.", [Value]). |
| 100 |
|
| 101 |
-ifdef(TEST). |
| 102 |
|
| 103 |
validate_test() -> |
| 104 |
F = fun(_, Value) -> |
| 105 |
try |
| 106 |
{ok, binary_to_atom(Value, latin1)} |
| 107 |
catch _:_ -> |
| 108 |
{error, not_a_binary} |
| 109 |
end |
| 110 |
end, |
| 111 |
%% Value, Constraints, Result. |
| 112 |
Tests = [ |
| 113 |
{<<>>, [], <<>>}, |
| 114 |
{<<"123">>, int, 123}, |
| 115 |
{<<"123">>, [int], 123}, |
| 116 |
{<<"123">>, [nonempty, int], 123}, |
| 117 |
{<<"123">>, [int, nonempty], 123}, |
| 118 |
{<<>>, nonempty, error}, |
| 119 |
{<<>>, [nonempty], error}, |
| 120 |
{<<"hello">>, F, hello}, |
| 121 |
{<<"hello">>, [F], hello}, |
| 122 |
{<<"123">>, [F, int], error}, |
| 123 |
{<<"123">>, [int, F], error}, |
| 124 |
{<<"hello">>, [nonempty, F], hello}, |
| 125 |
{<<"hello">>, [F, nonempty], hello} |
| 126 |
], |
| 127 |
[{lists:flatten(io_lib:format("~p, ~p", [V, C])), fun() -> |
| 128 |
case R of |
| 129 |
error -> {error, _} = validate(V, C); |
| 130 |
_ -> {ok, R} = validate(V, C) |
| 131 |
end |
| 132 |
end} || {V, C, R} <- Tests]. |
| 133 |
|
| 134 |
reverse_test() -> |
| 135 |
F = fun(_, Value) -> |
| 136 |
try |
| 137 |
{ok, atom_to_binary(Value, latin1)} |
| 138 |
catch _:_ -> |
| 139 |
{error, not_an_atom} |
| 140 |
end |
| 141 |
end, |
| 142 |
%% Value, Constraints, Result. |
| 143 |
Tests = [ |
| 144 |
{<<>>, [], <<>>}, |
| 145 |
{123, int, <<"123">>}, |
| 146 |
{123, [int], <<"123">>}, |
| 147 |
{123, [nonempty, int], <<"123">>}, |
| 148 |
{123, [int, nonempty], <<"123">>}, |
| 149 |
{<<>>, nonempty, error}, |
| 150 |
{<<>>, [nonempty], error}, |
| 151 |
{hello, F, <<"hello">>}, |
| 152 |
{hello, [F], <<"hello">>}, |
| 153 |
{123, [F, int], error}, |
| 154 |
{123, [int, F], error}, |
| 155 |
{hello, [nonempty, F], <<"hello">>}, |
| 156 |
{hello, [F, nonempty], <<"hello">>} |
| 157 |
], |
| 158 |
[{lists:flatten(io_lib:format("~p, ~p", [V, C])), fun() -> |
| 159 |
case R of |
| 160 |
error -> {error, _} = reverse(V, C); |
| 161 |
_ -> {ok, R} = reverse(V, C) |
| 162 |
end |
| 163 |
end} || {V, C, R} <- Tests]. |
| 164 |
|
| 165 |
int_format_error_test() -> |
| 166 |
{error, Reason} = validate(<<"string">>, int), |
| 167 |
Bin = iolist_to_binary(format_error(Reason)), |
| 168 |
true = is_binary(Bin), |
| 169 |
ok. |
| 170 |
|
| 171 |
int_max_digits_test() -> |
| 172 |
Max = list_to_binary(lists:duplicate(?MAX_DIGITS, $9)), |
| 173 |
Over = list_to_binary(lists:duplicate(?MAX_DIGITS + 1, $9)), |
| 174 |
NegMax = <<$-, Max/binary>>, |
| 175 |
NegOver = <<$-, Over/binary>>, |
| 176 |
{ok, 99999999999999999} = validate(Max, int), |
| 177 |
{error, {int, limit_reached, Over}} = validate(Over, int), |
| 178 |
{ok, -99999999999999999} = validate(NegMax, int), |
| 179 |
{error, {int, limit_reached, NegOver}} = validate(NegOver, int), |
| 180 |
%% Leading zeros count toward the digit budget. |
| 181 |
{ok, 9} = validate(<<"00000000000000009">>, int), |
| 182 |
{error, {int, limit_reached, <<"000000000000000009">>}} |
| 183 |
= validate(<<"000000000000000009">>, int), |
| 184 |
ok. |
| 185 |
|
| 186 |
nonempty_format_error_test() -> |
| 187 |
{error, Reason} = validate(<<>>, nonempty), |
| 188 |
Bin = iolist_to_binary(format_error(Reason)), |
| 189 |
true = is_binary(Bin), |
| 190 |
ok. |
| 191 |
|
| 192 |
fun_format_error_test() -> |
| 193 |
F = fun |
| 194 |
(format_error, {test, <<"value">>}) -> |
| 195 |
formatted; |
| 196 |
(_, _) -> |
| 197 |
{error, test} |
| 198 |
end, |
| 199 |
{error, Reason} = validate(<<"value">>, F), |
| 200 |
formatted = format_error(Reason), |
| 201 |
ok. |
| 202 |
|
| 203 |
-endif. |
| 204 |
|
| 205 |
%% Constraint generator. |
| 206 |
|
| 207 |
-spec from_fun(fun((any()) -> any())) -> fun(). |
| 208 |
|
| 209 |
from_fun(Fun) when is_function(Fun, 1) -> |
| 210 |
fun |
| 211 |
(forward, Value) -> |
| 212 |
try |
| 213 |
{ok, Fun(Value)} |
| 214 |
catch _:_ -> |
| 215 |
{error, not_valid} |
| 216 |
end; |
| 217 |
(reverse, _) -> |
| 218 |
{error, not_implemented}; |
| 219 |
(format_error, {not_valid, Value}) -> |
| 220 |
io_lib:format("The value ~0p is not valid.", [Value]); |
| 221 |
(format_error, {not_implemented, _}) -> |
| 222 |
<<"The reverse operation is not implemented.">> |
| 223 |
end. |
| 224 |
|
| 225 |
%% @todo Constraint generators with support for reverse |
| 226 |
%% or custom error formatting. |
| 227 |
|
| 228 |
-ifdef(TEST). |
| 229 |
|
| 230 |
from_fun_forward_test() -> |
| 231 |
Constraint = from_fun(fun cow_http:ensure_token/1), |
| 232 |
%% Value, Result. |
| 233 |
Tests = [ |
| 234 |
{<<>>, error}, |
| 235 |
{<<"value">>, <<"value">>}, |
| 236 |
{<<"some-weird-token^">>, <<"some-weird-token^">>}, |
| 237 |
{123, error}, |
| 238 |
{<<"invalid\r\ntoken">>, error} |
| 239 |
], |
| 240 |
[{lists:flatten(io_lib:format("~p", [V])), fun() -> |
| 241 |
case R of |
| 242 |
error -> {error, {Constraint, not_valid, V}} = validate(V, Constraint); |
| 243 |
_ -> {ok, R} = validate(V, Constraint) |
| 244 |
end |
| 245 |
end} || {V, R} <- Tests]. |
| 246 |
|
| 247 |
from_fun_reverse_test() -> |
| 248 |
Constraint = from_fun(fun cow_http:ensure_token/1), |
| 249 |
{error, {Constraint, not_implemented, <<"value">>}} |
| 250 |
= reverse(<<"value">>, [Constraint]), |
| 251 |
ok. |
| 252 |
|
| 253 |
from_fun_format_error_test() -> |
| 254 |
Constraint = from_fun(fun cow_http:ensure_token/1), |
| 255 |
[_|_] = format_error({Constraint, not_valid, <<"value">>}), |
| 256 |
<<_/bits>> = format_error({Constraint, not_implemented, <<"value">>}), |
| 257 |
ok. |
| 258 |
|
| 259 |
-endif. |