From 4d5099c329d6cc48123601965510f06099c6a186 Mon Sep 17 00:00:00 2001 From: benoitc Date: Thu, 27 Nov 2014 12:08:28 +0100 Subject: [PATCH] more handling of string in headers Handle key or value as strings in requests and convert it as binary. --- src/hackney_lib/hackney_headers.erl | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/hackney_lib/hackney_headers.erl b/src/hackney_lib/hackney_headers.erl index bb7a9441..1eb15af4 100644 --- a/src/hackney_lib/hackney_headers.erl +++ b/src/hackney_lib/hackney_headers.erl @@ -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 @@ -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),