From 6b14cd64fcfd607d7a9a4e24e95c49cd3643cb8b Mon Sep 17 00:00:00 2001 From: geeksilva97 Date: Sat, 3 Feb 2024 18:49:37 -0300 Subject: [PATCH] return allow header --- src/cowboy_rest.erl | 8 ++++++-- test/rest_handler_SUITE.erl | 12 ++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/cowboy_rest.erl b/src/cowboy_rest.erl index 003e5f9d..b5dc1196 100644 --- a/src/cowboy_rest.erl +++ b/src/cowboy_rest.erl @@ -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); diff --git a/test/rest_handler_SUITE.erl b/test/rest_handler_SUITE.erl index 510098f1..d43b8f16 100644 --- a/test/rest_handler_SUITE.erl +++ b/test/rest_handler_SUITE.erl @@ -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.