aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile10
-rw-r--r--ROADMAP.md62
-rw-r--r--guide/middlewares.md2
-rw-r--r--guide/routing.md46
-rw-r--r--rebar.config2
-rw-r--r--src/cowboy_dispatcher.erl291
-rw-r--r--src/cowboy_req.erl14
-rw-r--r--src/cowboy_router.erl522
-rw-r--r--test/autobahn_SUITE.erl4
-rw-r--r--test/http_SUITE.erl66
-rw-r--r--test/ws_SUITE.erl24
11 files changed, 622 insertions, 421 deletions
diff --git a/Makefile b/Makefile
index fb00e22..8403c22 100644
--- a/Makefile
+++ b/Makefile
@@ -24,14 +24,16 @@ deps/ranch:
MODULES = $(shell ls src/*.erl | sed 's/src\///;s/\.erl/,/' | sed '$$s/.$$//')
-app: deps/ranch
- @$(MAKE) -C $(DEPS_DIR)/ranch
- @mkdir -p ebin/
+app: deps/ranch ebin/$(PROJECT).app
@cat src/$(PROJECT).app.src \
| sed 's/{modules, \[\]}/{modules, \[$(MODULES)\]}/' \
> ebin/$(PROJECT).app
+ @$(MAKE) -C $(DEPS_DIR)/ranch
+
+ebin/$(PROJECT).app: src/*.erl
+ @mkdir -p ebin/
erlc -v $(ERLC_OPTS) -o ebin/ -pa ebin/ \
- src/$(PROJECT)_middleware.erl src/*.erl
+ src/$(PROJECT)_middleware.erl $?
clean:
-@$(MAKE) -C $(DEPS_DIR)/ranch clean
diff --git a/ROADMAP.md b/ROADMAP.md
index 7dc19af..2186387 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -6,38 +6,21 @@ list of planned changes and work to be done on the Cowboy
server. It is non-exhaustive and subject to change. Items
are not ordered.
- * Write more, better examples.
+ * Add and improve examples
- The first step would be to port misultin's examples
- to Cowboy. Then these examples could be completed with
- examples for Cowboy specific features.
+ * Improve user guide
- The extend/cowboy_examples is to be used for this. As
- it is a separate repository, we can organize the file
- structure as appropriate. Ideally we would have one
- complete example per folder.
+ We need feedback to improve the guide.
- Examples should be commented. They may or may not be
- used for writing the user guides.
-
- * Write user guides.
-
- We currently have good API documentation, but no step
- by step user guides.
-
- * Write more, better tests.
+ * Add and improve tests
Amongst the areas less tested there is protocol upgrades
and the REST handler.
- Current tests should be completed with unit tests
- where applicable. We should probably also test the
- dependencies used, like erlang:decode_packet/3.
-
While eunit and ct tests are fine, some parts of the
code could benefit from PropEr tests.
- * Continuous performance testing.
+ * Continuous performance testing
Initially dubbed the Horse project, Cowboy could benefit
from a continuous performance testing tool that would
@@ -49,46 +32,25 @@ are not ordered.
Cowboy to other servers and eventually take ideas from
the servers that outperform Cowboy for the task being tested.
- * Improve HTTP/1.0 support.
+ * Full HTTP/1.1 support
+
+ * Improved HTTP/1.0 support
Most of the work on Cowboy has been done with HTTP/1.1
in mind. But there is still a need for HTTP/1.0 code in
Cowboy. The server code should be reviewed and tested
to ensure compatibility with remaining HTTP/1.0 products.
- * Complete the work on Websockets.
-
- Now that the Autobahn test suite is available (make inttests),
- we have a definite way to know whether Cowboy's implementation
- of Websockets is right. The work can thus be completed. The
- remaining task is proper UTF8 handling.
+ * SPDY support
- * SPDY support.
+The following items pertain to Ranch.
- While SPDY probably won't be added directly to Cowboy, work
- has been started on making Cowboy use SPDY.
-
- * Transport upgrades.
-
- Some protocols allow an upgrade from TCP to SSL without
- closing the connection. This is currently not possible
- through the Cowboy API.
-
- * Resizing the acceptor pool.
+ * Resizing the acceptor pool
We should be able to add more acceptors to a pool but also
to remove some of them as needed.
- * Simplified dispatch list.
-
- For convenience purposes, the dispatch list should allow
- lists instead of binaries. The lists can be converted to
- binary by Cowboy at listener initialization.
-
- There has also been discussion on allowing the dispatch
- list to be hierarchical.
-
- * Add Transport:secure/0.
+ * Add Transport:secure/0
Currently Cowboy checks if a connection is secure by
checking if its name is 'ssl'. This isn't a very modular
diff --git a/guide/middlewares.md b/guide/middlewares.md
index 2f583cf..0ab6dc2 100644
--- a/guide/middlewares.md
+++ b/guide/middlewares.md
@@ -61,8 +61,6 @@ environment values to perform.
Routing middleware
------------------
-@todo Routing middleware value is renamed very soon.
-
The routing middleware requires the `dispatch` value. If routing
succeeds, it will put the handler name and options in the `handler`
and `handler_opts` values of the environment, respectively.
diff --git a/guide/routing.md b/guide/routing.md
index 2970b39..9d5c5af 100644
--- a/guide/routing.md
+++ b/guide/routing.md
@@ -1,9 +1,6 @@
Routing
=======
-@todo Note that this documentation is for the new routing interface
-not available in master at this point.
-
Purpose
-------
@@ -49,9 +46,9 @@ Finally, each path contains matching rules for the path along with
optional constraints, and gives us the handler module to be used
along with options that will be given to it on initialization.
-```
-Path1 = {PathMatch, Handler, Module}.
-Path2 = {PathMatch, Constraints, Handler, Module}.
+``` erlang
+Path1 = {PathMatch, Handler, Opts}.
+Path2 = {PathMatch, Constraints, Handler, Opts}.
```
Continue reading to learn more about the match syntax and the optional
@@ -112,11 +109,11 @@ HostMatch = ":subdomain.example.org".
```
If these two end up matching when routing, you will end up with two
-bindings defined, `subdomain` and `hat_name`, each containing the
+bindings defined, `subdomain` and `name`, each containing the
segment value where they were defined. For example, the URL
`http://test.example.org/hats/wild_cowboy_legendary/prices` will
result in having the value `test` bound to the name `subdomain`
-and the value `wild_cowboy_legendary` bound to the name `hat_name`.
+and the value `wild_cowboy_legendary` bound to the name `name`.
They can later be retrieved using `cowboy_req:binding/{2,3}`. The
binding name must be given as an atom.
@@ -156,9 +153,9 @@ PathMatch = "/hats/[...]".
HostMatch = "[...]ninenines.eu".
```
-Finally, if a binding appears twice in the routing rules, then the
-match will succeed only if they share the same value. This copies
-the Erlang pattern matching behavior.
+If a binding appears twice in the routing rules, then the match
+will succeed only if they share the same value. This copies the
+Erlang pattern matching behavior.
``` erlang
PathMatch = "/hats/:name/:name".
@@ -180,12 +177,28 @@ PathMatch = "/:user/[...]".
HostMatch = ":user.github.com".
```
+Finally, there are two special match values that can be used. The
+first is the atom `'_'` which will match any host or path.
+
+``` erlang
+PathMatch = '_'.
+HostMatch = '_'.
+```
+
+The second is the special host match `"*"` which will match the
+wildcard path, generally used alongside the `OPTIONS` method.
+
+``` erlang
+HostMatch = "*".
+```
+
Constraints
-----------
After the matching has completed, the resulting bindings can be tested
-against a set of constraints. The match will succeed only if they all
-succeed.
+against a set of constraints. Constraints are only tested when the
+binding is defined. They run in the order you defined them. The match
+will succeed only if they all succeed.
They are always given as a two or three elements tuple, where the first
element is the name of the binding, the second element is the constraint's
@@ -194,7 +207,7 @@ name, and the optional third element is the constraint's arguments.
The following constraints are currently defined:
* {Name, int}
- * {Name, function, (fun(Value) -> true | {true, NewValue} | false)}
+ * {Name, function, fun ((Value) -> true | {true, NewValue} | false)}
The `int` constraint will check if the binding is a binary string
representing an integer, and if it is, will convert the value to integer.
@@ -202,6 +215,7 @@ representing an integer, and if it is, will convert the value to integer.
The `function` constraint will pass the binding value to a user specified
function that receives the binary value as its only argument and must
return whether it fulfills the constraint, optionally modifying the value.
+The value thus returned can be of any type.
Note that constraint functions SHOULD be pure and MUST NOT crash.
@@ -212,10 +226,10 @@ The structure defined in this chapter needs to be compiled before it is
passed to Cowboy. This allows Cowboy to efficiently lookup the correct
handler to run instead of having to parse the routes repeatedly.
-This can be done with a simple call to `cowboy_routing:compile/1`.
+This can be done with a simple call to `cowboy_router:compile/1`.
``` erlang
-{ok, Routes} = cowboy_routing:compile([
+{ok, Routes} = cowboy_router:compile([
%% {HostMatch, list({PathMatch, Handler, Opts})}
{'_', [{'_', my_handler, []}]}
]),
diff --git a/rebar.config b/rebar.config
index c3bfeb6..ba92ee5 100644
--- a/rebar.config
+++ b/rebar.config
@@ -1,3 +1,3 @@
{deps, [
- {ranch, "0\\.6\\.0.*", {git, "git://github.com/extend/ranch.git", "0.6.0"}}
+ {ranch, ".*", {git, "git://github.com/extend/ranch.git", "0.6.1"}}
]}.
diff --git a/src/cowboy_dispatcher.erl b/src/cowboy_dispatcher.erl
deleted file mode 100644
index ead2c96..0000000
--- a/src/cowboy_dispatcher.erl
+++ /dev/null
@@ -1,291 +0,0 @@
-%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]>
-%% Copyright (c) 2011, Anthony Ramine <[email protected]>
-%%
-%% 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.
-
-%% @doc Dispatch requests according to a hostname and path.
--module(cowboy_dispatcher).
-
-%% API.
--export([match/3]).
-
--type bindings() :: [{atom(), binary()}].
--type tokens() :: [binary()].
--type match_rule() :: '_' | <<_:8>> | [binary() | '_' | '...' | atom()].
--type dispatch_path() :: [{match_rule(), module(), any()}].
--type dispatch_rule() :: {Host::match_rule(), Path::dispatch_path()}.
--type dispatch_rules() :: [dispatch_rule()].
-
--export_type([bindings/0]).
--export_type([tokens/0]).
--export_type([dispatch_rules/0]).
-
--ifdef(TEST).
--include_lib("eunit/include/eunit.hrl").
--endif.
-
-%% API.
-
-%% @doc Match hostname tokens and path tokens against dispatch rules.
-%%
-%% It is typically used for matching tokens for the hostname and path of
-%% the request against a global dispatch rule for your listener.
-%%
-%% Dispatch rules are a list of <em>{Hostname, PathRules}</em> tuples, with
-%% <em>PathRules</em> being a list of <em>{Path, HandlerMod, HandlerOpts}</em>.
-%%
-%% <em>Hostname</em> and <em>Path</em> are match rules and can be either the
-%% atom <em>'_'</em>, which matches everything, `<<"*">>', which match the
-%% wildcard path, or a list of tokens.
-%%
-%% Each token can be either a binary, the atom <em>'_'</em>,
-%% the atom '...' or a named atom. A binary token must match exactly,
-%% <em>'_'</em> matches everything for a single token, <em>'...'</em> matches
-%% everything for the rest of the tokens and a named atom will bind the
-%% corresponding token value and return it.
-%%
-%% The list of hostname tokens is reversed before matching. For example, if
-%% we were to match "www.ninenines.eu", we would first match "eu", then
-%% "ninenines", then "www". This means that in the context of hostnames,
-%% the <em>'...'</em> atom matches properly the lower levels of the domain
-%% as would be expected.
-%%
-%% When a result is found, this function will return the handler module and
-%% options found in the dispatch list, a key-value list of bindings and
-%% the tokens that were matched by the <em>'...'</em> atom for both the
-%% hostname and path.
--spec match(dispatch_rules(), Host::binary() | tokens(), Path::binary())
- -> {ok, module(), any(), bindings(),
- HostInfo::undefined | tokens(),
- PathInfo::undefined | tokens()}
- | {error, notfound, host} | {error, notfound, path}
- | {error, badrequest, path}.
-match([], _, _) ->
- {error, notfound, host};
-match([{'_', PathMatchs}|_Tail], _, Path) ->
- match_path(PathMatchs, undefined, Path, []);
-match([{HostMatch, PathMatchs}|Tail], Tokens, Path)
- when is_list(Tokens) ->
- case list_match(Tokens, lists:reverse(HostMatch), []) of
- false ->
- match(Tail, Tokens, Path);
- {true, Bindings, undefined} ->
- match_path(PathMatchs, undefined, Path, Bindings);
- {true, Bindings, HostInfo} ->
- match_path(PathMatchs, lists:reverse(HostInfo),
- Path, Bindings)
- end;
-match(Dispatch, Host, Path) ->
- match(Dispatch, split_host(Host), Path).
-
--spec match_path(dispatch_path(),
- HostInfo::undefined | tokens(), binary() | tokens(), bindings())
- -> {ok, module(), any(), bindings(),
- HostInfo::undefined | tokens(),
- PathInfo::undefined | tokens()}
- | {error, notfound, path} | {error, badrequest, path}.
-match_path([], _, _, _) ->
- {error, notfound, path};
-match_path([{'_', Handler, Opts}|_Tail], HostInfo, _, Bindings) ->
- {ok, Handler, Opts, Bindings, HostInfo, undefined};
-match_path([{<<"*">>, Handler, Opts}|_Tail], HostInfo, <<"*">>, Bindings) ->
- {ok, Handler, Opts, Bindings, HostInfo, undefined};
-match_path([{PathMatch, Handler, Opts}|Tail], HostInfo, Tokens,
- Bindings) when is_list(Tokens) ->
- case list_match(Tokens, PathMatch, []) of
- false ->
- match_path(Tail, HostInfo, Tokens, Bindings);
- {true, PathBinds, PathInfo} ->
- {ok, Handler, Opts, Bindings ++ PathBinds, HostInfo, PathInfo}
- end;
-match_path(_Dispatch, _HostInfo, badrequest, _Bindings) ->
- {error, badrequest, path};
-match_path(Dispatch, HostInfo, Path, Bindings) ->
- match_path(Dispatch, HostInfo, split_path(Path), Bindings).
-
-%% Internal.
-
-%% @doc Split a hostname into a list of tokens.
--spec split_host(binary()) -> tokens().
-split_host(Host) ->
- split_host(Host, []).
-
-split_host(Host, Acc) ->
- case binary:match(Host, <<".">>) of
- nomatch when Host =:= <<>> ->
- Acc;
- nomatch ->
- [Host|Acc];
- {Pos, _} ->
- << Segment:Pos/binary, _:8, Rest/bits >> = Host,
- false = byte_size(Segment) == 0,
- split_host(Rest, [Segment|Acc])
- end.
-
-%% @doc Split a path into a list of path segments.
-%%
-%% Following RFC2396, this function may return path segments containing any
-%% character, including <em>/</em> if, and only if, a <em>/</em> was escaped
-%% and part of a path segment.
--spec split_path(binary()) -> tokens().
-split_path(<< $/, Path/bits >>) ->
- split_path(Path, []);
-split_path(_) ->
- badrequest.
-
-split_path(Path, Acc) ->
- try
- case binary:match(Path, <<"/">>) of
- nomatch when Path =:= <<>> ->
- lists:reverse([cowboy_http:urldecode(S) || S <- Acc]);
- nomatch ->
- lists:reverse([cowboy_http:urldecode(S) || S <- [Path|Acc]]);
- {Pos, _} ->
- << Segment:Pos/binary, _:8, Rest/bits >> = Path,
- split_path(Rest, [Segment|Acc])
- end
- catch
- error:badarg ->
- badrequest
- end.
-
--spec list_match(tokens(), match_rule(), bindings())
- -> {true, bindings(), undefined | 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 = [
- {<<"">>, []},
- {<<"*">>, [<<"*">>]},
- {<<"cowboy.ninenines.eu">>,
- [<<"eu">>, <<"ninenines">>, <<"cowboy">>]},
- {<<"ninenines.eu">>,
- [<<"eu">>, <<"ninenines">>]},
- {<<"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">>,
- [<<"z">>, <<"y">>, <<"x">>, <<"w">>, <<"v">>, <<"u">>, <<"t">>,
- <<"s">>, <<"r">>, <<"q">>, <<"p">>, <<"o">>, <<"n">>, <<"m">>,
- <<"l">>, <<"k">>, <<"j">>, <<"i">>, <<"h">>, <<"g">>, <<"f">>,
- <<"e">>, <<"d">>, <<"c">>, <<"b">>, <<"a">>]}
- ],
- [{H, fun() -> R = split_host(H) end} || {H, R} <- Tests].
-
-split_path_test_() ->
- %% {Path, Result, QueryString}
- Tests = [
- {<<"/">>, []},
- {<<"/extend//cowboy">>, [<<"extend">>, <<>>, <<"cowboy">>]},
- {<<"/users">>, [<<"users">>]},
- {<<"/users/42/friends">>, [<<"users">>, <<"42">>, <<"friends">>]},
- {<<"/users/a+b/c%21d">>, [<<"users">>, <<"a b">>, <<"c!d">>]}
- ],
- [{P, fun() -> R = split_path(P) end} || {P, R} <- Tests].
-
-match_test_() ->
- Dispatch = [
- {[<<"www">>, '_', <<"ninenines">>, <<"eu">>], [
- {[<<"users">>, '_', <<"mails">>], match_any_subdomain_users, []}
- ]},
- {[<<"ninenines">>, <<"eu">>], [
- {[<<"users">>, id, <<"friends">>], match_extend_users_friends, []},
- {'_', match_extend, []}
- ]},
- {[<<"ninenines">>, 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.ninenines.eu">>, <<"/users/42/mails">>,
- {ok, match_any_subdomain_users, [], []}},
- {<<"www.ninenines.eu">>, <<"/users/42/mails">>,
- {ok, match_any, [], []}},
- {<<"www.ninenines.eu">>, <<"/">>,
- {ok, match_any, [], []}},
- {<<"www.any.ninenines.eu">>, <<"/not_users/42/mails">>,
- {error, notfound, path}},
- {<<"ninenines.eu">>, <<"/">>,
- {ok, match_extend, [], []}},
- {<<"ninenines.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">>}]}},
- {<<"ninenines.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(Dispatch, H, P)
- end} || {H, P, {ok, Handler, Opts, Binds}} <- Tests].
-
-match_info_test_() ->
- Dispatch = [
- {[<<"www">>, <<"ninenines">>, <<"eu">>], [
- {[<<"pathinfo">>, <<"is">>, <<"next">>, '...'], match_path, []}
- ]},
- {['...', <<"ninenines">>, <<"eu">>], [
- {'_', match_any, []}
- ]}
- ],
- Tests = [
- {<<"ninenines.eu">>, <<"/">>,
- {ok, match_any, [], [], [], undefined}},
- {<<"bugs.ninenines.eu">>, <<"/">>,
- {ok, match_any, [], [], [<<"bugs">>], undefined}},
- {<<"cowboy.bugs.ninenines.eu">>, <<"/">>,
- {ok, match_any, [], [], [<<"cowboy">>, <<"bugs">>], undefined}},
- {<<"www.ninenines.eu">>, <<"/pathinfo/is/next">>,
- {ok, match_path, [], [], undefined, []}},
- {<<"www.ninenines.eu">>, <<"/pathinfo/is/next/path_info">>,
- {ok, match_path, [], [], undefined, [<<"path_info">>]}},
- {<<"www.ninenines.eu">>, <<"/pathinfo/is/next/foo/bar">>,
- {ok, match_path, [], [], undefined, [<<"foo">>, <<"bar">>]}}
- ],
- [{lists:flatten(io_lib:format("~p, ~p", [H, P])), fun() ->
- R = match(Dispatch, H, P)
- end} || {H, P, R} <- Tests].
-
--endif.
diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl
index 7f7ef32..e9d5158 100644
--- a/src/cowboy_req.erl
+++ b/src/cowboy_req.erl
@@ -137,14 +137,14 @@
version = {1, 1} :: cowboy_http:version(),
peer = undefined :: undefined | {inet:ip_address(), inet:port_number()},
host = undefined :: undefined | binary(),
- host_info = undefined :: undefined | cowboy_dispatcher:tokens(),
+ host_info = undefined :: undefined | cowboy_router:tokens(),
port = undefined :: undefined | inet:port_number(),
path = undefined :: binary(),
- path_info = undefined :: undefined | cowboy_dispatcher:tokens(),
+ path_info = undefined :: undefined | cowboy_router:tokens(),
qs = undefined :: binary(),
qs_vals = undefined :: undefined | list({binary(), binary() | true}),
fragment = undefined :: binary(),
- bindings = undefined :: undefined | cowboy_dispatcher:bindings(),
+ bindings = undefined :: undefined | cowboy_router:bindings(),
headers = [] :: cowboy_http:headers(),
p_headers = [] :: [any()], %% @todo Improve those specs.
cookies = undefined :: undefined | [{binary(), binary()}],
@@ -256,7 +256,7 @@ host(Req) ->
%% @doc Return the extra host information obtained from partially matching
%% the hostname using <em>'...'</em>.
-spec host_info(Req)
- -> {cowboy_dispatcher:tokens() | undefined, Req} when Req::req().
+ -> {cowboy_router:tokens() | undefined, Req} when Req::req().
host_info(Req) ->
{Req#http_req.host_info, Req}.
@@ -273,7 +273,7 @@ path(Req) ->
%% @doc Return the extra path information obtained from partially matching
%% the patch using <em>'...'</em>.
-spec path_info(Req)
- -> {cowboy_dispatcher:tokens() | undefined, Req} when Req::req().
+ -> {cowboy_router:tokens() | undefined, Req} when Req::req().
path_info(Req) ->
{Req#http_req.path_info, Req}.
@@ -1122,8 +1122,8 @@ set([{transport, Val}|Tail], Req) -> set(Tail, Req#http_req{transport=Val});
set([{version, Val}|Tail], Req) -> set(Tail, Req#http_req{version=Val}).
%% @private
--spec set_bindings(cowboy_dispatcher:tokens(), cowboy_dispatcher:tokens(),
- cowboy_dispatcher:bindings(), Req) -> Req when Req::req().
+-spec set_bindings(cowboy_router:tokens(), cowboy_router:tokens(),
+ cowboy_router:bindings(), Req) -> Req when Req::req().
set_bindings(HostInfo, PathInfo, Bindings, Req) ->
Req#http_req{host_info=HostInfo, path_info=PathInfo,
bindings=Bindings}.
diff --git a/src/cowboy_router.erl b/src/cowboy_router.erl
index 35c9396..a4597ed 100644
--- a/src/cowboy_router.erl
+++ b/src/cowboy_router.erl
@@ -22,13 +22,147 @@
%%
%% If the route cannot be found, processing stops with either
%% a 400 or a 404 reply.
-%%
-%% @see cowboy_dispatcher
-module(cowboy_router).
-behaviour(cowboy_middleware).
+-export([compile/1]).
-export([execute/2]).
+-type bindings() :: [{atom(), binary()}].
+-type tokens() :: [binary()].
+-export_type([bindings/0]).
+-export_type([tokens/0]).
+
+-type constraints() :: [{atom(), int}
+ | {atom(), function, fun ((binary()) -> true | {true, any()} | false)}].
+-export_type([constraints/0]).
+
+-type route_match() :: binary() | string().
+-type route_path() :: {Path::route_match(), Handler::module(), Opts::any()}
+ | {Path::route_match(), constraints(), Handler::module(), Opts::any()}.
+-type route_rule() :: {Host::route_match(), Paths::[route_path()]}
+ | {Host::route_match(), constraints(), Paths::[route_path()]}.
+-opaque routes() :: [route_rule()].
+-export_type([routes/0]).
+
+-type dispatch_match() :: '_' | <<_:8>> | [binary() | '_' | '...' | atom()].
+-type dispatch_path() :: {dispatch_match(), module(), any()}.
+-type dispatch_rule() :: {Host::dispatch_match(), Paths::[dispatch_path()]}.
+-opaque dispatch_rules() :: [dispatch_rule()].
+-export_type([dispatch_rules/0]).
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+-endif.
+
+%% @doc Compile a list of routes into the dispatch format used
+%% by Cowboy's routing.
+-spec compile(routes()) -> dispatch_rules().
+compile(Routes) ->
+ compile(Routes, []).
+
+compile([], Acc) ->
+ lists:reverse(Acc);
+compile([{Host, Paths}|Tail], Acc) ->
+ compile([{Host, [], Paths}|Tail], Acc);
+compile([{HostMatch, Constraints, Paths}|Tail], Acc) ->
+ HostRules = case HostMatch of
+ '_' -> '_';
+ _ -> compile_host(HostMatch)
+ end,
+ PathRules = compile_paths(Paths, []),
+ Hosts = case HostRules of
+ '_' -> [{'_', Constraints, PathRules}];
+ _ -> [{R, Constraints, PathRules} || R <- HostRules]
+ end,
+ compile(Tail, Hosts ++ Acc).
+
+compile_host(HostMatch) when is_list(HostMatch) ->
+ compile_host(unicode:characters_to_binary(HostMatch));
+compile_host(HostMatch) when is_binary(HostMatch) ->
+ compile_rules(HostMatch, $., [], [], <<>>).
+
+compile_paths([], Acc) ->
+ lists:reverse(Acc);
+compile_paths([{PathMatch, Handler, Opts}|Tail], Acc) ->
+ compile_paths([{PathMatch, [], Handler, Opts}|Tail], Acc);
+compile_paths([{PathMatch, Constraints, Handler, Opts}|Tail], Acc)
+ when is_list(PathMatch) ->
+ compile_paths([{unicode:characters_to_binary(PathMatch),
+ Constraints, Handler, Opts}|Tail], Acc);
+compile_paths([{'_', Constraints, Handler, Opts}|Tail], Acc) ->
+ compile_paths(Tail, [{'_', Constraints, Handler, Opts}] ++ Acc);
+compile_paths([{<< $/, PathMatch/binary >>, Constraints, Handler, Opts}|Tail],
+ Acc) ->
+ PathRules = compile_rules(PathMatch, $/, [], [], <<>>),
+ Paths = [{lists:reverse(R), Constraints, Handler, Opts} || R <- PathRules],
+ compile_paths(Tail, Paths ++ Acc).
+
+compile_rules(<<>>, _, Segments, Rules, <<>>) ->
+ [Segments|Rules];
+compile_rules(<<>>, _, Segments, Rules, Acc) ->
+ [[Acc|Segments]|Rules];
+compile_rules(<< S, Rest/binary >>, S, Segments, Rules, <<>>) ->
+ compile_rules(Rest, S, Segments, Rules, <<>>);
+compile_rules(<< S, Rest/binary >>, S, Segments, Rules, Acc) ->
+ compile_rules(Rest, S, [Acc|Segments], Rules, <<>>);
+compile_rules(<< $:, Rest/binary >>, S, Segments, Rules, <<>>) ->
+ {NameBin, Rest2} = compile_binding(Rest, S, <<>>),
+ Name = binary_to_atom(NameBin, utf8),
+ compile_rules(Rest2, S, Segments, Rules, Name);
+compile_rules(<< $:, _/binary >>, _, _, _, _) ->
+ erlang:error(badarg);
+compile_rules(<< $[, $., $., $., $], Rest/binary >>, S, Segments, Rules, Acc)
+ when Acc =:= <<>> ->
+ compile_rules(Rest, S, ['...'|Segments], Rules, Acc);
+compile_rules(<< $[, $., $., $., $], Rest/binary >>, S, Segments, Rules, Acc) ->
+ compile_rules(Rest, S, ['...', Acc|Segments], Rules, Acc);
+compile_rules(<< $[, S, Rest/binary >>, S, Segments, Rules, Acc) ->
+ compile_brackets(Rest, S, [Acc|Segments], Rules);
+compile_rules(<< $[, Rest/binary >>, S, Segments, Rules, <<>>) ->
+ compile_brackets(Rest, S, Segments, Rules);
+%% Open bracket in the middle of a segment.
+compile_rules(<< $[, _/binary >>, _, _, _, _) ->
+ erlang:error(badarg);
+%% Missing an open bracket.
+compile_rules(<< $], _/binary >>, _, _, _, _) ->
+ erlang:error(badarg);
+compile_rules(<< C, Rest/binary >>, S, Segments, Rules, Acc) ->
+ compile_rules(Rest, S, Segments, Rules, << Acc/binary, C >>).
+
+%% Everything past $: until $. or $[ or $] or end of binary
+%% is the binding name.
+compile_binding(<<>>, _, <<>>) ->
+ erlang:error(badarg);
+compile_binding(Rest = <<>>, _, Acc) ->
+ {Acc, Rest};
+compile_binding(Rest = << C, _/binary >>, S, Acc)
+ when C =:= S; C =:= $[; C =:= $] ->
+ {Acc, Rest};
+compile_binding(<< C, Rest/binary >>, S, Acc) ->
+ compile_binding(Rest, S, << Acc/binary, C >>).
+
+compile_brackets(Rest, S, Segments, Rules) ->
+ {Bracket, Rest2} = compile_brackets_split(Rest, <<>>, 0),
+ Rules1 = compile_rules(Rest2, S, Segments, [], <<>>),
+ Rules2 = compile_rules(<< Bracket/binary, Rest2/binary >>,
+ S, Segments, [], <<>>),
+ Rules ++ Rules2 ++ Rules1.
+
+%% Missing a close bracket.
+compile_brackets_split(<<>>, _, _) ->
+ erlang:error(badarg);
+%% Make sure we don't confuse the closing bracket we're looking for.
+compile_brackets_split(<< C, Rest/binary >>, Acc, N) when C =:= $[ ->
+ compile_brackets_split(Rest, << Acc/binary, C >>, N + 1);
+compile_brackets_split(<< C, Rest/binary >>, Acc, N) when C =:= $], N > 0 ->
+ compile_brackets_split(Rest, << Acc/binary, C >>, N - 1);
+%% That's the right one.
+compile_brackets_split(<< $], Rest/binary >>, Acc, 0) ->
+ {Acc, Rest};
+compile_brackets_split(<< C, Rest/binary >>, Acc, N) ->
+ compile_brackets_split(Rest, << Acc/binary, C >>, N).
+
%% @private
-spec execute(Req, Env)
-> {ok, Req, Env} | {error, 400 | 404, Req}
@@ -36,7 +170,7 @@
execute(Req, Env) ->
{_, Dispatch} = lists:keyfind(dispatch, 1, Env),
[Host, Path] = cowboy_req:get([host, path], Req),
- case cowboy_dispatcher:match(Dispatch, Host, Path) of
+ case match(Dispatch, Host, Path) of
{ok, Handler, HandlerOpts, Bindings, HostInfo, PathInfo} ->
Req2 = cowboy_req:set_bindings(HostInfo, PathInfo, Bindings, Req),
{ok, Req2, [{handler, Handler}, {handler_opts, HandlerOpts}|Env]};
@@ -47,3 +181,385 @@ execute(Req, Env) ->
{error, notfound, path} ->
{error, 404, Req}
end.
+
+%% Internal.
+
+%% @doc Match hostname tokens and path tokens against dispatch rules.
+%%
+%% It is typically used for matching tokens for the hostname and path of
+%% the request against a global dispatch rule for your listener.
+%%
+%% Dispatch rules are a list of <em>{Hostname, PathRules}</em> tuples, with
+%% <em>PathRules</em> being a list of <em>{Path, HandlerMod, HandlerOpts}</em>.
+%%
+%% <em>Hostname</em> and <em>Path</em> are match rules and can be either the
+%% atom <em>'_'</em>, which matches everything, `<<"*">>', which match the
+%% wildcard path, or a list of tokens.
+%%
+%% Each token can be either a binary, the atom <em>'_'</em>,
+%% the atom '...' or a named atom. A binary token must match exactly,
+%% <em>'_'</em> matches everything for a single token, <em>'...'</em> matches
+%% everything for the rest of the tokens and a named atom will bind the
+%% corresponding token value and return it.
+%%
+%% The list of hostname tokens is reversed before matching. For example, if
+%% we were to match "www.ninenines.eu", we would first match "eu", then
+%% "ninenines", then "www". This means that in the context of hostnames,
+%% the <em>'...'</em> atom matches properly the lower levels of the domain
+%% as would be expected.
+%%
+%% When a result is found, this function will return the handler module and
+%% options found in the dispatch list, a key-value list of bindings and
+%% the tokens that were matched by the <em>'...'</em> atom for both the
+%% hostname and path.
+-spec match(dispatch_rules(), Host::binary() | tokens(), Path::binary())
+ -> {ok, module(), any(), bindings(),
+ HostInfo::undefined | tokens(),
+ PathInfo::undefined | tokens()}
+ | {error, notfound, host} | {error, notfound, path}
+ | {error, badrequest, path}.
+match([], _, _) ->
+ {error, notfound, host};
+%% If the host is '_' then there can be no constraints.
+match([{'_', [], PathMatchs}|_Tail], _, Path) ->
+ match_path(PathMatchs, undefined, Path, []);
+match([{HostMatch, Constraints, PathMatchs}|Tail], Tokens, Path)
+ when is_list(Tokens) ->
+ case list_match(Tokens, HostMatch, []) of
+ false ->
+ match(Tail, Tokens, Path);
+ {true, Bindings, HostInfo} ->
+ HostInfo2 = case HostInfo of
+ undefined -> undefined;
+ _ -> lists:reverse(HostInfo)
+ end,
+ case check_constraints(Constraints, Bindings) of
+ {ok, Bindings2} ->
+ match_path(PathMatchs, HostInfo2, Path, Bindings2);
+ nomatch ->
+ match(Tail, Tokens, Path)
+ end
+ end;
+match(Dispatch, Host, Path) ->
+ match(Dispatch, split_host(Host), Path).
+
+-spec match_path([dispatch_path()],
+ HostInfo::undefined | tokens(), binary() | tokens(), bindings())
+ -> {ok, module(), any(), bindings(),
+ HostInfo::undefined | tokens(),
+ PathInfo::undefined | tokens()}
+ | {error, notfound, path} | {error, badrequest, path}.
+match_path([], _, _, _) ->
+ {error, notfound, path};
+%% If the path is '_' then there can be no constraints.
+match_path([{'_', [], Handler, Opts}|_Tail], HostInfo, _, Bindings) ->
+ {ok, Handler, Opts, Bindings, HostInfo, undefined};
+match_path([{<<"*">>, _Constraints, Handler, Opts}|_Tail], HostInfo, <<"*">>, Bindings) ->
+ {ok, Handler, Opts, Bindings, HostInfo, undefined};
+match_path([{PathMatch, Constraints, Handler, Opts}|Tail], HostInfo, Tokens,
+ Bindings) when is_list(Tokens) ->
+ case list_match(Tokens, PathMatch, Bindings) of
+ false ->
+ match_path(Tail, HostInfo, Tokens, Bindings);
+ {true, PathBinds, PathInfo} ->
+ case check_constraints(Constraints, PathBinds) of
+ {ok, PathBinds2} ->
+ {ok, Handler, Opts, PathBinds2, HostInfo, PathInfo};
+ nomatch ->
+ match_path(Tail, HostInfo, Tokens, Bindings)
+ end
+ end;
+match_path(_Dispatch, _HostInfo, badrequest, _Bindings) ->
+ {error, badrequest, path};
+match_path(Dispatch, HostInfo, Path, Bindings) ->
+ match_path(Dispatch, HostInfo, split_path(Path), Bindings).
+
+check_constraints([], Bindings) ->
+ {ok, Bindings};
+check_constraints([Constraint|Tail], Bindings) ->
+ Name = element(1, Constraint),
+ case lists:keyfind(Name, 1, Bindings) of
+ false ->
+ check_constraints(Tail, Bindings);
+ {_, Value} ->
+ case check_constraint(Constraint, Value) of
+ true ->
+ check_constraints(Tail, Bindings);
+ {true, Value2} ->
+ Bindings2 = lists:keyreplace(Name, 1, Bindings,
+ {Name, Value2}),
+ check_constraints(Tail, Bindings2);
+ false ->
+ nomatch
+ end
+ end.
+
+check_constraint({_, int}, Value) ->
+ try {true, list_to_integer(binary_to_list(Value))}
+ catch _:_ -> false
+ end;
+check_constraint({_, function, Fun}, Value) ->
+ Fun(Value).
+
+%% @doc Split a hostname into a list of tokens.
+-spec split_host(binary()) -> tokens().
+split_host(Host) ->
+ split_host(Host, []).
+
+split_host(Host, Acc) ->
+ case binary:match(Host, <<".">>) of
+ nomatch when Host =:= <<>> ->
+ Acc;
+ nomatch ->
+ [Host|Acc];
+ {Pos, _} ->
+ << Segment:Pos/binary, _:8, Rest/bits >> = Host,
+ false = byte_size(Segment) == 0,
+ split_host(Rest, [Segment|Acc])
+ end.
+
+%% @doc Split a path into a list of path segments.
+%%
+%% Following RFC2396, this function may return path segments containing any
+%% character, including <em>/</em> if, and only if, a <em>/</em> was escaped
+%% and part of a path segment.
+-spec split_path(binary()) -> tokens().
+split_path(<< $/, Path/bits >>) ->
+ split_path(Path, []);
+split_path(_) ->
+ badrequest.
+
+split_path(Path, Acc) ->
+ try
+ case binary:match(Path, <<"/">>) of
+ nomatch when Path =:= <<>> ->
+ lists:reverse([cowboy_http:urldecode(S) || S <- Acc]);
+ nomatch ->
+ lists:reverse([cowboy_http:urldecode(S) || S <- [Path|Acc]]);
+ {Pos, _} ->
+ << Segment:Pos/binary, _:8, Rest/bits >> = Path,
+ split_path(Rest, [Segment|Acc])
+ end
+ catch
+ error:badarg ->
+ badrequest
+ end.
+
+-spec list_match(tokens(), dispatch_match(), bindings())
+ -> {true, bindings(), undefined | 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,
+%% unless V was already defined and E isn't identical to the previous value.
+list_match([E|Tail], [V|TailMatch], Binds) when is_atom(V) ->
+ case lists:keyfind(V, 1, Binds) of
+ {_, E} ->
+ list_match(Tail, TailMatch, Binds);
+ {_, _} ->
+ false;
+ false ->
+ list_match(Tail, TailMatch, [{V, E}|Binds])
+ end;
+%% Match complete.
+list_match([], [], Binds) ->
+ {true, Binds, undefined};
+%% Values don't match, stop.
+list_match(_List, _Match, _Binds) ->
+ false.
+
+%% Tests.
+
+-ifdef(TEST).
+
+compile_test_() ->
+ %% {Routes, Result}
+ Tests = [
+ %% Match any host and path.
+ {[{'_', [{'_', h, o}]}],
+ [{'_', [], [{'_', [], h, o}]}]},
+ {[{"cowboy.example.org",
+ [{"/", ha, oa}, {"/path/to/resource", hb, ob}]}],
+ [{[<<"org">>, <<"example">>, <<"cowboy">>], [], [
+ {[], [], ha, oa},
+ {[<<"path">>, <<"to">>, <<"resource">>], [], hb, ob}]}]},
+ {[{'_', [{"/path/to/resource/", h, o}]}],
+ [{'_', [], [{[<<"path">>, <<"to">>, <<"resource">>], [], h, o}]}]},
+ {[{"cowboy.example.org.", [{'_', h, o}]}],
+ [{[<<"org">>, <<"example">>, <<"cowboy">>], [], [{'_', [], h, o}]}]},
+ {[{".cowboy.example.org", [{'_', h, o}]}],
+ [{[<<"org">>, <<"example">>, <<"cowboy">>], [], [{'_', [], h, o}]}]},
+ {[{":subdomain.example.org", [{"/hats/:name/prices", h, o}]}],
+ [{[<<"org">>, <<"example">>, subdomain], [], [
+ {[<<"hats">>, name, <<"prices">>], [], h, o}]}]},
+ {[{"ninenines.:_", [{"/hats/:_", h, o}]}],
+ [{['_', <<"ninenines">>], [], [{[<<"hats">>, '_'], [], h, o}]}]},
+ {[{"[www.]ninenines.eu",
+ [{"/horses", h, o}, {"/hats/[page/:number]", h, o}]}], [
+ {[<<"eu">>, <<"ninenines">>], [], [
+ {[<<"horses">>], [], h, o},
+ {[<<"hats">>], [], h, o},
+ {[<<"hats">>, <<"page">>, number], [], h, o}]},
+ {[<<"eu">>, <<"ninenines">>, <<"www">>], [], [
+ {[<<"horses">>], [], h, o},
+ {[<<"hats">>], [], h, o},
+ {[<<"hats">>, <<"page">>, number], [], h, o}]}]},
+ {[{'_', [{"/hats/[page/[:number]]", h, o}]}], [{'_', [], [
+ {[<<"hats">>], [], h, o},
+ {[<<"hats">>, <<"page">>], [], h, o},
+ {[<<"hats">>, <<"page">>, number], [], h, o}]}]},
+ {[{"[...]ninenines.eu", [{"/hats/[...]", h, o}]}],
+ [{[<<"eu">>, <<"ninenines">>, '...'], [], [
+ {[<<"hats">>, '...'], [], h, o}]}]}
+ ],
+ [{lists:flatten(io_lib:format("~p", [Rt])),
+ fun() -> Rs = compile(Rt) end} || {Rt, Rs} <- Tests].
+
+split_host_test_() ->
+ %% {Host, Result}
+ Tests = [
+ {<<"">>, []},
+ {<<"*">>, [<<"*">>]},
+ {<<"cowboy.ninenines.eu">>,
+ [<<"eu">>, <<"ninenines">>, <<"cowboy">>]},
+ {<<"ninenines.eu">>,
+ [<<"eu">>, <<"ninenines">>]},
+ {<<"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">>,
+ [<<"z">>, <<"y">>, <<"x">>, <<"w">>, <<"v">>, <<"u">>, <<"t">>,
+ <<"s">>, <<"r">>, <<"q">>, <<"p">>, <<"o">>, <<"n">>, <<"m">>,
+ <<"l">>, <<"k">>, <<"j">>, <<"i">>, <<"h">>, <<"g">>, <<"f">>,
+ <<"e">>, <<"d">>, <<"c">>, <<"b">>, <<"a">>]}
+ ],
+ [{H, fun() -> R = split_host(H) end} || {H, R} <- Tests].
+
+split_path_test_() ->
+ %% {Path, Result, QueryString}
+ Tests = [
+ {<<"/">>, []},
+ {<<"/extend//cowboy">>, [<<"extend">>, <<>>, <<"cowboy">>]},
+ {<<"/users">>, [<<"users">>]},
+ {<<"/users/42/friends">>, [<<"users">>, <<"42">>, <<"friends">>]},
+ {<<"/users/a+b/c%21d">>, [<<"users">>, <<"a b">>, <<"c!d">>]}
+ ],
+ [{P, fun() -> R = split_path(P) end} || {P, R} <- Tests].
+
+match_test_() ->
+ Dispatch = [
+ {[<<"eu">>, <<"ninenines">>, '_', <<"www">>], [], [
+ {[<<"users">>, '_', <<"mails">>], [], match_any_subdomain_users, []}
+ ]},
+ {[<<"eu">>, <<"ninenines">>], [], [
+ {[<<"users">>, id, <<"friends">>], [], match_extend_users_friends, []},
+ {'_', [], match_extend, []}
+ ]},
+ {[var, <<"ninenines">>], [], [
+ {[<<"threads">>, var], [], match_duplicate_vars,
+ [we, {expect, two}, var, here]}
+ ]},
+ {[ext, <<"erlang">>], [], [
+ {'_', [], match_erlang_ext, []}
+ ]},
+ {'_', [], [
+ {[<<"users">>, id, <<"friends">>], [], match_users_friends, []},
+ {'_', [], match_any, []}
+ ]}
+ ],
+ %% {Host, Path, Result}
+ Tests = [
+ {<<"any">>, <<"/">>, {ok, match_any, [], []}},
+ {<<"www.any.ninenines.eu">>, <<"/users/42/mails">>,
+ {ok, match_any_subdomain_users, [], []}},
+ {<<"www.ninenines.eu">>, <<"/users/42/mails">>,
+ {ok, match_any, [], []}},
+ {<<"www.ninenines.eu">>, <<"/">>,
+ {ok, match_any, [], []}},
+ {<<"www.any.ninenines.eu">>, <<"/not_users/42/mails">>,
+ {error, notfound, path}},
+ {<<"ninenines.eu">>, <<"/">>,
+ {ok, match_extend, [], []}},
+ {<<"ninenines.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">>}]}}
+ ],
+ [{lists:flatten(io_lib:format("~p, ~p", [H, P])), fun() ->
+ {ok, Handler, Opts, Binds, undefined, undefined}
+ = match(Dispatch, H, P)
+ end} || {H, P, {ok, Handler, Opts, Binds}} <- Tests].
+
+match_info_test_() ->
+ Dispatch = [
+ {[<<"eu">>, <<"ninenines">>, <<"www">>], [], [
+ {[<<"pathinfo">>, <<"is">>, <<"next">>, '...'], [], match_path, []}
+ ]},
+ {[<<"eu">>, <<"ninenines">>, '...'], [], [
+ {'_', [], match_any, []}
+ ]}
+ ],
+ Tests = [
+ {<<"ninenines.eu">>, <<"/">>,
+ {ok, match_any, [], [], [], undefined}},
+ {<<"bugs.ninenines.eu">>, <<"/">>,
+ {ok, match_any, [], [], [<<"bugs">>], undefined}},
+ {<<"cowboy.bugs.ninenines.eu">>, <<"/">>,
+ {ok, match_any, [], [], [<<"cowboy">>, <<"bugs">>], undefined}},
+ {<<"www.ninenines.eu">>, <<"/pathinfo/is/next">>,
+ {ok, match_path, [], [], undefined, []}},
+ {<<"www.ninenines.eu">>, <<"/pathinfo/is/next/path_info">>,
+ {ok, match_path, [], [], undefined, [<<"path_info">>]}},
+ {<<"www.ninenines.eu">>, <<"/pathinfo/is/next/foo/bar">>,
+ {ok, match_path, [], [], undefined, [<<"foo">>, <<"bar">>]}}
+ ],
+ [{lists:flatten(io_lib:format("~p, ~p", [H, P])), fun() ->
+ R = match(Dispatch, H, P)
+ end} || {H, P, R} <- Tests].
+
+match_constraints_test() ->
+ Dispatch = [{'_', [],
+ [{[<<"path">>, value], [{value, int}], match, []}]}],
+ {ok, _, [], [{value, 123}], _, _} = match(Dispatch,
+ <<"ninenines.eu">>, <<"/path/123">>),
+ {ok, _, [], [{value, 123}], _, _} = match(Dispatch,
+ <<"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.
+
+match_same_bindings_test() ->
+ Dispatch = [{[same, same], [], [{'_', [], match, []}]}],
+ {ok, _, [], [{same, <<"eu">>}], _, _} = match(Dispatch,
+ <<"eu.eu">>, <<"/">>),
+ {error, notfound, host} = match(Dispatch,
+ <<"ninenines.eu">>, <<"/">>),
+ Dispatch2 = [{[<<"eu">>, <<"ninenines">>, user], [],
+ [{[<<"path">>, user], [], match, []}]}],
+ {ok, _, [], [{user, <<"essen">>}], _, _} = match(Dispatch2,
+ <<"essen.ninenines.eu">>, <<"/path/essen">>),
+ {ok, _, [], [{user, <<"essen">>}], _, _} = match(Dispatch2,
+ <<"essen.ninenines.eu">>, <<"/path/essen/">>),
+ {error, notfound, path} = match(Dispatch2,
+ <<"essen.ninenines.eu">>, <<"/path/notessen">>),
+ Dispatch3 = [{'_', [], [{[same, same], [], match, []}]}],
+ {ok, _, [], [{same, <<"path">>}], _, _} = match(Dispatch3,
+ <<"ninenines.eu">>, <<"/path/path">>),
+ {error, notfound, path} = match(Dispatch3,
+ <<"ninenines.eu">>, <<"/path/to">>),
+ ok.
+
+-endif.
diff --git a/test/autobahn_SUITE.erl b/test/autobahn_SUITE.erl
index f9d5a01..61cf631 100644
--- a/test/autobahn_SUITE.erl
+++ b/test/autobahn_SUITE.erl
@@ -75,8 +75,8 @@ end_per_group(Listener, _Config) ->
%% Dispatch configuration.
init_dispatch() ->
- [{[<<"localhost">>], [
- {[<<"echo">>], websocket_echo_handler, []}]}].
+ cowboy_router:compile([{"localhost", [
+ {"/echo", websocket_echo_handler, []}]}]).
%% autobahn cases
diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl
index 61316db..25ce595 100644
--- a/test/http_SUITE.erl
+++ b/test/http_SUITE.erl
@@ -283,58 +283,58 @@ end_per_group(Name, _) ->
%% Dispatch configuration.
init_dispatch(Config) ->
- [
- {[<<"localhost">>], [
- {[<<"chunked_response">>], chunked_handler, []},
- {[<<"init_shutdown">>], http_handler_init_shutdown, []},
- {[<<"long_polling">>], http_handler_long_polling, []},
- {[<<"headers">>, <<"dupe">>], http_handler,
+ cowboy_router:compile([
+ {"localhost", [
+ {"/chunked_response", chunked_handler, []},
+ {"/init_shutdown", http_handler_init_shutdown, []},
+ {"/long_polling", http_handler_long_polling, []},
+ {"/headers/dupe", http_handler,
[{headers, [{<<"connection">>, <<"close">>}]}]},
- {[<<"set_resp">>, <<"header">>], http_handler_set_resp,
+ {"/set_resp/header", http_handler_set_resp,
[{headers, [{<<"vary">>, <<"Accept">>}]}]},
- {[<<"set_resp">>, <<"overwrite">>], http_handler_set_resp,
+ {"/set_resp/overwrite", http_handler_set_resp,
[{headers, [{<<"server">>, <<"DesireDrive/1.0">>}]}]},
- {[<<"set_resp">>, <<"body">>], http_handler_set_resp,
+ {"/set_resp/body", http_handler_set_resp,
[{body, <<"A flameless dance does not equal a cycle">>}]},
- {[<<"stream_body">>, <<"set_resp">>], http_handler_stream_body,
+ {"/stream_body/set_resp", http_handler_stream_body,
[{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
- {[<<"stream_body">>, <<"set_resp_close">>],
+ {"/stream_body/set_resp_close",
http_handler_stream_body, [
{reply, set_resp_close},
{body, <<"stream_body_set_resp_close">>}]},
- {[<<"static">>, '...'], cowboy_static,
+ {"/static/[...]", cowboy_static,
[{directory, ?config(static_dir, Config)},
{mimetypes, [{<<".css">>, [<<"text/css">>]}]}]},
- {[<<"static_mimetypes_function">>, '...'], cowboy_static,
+ {"/static_mimetypes_function/[...]", cowboy_static,
[{directory, ?config(static_dir, Config)},
{mimetypes, {fun(Path, data) when is_binary(Path) ->
[<<"text/html">>] end, data}}]},
- {[<<"handler_errors">>], http_handler_errors, []},
- {[<<"static_attribute_etag">>, '...'], cowboy_static,
+ {"/handler_errors", http_handler_errors, []},
+ {"/static_attribute_etag/[...]", cowboy_static,
[{directory, ?config(static_dir, Config)},
{etag, {attributes, [filepath, filesize, inode, mtime]}}]},
- {[<<"static_function_etag">>, '...'], cowboy_static,
+ {"/static_function_etag/[...]", cowboy_static,
[{directory, ?config(static_dir, Config)},
{etag, {fun static_function_etag/2, etag_data}}]},
- {[<<"static_specify_file">>, '...'], cowboy_static,
+ {"/static_specify_file/[...]", cowboy_static,
[{directory, ?config(static_dir, Config)},
{mimetypes, [{<<".css">>, [<<"text/css">>]}]},
{file, <<"test_file.css">>}]},
- {[<<"multipart">>], http_handler_multipart, []},
- {[<<"echo">>, <<"body">>], http_handler_echo_body, []},
- {[<<"bad_accept">>], rest_simple_resource, []},
- {[<<"simple">>], rest_simple_resource, []},
- {[<<"forbidden_post">>], rest_forbidden_resource, [true]},
- {[<<"simple_post">>], rest_forbidden_resource, [false]},
- {[<<"missing_get_callbacks">>], rest_missing_callbacks, []},
- {[<<"missing_put_callbacks">>], rest_missing_callbacks, []},
- {[<<"nodelete">>], rest_nodelete_resource, []},
- {[<<"resetags">>], rest_resource_etags, []},
- {[<<"rest_expires">>], rest_expires, []},
- {[<<"loop_timeout">>], http_handler_loop_timeout, []},
- {[], http_handler, []}
+ {"/multipart", http_handler_multipart, []},
+ {"/echo/body", http_handler_echo_body, []},
+ {"/bad_accept", rest_simple_resource, []},
+ {"/simple", rest_simple_resource, []},
+ {"/forbidden_post", rest_forbidden_resource, [true]},
+ {"/simple_post", rest_forbidden_resource, [false]},
+ {"/missing_get_callbacks", rest_missing_callbacks, []},
+ {"/missing_put_callbacks", rest_missing_callbacks, []},
+ {"/nodelete", rest_nodelete_resource, []},
+ {"/resetags", rest_resource_etags, []},
+ {"/rest_expires", rest_expires, []},
+ {"/loop_timeout", http_handler_loop_timeout, []},
+ {"/", http_handler, []}
]}
- ].
+ ]).
init_static_dir(Config) ->
Dir = filename:join(?config(priv_dir, Config), "static"),
@@ -576,8 +576,8 @@ http10_hostless(Config) ->
ranch:start_listener(Name, 5,
?config(transport, Config), ?config(opts, Config) ++ [{port, Port10}],
cowboy_protocol, [
- {env, [{dispatch, [{'_', [
- {[<<"http1.0">>, <<"hostless">>], http_handler, []}]}]}]},
+ {env, [{dispatch, cowboy_router:compile([
+ {'_', [{"/http1.0/hostless", http_handler, []}]}])}]},
{max_keepalive, 50},
{timeout, 500}]
),
diff --git a/test/ws_SUITE.erl b/test/ws_SUITE.erl
index 61bb236..06d4b3e 100644
--- a/test/ws_SUITE.erl
+++ b/test/ws_SUITE.erl
@@ -88,35 +88,35 @@ end_per_group(Listener, _Config) ->
%% Dispatch configuration.
init_dispatch() ->
- [
- {[<<"localhost">>], [
- {[<<"websocket">>], websocket_handler, []},
- {[<<"ws_echo_handler">>], websocket_echo_handler, []},
- {[<<"ws_init_shutdown">>], websocket_handler_init_shutdown, []},
- {[<<"ws_send_many">>], ws_send_many_handler, [
+ cowboy_router:compile([
+ {"localhost", [
+ {"/websocket", websocket_handler, []},
+ {"/ws_echo_handler", websocket_echo_handler, []},
+ {"/ws_init_shutdown", websocket_handler_init_shutdown, []},
+ {"/ws_send_many", ws_send_many_handler, [
{sequence, [
{text, <<"one">>},
{text, <<"two">>},
{text, <<"seven!">>}]}
]},
- {[<<"ws_send_close">>], ws_send_many_handler, [
+ {"/ws_send_close", ws_send_many_handler, [
{sequence, [
{text, <<"send">>},
close,
{text, <<"won't be received">>}]}
]},
- {[<<"ws_send_close_payload">>], ws_send_many_handler, [
+ {"/ws_send_close_payload", ws_send_many_handler, [
{sequence, [
{text, <<"send">>},
{close, 1001, <<"some text!">>},
{text, <<"won't be received">>}]}
]},
- {[<<"ws_timeout_hibernate">>], ws_timeout_hibernate_handler, []},
- {[<<"ws_timeout_cancel">>], ws_timeout_cancel_handler, []},
- {[<<"ws_upgrade_with_opts">>], ws_upgrade_with_opts_handler,
+ {"/ws_timeout_hibernate", ws_timeout_hibernate_handler, []},
+ {"/ws_timeout_cancel", ws_timeout_cancel_handler, []},
+ {"/ws_upgrade_with_opts", ws_upgrade_with_opts_handler,
<<"failure">>}
]}
- ].
+ ]).
%% ws and wss.