fix: support ipv6 at ip_port

This commit is contained in:
Stefan Strigler 2023-02-15 10:37:13 +01:00
parent 4417ea9db7
commit 9be73bfff0
1 changed files with 23 additions and 6 deletions

View File

@ -582,14 +582,15 @@ to_timestamp(B) when is_binary(B) ->
binary_to_integer(B). binary_to_integer(B).
to_ip(IP0) when is_binary(IP0) -> to_ip(IP0) when is_binary(IP0) ->
{ok, IP} = inet:parse_address(binary_to_list(IP0)), ensure_ok(inet:parse_address(binary_to_list(IP0))).
IP.
to_ip_port(IPAddress) -> to_ip_port(IPAddress) ->
[IP0, Port0] = string:tokens(binary_to_list(IPAddress), ":"), ensure_ok(emqx_schema:to_ip_port(IPAddress)).
{ok, IP} = inet:parse_address(IP0),
Port = list_to_integer(Port0), ensure_ok({ok, V}) ->
{IP, Port}. V;
ensure_ok({error, _R} = E) ->
throw(E).
b2i(Bin) when is_binary(Bin) -> b2i(Bin) when is_binary(Bin) ->
binary_to_integer(Bin); binary_to_integer(Bin);
@ -648,9 +649,25 @@ params2qs_test_() ->
[ [
?_assertEqual({10, {ExpectedQs, FuzzyNQString}}, parse_qstring(QString, QSchema)), ?_assertEqual({10, {ExpectedQs, FuzzyNQString}}, parse_qstring(QString, QSchema)),
?_assertEqual({0, {[], []}}, parse_qstring([{not_a_predefined_params, val}], QSchema)), ?_assertEqual({0, {[], []}}, parse_qstring([{not_a_predefined_params, val}], QSchema)),
?_assertEqual(
{1, {[{ip, '=:=', {0, 0, 0, 0, 0, 0, 0, 1}}], []}},
parse_qstring([{<<"ip">>, <<"::1">>}], QSchema)
),
?_assertEqual(
{1, {[{ip_port, '=:=', {{0, 0, 0, 0, 0, 0, 0, 1}, 8888}}], []}},
parse_qstring([{<<"ip_port">>, <<"::1:8888">>}], QSchema)
),
?_assertThrow( ?_assertThrow(
{bad_value_type, {<<"ip">>, ip, <<"helloworld">>}}, {bad_value_type, {<<"ip">>, ip, <<"helloworld">>}},
parse_qstring([{<<"ip">>, <<"helloworld">>}], QSchema) parse_qstring([{<<"ip">>, <<"helloworld">>}], QSchema)
),
?_assertThrow(
{bad_value_type, {<<"ip_port">>, ip_port, <<"127.0.0.1">>}},
parse_qstring([{<<"ip_port">>, <<"127.0.0.1">>}], QSchema)
),
?_assertThrow(
{bad_value_type, {<<"ip_port">>, ip_port, <<"helloworld:abcd">>}},
parse_qstring([{<<"ip_port">>, <<"helloworld:abcd">>}], QSchema)
) )
]. ].