From 067909111326ca96609d89643771a29fede052d7 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Sat, 14 Nov 2020 12:49:40 -0700 Subject: [PATCH] on otp-21+ use uri_string to parse uri --- src/elli_http.erl | 49 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/src/elli_http.erl b/src/elli_http.erl index b7ca014..4404a76 100644 --- a/src/elli_http.erl +++ b/src/elli_http.erl @@ -740,16 +740,45 @@ search(Pred, []) when is_function(Pred, 1) -> %% PATH HELPERS %% -parse_path({abs_path, FullPath}) -> - Parsed = case binary:split(FullPath, [<<"?">>]) of - [URL] -> {FullPath, split_path(URL), []}; - [URL, Args] -> {FullPath, split_path(URL), split_args(Args)} - end, - {ok, {undefined, undefined, undefined}, Parsed}; -parse_path({absoluteURI, Scheme, Host, Port, Path}) -> - setelement(2, parse_path({abs_path, Path}), {Scheme, Host, Port}); -parse_path(_) -> - {error, unsupported_uri}. +-ifdef(OTP_RELEASE). + -if(?OTP_RELEASE >= 22). + parse_path({abs_path, FullPath}) -> + URIMap = uri_string:parse(FullPath), + Host = maps:get(host, URIMap, undefined), + Scheme = maps:get(scheme, URIMap, undefined), + Path = maps:get(path, URIMap, <<>>), + Query = maps:get(query, URIMap, <<>>), + Port = maps:get(port, URIMap, case Scheme of http -> 80; https -> 443; _ -> undefined end), + {ok, {Scheme, Host, Port}, {Path, split_path(Path), uri_string:dissect_query(Query)}}; + parse_path({absoluteURI, Scheme, Host, Port, Path}) -> + setelement(2, parse_path({abs_path, Path}), {Scheme, Host, Port}); + parse_path(_) -> + {error, unsupported_uri}. + -else. + parse_path({abs_path, FullPath}) -> + Parsed = case binary:split(FullPath, [<<"?">>]) of + [URL] -> {FullPath, split_path(URL), []}; + [URL, Args] -> {FullPath, split_path(URL), split_args(Args)} + end, + {ok, {undefined, undefined, undefined}, Parsed}; + parse_path({absoluteURI, Scheme, Host, Port, Path}) -> + setelement(2, parse_path({abs_path, Path}), {Scheme, Host, Port}); + parse_path(_) -> + {error, unsupported_uri}. + -endif. +-else. + %% same as else branch above. can drop this when only OTP 21+ is supported + parse_path({abs_path, FullPath}) -> + Parsed = case binary:split(FullPath, [<<"?">>]) of + [URL] -> {FullPath, split_path(URL), []}; + [URL, Args] -> {FullPath, split_path(URL), split_args(Args)} + end, + {ok, {undefined, undefined, undefined}, Parsed}; + parse_path({absoluteURI, Scheme, Host, Port, Path}) -> + setelement(2, parse_path({abs_path, Path}), {Scheme, Host, Port}); + parse_path(_) -> + {error, unsupported_uri}. +-endif. split_path(Path) -> [P || P <- binary:split(Path, [<<"/">>], [global]),