0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

return allow header

This commit is contained in:
geeksilva97 2024-02-03 18:49:37 -03:00
parent 5fe1be7007
commit 6b14cd64fc
2 changed files with 18 additions and 2 deletions

View file

@ -328,7 +328,9 @@ uri_too_long(Req, State) ->
allowed_methods(Req, State=#state{method=Method}) ->
case call(Req, State, allowed_methods) of
no_call when Method =:= <<"HEAD">>; Method =:= <<"GET">> ->
next(Req, State, fun malformed_request/2);
Allow = <<"HEAD, GET, OPTIONS">>,
Req2 = cowboy_req:set_resp_header(<<"allow">>, Allow, Req),
next(Req2, State, fun malformed_request/2);
no_call when Method =:= <<"OPTIONS">> ->
next(Req, State#state{allowed_methods=
[<<"HEAD">>, <<"GET">>, <<"OPTIONS">>]},
@ -343,7 +345,9 @@ allowed_methods(Req, State=#state{method=Method}) ->
{List, Req2, State2} ->
case lists:member(Method, List) of
true when Method =:= <<"OPTIONS">> ->
next(Req2, State2#state{allowed_methods=List},
<< ", ", Allow/binary >> = << << ", ", M/binary >> || M <- 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);

View file

@ -778,6 +778,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)"),
@ -799,6 +810,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.