mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Add support for the '*' path.
Mostly used by the following request: OPTIONS * HTTP/1.1
This commit is contained in:
parent
2c52a30b0a
commit
a4f8bb6573
3 changed files with 13 additions and 4 deletions
|
@ -25,8 +25,8 @@
|
||||||
-type port_number() :: 0..65535.
|
-type port_number() :: 0..65535.
|
||||||
|
|
||||||
-type bindings() :: list({Key::atom(), Value::string()}).
|
-type bindings() :: list({Key::atom(), Value::string()}).
|
||||||
-type path_tokens() :: list(string()).
|
-type path_tokens() :: '*' | list(string()).
|
||||||
-type match() :: '_' | list(string() | '_' | atom()).
|
-type match() :: '_' | '*' | list(string() | '_' | atom()).
|
||||||
|
|
||||||
-type dispatch_rules() :: {Host::match(), list({Path::match(),
|
-type dispatch_rules() :: {Host::match(), list({Path::match(),
|
||||||
Handler::module(), Opts::term()})}.
|
Handler::module(), Opts::term()})}.
|
||||||
|
|
|
@ -25,6 +25,8 @@ split_host(Host) ->
|
||||||
string:tokens(Host, ".").
|
string:tokens(Host, ".").
|
||||||
|
|
||||||
-spec split_path(Path::string()) -> {Tokens::path_tokens(), Qs::string()}.
|
-spec split_path(Path::string()) -> {Tokens::path_tokens(), Qs::string()}.
|
||||||
|
split_path('*') ->
|
||||||
|
{'*', []};
|
||||||
split_path(Path) ->
|
split_path(Path) ->
|
||||||
case string:chr(Path, $?) of
|
case string:chr(Path, $?) of
|
||||||
0 ->
|
0 ->
|
||||||
|
@ -57,6 +59,8 @@ match_path(_Path, [], _HostBinds) ->
|
||||||
{error, notfound, path};
|
{error, notfound, path};
|
||||||
match_path(_Path, [{'_', Handler, Opts}|_Tail], HostBinds) ->
|
match_path(_Path, [{'_', Handler, Opts}|_Tail], HostBinds) ->
|
||||||
{ok, Handler, Opts, HostBinds};
|
{ok, Handler, Opts, HostBinds};
|
||||||
|
match_path('*', [{'*', Handler, Opts}|_Tail], HostBinds) ->
|
||||||
|
{ok, Handler, Opts, HostBinds};
|
||||||
match_path(Path, [{PathMatch, Handler, Opts}|Tail], HostBinds) ->
|
match_path(Path, [{PathMatch, Handler, Opts}|Tail], HostBinds) ->
|
||||||
case try_match(path, Path, PathMatch) of
|
case try_match(path, Path, PathMatch) of
|
||||||
false ->
|
false ->
|
||||||
|
@ -120,7 +124,6 @@ split_path_test_() ->
|
||||||
Tests = [
|
Tests = [
|
||||||
{"?", [], []},
|
{"?", [], []},
|
||||||
{"???", [], "??"},
|
{"???", [], "??"},
|
||||||
{"*", ["*"], []},
|
|
||||||
{"/", [], []},
|
{"/", [], []},
|
||||||
{"/users", ["users"], []},
|
{"/users", ["users"], []},
|
||||||
{"/users?", ["users"], []},
|
{"/users?", ["users"], []},
|
||||||
|
@ -128,7 +131,8 @@ split_path_test_() ->
|
||||||
{"/users/42/friends?a=b&c=d&e=notsure?whatever",
|
{"/users/42/friends?a=b&c=d&e=notsure?whatever",
|
||||||
["users", "42", "friends"], "a=b&c=d&e=notsure?whatever"}
|
["users", "42", "friends"], "a=b&c=d&e=notsure?whatever"}
|
||||||
],
|
],
|
||||||
[{P, fun() -> {R, Qs} = split_path(P) end} || {P, R, Qs} <- Tests].
|
[{"atom '*'", fun() -> {'*', []} = split_path('*') end}]
|
||||||
|
++ [{P, fun() -> {R, Qs} = split_path(P) end} || {P, R, Qs} <- Tests].
|
||||||
|
|
||||||
match_test_() ->
|
match_test_() ->
|
||||||
Dispatch = [
|
Dispatch = [
|
||||||
|
|
|
@ -68,6 +68,11 @@ request({http_request, Method, {abs_path, AbsPath}, Version},
|
||||||
{ok, Peer} = Transport:peername(Socket),
|
{ok, Peer} = Transport:peername(Socket),
|
||||||
wait_header(#http_req{socket=Socket, transport=Transport, method=Method,
|
wait_header(#http_req{socket=Socket, transport=Transport, method=Method,
|
||||||
version=Version, peer=Peer, path=Path, raw_qs=Qs}, State);
|
version=Version, peer=Peer, path=Path, raw_qs=Qs}, State);
|
||||||
|
request({http_request, Method, '*', Version},
|
||||||
|
State=#state{socket=Socket, transport=Transport}) ->
|
||||||
|
{ok, Peer} = Transport:peername(Socket),
|
||||||
|
wait_header(#http_req{socket=Socket, transport=Transport, method=Method,
|
||||||
|
version=Version, peer=Peer, path='*', raw_qs=[]}, State);
|
||||||
request({http_request, _Method, _URI, _Version}, State) ->
|
request({http_request, _Method, _URI, _Version}, State) ->
|
||||||
error_terminate(501, State);
|
error_terminate(501, State);
|
||||||
request({http_error, "\r\n"}, State) ->
|
request({http_error, "\r\n"}, State) ->
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue