-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
geeksilva97
wants to merge
6
commits into
ninenines:master
Choose a base branch
from
geeksilva97:issue-1295
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
05509c6
nothing interesting yet
geeksilva97 c6491b8
add clause for processing given headers
geeksilva97 9af49cd
add base bitstring concat
geeksilva97 d43807d
add minor tests
geeksilva97 ded96a1
handle iodata
geeksilva97 c6df5dd
remove unused files
geeksilva97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]). | ||
|
@@ -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() -> | ||
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. | ||
|
@@ -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) -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @essen I wonder if to have some consistency with the |
||
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">> := _}, _) -> | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.