cowboy_router.erl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. %% Copyright (c) 2011-2013, Loïc Hoguin <essen@ninenines.eu>
  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. %% @doc Routing middleware.
  15. %%
  16. %% Resolve the handler to be used for the request based on the
  17. %% routing information found in the <em>dispatch</em> environment value.
  18. %% When found, the handler module and associated data are added to
  19. %% the environment as the <em>handler</em> and <em>handler_opts</em> values
  20. %% respectively.
  21. %%
  22. %% If the route cannot be found, processing stops with either
  23. %% a 400 or a 404 reply.
  24. -module(cowboy_router).
  25. -behaviour(cowboy_middleware).
  26. -export([execute/2]).
  27. -type bindings() :: [{atom(), binary()}].
  28. -type tokens() :: [binary()].
  29. -type match_rule() :: '_' | <<_:8>> | [binary() | '_' | '...' | atom()].
  30. -type dispatch_path() :: [{match_rule(), module(), any()}].
  31. -type dispatch_rule() :: {Host::match_rule(), Path::dispatch_path()}.
  32. -type dispatch_rules() :: [dispatch_rule()].
  33. -export_type([bindings/0]).
  34. -export_type([tokens/0]).
  35. -export_type([dispatch_rules/0]).
  36. -ifdef(TEST).
  37. -include_lib("eunit/include/eunit.hrl").
  38. -endif.
  39. %% @private
  40. -spec execute(Req, Env)
  41. -> {ok, Req, Env} | {error, 400 | 404, Req}
  42. when Req::cowboy_req:req(), Env::cowboy_middleware:env().
  43. execute(Req, Env) ->
  44. {_, Dispatch} = lists:keyfind(dispatch, 1, Env),
  45. [Host, Path] = cowboy_req:get([host, path], Req),
  46. case match(Dispatch, Host, Path) of
  47. {ok, Handler, HandlerOpts, Bindings, HostInfo, PathInfo} ->
  48. Req2 = cowboy_req:set_bindings(HostInfo, PathInfo, Bindings, Req),
  49. {ok, Req2, [{handler, Handler}, {handler_opts, HandlerOpts}|Env]};
  50. {error, notfound, host} ->
  51. {error, 400, Req};
  52. {error, badrequest, path} ->
  53. {error, 400, Req};
  54. {error, notfound, path} ->
  55. {error, 404, Req}
  56. end.
  57. %% Internal.
  58. %% @doc Match hostname tokens and path tokens against dispatch rules.
  59. %%
  60. %% It is typically used for matching tokens for the hostname and path of
  61. %% the request against a global dispatch rule for your listener.
  62. %%
  63. %% Dispatch rules are a list of <em>{Hostname, PathRules}</em> tuples, with
  64. %% <em>PathRules</em> being a list of <em>{Path, HandlerMod, HandlerOpts}</em>.
  65. %%
  66. %% <em>Hostname</em> and <em>Path</em> are match rules and can be either the
  67. %% atom <em>'_'</em>, which matches everything, `<<"*">>', which match the
  68. %% wildcard path, or a list of tokens.
  69. %%
  70. %% Each token can be either a binary, the atom <em>'_'</em>,
  71. %% the atom '...' or a named atom. A binary token must match exactly,
  72. %% <em>'_'</em> matches everything for a single token, <em>'...'</em> matches
  73. %% everything for the rest of the tokens and a named atom will bind the
  74. %% corresponding token value and return it.
  75. %%
  76. %% The list of hostname tokens is reversed before matching. For example, if
  77. %% we were to match "www.ninenines.eu", we would first match "eu", then
  78. %% "ninenines", then "www". This means that in the context of hostnames,
  79. %% the <em>'...'</em> atom matches properly the lower levels of the domain
  80. %% as would be expected.
  81. %%
  82. %% When a result is found, this function will return the handler module and
  83. %% options found in the dispatch list, a key-value list of bindings and
  84. %% the tokens that were matched by the <em>'...'</em> atom for both the
  85. %% hostname and path.
  86. -spec match(dispatch_rules(), Host::binary() | tokens(), Path::binary())
  87. -> {ok, module(), any(), bindings(),
  88. HostInfo::undefined | tokens(),
  89. PathInfo::undefined | tokens()}
  90. | {error, notfound, host} | {error, notfound, path}
  91. | {error, badrequest, path}.
  92. match([], _, _) ->
  93. {error, notfound, host};
  94. match([{'_', PathMatchs}|_Tail], _, Path) ->
  95. match_path(PathMatchs, undefined, Path, []);
  96. match([{HostMatch, PathMatchs}|Tail], Tokens, Path)
  97. when is_list(Tokens) ->
  98. case list_match(Tokens, lists:reverse(HostMatch), []) of
  99. false ->
  100. match(Tail, Tokens, Path);
  101. {true, Bindings, undefined} ->
  102. match_path(PathMatchs, undefined, Path, Bindings);
  103. {true, Bindings, HostInfo} ->
  104. match_path(PathMatchs, lists:reverse(HostInfo),
  105. Path, Bindings)
  106. end;
  107. match(Dispatch, Host, Path) ->
  108. match(Dispatch, split_host(Host), Path).
  109. -spec match_path(dispatch_path(),
  110. HostInfo::undefined | tokens(), binary() | tokens(), bindings())
  111. -> {ok, module(), any(), bindings(),
  112. HostInfo::undefined | tokens(),
  113. PathInfo::undefined | tokens()}
  114. | {error, notfound, path} | {error, badrequest, path}.
  115. match_path([], _, _, _) ->
  116. {error, notfound, path};
  117. match_path([{'_', Handler, Opts}|_Tail], HostInfo, _, Bindings) ->
  118. {ok, Handler, Opts, Bindings, HostInfo, undefined};
  119. match_path([{<<"*">>, Handler, Opts}|_Tail], HostInfo, <<"*">>, Bindings) ->
  120. {ok, Handler, Opts, Bindings, HostInfo, undefined};
  121. match_path([{PathMatch, Handler, Opts}|Tail], HostInfo, Tokens,
  122. Bindings) when is_list(Tokens) ->
  123. case list_match(Tokens, PathMatch, []) of
  124. false ->
  125. match_path(Tail, HostInfo, Tokens, Bindings);
  126. {true, PathBinds, PathInfo} ->
  127. {ok, Handler, Opts, Bindings ++ PathBinds, HostInfo, PathInfo}
  128. end;
  129. match_path(_Dispatch, _HostInfo, badrequest, _Bindings) ->
  130. {error, badrequest, path};
  131. match_path(Dispatch, HostInfo, Path, Bindings) ->
  132. match_path(Dispatch, HostInfo, split_path(Path), Bindings).
  133. %% @doc Split a hostname into a list of tokens.
  134. -spec split_host(binary()) -> tokens().
  135. split_host(Host) ->
  136. split_host(Host, []).
  137. split_host(Host, Acc) ->
  138. case binary:match(Host, <<".">>) of
  139. nomatch when Host =:= <<>> ->
  140. Acc;
  141. nomatch ->
  142. [Host|Acc];
  143. {Pos, _} ->
  144. << Segment:Pos/binary, _:8, Rest/bits >> = Host,
  145. false = byte_size(Segment) == 0,
  146. split_host(Rest, [Segment|Acc])
  147. end.
  148. %% @doc Split a path into a list of path segments.
  149. %%
  150. %% Following RFC2396, this function may return path segments containing any
  151. %% character, including <em>/</em> if, and only if, a <em>/</em> was escaped
  152. %% and part of a path segment.
  153. -spec split_path(binary()) -> tokens().
  154. split_path(<< $/, Path/bits >>) ->
  155. split_path(Path, []);
  156. split_path(_) ->
  157. badrequest.
  158. split_path(Path, Acc) ->
  159. try
  160. case binary:match(Path, <<"/">>) of
  161. nomatch when Path =:= <<>> ->
  162. lists:reverse([cowboy_http:urldecode(S) || S <- Acc]);
  163. nomatch ->
  164. lists:reverse([cowboy_http:urldecode(S) || S <- [Path|Acc]]);
  165. {Pos, _} ->
  166. << Segment:Pos/binary, _:8, Rest/bits >> = Path,
  167. split_path(Rest, [Segment|Acc])
  168. end
  169. catch
  170. error:badarg ->
  171. badrequest
  172. end.
  173. -spec list_match(tokens(), match_rule(), bindings())
  174. -> {true, bindings(), undefined | tokens()} | false.
  175. %% Atom '...' matches any trailing path, stop right now.
  176. list_match(List, ['...'], Binds) ->
  177. {true, Binds, List};
  178. %% Atom '_' matches anything, continue.
  179. list_match([_E|Tail], ['_'|TailMatch], Binds) ->
  180. list_match(Tail, TailMatch, Binds);
  181. %% Both values match, continue.
  182. list_match([E|Tail], [E|TailMatch], Binds) ->
  183. list_match(Tail, TailMatch, Binds);
  184. %% Bind E to the variable name V and continue.
  185. list_match([E|Tail], [V|TailMatch], Binds) when is_atom(V) ->
  186. list_match(Tail, TailMatch, [{V, E}|Binds]);
  187. %% Match complete.
  188. list_match([], [], Binds) ->
  189. {true, Binds, undefined};
  190. %% Values don't match, stop.
  191. list_match(_List, _Match, _Binds) ->
  192. false.
  193. %% Tests.
  194. -ifdef(TEST).
  195. split_host_test_() ->
  196. %% {Host, Result}
  197. Tests = [
  198. {<<"">>, []},
  199. {<<"*">>, [<<"*">>]},
  200. {<<"cowboy.ninenines.eu">>,
  201. [<<"eu">>, <<"ninenines">>, <<"cowboy">>]},
  202. {<<"ninenines.eu">>,
  203. [<<"eu">>, <<"ninenines">>]},
  204. {<<"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">>,
  205. [<<"z">>, <<"y">>, <<"x">>, <<"w">>, <<"v">>, <<"u">>, <<"t">>,
  206. <<"s">>, <<"r">>, <<"q">>, <<"p">>, <<"o">>, <<"n">>, <<"m">>,
  207. <<"l">>, <<"k">>, <<"j">>, <<"i">>, <<"h">>, <<"g">>, <<"f">>,
  208. <<"e">>, <<"d">>, <<"c">>, <<"b">>, <<"a">>]}
  209. ],
  210. [{H, fun() -> R = split_host(H) end} || {H, R} <- Tests].
  211. split_path_test_() ->
  212. %% {Path, Result, QueryString}
  213. Tests = [
  214. {<<"/">>, []},
  215. {<<"/extend//cowboy">>, [<<"extend">>, <<>>, <<"cowboy">>]},
  216. {<<"/users">>, [<<"users">>]},
  217. {<<"/users/42/friends">>, [<<"users">>, <<"42">>, <<"friends">>]},
  218. {<<"/users/a+b/c%21d">>, [<<"users">>, <<"a b">>, <<"c!d">>]}
  219. ],
  220. [{P, fun() -> R = split_path(P) end} || {P, R} <- Tests].
  221. match_test_() ->
  222. Dispatch = [
  223. {[<<"www">>, '_', <<"ninenines">>, <<"eu">>], [
  224. {[<<"users">>, '_', <<"mails">>], match_any_subdomain_users, []}
  225. ]},
  226. {[<<"ninenines">>, <<"eu">>], [
  227. {[<<"users">>, id, <<"friends">>], match_extend_users_friends, []},
  228. {'_', match_extend, []}
  229. ]},
  230. {[<<"ninenines">>, var], [
  231. {[<<"threads">>, var], match_duplicate_vars,
  232. [we, {expect, two}, var, here]}
  233. ]},
  234. {[<<"erlang">>, ext], [
  235. {'_', match_erlang_ext, []}
  236. ]},
  237. {'_', [
  238. {[<<"users">>, id, <<"friends">>], match_users_friends, []},
  239. {'_', match_any, []}
  240. ]}
  241. ],
  242. %% {Host, Path, Result}
  243. Tests = [
  244. {<<"any">>, <<"/">>, {ok, match_any, [], []}},
  245. {<<"www.any.ninenines.eu">>, <<"/users/42/mails">>,
  246. {ok, match_any_subdomain_users, [], []}},
  247. {<<"www.ninenines.eu">>, <<"/users/42/mails">>,
  248. {ok, match_any, [], []}},
  249. {<<"www.ninenines.eu">>, <<"/">>,
  250. {ok, match_any, [], []}},
  251. {<<"www.any.ninenines.eu">>, <<"/not_users/42/mails">>,
  252. {error, notfound, path}},
  253. {<<"ninenines.eu">>, <<"/">>,
  254. {ok, match_extend, [], []}},
  255. {<<"ninenines.eu">>, <<"/users/42/friends">>,
  256. {ok, match_extend_users_friends, [], [{id, <<"42">>}]}},
  257. {<<"erlang.fr">>, '_',
  258. {ok, match_erlang_ext, [], [{ext, <<"fr">>}]}},
  259. {<<"any">>, <<"/users/444/friends">>,
  260. {ok, match_users_friends, [], [{id, <<"444">>}]}},
  261. {<<"ninenines.fr">>, <<"/threads/987">>,
  262. {ok, match_duplicate_vars, [we, {expect, two}, var, here],
  263. [{var, <<"fr">>}, {var, <<"987">>}]}}
  264. ],
  265. [{lists:flatten(io_lib:format("~p, ~p", [H, P])), fun() ->
  266. {ok, Handler, Opts, Binds, undefined, undefined}
  267. = match(Dispatch, H, P)
  268. end} || {H, P, {ok, Handler, Opts, Binds}} <- Tests].
  269. match_info_test_() ->
  270. Dispatch = [
  271. {[<<"www">>, <<"ninenines">>, <<"eu">>], [
  272. {[<<"pathinfo">>, <<"is">>, <<"next">>, '...'], match_path, []}
  273. ]},
  274. {['...', <<"ninenines">>, <<"eu">>], [
  275. {'_', match_any, []}
  276. ]}
  277. ],
  278. Tests = [
  279. {<<"ninenines.eu">>, <<"/">>,
  280. {ok, match_any, [], [], [], undefined}},
  281. {<<"bugs.ninenines.eu">>, <<"/">>,
  282. {ok, match_any, [], [], [<<"bugs">>], undefined}},
  283. {<<"cowboy.bugs.ninenines.eu">>, <<"/">>,
  284. {ok, match_any, [], [], [<<"cowboy">>, <<"bugs">>], undefined}},
  285. {<<"www.ninenines.eu">>, <<"/pathinfo/is/next">>,
  286. {ok, match_path, [], [], undefined, []}},
  287. {<<"www.ninenines.eu">>, <<"/pathinfo/is/next/path_info">>,
  288. {ok, match_path, [], [], undefined, [<<"path_info">>]}},
  289. {<<"www.ninenines.eu">>, <<"/pathinfo/is/next/foo/bar">>,
  290. {ok, match_path, [], [], undefined, [<<"foo">>, <<"bar">>]}}
  291. ],
  292. [{lists:flatten(io_lib:format("~p, ~p", [H, P])), fun() ->
  293. R = match(Dispatch, H, P)
  294. end} || {H, P, R} <- Tests].
  295. -endif.