mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
Build Allow header with a binary comprehension
This commit is contained in:
parent
5755d1155f
commit
8d7350fa84
1 changed files with 6 additions and 15 deletions
|
@ -146,20 +146,10 @@ allowed_methods(Req, State=#state{method=Method}) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
method_not_allowed(Req, State, Methods) ->
|
method_not_allowed(Req, State, Methods) ->
|
||||||
Req2 = cowboy_req:set_resp_header(
|
<< ", ", Allow/binary >> = << << ", ", M/binary >> || M <- Methods >>,
|
||||||
<<"allow">>, build_allow_header(Methods, []), Req),
|
Req2 = cowboy_req:set_resp_header(<<"allow">>, Allow, Req),
|
||||||
respond(Req2, State, 405).
|
respond(Req2, State, 405).
|
||||||
|
|
||||||
build_allow_header([], []) ->
|
|
||||||
<<>>;
|
|
||||||
build_allow_header([], [_Ignore|Acc]) ->
|
|
||||||
lists:reverse(Acc);
|
|
||||||
build_allow_header([Method|Tail], Acc) when is_atom(Method) ->
|
|
||||||
Method2 = list_to_binary(atom_to_list(Method)),
|
|
||||||
build_allow_header(Tail, [<<", ">>, Method2|Acc]);
|
|
||||||
build_allow_header([Method|Tail], Acc) ->
|
|
||||||
build_allow_header(Tail, [<<", ">>, Method|Acc]).
|
|
||||||
|
|
||||||
malformed_request(Req, State) ->
|
malformed_request(Req, State) ->
|
||||||
expect(Req, State, malformed_request, false, fun is_authorized/2, 400).
|
expect(Req, State, malformed_request, false, fun is_authorized/2, 400).
|
||||||
|
|
||||||
|
@ -194,11 +184,12 @@ valid_entity_length(Req, State) ->
|
||||||
|
|
||||||
%% If you need to add additional headers to the response at this point,
|
%% If you need to add additional headers to the response at this point,
|
||||||
%% you should do it directly in the options/2 call using set_resp_headers.
|
%% you should do it directly in the options/2 call using set_resp_headers.
|
||||||
options(Req, State=#state{allowed_methods=Allow, method= <<"OPTIONS">>}) ->
|
options(Req, State=#state{allowed_methods=Methods, method= <<"OPTIONS">>}) ->
|
||||||
case call(Req, State, options) of
|
case call(Req, State, options) of
|
||||||
no_call ->
|
no_call ->
|
||||||
Req2 = cowboy_req:set_resp_header(<<"allow">>,
|
<< ", ", Allow/binary >>
|
||||||
build_allow_header(Allow, []), Req),
|
= << << ", ", M/binary >> || M <- Methods >>,
|
||||||
|
Req2 = cowboy_req:set_resp_header(<<"allow">>, Allow, Req),
|
||||||
respond(Req2, State, 200);
|
respond(Req2, State, 200);
|
||||||
{halt, Req2, HandlerState} ->
|
{halt, Req2, HandlerState} ->
|
||||||
terminate(Req2, State#state{handler_state=HandlerState});
|
terminate(Req2, State#state{handler_state=HandlerState});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue