123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- %% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.eu>
- %% Copyright (c) 2011, Anthony Ramine <nox@dev-extend.eu>
- %%
- %% Permission to use, copy, modify, and/or distribute this software for any
- %% purpose with or without fee is hereby granted, provided that the above
- %% copyright notice and this permission notice appear in all copies.
- %%
- %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- -module(cowboy_dispatcher).
- -export([split_host/1, split_path/1, match/3]). %% API.
- -type bindings() :: list({atom(), binary()}).
- -type path_tokens() :: list(binary()).
- -type match_rule() :: '_' | '*' | list(binary() | '_' | atom()).
- -type dispatch_path() :: list({match_rule(), module(), any()}).
- -type dispatch_rule() :: {Host::match_rule(), Path::dispatch_path()}.
- -type dispatch_rules() :: list(dispatch_rule()).
- -export_type([bindings/0, path_tokens/0, dispatch_rules/0]).
- -include_lib("eunit/include/eunit.hrl").
- %% API.
- -spec split_host(binary())
- -> {path_tokens(), binary(), undefined | inet:ip_port()}.
- split_host(<<>>) ->
- {[], <<>>, undefined};
- split_host(Host) ->
- case binary:split(Host, <<":">>) of
- [Host] ->
- {binary:split(Host, <<".">>, [global, trim]), Host, undefined};
- [Host2, Port] ->
- {binary:split(Host2, <<".">>, [global, trim]), Host2,
- list_to_integer(binary_to_list(Port))}
- end.
- -spec split_path(binary()) -> {path_tokens(), binary(), binary()}.
- split_path(Path) ->
- case binary:split(Path, <<"?">>) of
- [Path] -> {do_split_path(Path, <<"/">>), Path, <<>>};
- [<<>>, Qs] -> {[], <<>>, Qs};
- [Path2, Qs] -> {do_split_path(Path2, <<"/">>), Path2, Qs}
- end.
- -spec do_split_path(binary(), <<_:8>>) -> path_tokens().
- do_split_path(RawPath, Separator) ->
- case binary:split(RawPath, Separator, [global, trim]) of
- [<<>>|Path] -> Path;
- Path -> Path
- end.
- -spec match(Host::path_tokens(), Path::path_tokens(), dispatch_rules())
- -> {ok, module(), any(), bindings(),
- HostInfo::undefined | path_tokens(),
- PathInfo::undefined | path_tokens()}
- | {error, notfound, host} | {error, notfound, path}.
- match(_Host, _Path, []) ->
- {error, notfound, host};
- match(_Host, Path, [{'_', PathMatchs}|_Tail]) ->
- match_path(Path, PathMatchs, [], undefined);
- match(Host, Path, [{HostMatch, PathMatchs}|Tail]) ->
- case try_match(host, Host, HostMatch) of
- false ->
- match(Host, Path, Tail);
- {true, HostBinds, undefined} ->
- match_path(Path, PathMatchs, HostBinds, undefined);
- {true, HostBinds, HostInfo} ->
- match_path(Path, PathMatchs, HostBinds, lists:reverse(HostInfo))
- end.
- -spec match_path(path_tokens(), dispatch_path(), bindings(),
- HostInfo::undefined | path_tokens())
- -> {ok, module(), any(), bindings(),
- HostInfo::undefined | path_tokens(),
- PathInfo::undefined | path_tokens()}
- | {error, notfound, path}.
- match_path(_Path, [], _HostBinds, _HostInfo) ->
- {error, notfound, path};
- match_path(_Path, [{'_', Handler, Opts}|_Tail], HostBinds, HostInfo) ->
- {ok, Handler, Opts, HostBinds, HostInfo, undefined};
- match_path('*', [{'*', Handler, Opts}|_Tail], HostBinds, HostInfo) ->
- {ok, Handler, Opts, HostBinds, HostInfo, undefined};
- match_path(Path, [{PathMatch, Handler, Opts}|Tail], HostBinds, HostInfo) ->
- case try_match(path, Path, PathMatch) of
- false ->
- match_path(Path, Tail, HostBinds, HostInfo);
- {true, PathBinds, PathInfo} ->
- {ok, Handler, Opts, HostBinds ++ PathBinds, HostInfo, PathInfo}
- end.
- %% Internal.
- -spec try_match(host | path, path_tokens(), match_rule())
- -> {true, bindings(), undefined | path_tokens()} | false.
- try_match(host, List, Match) ->
- list_match(lists:reverse(List), lists:reverse(Match), []);
- try_match(path, List, Match) ->
- list_match(List, Match, []).
- -spec list_match(path_tokens(), match_rule(), bindings())
- -> {true, bindings(), undefined | path_tokens()} | false.
- %% Atom '...' matches any trailing path, stop right now.
- list_match(List, ['...'], Binds) ->
- {true, Binds, List};
- %% Atom '_' matches anything, continue.
- list_match([_E|Tail], ['_'|TailMatch], Binds) ->
- list_match(Tail, TailMatch, Binds);
- %% Both values match, continue.
- list_match([E|Tail], [E|TailMatch], Binds) ->
- list_match(Tail, TailMatch, Binds);
- %% Bind E to the variable name V and continue.
- list_match([E|Tail], [V|TailMatch], Binds) when is_atom(V) ->
- list_match(Tail, TailMatch, [{V, E}|Binds]);
- %% Match complete.
- list_match([], [], Binds) ->
- {true, Binds, undefined};
- %% Values don't match, stop.
- list_match(_List, _Match, _Binds) ->
- false.
- %% Tests.
- -ifdef(TEST).
- split_host_test_() ->
- %% {Host, Result}
- Tests = [
- {<<"">>, {[], <<"">>, undefined}},
- {<<".........">>, {[], <<".........">>, undefined}},
- {<<"*">>, {[<<"*">>], <<"*">>, undefined}},
- {<<"cowboy.dev-extend.eu">>,
- {[<<"cowboy">>, <<"dev-extend">>, <<"eu">>],
- <<"cowboy.dev-extend.eu">>, undefined}},
- {<<"dev-extend..eu">>,
- {[<<"dev-extend">>, <<>>, <<"eu">>],
- <<"dev-extend..eu">>, undefined}},
- {<<"dev-extend.eu">>,
- {[<<"dev-extend">>, <<"eu">>], <<"dev-extend.eu">>, undefined}},
- {<<"dev-extend.eu:8080">>,
- {[<<"dev-extend">>, <<"eu">>], <<"dev-extend.eu">>, 8080}},
- {<<"a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z">>,
- {[<<"a">>, <<"b">>, <<"c">>, <<"d">>, <<"e">>, <<"f">>, <<"g">>,
- <<"h">>, <<"i">>, <<"j">>, <<"k">>, <<"l">>, <<"m">>, <<"n">>,
- <<"o">>, <<"p">>, <<"q">>, <<"r">>, <<"s">>, <<"t">>, <<"u">>,
- <<"v">>, <<"w">>, <<"x">>, <<"y">>, <<"z">>],
- <<"a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z">>,
- undefined}}
- ],
- [{H, fun() -> R = split_host(H) end} || {H, R} <- Tests].
- split_host_fail_test_() ->
- Tests = [
- <<"dev-extend.eu:owns">>,
- <<"dev-extend.eu: owns">>,
- <<"dev-extend.eu:42fun">>,
- <<"dev-extend.eu: 42fun">>,
- <<"dev-extend.eu:42 fun">>,
- <<"dev-extend.eu:fun 42">>,
- <<"dev-extend.eu: 42">>,
- <<":owns">>,
- <<":42 fun">>
- ],
- [{H, fun() -> case catch split_host(H) of
- {'EXIT', _Reason} -> ok
- end end} || H <- Tests].
- split_path_test_() ->
- %% {Path, Result, QueryString}
- Tests = [
- {<<"?">>, [], <<"">>, <<"">>},
- {<<"???">>, [], <<"">>, <<"??">>},
- {<<"/">>, [], <<"/">>, <<"">>},
- {<<"/users">>, [<<"users">>], <<"/users">>, <<"">>},
- {<<"/users?">>, [<<"users">>], <<"/users">>, <<"">>},
- {<<"/users?a">>, [<<"users">>], <<"/users">>, <<"a">>},
- {<<"/users/42/friends?a=b&c=d&e=notsure?whatever">>,
- [<<"users">>, <<"42">>, <<"friends">>],
- <<"/users/42/friends">>, <<"a=b&c=d&e=notsure?whatever">>}
- ],
- [{P, fun() -> {R, RawP, Qs} = split_path(P) end}
- || {P, R, RawP, Qs} <- Tests].
- match_test_() ->
- Dispatch = [
- {[<<"www">>, '_', <<"dev-extend">>, <<"eu">>], [
- {[<<"users">>, '_', <<"mails">>], match_any_subdomain_users, []}
- ]},
- {[<<"dev-extend">>, <<"eu">>], [
- {[<<"users">>, id, <<"friends">>], match_extend_users_friends, []},
- {'_', match_extend, []}
- ]},
- {[<<"dev-extend">>, var], [
- {[<<"threads">>, var], match_duplicate_vars,
- [we, {expect, two}, var, here]}
- ]},
- {[<<"erlang">>, ext], [
- {'_', match_erlang_ext, []}
- ]},
- {'_', [
- {[<<"users">>, id, <<"friends">>], match_users_friends, []},
- {'_', match_any, []}
- ]}
- ],
- %% {Host, Path, Result}
- Tests = [
- {[<<"any">>], [], {ok, match_any, [], []}},
- {[<<"www">>, <<"any">>, <<"dev-extend">>, <<"eu">>],
- [<<"users">>, <<"42">>, <<"mails">>],
- {ok, match_any_subdomain_users, [], []}},
- {[<<"www">>, <<"dev-extend">>, <<"eu">>],
- [<<"users">>, <<"42">>, <<"mails">>], {ok, match_any, [], []}},
- {[<<"www">>, <<"dev-extend">>, <<"eu">>], [], {ok, match_any, [], []}},
- {[<<"www">>, <<"any">>, <<"dev-extend">>, <<"eu">>],
- [<<"not_users">>, <<"42">>, <<"mails">>], {error, notfound, path}},
- {[<<"dev-extend">>, <<"eu">>], [], {ok, match_extend, [], []}},
- {[<<"dev-extend">>, <<"eu">>], [<<"users">>, <<"42">>, <<"friends">>],
- {ok, match_extend_users_friends, [], [{id, <<"42">>}]}},
- {[<<"erlang">>, <<"fr">>], '_',
- {ok, match_erlang_ext, [], [{ext, <<"fr">>}]}},
- {[<<"any">>], [<<"users">>, <<"444">>, <<"friends">>],
- {ok, match_users_friends, [], [{id, <<"444">>}]}},
- {[<<"dev-extend">>, <<"fr">>], [<<"threads">>, <<"987">>],
- {ok, match_duplicate_vars, [we, {expect, two}, var, here],
- [{var, <<"fr">>}, {var, <<"987">>}]}}
- ],
- [{lists:flatten(io_lib:format("~p, ~p", [H, P])), fun() ->
- {ok, Handler, Opts, Binds, undefined, undefined} = match(H, P, Dispatch)
- end} || {H, P, {ok, Handler, Opts, Binds}} <- Tests].
- match_info_test_() ->
- Dispatch = [
- {[<<"www">>, <<"dev-extend">>, <<"eu">>], [
- {[<<"pathinfo">>, <<"is">>, <<"next">>, '...'], match_path, []}
- ]},
- {['...', <<"dev-extend">>, <<"eu">>], [
- {'_', match_any, []}
- ]}
- ],
- Tests = [
- {[<<"dev-extend">>, <<"eu">>], [],
- {ok, match_any, [], [], [], undefined}},
- {[<<"bugs">>, <<"dev-extend">>, <<"eu">>], [],
- {ok, match_any, [], [], [<<"bugs">>], undefined}},
- {[<<"cowboy">>, <<"bugs">>, <<"dev-extend">>, <<"eu">>], [],
- {ok, match_any, [], [], [<<"cowboy">>, <<"bugs">>], undefined}},
- {[<<"www">>, <<"dev-extend">>, <<"eu">>],
- [<<"pathinfo">>, <<"is">>, <<"next">>],
- {ok, match_path, [], [], undefined, []}},
- {[<<"www">>, <<"dev-extend">>, <<"eu">>],
- [<<"pathinfo">>, <<"is">>, <<"next">>, <<"path_info">>],
- {ok, match_path, [], [], undefined, [<<"path_info">>]}},
- {[<<"www">>, <<"dev-extend">>, <<"eu">>],
- [<<"pathinfo">>, <<"is">>, <<"next">>, <<"foo">>, <<"bar">>],
- {ok, match_path, [], [], undefined, [<<"foo">>, <<"bar">>]}}
- ],
- [{lists:flatten(io_lib:format("~p, ~p", [H, P])), fun() ->
- R = match(H, P, Dispatch)
- end} || {H, P, R} <- Tests].
- -endif.
|