cowboy_dispatcher.erl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. %% Copyright (c) 2011-2012, Loïc Hoguin <essen@ninenines.eu>
  2. %% Copyright (c) 2011, Anthony Ramine <nox@dev-extend.eu>
  3. %%
  4. %% Permission to use, copy, modify, and/or distribute this software for any
  5. %% purpose with or without fee is hereby granted, provided that the above
  6. %% copyright notice and this permission notice appear in all copies.
  7. %%
  8. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. %% @doc Dispatch requests according to a hostname and path.
  16. -module(cowboy_dispatcher).
  17. %% API.
  18. -export([split_host/1]).
  19. -export([split_path/2]).
  20. -export([match/3]).
  21. -type bindings() :: list({atom(), binary()}).
  22. -type tokens() :: list(binary()).
  23. -type match_rule() :: '_' | '*' | list(binary() | '_' | '...' | atom()).
  24. -type dispatch_path() :: list({match_rule(), module(), any()}).
  25. -type dispatch_rule() :: {Host::match_rule(), Path::dispatch_path()}.
  26. -type dispatch_rules() :: list(dispatch_rule()).
  27. -export_type([bindings/0]).
  28. -export_type([tokens/0]).
  29. -export_type([dispatch_rules/0]).
  30. -include_lib("eunit/include/eunit.hrl").
  31. %% API.
  32. %% @doc Split a hostname into a list of tokens.
  33. -spec split_host(binary())
  34. -> {tokens(), binary(), undefined | inet:port_number()}.
  35. split_host(<<>>) ->
  36. {[], <<>>, undefined};
  37. split_host(Host) ->
  38. case binary:split(Host, <<":">>) of
  39. [Host] ->
  40. {binary:split(Host, <<".">>, [global, trim]), Host, undefined};
  41. [Host2, Port] ->
  42. {binary:split(Host2, <<".">>, [global, trim]), Host2,
  43. list_to_integer(binary_to_list(Port))}
  44. end.
  45. %% @doc Split a path into a list of path segments.
  46. %%
  47. %% Following RFC2396, this function may return path segments containing any
  48. %% character, including <em>/</em> if, and only if, a <em>/</em> was escaped
  49. %% and part of a path segment.
  50. -spec split_path(binary(), fun((binary()) -> binary())) ->
  51. {tokens(), binary(), binary()}.
  52. split_path(Path, URLDec) ->
  53. case binary:split(Path, <<"?">>) of
  54. [Path] -> {do_split_path(Path, <<"/">>, URLDec), Path, <<>>};
  55. [<<>>, Qs] -> {[], <<>>, Qs};
  56. [Path2, Qs] -> {do_split_path(Path2, <<"/">>, URLDec), Path2, Qs}
  57. end.
  58. -spec do_split_path(binary(), <<_:8>>, fun((binary()) -> binary())) -> tokens().
  59. do_split_path(RawPath, Separator, URLDec) ->
  60. EncodedPath = case binary:split(RawPath, Separator, [global, trim]) of
  61. [<<>>|Path] -> Path;
  62. Path -> Path
  63. end,
  64. [URLDec(Token) || Token <- EncodedPath].
  65. %% @doc Match hostname tokens and path tokens against dispatch rules.
  66. %%
  67. %% It is typically used for matching tokens for the hostname and path of
  68. %% the request against a global dispatch rule for your listener.
  69. %%
  70. %% Dispatch rules are a list of <em>{Hostname, PathRules}</em> tuples, with
  71. %% <em>PathRules</em> being a list of <em>{Path, HandlerMod, HandlerOpts}</em>.
  72. %%
  73. %% <em>Hostname</em> and <em>Path</em> are match rules and can be either the
  74. %% atom <em>'_'</em>, which matches everything for a single token, the atom
  75. %% <em>'*'</em>, which matches everything for the rest of the tokens, or a
  76. %% list of tokens. Each token can be either a binary, the atom <em>'_'</em>,
  77. %% the atom '...' or a named atom. A binary token must match exactly,
  78. %% <em>'_'</em> matches everything for a single token, <em>'...'</em> matches
  79. %% everything for the rest of the tokens and a named atom will bind the
  80. %% corresponding token value and return it.
  81. %%
  82. %% The list of hostname tokens is reversed before matching. For example, if
  83. %% we were to match "www.ninenines.eu", we would first match "eu", then
  84. %% "ninenines", then "www". This means that in the context of hostnames,
  85. %% the <em>'...'</em> atom matches properly the lower levels of the domain
  86. %% as would be expected.
  87. %%
  88. %% When a result is found, this function will return the handler module and
  89. %% options found in the dispatch list, a key-value list of bindings and
  90. %% the tokens that were matched by the <em>'...'</em> atom for both the
  91. %% hostname and path.
  92. -spec match(Host::tokens(), Path::tokens(), dispatch_rules())
  93. -> {ok, module(), any(), bindings(),
  94. HostInfo::undefined | tokens(),
  95. PathInfo::undefined | tokens()}
  96. | {error, notfound, host} | {error, notfound, path}.
  97. match(_Host, _Path, []) ->
  98. {error, notfound, host};
  99. match(_Host, Path, [{'_', PathMatchs}|_Tail]) ->
  100. match_path(Path, PathMatchs, [], undefined);
  101. match(Host, Path, [{HostMatch, PathMatchs}|Tail]) ->
  102. case try_match(host, Host, HostMatch) of
  103. false ->
  104. match(Host, Path, Tail);
  105. {true, HostBinds, undefined} ->
  106. match_path(Path, PathMatchs, HostBinds, undefined);
  107. {true, HostBinds, HostInfo} ->
  108. match_path(Path, PathMatchs, HostBinds, lists:reverse(HostInfo))
  109. end.
  110. -spec match_path(tokens(), dispatch_path(), bindings(),
  111. HostInfo::undefined | tokens())
  112. -> {ok, module(), any(), bindings(),
  113. HostInfo::undefined | tokens(),
  114. PathInfo::undefined | tokens()}
  115. | {error, notfound, path}.
  116. match_path(_Path, [], _HostBinds, _HostInfo) ->
  117. {error, notfound, path};
  118. match_path(_Path, [{'_', Handler, Opts}|_Tail], HostBinds, HostInfo) ->
  119. {ok, Handler, Opts, HostBinds, HostInfo, undefined};
  120. match_path('*', [{'*', Handler, Opts}|_Tail], HostBinds, HostInfo) ->
  121. {ok, Handler, Opts, HostBinds, HostInfo, undefined};
  122. match_path(Path, [{PathMatch, Handler, Opts}|Tail], HostBinds, HostInfo) ->
  123. case try_match(path, Path, PathMatch) of
  124. false ->
  125. match_path(Path, Tail, HostBinds, HostInfo);
  126. {true, PathBinds, PathInfo} ->
  127. {ok, Handler, Opts, HostBinds ++ PathBinds, HostInfo, PathInfo}
  128. end.
  129. %% Internal.
  130. -spec try_match(host | path, tokens(), match_rule())
  131. -> {true, bindings(), undefined | tokens()} | false.
  132. try_match(host, List, Match) ->
  133. list_match(lists:reverse(List), lists:reverse(Match), []);
  134. try_match(path, List, Match) ->
  135. list_match(List, Match, []).
  136. -spec list_match(tokens(), match_rule(), bindings())
  137. -> {true, bindings(), undefined | tokens()} | false.
  138. %% Atom '...' matches any trailing path, stop right now.
  139. list_match(List, ['...'], Binds) ->
  140. {true, Binds, List};
  141. %% Atom '_' matches anything, continue.
  142. list_match([_E|Tail], ['_'|TailMatch], Binds) ->
  143. list_match(Tail, TailMatch, Binds);
  144. %% Both values match, continue.
  145. list_match([E|Tail], [E|TailMatch], Binds) ->
  146. list_match(Tail, TailMatch, Binds);
  147. %% Bind E to the variable name V and continue.
  148. list_match([E|Tail], [V|TailMatch], Binds) when is_atom(V) ->
  149. list_match(Tail, TailMatch, [{V, E}|Binds]);
  150. %% Match complete.
  151. list_match([], [], Binds) ->
  152. {true, Binds, undefined};
  153. %% Values don't match, stop.
  154. list_match(_List, _Match, _Binds) ->
  155. false.
  156. %% Tests.
  157. -ifdef(TEST).
  158. split_host_test_() ->
  159. %% {Host, Result}
  160. Tests = [
  161. {<<"">>, {[], <<"">>, undefined}},
  162. {<<".........">>, {[], <<".........">>, undefined}},
  163. {<<"*">>, {[<<"*">>], <<"*">>, undefined}},
  164. {<<"cowboy.ninenines.eu">>,
  165. {[<<"cowboy">>, <<"ninenines">>, <<"eu">>],
  166. <<"cowboy.ninenines.eu">>, undefined}},
  167. {<<"ninenines..eu">>,
  168. {[<<"ninenines">>, <<>>, <<"eu">>],
  169. <<"ninenines..eu">>, undefined}},
  170. {<<"ninenines.eu">>,
  171. {[<<"ninenines">>, <<"eu">>], <<"ninenines.eu">>, undefined}},
  172. {<<"ninenines.eu:8080">>,
  173. {[<<"ninenines">>, <<"eu">>], <<"ninenines.eu">>, 8080}},
  174. {<<"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">>,
  175. {[<<"a">>, <<"b">>, <<"c">>, <<"d">>, <<"e">>, <<"f">>, <<"g">>,
  176. <<"h">>, <<"i">>, <<"j">>, <<"k">>, <<"l">>, <<"m">>, <<"n">>,
  177. <<"o">>, <<"p">>, <<"q">>, <<"r">>, <<"s">>, <<"t">>, <<"u">>,
  178. <<"v">>, <<"w">>, <<"x">>, <<"y">>, <<"z">>],
  179. <<"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">>,
  180. undefined}}
  181. ],
  182. [{H, fun() -> R = split_host(H) end} || {H, R} <- Tests].
  183. split_host_fail_test_() ->
  184. Tests = [
  185. <<"ninenines.eu:owns">>,
  186. <<"ninenines.eu: owns">>,
  187. <<"ninenines.eu:42fun">>,
  188. <<"ninenines.eu: 42fun">>,
  189. <<"ninenines.eu:42 fun">>,
  190. <<"ninenines.eu:fun 42">>,
  191. <<"ninenines.eu: 42">>,
  192. <<":owns">>,
  193. <<":42 fun">>
  194. ],
  195. [{H, fun() -> case catch split_host(H) of
  196. {'EXIT', _Reason} -> ok
  197. end end} || H <- Tests].
  198. split_path_test_() ->
  199. %% {Path, Result, QueryString}
  200. Tests = [
  201. {<<"?">>, [], <<"">>, <<"">>},
  202. {<<"???">>, [], <<"">>, <<"??">>},
  203. {<<"/">>, [], <<"/">>, <<"">>},
  204. {<<"/extend//cowboy">>, [<<"extend">>, <<>>, <<"cowboy">>],
  205. <<"/extend//cowboy">>, <<>>},
  206. {<<"/users">>, [<<"users">>], <<"/users">>, <<"">>},
  207. {<<"/users?">>, [<<"users">>], <<"/users">>, <<"">>},
  208. {<<"/users?a">>, [<<"users">>], <<"/users">>, <<"a">>},
  209. {<<"/users/42/friends?a=b&c=d&e=notsure?whatever">>,
  210. [<<"users">>, <<"42">>, <<"friends">>],
  211. <<"/users/42/friends">>, <<"a=b&c=d&e=notsure?whatever">>},
  212. {<<"/users/a+b/c%21d?e+f=g+h">>,
  213. [<<"users">>, <<"a b">>, <<"c!d">>],
  214. <<"/users/a+b/c%21d">>, <<"e+f=g+h">>}
  215. ],
  216. URLDecode = fun(Bin) -> cowboy_http:urldecode(Bin, crash) end,
  217. [{P, fun() -> {R, RawP, Qs} = split_path(P, URLDecode) end}
  218. || {P, R, RawP, Qs} <- Tests].
  219. match_test_() ->
  220. Dispatch = [
  221. {[<<"www">>, '_', <<"ninenines">>, <<"eu">>], [
  222. {[<<"users">>, '_', <<"mails">>], match_any_subdomain_users, []}
  223. ]},
  224. {[<<"ninenines">>, <<"eu">>], [
  225. {[<<"users">>, id, <<"friends">>], match_extend_users_friends, []},
  226. {'_', match_extend, []}
  227. ]},
  228. {[<<"ninenines">>, var], [
  229. {[<<"threads">>, var], match_duplicate_vars,
  230. [we, {expect, two}, var, here]}
  231. ]},
  232. {[<<"erlang">>, ext], [
  233. {'_', match_erlang_ext, []}
  234. ]},
  235. {'_', [
  236. {[<<"users">>, id, <<"friends">>], match_users_friends, []},
  237. {'_', match_any, []}
  238. ]}
  239. ],
  240. %% {Host, Path, Result}
  241. Tests = [
  242. {[<<"any">>], [], {ok, match_any, [], []}},
  243. {[<<"www">>, <<"any">>, <<"ninenines">>, <<"eu">>],
  244. [<<"users">>, <<"42">>, <<"mails">>],
  245. {ok, match_any_subdomain_users, [], []}},
  246. {[<<"www">>, <<"ninenines">>, <<"eu">>],
  247. [<<"users">>, <<"42">>, <<"mails">>], {ok, match_any, [], []}},
  248. {[<<"www">>, <<"ninenines">>, <<"eu">>], [], {ok, match_any, [], []}},
  249. {[<<"www">>, <<"any">>, <<"ninenines">>, <<"eu">>],
  250. [<<"not_users">>, <<"42">>, <<"mails">>], {error, notfound, path}},
  251. {[<<"ninenines">>, <<"eu">>], [], {ok, match_extend, [], []}},
  252. {[<<"ninenines">>, <<"eu">>], [<<"users">>, <<"42">>, <<"friends">>],
  253. {ok, match_extend_users_friends, [], [{id, <<"42">>}]}},
  254. {[<<"erlang">>, <<"fr">>], '_',
  255. {ok, match_erlang_ext, [], [{ext, <<"fr">>}]}},
  256. {[<<"any">>], [<<"users">>, <<"444">>, <<"friends">>],
  257. {ok, match_users_friends, [], [{id, <<"444">>}]}},
  258. {[<<"ninenines">>, <<"fr">>], [<<"threads">>, <<"987">>],
  259. {ok, match_duplicate_vars, [we, {expect, two}, var, here],
  260. [{var, <<"fr">>}, {var, <<"987">>}]}}
  261. ],
  262. [{lists:flatten(io_lib:format("~p, ~p", [H, P])), fun() ->
  263. {ok, Handler, Opts, Binds, undefined, undefined} = match(H, P, Dispatch)
  264. end} || {H, P, {ok, Handler, Opts, Binds}} <- Tests].
  265. match_info_test_() ->
  266. Dispatch = [
  267. {[<<"www">>, <<"ninenines">>, <<"eu">>], [
  268. {[<<"pathinfo">>, <<"is">>, <<"next">>, '...'], match_path, []}
  269. ]},
  270. {['...', <<"ninenines">>, <<"eu">>], [
  271. {'_', match_any, []}
  272. ]}
  273. ],
  274. Tests = [
  275. {[<<"ninenines">>, <<"eu">>], [],
  276. {ok, match_any, [], [], [], undefined}},
  277. {[<<"bugs">>, <<"ninenines">>, <<"eu">>], [],
  278. {ok, match_any, [], [], [<<"bugs">>], undefined}},
  279. {[<<"cowboy">>, <<"bugs">>, <<"ninenines">>, <<"eu">>], [],
  280. {ok, match_any, [], [], [<<"cowboy">>, <<"bugs">>], undefined}},
  281. {[<<"www">>, <<"ninenines">>, <<"eu">>],
  282. [<<"pathinfo">>, <<"is">>, <<"next">>],
  283. {ok, match_path, [], [], undefined, []}},
  284. {[<<"www">>, <<"ninenines">>, <<"eu">>],
  285. [<<"pathinfo">>, <<"is">>, <<"next">>, <<"path_info">>],
  286. {ok, match_path, [], [], undefined, [<<"path_info">>]}},
  287. {[<<"www">>, <<"ninenines">>, <<"eu">>],
  288. [<<"pathinfo">>, <<"is">>, <<"next">>, <<"foo">>, <<"bar">>],
  289. {ok, match_path, [], [], undefined, [<<"foo">>, <<"bar">>]}}
  290. ],
  291. [{lists:flatten(io_lib:format("~p, ~p", [H, P])), fun() ->
  292. R = match(H, P, Dispatch)
  293. end} || {H, P, R} <- Tests].
  294. -endif.