From 3df6822ade362d37fe2f6dd146ce5e4f7602e9dd Mon Sep 17 00:00:00 2001 From: geeksilva97 Date: Sun, 4 Feb 2024 10:22:11 -0300 Subject: [PATCH] add function to compute allow header content from list --- src/cowboy_rest.erl | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/cowboy_rest.erl b/src/cowboy_rest.erl index b5dc1196..8efcfc5b 100644 --- a/src/cowboy_rest.erl +++ b/src/cowboy_rest.erl @@ -324,16 +324,20 @@ 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">> -> - Allow = <<"HEAD, GET, OPTIONS">>, + 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">> -> - 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,