Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set_resp_headers_list #1634

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/cowboy_req.erl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
-export([resp_headers/1]).
-export([set_resp_header/3]).
-export([set_resp_headers/2]).
-export([set_resp_headers_list/2]).
-export([has_resp_header/2]).
-export([delete_resp_header/2]).
-export([set_resp_body/2]).
Expand Down Expand Up @@ -366,6 +367,30 @@ uri2_test() ->
<<"http://localhost/path?dummy=2785">> = iolist_to_binary(uri(Req, #{port => 80})),
<<"https://localhost/path?dummy=2785">> = iolist_to_binary(uri(Req, #{scheme => "https", port => 443})),
ok.

resp_headers_test() ->
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not needed we can remove this unit test. I was using it to validate the function behavior.

Req = #{
scheme => <<"http">>, host => <<"localhost">>, port => 8080,
path => <<"/path">>, qs => <<"dummy=2785">>
},
#{resp_headers := RespHeaders} = set_resp_headers_list([
{<<"Name">>, <<"Cormano">>},
{<<"Name">>, <<"Paco">>},
{<<"X-MyHeader">>, <<"custom-header">>},
{<<"game-name">>, "Sunset"},
{<<"game-name">>, "Riders"},
{<<"header">>, ["io", "data"]},
{<<"header">>, ["header"]}
], #{}, Req),

#{
<<"Name">> := <<"Cormano, Paco">>,
<<"X-MyHeader">> := <<"custom-header">>,
<<"game-name">> := <<"Sunset, Riders">>,
<<"header">> := <<"iodata, header">>
} = RespHeaders,

ok.
-endif.

-spec binding(atom(), req()) -> any() | undefined.
Expand Down Expand Up @@ -726,6 +751,30 @@ set_resp_header(Name, Value, Req=#{resp_headers := RespHeaders}) ->
set_resp_header(Name,Value, Req) ->
Req#{resp_headers => #{Name => Value}}.

% @todo make it work with iodata(), for now only bitstrings are accepted

-spec set_resp_headers_list(list({ binary(), iodata() }), Req)
-> Req when Req::req().
set_resp_headers_list(HeaderTupleList, Req) ->
set_resp_headers_list(HeaderTupleList, #{}, Req).

set_resp_headers_list([], Map, Req) ->
set_resp_headers(Map, Req);

set_resp_headers_list([{<<"set-cookie">>, _} | Headers], Map, Req) ->
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@essen I wonder if to have some consistency with the set_resp_header we should instead throw an error. What do you say?

set_resp_headers_list(Headers, Map, Req);
set_resp_headers_list([{Name, Value} | Headers], Map, Req) ->
BinaryValue = iolist_to_binary(Value),
NewHeaderValue = case maps:get(Name, Map, undefined) of
undefined -> BinaryValue;
ExistingValue ->
<<ExistingValue/binary, ", ", BinaryValue/binary>>
end,

Map1 = maps:put(Name, NewHeaderValue, Map),

set_resp_headers_list(Headers, Map1, Req).

-spec set_resp_headers(cowboy:http_headers(), Req)
-> Req when Req::req().
set_resp_headers(#{<<"set-cookie">> := _}, _) ->
Expand Down