aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_router.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2013-01-28 23:39:21 +0100
committerLoïc Hoguin <[email protected]>2013-01-28 23:39:21 +0100
commitc2c333de8add734c5046c7cf2bf97e9bf8799617 (patch)
tree0b315561d70c9f64c2ae8789826a6392281a6819 /src/cowboy_router.erl
parenta5a69353f1e5de41337d31943f70030261ee45dc (diff)
downloadcowboy-c2c333de8add734c5046c7cf2bf97e9bf8799617.tar.gz
cowboy-c2c333de8add734c5046c7cf2bf97e9bf8799617.tar.bz2
cowboy-c2c333de8add734c5046c7cf2bf97e9bf8799617.zip
Add the 'function' constraint
Diffstat (limited to 'src/cowboy_router.erl')
-rw-r--r--src/cowboy_router.erl15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/cowboy_router.erl b/src/cowboy_router.erl
index 7096bd3..f2d1127 100644
--- a/src/cowboy_router.erl
+++ b/src/cowboy_router.erl
@@ -33,7 +33,8 @@
-export_type([bindings/0]).
-export_type([tokens/0]).
--type constraints() :: [{atom(), int}].
+-type constraints() :: [{atom(), int}
+ | {atom(), function, fun ((binary()) -> true | {true, any()} | false)}].
-export_type([constraints/0]).
-type route_match() :: binary() | string().
@@ -297,7 +298,9 @@ check_constraints([Constraint|Tail], Bindings) ->
check_constraint({_, int}, Value) ->
try {true, list_to_integer(binary_to_list(Value))}
catch _:_ -> false
- end.
+ end;
+check_constraint({_, function, Fun}, Value) ->
+ Fun(Value).
%% @doc Split a hostname into a list of tokens.
-spec split_host(binary()) -> tokens().
@@ -524,6 +527,14 @@ match_constraints_test() ->
<<"ninenines.eu">>, <<"/path/123/">>),
{error, notfound, path} = match(Dispatch,
<<"ninenines.eu">>, <<"/path/NaN/">>),
+ Dispatch2 = [{'_', [],
+ [{[<<"path">>, username], [{username, function,
+ fun(Value) -> Value =:= cowboy_bstr:to_lower(Value) end}],
+ match, []}]}],
+ {ok, _, [], [{username, <<"essen">>}], _, _} = match(Dispatch2,
+ <<"ninenines.eu">>, <<"/path/essen">>),
+ {error, notfound, path} = match(Dispatch2,
+ <<"ninenines.eu">>, <<"/path/ESSEN">>),
ok.
-endif.