Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return allow header #1633

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
23 changes: 16 additions & 7 deletions src/cowboy_rest.erl
Original file line number Diff line number Diff line change
Expand Up @@ -324,29 +324,38 @@ known_methods(Req, State=#state{method=Method}) ->
uri_too_long(Req, State) ->
expect(Req, State, uri_too_long, false, fun allowed_methods/2, 414).

stringify_allowed_methods(MethodList) when is_list(MethodList) ->
<< ", ", Allow/binary >> = << << ", ", M/binary >> || M <- MethodList >>,
Allow.

%% allowed_methods/2 should return a list of binary methods.
allowed_methods(Req, State=#state{method=Method}) ->
DefaultAllowedMethods = [<<"HEAD">>, <<"GET">>, <<"OPTIONS">>],
case call(Req, State, allowed_methods) of
no_call when Method =:= <<"HEAD">>; Method =:= <<"GET">> ->
next(Req, State, fun malformed_request/2);
Allow = stringify_allowed_methods(DefaultAllowedMethods),
Req2 = cowboy_req:set_resp_header(<<"allow">>, Allow, Req),
next(Req2, State, fun malformed_request/2);
no_call when Method =:= <<"OPTIONS">> ->
geeksilva97 marked this conversation as resolved.
Show resolved Hide resolved
next(Req, State#state{allowed_methods=
[<<"HEAD">>, <<"GET">>, <<"OPTIONS">>]},
next(Req, State#state{allowed_methods=DefaultAllowedMethods},
fun malformed_request/2);
no_call ->
method_not_allowed(Req, State,
[<<"HEAD">>, <<"GET">>, <<"OPTIONS">>]);
method_not_allowed(Req, State, DefaultAllowedMethods);
{stop, Req2, State2} ->
terminate(Req2, State2);
{Switch, Req2, State2} when element(1, Switch) =:= switch_handler ->
switch_handler(Switch, Req2, State2);
{List, Req2, State2} ->
case lists:member(Method, List) of
true when Method =:= <<"OPTIONS">> ->
next(Req2, State2#state{allowed_methods=List},
Allow = stringify_allowed_methods(List),
Req3 = cowboy_req:set_resp_header(<<"allow">>, Allow, Req2),
next(Req3, State2#state{allowed_methods=List},
fun malformed_request/2);
true ->
next(Req2, State2, fun malformed_request/2);
Allow = stringify_allowed_methods(List),
Req3 = cowboy_req:set_resp_header(<<"allow">>, Allow, Req2),
next(Req3, State2, fun malformed_request/2);
false ->
method_not_allowed(Req2, State2, List)
end
Expand Down
19 changes: 17 additions & 2 deletions test/rest_handler_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,8 @@ delete_resource_missing(Config) ->
Ref = gun:delete(ConnPid, "/delete_resource?missing", [
{<<"accept-encoding">>, <<"gzip">>}
]),
{response, _, 500, _} = gun:await(ConnPid, Ref),
{response, _, 500, Headers} = gun:await(ConnPid, Ref),
{_, <<"DELETE">>} = lists:keyfind(<<"allow">>, 1, Headers),
ok.

create_resource_created(Config) ->
Expand All @@ -483,7 +484,8 @@ create_resource_created(Config) ->
Ref = gun:post(ConnPid, "/create_resource?created", [
{<<"content-type">>, <<"application/text">>}
], <<"hello">>, #{}),
{response, _, 201, _} = gun:await(ConnPid, Ref),
{response, _, 201, Headers} = gun:await(ConnPid, Ref),
{_, <<"POST">>} = lists:keyfind(<<"allow">>, 1, Headers),
geeksilva97 marked this conversation as resolved.
Show resolved Hide resolved
ok.

create_resource_see_other(Config) ->
Expand All @@ -496,6 +498,7 @@ create_resource_see_other(Config) ->
], <<"hello">>, #{}),
{response, _, 303, RespHeaders} = gun:await(ConnPid, Ref),
{_, _} = lists:keyfind(<<"location">>, 1, RespHeaders),
{_, <<"POST">>} = lists:keyfind(<<"allow">>, 1, RespHeaders),
ok.

error_on_malformed_accept(Config) ->
Expand Down Expand Up @@ -778,6 +781,17 @@ last_modified_missing(Config) ->
false = lists:keyfind(<<"last-modified">>, 1, Headers),
ok.

head_call(Config) ->
doc("A successful HEAD request to a simple handler results in "
"a 200 OK response with the allow header set. (RFC7231 4.3.7)"),
ConnPid = gun_open(Config),
Ref = gun:head(ConnPid, "/", [
{<<"accept-encoding">>, <<"gzip">>}
]),
{response, fin, 200, Headers} = gun:await(ConnPid, Ref),
{_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
ok.

options_missing(Config) ->
doc("A successful OPTIONS request to a simple handler results in "
"a 200 OK response with the allow header set. (RFC7231 4.3.7)"),
Expand All @@ -799,6 +813,7 @@ provide_callback(Config) ->
]),
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
{_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
{_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
{ok, <<"This is REST!">>} = gun:await_body(ConnPid, Ref),
ok.

Expand Down