fix(http): fix duplicate http headers

This commit is contained in:
zhouzb 2021-11-17 14:14:12 +08:00
parent e34055b6ef
commit 5e3fe6714e
6 changed files with 11 additions and 14 deletions

View File

@ -150,7 +150,7 @@ ensure_content_type_header(Method, Headers)
when Method =:= post orelse Method =:= put ->
Headers;
ensure_content_type_header(_Method, Headers) ->
lists:keydelete("content-type", 1, Headers).
lists:keydelete(<<"content-type">>, 1, Headers).
path(#{path := "", 'query' := Query}) ->
"?" ++ Query;

View File

@ -32,7 +32,7 @@ request(PoolName, get, Path, Headers, Params, Timeout) ->
reply(ehttpc:request(PoolName, get, {NewPath, Headers}, Timeout));
request(PoolName, post, Path, Headers, Params, Timeout) ->
Body = case proplists:get_value("content-type", Headers) of
Body = case proplists:get_value(<<"content-type">>, Headers) of
"application/x-www-form-urlencoded" ->
cow_qs:qs(bin_kw(Params));
"application/json" ->

View File

@ -295,7 +295,7 @@ create_req(_, Path, Headers, Body) ->
parse_action_params(Params = #{<<"url">> := URL}) ->
{ok, #{path := CommonPath}} = emqx_http_lib:uri_parse(URL),
Method = method(maps:get(<<"method">>, Params, <<"POST">>)),
Headers = headers(maps:get(<<"headers">>, Params, undefined)),
Headers = headers(maps:get(<<"headers">>, Params, #{})),
NHeaders = ensure_content_type_header(Headers, Method),
#{method => Method,
path => merge_path(CommonPath, maps:get(<<"path">>, Params, <<>>)),
@ -307,7 +307,7 @@ parse_action_params(Params = #{<<"url">> := URL}) ->
ensure_content_type_header(Headers, Method) when Method =:= post orelse Method =:= put ->
Headers;
ensure_content_type_header(Headers, _Method) ->
lists:keydelete("content-type", 1, Headers).
lists:keydelete(<<"content-type">>, 1, Headers).
merge_path(CommonPath, <<>>) ->
l2b(CommonPath);
@ -326,11 +326,8 @@ method(POST) when POST == <<"POST">>; POST == <<"post">> -> post;
method(PUT) when PUT == <<"PUT">>; PUT == <<"put">> -> put;
method(DEL) when DEL == <<"DELETE">>; DEL == <<"delete">> -> delete.
headers(undefined) -> [];
headers(Headers) when is_map(Headers) ->
headers(maps:to_list(Headers));
headers(Headers) when is_list(Headers) ->
[{string:to_lower(str(K)), str(V)} || {K, V} <- Headers].
headers(Headers) ->
emqx_http_lib:normalise_headers(maps:to_list(Headers)).
str(Str) when is_list(Str) -> Str;
str(Atom) when is_atom(Atom) -> atom_to_list(Atom);

View File

@ -87,7 +87,7 @@ translate_env() ->
application:set_env(?APP, path, Path),
application:set_env(?APP, pool_opts, PoolOpts),
Headers = application:get_env(?APP, headers, []),
NHeaders = set_content_type(Headers),
NHeaders = set_content_type(emqx_http_lib:normalise_headers(Headers)),
application:set_env(?APP, headers, NHeaders).
path(#{path := "", 'query' := Query}) ->
@ -100,5 +100,5 @@ path(#{path := Path}) ->
Path.
set_content_type(Headers) ->
NHeaders = proplists:delete(<<"Content-Type">>, proplists:delete(<<"content-type">>, Headers)),
NHeaders = proplists:delete(<<"content-type">>, Headers),
[{<<"content-type">>, <<"application/json">>} | NHeaders].

View File

@ -96,11 +96,11 @@ do_parse(URI) ->
%% underscores replaced with hyphens
%% NOTE: assuming the input Headers list is a proplists,
%% that is, when a key is duplicated, list header overrides tail
%% e.g. [{"Content_Type", "applicaiton/binary"}, {"content-type", "applicaiton/json"}]
%% e.g. [{"Content_Type", "applicaiton/binary"}, {<<"content-type">>, "applicaiton/json"}]
%% results in: [{"content-type", "applicaiton/binary"}]
normalise_headers(Headers0) ->
F = fun({K0, V}) ->
K = re:replace(K0, "_", "-", [{return,list}]),
K = re:replace(K0, "_", "-", [{return,binary}]),
{string:lowercase(K), V}
end,
Headers = lists:map(F, Headers0),

View File

@ -89,6 +89,6 @@ uri_parse_test_() ->
].
normalise_headers_test() ->
?assertEqual([{"content-type", "applicaiton/binary"}],
?assertEqual([{<<"content-type">>, "applicaiton/binary"}],
emqx_http_lib:normalise_headers([{"Content_Type", "applicaiton/binary"},
{"content-type", "applicaiton/json"}])).