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 ->
|
when Method =:= post orelse Method =:= put ->
|
||||||
Headers;
|
Headers;
|
||||||
ensure_content_type_header(_Method, Headers) ->
|
ensure_content_type_header(_Method, Headers) ->
|
||||||
lists:keydelete("content-type", 1, Headers).
|
lists:keydelete(<<"content-type">>, 1, Headers).
|
||||||
|
|
||||||
path(#{path := "", 'query' := Query}) ->
|
path(#{path := "", 'query' := Query}) ->
|
||||||
"?" ++ Query;
|
"?" ++ Query;
|
||||||
|
|
|
@ -32,7 +32,7 @@ request(PoolName, get, Path, Headers, Params, Timeout) ->
|
||||||
reply(ehttpc:request(PoolName, get, {NewPath, Headers}, Timeout));
|
reply(ehttpc:request(PoolName, get, {NewPath, Headers}, Timeout));
|
||||||
|
|
||||||
request(PoolName, post, Path, Headers, Params, 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" ->
|
"application/x-www-form-urlencoded" ->
|
||||||
cow_qs:qs(bin_kw(Params));
|
cow_qs:qs(bin_kw(Params));
|
||||||
"application/json" ->
|
"application/json" ->
|
||||||
|
|
|
@ -295,7 +295,7 @@ create_req(_, Path, Headers, Body) ->
|
||||||
parse_action_params(Params = #{<<"url">> := URL}) ->
|
parse_action_params(Params = #{<<"url">> := URL}) ->
|
||||||
{ok, #{path := CommonPath}} = emqx_http_lib:uri_parse(URL),
|
{ok, #{path := CommonPath}} = emqx_http_lib:uri_parse(URL),
|
||||||
Method = method(maps:get(<<"method">>, Params, <<"POST">>)),
|
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),
|
NHeaders = ensure_content_type_header(Headers, Method),
|
||||||
#{method => Method,
|
#{method => Method,
|
||||||
path => merge_path(CommonPath, maps:get(<<"path">>, Params, <<>>)),
|
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 ->
|
ensure_content_type_header(Headers, Method) when Method =:= post orelse Method =:= put ->
|
||||||
Headers;
|
Headers;
|
||||||
ensure_content_type_header(Headers, _Method) ->
|
ensure_content_type_header(Headers, _Method) ->
|
||||||
lists:keydelete("content-type", 1, Headers).
|
lists:keydelete(<<"content-type">>, 1, Headers).
|
||||||
|
|
||||||
merge_path(CommonPath, <<>>) ->
|
merge_path(CommonPath, <<>>) ->
|
||||||
l2b(CommonPath);
|
l2b(CommonPath);
|
||||||
|
@ -326,11 +326,8 @@ method(POST) when POST == <<"POST">>; POST == <<"post">> -> post;
|
||||||
method(PUT) when PUT == <<"PUT">>; PUT == <<"put">> -> put;
|
method(PUT) when PUT == <<"PUT">>; PUT == <<"put">> -> put;
|
||||||
method(DEL) when DEL == <<"DELETE">>; DEL == <<"delete">> -> delete.
|
method(DEL) when DEL == <<"DELETE">>; DEL == <<"delete">> -> delete.
|
||||||
|
|
||||||
headers(undefined) -> [];
|
headers(Headers) ->
|
||||||
headers(Headers) when is_map(Headers) ->
|
emqx_http_lib:normalise_headers(maps:to_list(Headers)).
|
||||||
headers(maps:to_list(Headers));
|
|
||||||
headers(Headers) when is_list(Headers) ->
|
|
||||||
[{string:to_lower(str(K)), str(V)} || {K, V} <- Headers].
|
|
||||||
|
|
||||||
str(Str) when is_list(Str) -> Str;
|
str(Str) when is_list(Str) -> Str;
|
||||||
str(Atom) when is_atom(Atom) -> atom_to_list(Atom);
|
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, path, Path),
|
||||||
application:set_env(?APP, pool_opts, PoolOpts),
|
application:set_env(?APP, pool_opts, PoolOpts),
|
||||||
Headers = application:get_env(?APP, headers, []),
|
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).
|
application:set_env(?APP, headers, NHeaders).
|
||||||
|
|
||||||
path(#{path := "", 'query' := Query}) ->
|
path(#{path := "", 'query' := Query}) ->
|
||||||
|
@ -100,5 +100,5 @@ path(#{path := Path}) ->
|
||||||
Path.
|
Path.
|
||||||
|
|
||||||
set_content_type(Headers) ->
|
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].
|
[{<<"content-type">>, <<"application/json">>} | NHeaders].
|
||||||
|
|
|
@ -96,11 +96,11 @@ do_parse(URI) ->
|
||||||
%% underscores replaced with hyphens
|
%% underscores replaced with hyphens
|
||||||
%% NOTE: assuming the input Headers list is a proplists,
|
%% NOTE: assuming the input Headers list is a proplists,
|
||||||
%% that is, when a key is duplicated, list header overrides tail
|
%% 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"}]
|
%% results in: [{"content-type", "applicaiton/binary"}]
|
||||||
normalise_headers(Headers0) ->
|
normalise_headers(Headers0) ->
|
||||||
F = fun({K0, V}) ->
|
F = fun({K0, V}) ->
|
||||||
K = re:replace(K0, "_", "-", [{return,list}]),
|
K = re:replace(K0, "_", "-", [{return,binary}]),
|
||||||
{string:lowercase(K), V}
|
{string:lowercase(K), V}
|
||||||
end,
|
end,
|
||||||
Headers = lists:map(F, Headers0),
|
Headers = lists:map(F, Headers0),
|
||||||
|
|
|
@ -89,6 +89,6 @@ uri_parse_test_() ->
|
||||||
].
|
].
|
||||||
|
|
||||||
normalise_headers_test() ->
|
normalise_headers_test() ->
|
||||||
?assertEqual([{"content-type", "applicaiton/binary"}],
|
?assertEqual([{<<"content-type">>, "applicaiton/binary"}],
|
||||||
emqx_http_lib:normalise_headers([{"Content_Type", "applicaiton/binary"},
|
emqx_http_lib:normalise_headers([{"Content_Type", "applicaiton/binary"},
|
||||||
{"content-type", "applicaiton/json"}])).
|
{"content-type", "applicaiton/json"}])).
|
||||||
|
|
Loading…
Reference in New Issue