From 54a1fcc5e8bca191392b492882ce98cb73388b81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chmielowski?= Date: Thu, 5 May 2022 13:07:03 +0200 Subject: Improve misc:uri_parse --- src/misc.erl | 23 +++++++++++++++-------- src/mod_http_upload.erl | 2 +- src/mod_sip.erl | 2 +- 3 files changed, 17 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/misc.erl b/src/misc.erl index b502971a0..0733ef298 100644 --- a/src/misc.erl +++ b/src/misc.erl @@ -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. diff --git a/src/mod_http_upload.erl b/src/mod_http_upload.erl index 88be57718..0544a837d 100644 --- a/src/mod_http_upload.erl +++ b/src/mod_http_upload.erl @@ -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, $/), diff --git a/src/mod_sip.erl b/src/mod_sip.erl index 5c1e09467..6a710a6e8 100644 --- a/src/mod_sip.erl +++ b/src/mod_sip.erl @@ -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) -- cgit v1.2.3