Skip to content

Commit

Permalink
more handling of string in headers
Browse files Browse the repository at this point in the history
Handle key or value as strings in requests and convert it as binary.
  • Loading branch information
benoitc committed Nov 27, 2014
1 parent 122d1ee commit 4d5099c
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/hackney_lib/hackney_headers.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,22 @@ new({dict, _}=D) ->
new(Headers) when is_list(Headers) ->
lists:foldl(fun
({K, V}, D) ->
insert(K, V, D);
insert(hackney_bstr:to_binary(K), hackney_bstr:to_binary(V), D);
({K, V, P}, D) ->
insert(K, header_value(V, P), D)
insert(hackney_bstr:to_binary(K), header_value(V, P), D)
end, dict:new(), Headers).

%% @doc extend the headers with a new list of `{Key, Value}' pair.
update(Headers, KVs) ->
lists:foldl(fun
({K,_V}=KV, D) ->
dict:store(hackney_bstr:to_lower(K), KV, D);
({K, V}, D) ->
K1 = hackney_bstr:to_binary(K),
V1 = hackney_bstr:to_binary(V),
dict:store(hackney_bstr:to_lower(K1), {K1, V1}, D);
({K, V, P}, D) ->
K1 = hackney_bstr:to_binary(K),
V1 = header_value(V, P),
dict:store(hackney_bstr:to_lower(K), {K, V1}, D)
dict:store(hackney_bstr:to_lower(K1), {K1, V1}, D)
end, Headers, KVs).

%% convert the header to a list
Expand Down Expand Up @@ -130,6 +133,8 @@ make_header(Name, Value, Params) ->
<< Name/binary, ": ", Value1/binary >>.

%% @doc join value and params in a binary
header_value(Value, Params) when is_list(Value) ->
header_value(list_to_binary(Value), Params);
header_value(Value, Params) ->
Params1 = lists:foldl(fun({K, V}, Acc) ->
K1 = hackney_bstr:to_binary(K),
Expand Down

0 comments on commit 4d5099c

Please sign in to comment.