refactor(inet parse): refactor inet parse
This commit is contained in:
parent
67e56658a7
commit
b25dedccc9
|
@ -61,20 +61,7 @@ translate_env(EnvName) ->
|
||||||
_ -> 80
|
_ -> 80
|
||||||
end),
|
end),
|
||||||
Path = path(Path0),
|
Path = path(Path0),
|
||||||
Host = case inet:parse_address(Host0) of
|
{Inet, Host} = parse_host(Host0),
|
||||||
{ok, {_,_,_,_} = Addr} -> Addr;
|
|
||||||
{ok, {_,_,_,_,_,_,_,_} = Addr} -> Addr;
|
|
||||||
{error, einval} -> Host0
|
|
||||||
end,
|
|
||||||
Inet = case Host of
|
|
||||||
{_,_,_,_} -> inet;
|
|
||||||
{_,_,_,_,_,_,_,_} -> inet6;
|
|
||||||
_ ->
|
|
||||||
case inet:getaddr(Host, inet6) of
|
|
||||||
{error, _} -> inet;
|
|
||||||
{ok, _} -> inet6
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
MoreOpts = case Scheme of
|
MoreOpts = case Scheme of
|
||||||
"http" ->
|
"http" ->
|
||||||
[{transport_opts, [Inet]}];
|
[{transport_opts, [Inet]}];
|
||||||
|
@ -152,6 +139,17 @@ unload_hooks() ->
|
||||||
ehttpc_sup:stop_pool('emqx_auth_http/acl_req'),
|
ehttpc_sup:stop_pool('emqx_auth_http/acl_req'),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
parse_host(Host) ->
|
||||||
|
case inet:parse_address(Host) of
|
||||||
|
{ok, Addr} when size(Addr) =:= 4 -> {inet, Addr};
|
||||||
|
{ok, Addr} when size(Addr) =:= 8 -> {inet6, Addr};
|
||||||
|
{error, einval} ->
|
||||||
|
case inet:getaddr(Host, inet6) of
|
||||||
|
{ok, _} -> {inet6, Host};
|
||||||
|
{error, _} -> {inet, Host}
|
||||||
|
end
|
||||||
|
end.
|
||||||
|
|
||||||
to_lower(Headers) ->
|
to_lower(Headers) ->
|
||||||
[{string:to_lower(K), V} || {K, V} <- Headers].
|
[{string:to_lower(K), V} || {K, V} <- Headers].
|
||||||
|
|
||||||
|
|
|
@ -340,20 +340,7 @@ pool_opts(Params = #{<<"url">> := URL}) ->
|
||||||
end),
|
end),
|
||||||
PoolSize = maps:get(<<"pool_size">>, Params, 32),
|
PoolSize = maps:get(<<"pool_size">>, Params, 32),
|
||||||
ConnectTimeout = timer:seconds(maps:get(<<"connect_timeout">>, Params, 5)),
|
ConnectTimeout = timer:seconds(maps:get(<<"connect_timeout">>, Params, 5)),
|
||||||
Host = case inet:parse_address(Host0) of
|
{Inet, Host} = parse_host(Host0),
|
||||||
{ok, {_,_,_,_} = Addr} -> Addr;
|
|
||||||
{ok, {_,_,_,_,_,_,_,_} = Addr} -> Addr;
|
|
||||||
{error, einval} -> Host0
|
|
||||||
end,
|
|
||||||
Inet = case Host of
|
|
||||||
{_,_,_,_} -> inet;
|
|
||||||
{_,_,_,_,_,_,_,_} -> inet6;
|
|
||||||
_ ->
|
|
||||||
case inet:getaddr(Host, inet6) of
|
|
||||||
{error, _} -> inet;
|
|
||||||
{ok, _} -> inet6
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
MoreOpts = case Scheme of
|
MoreOpts = case Scheme of
|
||||||
"http" ->
|
"http" ->
|
||||||
[{transport_opts, [Inet]}];
|
[{transport_opts, [Inet]}];
|
||||||
|
@ -388,3 +375,14 @@ pool_opts(Params = #{<<"url">> := URL}) ->
|
||||||
|
|
||||||
pool_name(ResId) ->
|
pool_name(ResId) ->
|
||||||
list_to_atom("webhook:" ++ str(ResId)).
|
list_to_atom("webhook:" ++ str(ResId)).
|
||||||
|
|
||||||
|
parse_host(Host) ->
|
||||||
|
case inet:parse_address(Host) of
|
||||||
|
{ok, Addr} when size(Addr) =:= 4 -> {inet, Addr};
|
||||||
|
{ok, Addr} when size(Addr) =:= 8 -> {inet6, Addr};
|
||||||
|
{error, einval} ->
|
||||||
|
case inet:getaddr(Host, inet6) of
|
||||||
|
{ok, _} -> {inet6, Host};
|
||||||
|
{error, _} -> {inet, Host}
|
||||||
|
end
|
||||||
|
end.
|
||||||
|
|
|
@ -58,20 +58,7 @@ translate_env() ->
|
||||||
_ -> 80
|
_ -> 80
|
||||||
end),
|
end),
|
||||||
Path = path(Path0),
|
Path = path(Path0),
|
||||||
Host = case inet:parse_address(Host0) of
|
{Inet, Host} = parse_host(Host0),
|
||||||
{ok, {_,_,_,_} = Addr} -> Addr;
|
|
||||||
{ok, {_,_,_,_,_,_,_,_} = Addr} -> Addr;
|
|
||||||
{error, einval} -> Host0
|
|
||||||
end,
|
|
||||||
Inet = case Host of
|
|
||||||
{_,_,_,_} -> inet;
|
|
||||||
{_,_,_,_,_,_,_,_} -> inet6;
|
|
||||||
_ ->
|
|
||||||
case inet:getaddr(Host, inet6) of
|
|
||||||
{error, _} -> inet;
|
|
||||||
{ok, _} -> inet6
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
PoolSize = application:get_env(?APP, pool_size, 32),
|
PoolSize = application:get_env(?APP, pool_size, 32),
|
||||||
MoreOpts = case Scheme of
|
MoreOpts = case Scheme of
|
||||||
"http" ->
|
"http" ->
|
||||||
|
@ -118,4 +105,15 @@ 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">>, proplists:delete(<<"content-type">>, Headers)),
|
||||||
[{<<"content-type">>, <<"application/json">>} | NHeaders].
|
[{<<"content-type">>, <<"application/json">>} | NHeaders].
|
||||||
|
|
||||||
|
parse_host(Host) ->
|
||||||
|
case inet:parse_address(Host) of
|
||||||
|
{ok, Addr} when size(Addr) =:= 4 -> {inet, Addr};
|
||||||
|
{ok, Addr} when size(Addr) =:= 8 -> {inet6, Addr};
|
||||||
|
{error, einval} ->
|
||||||
|
case inet:getaddr(Host, inet6) of
|
||||||
|
{ok, _} -> {inet6, Host};
|
||||||
|
{error, _} -> {inet, Host}
|
||||||
|
end
|
||||||
|
end.
|
Loading…
Reference in New Issue