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

add function to compute allow header content from list

This commit is contained in:
geeksilva97 2024-02-04 10:22:11 -03:00
parent 6b14cd64fc
commit 3df6822ade

View file

@ -324,16 +324,20 @@ known_methods(Req, State=#state{method=Method}) ->
uri_too_long(Req, State) -> uri_too_long(Req, State) ->
expect(Req, State, uri_too_long, false, fun allowed_methods/2, 414). 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/2 should return a list of binary methods.
allowed_methods(Req, State=#state{method=Method}) -> allowed_methods(Req, State=#state{method=Method}) ->
DefaultAllowedMethods = [<<"HEAD">>, <<"GET">>, <<"OPTIONS">>],
case call(Req, State, allowed_methods) of case call(Req, State, allowed_methods) of
no_call when Method =:= <<"HEAD">>; Method =:= <<"GET">> -> 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), Req2 = cowboy_req:set_resp_header(<<"allow">>, Allow, Req),
next(Req2, State, fun malformed_request/2); next(Req2, State, fun malformed_request/2);
no_call when Method =:= <<"OPTIONS">> -> no_call when Method =:= <<"OPTIONS">> ->
next(Req, State#state{allowed_methods= next(Req, State#state{allowed_methods=DefaultAllowedMethods},
[<<"HEAD">>, <<"GET">>, <<"OPTIONS">>]},
fun malformed_request/2); fun malformed_request/2);
no_call -> no_call ->
method_not_allowed(Req, State, method_not_allowed(Req, State,