fix(gw): use emqx_http_lib to parse uri

This commit is contained in:
JianBo He 2021-12-29 19:04:49 +08:00
parent 388fdc6057
commit 2a20f110b9
2 changed files with 11 additions and 22 deletions

View File

@ -46,7 +46,6 @@
]). ]).
-export([ stringfy/1 -export([ stringfy/1
, parse_address/1
]). ]).
-export([ normalize_config/1 -export([ normalize_config/1
@ -332,19 +331,6 @@ stringfy(T) when is_list(T); is_binary(T) ->
stringfy(T) -> stringfy(T) ->
iolist_to_binary(io_lib:format("~0p", [T])). iolist_to_binary(io_lib:format("~0p", [T])).
-spec parse_address(binary() | list()) -> {list(), integer()}.
parse_address(S) when is_binary(S); is_list(S) ->
S1 = case is_binary(S) of
true -> lists:reverse(binary_to_list(S));
_ -> lists:reverse(S)
end,
case re:split(S1, ":", [{parts, 2}, {return, list}]) of
[Port0, Host0] ->
{lists:reverse(Host0), list_to_integer(lists:reverse(Port0))};
_ ->
error(badarg)
end.
-spec normalize_config(emqx_config:config()) -spec normalize_config(emqx_config:config())
-> list({ Type :: udp | tcp | ssl | dtls -> list({ Type :: udp | tcp | ssl | dtls
, Name :: atom() , Name :: atom()

View File

@ -148,13 +148,16 @@ stop_grpc_server(GwName) ->
start_grpc_client_channel(_GwName, undefined) -> start_grpc_client_channel(_GwName, undefined) ->
undefined; undefined;
start_grpc_client_channel(GwName, Options = #{address := Address}) -> start_grpc_client_channel(GwName, Options = #{address := Address}) ->
{Host, Port} = try emqx_gateway_utils:parse_address(Address) #{host := Host, port := Port} =
catch error : badarg -> case emqx_http_lib:uri_parse(Address) of
throw({badconf, #{key => address, {ok, URIMap0} -> URIMap0;
value => Address, {error, _Reason} ->
reason => illegal_grpc_address throw({badconf, #{key => address,
}}) value => Address,
end, reason => illegal_grpc_address
}})
end,
case maps:to_list(maps:get(ssl, Options, #{})) of case maps:to_list(maps:get(ssl, Options, #{})) of
[] -> [] ->
SvrAddr = compose_http_uri(http, Host, Port), SvrAddr = compose_http_uri(http, Host, Port),
@ -170,7 +173,7 @@ start_grpc_client_channel(GwName, Options = #{address := Address}) ->
compose_http_uri(Scheme, Host, Port) -> compose_http_uri(Scheme, Host, Port) ->
lists:flatten( lists:flatten(
io_lib:format( io_lib:format(
"~s://~s:~w", [Scheme, Host, Port])). "~s://~s:~w", [Scheme, inet:ntoa(Host), Port])).
stop_grpc_client_channel(GwName) -> stop_grpc_client_channel(GwName) ->
_ = grpc_client_sup:stop_channel_pool(GwName), _ = grpc_client_sup:stop_channel_pool(GwName),