25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-22 16:20:52 +01:00

Improve misc:uri_parse

This commit is contained in:
Paweł Chmielowski 2022-05-05 13:07:03 +02:00
parent c3d8c3d9c5
commit 54a1fcc5e8
3 changed files with 17 additions and 10 deletions

View File

@ -56,23 +56,30 @@
-type distance_cache() :: #{{string(), string()} => non_neg_integer()}.
-spec uri_parse(binary()|string()) -> {ok, string(), string(), number(), string(), string()} | {error, term()}.
-ifdef(USE_OLD_HTTP_URI).
uri_parse(URL) when is_binary(URL) ->
uri_parse(binary_to_list(URL));
uri_parse(URL) ->
{ok, {Scheme, _UserInfo, Host, Port, Path, _Query}} = http_uri:parse(URL),
{ok, Scheme, Host, Port, Path}.
case http_uri:parse(URL) of
{ok, {Scheme, _UserInfo, Host, Port, Path, Query}} ->
{ok, Scheme, Host, Port, Path, Query};
{error, _} = E ->
E
end.
-else.
uri_parse(URL) when is_binary(URL) ->
uri_parse(binary_to_list(URL));
uri_parse(URL) ->
case uri_string:parse(URL) of
#{scheme := Scheme, host := Host, port := Port, path := Path} ->
{ok, Scheme, Host, Port, Path};
#{scheme := "https", host := Host, path := Path} ->
{ok, "https", Host, 443, Path};
#{scheme := "http", host := Host, path := Path} ->
{ok, "http", Host, 80, Path}
#{scheme := Scheme, host := Host, port := Port, path := Path} = M1 ->
{ok, Scheme, Host, Port, Path, maps:get(query, M1, "")};
#{scheme := "https", host := Host, path := Path} = M2 ->
{ok, "https", Host, 443, Path, maps:get(query, M2, "")};
#{scheme := "http", host := Host, path := Path} = M3 ->
{ok, "http", Host, 80, Path, maps:get(query, M3, "")};
{error, Atom, _} ->
{error, Atom}
end.
-endif.

View File

@ -713,7 +713,7 @@ get_proc_name(ServerHost, ModuleName) ->
-spec get_proc_name(binary(), atom(), binary()) -> atom().
get_proc_name(ServerHost, ModuleName, PutURL) ->
%% Once we depend on OTP >= 20.0, we can use binaries with http_uri.
{ok, _Scheme, Host0, _Port, Path0} =
{ok, _Scheme, Host0, _Port, Path0, _Query} =
misc:uri_parse(expand_host(PutURL, ServerHost)),
Host = jid:nameprep(iolist_to_binary(Host0)),
Path = str:strip(iolist_to_binary(Path0), right, $/),

View File

@ -357,7 +357,7 @@ mod_opt_type(via) ->
(econf:and_then(
econf:url([tls, tcp, udp]),
fun(URI) ->
{ok, Type, Host, Port, _} =
{ok, Type, Host, Port, _, _} =
misc:uri_parse(URI),
{Type, {unicode:characters_to_binary(Host), Port}}
end))(U)