benoitc / hackney

simple HTTP client in Erlang
Other
1.34k stars 427 forks source link

`content-type` corrupted if supplied as list/string instead of binary #721

Closed afhole closed 1 year ago

afhole commented 1 year ago

Headers can be supplied as strings without issue, except for the content-type header. In this case the header is converted to the ASCII value for the first character of the supplied value. This appears to be caused by the following calls to hackney_headers_new:store: https://github.com/benoitc/hackney/blob/d6d075f9edcbb7e0b5c9418dba5dd34f2c95bc21/src/hackney_request.erl#L390-L395

The list is treated as a multivalued header and each character of the string is added as a new header: https://github.com/benoitc/hackney/blob/d6d075f9edcbb7e0b5c9418dba5dd34f2c95bc21/src/hackney_headers_new.erl#L68-L76

rp(hackney_headers_new:store([{"content-type", "application/json"}], hackney_headers_new:new())).
{16,
 {dict,1,16,16,8,80,48,
       {[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
       {{[],[],[],[],[],[],[],[],[],[],
         [[<<"content-type">>,
           {0,"content-type",97},
           {1,"content-type",112},
           {2,"content-type",112},
           {3,"content-type",108},
           {4,"content-type",105},
           {5,"content-type",99},
           {6,"content-type",97},
           {7,"content-type",116},
           {8,"content-type",105},
           {9,"content-type",111},
           {10,"content-type",110},
           {11,"content-type",47},
           {12,"content-type",106},
           {13,"content-type",115},
           {14,"content-type",111},
           {15,"content-type",110}]],
         [],[],[],[],[]}}}}
hackney_headers_new:get_value(<<"content-type">>,
  hackney_headers_new:store([{"content-type", "application/json"}], hackney_headers_new:new())).
97
benoitc commented 1 year ago

It's expected. Value type is -type value() :: binary() | {binary() | list({binary(), binary()} | binary())}.

Key can be a binary or string for convenience, but newt major version will only expect key as binary.