fix(http): fix duplicate http headers
This commit is contained in:
parent
e34055b6ef
commit
5e3fe6714e
|
@ -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;
|
||||
|
|
|
@ -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" ->
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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].
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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"}])).
|
||||
|
|
Loading…
Reference in New Issue