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

Add support for lists in cowboy_req:set_resp_headers

This is meant to be used with clients such as Gun to simplify
proxying and similar operations. The set-cookie header must
not be set this way so there is still some extra processing
to be done to fully translate a Gun response into a Cowboy
response.
This commit is contained in:
Loïc Hoguin 2025-02-11 12:00:03 +01:00
parent f316a65906
commit e8a1868033
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764
4 changed files with 48 additions and 7 deletions

View file

@ -726,8 +726,10 @@ set_resp_header(Name, Value, Req=#{resp_headers := RespHeaders}) ->
set_resp_header(Name,Value, Req) ->
Req#{resp_headers => #{Name => Value}}.
-spec set_resp_headers(cowboy:http_headers(), Req)
-spec set_resp_headers(cowboy:http_headers() | [{binary(), iodata()}], Req)
-> Req when Req::req().
set_resp_headers(Headers, Req) when is_list(Headers) ->
set_resp_headers_list(Headers, Req, #{});
set_resp_headers(#{<<"set-cookie">> := _}, _) ->
exit({response_error, invalid_header,
'Response cookies must be set using cowboy_req:set_resp_cookie/3,4.'});
@ -736,6 +738,19 @@ set_resp_headers(Headers, Req=#{resp_headers := RespHeaders}) ->
set_resp_headers(Headers, Req) ->
Req#{resp_headers => Headers}.
set_resp_headers_list([], Req, Acc) ->
set_resp_headers(Acc, Req);
set_resp_headers_list([{<<"set-cookie">>, _}|_], _, _) ->
exit({response_error, invalid_header,
'Response cookies must be set using cowboy_req:set_resp_cookie/3,4.'});
set_resp_headers_list([{Name, Value}|Tail], Req, Acc) ->
case Acc of
#{Name := ValueAcc} ->
set_resp_headers_list(Tail, Req, Acc#{Name => [ValueAcc, <<", ">>, Value]});
_ ->
set_resp_headers_list(Tail, Req, Acc#{Name => Value})
end.
-spec resp_header(binary(), req()) -> binary() | undefined.
resp_header(Name, Req) ->
resp_header(Name, Req, undefined).