Skip to content

Commit

Permalink
on otp-21+ use uri_string to parse uri
Browse files Browse the repository at this point in the history
  • Loading branch information
tsloughter committed Nov 14, 2020
1 parent 968afee commit b99032d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/elli_http.erl
Expand Up @@ -692,6 +692,20 @@ content_length(Headers, Body)->
%% PATH HELPERS
%%

-ifdef(OTP_RELEASE).
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), []};
Expand All @@ -702,6 +716,7 @@ 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]),
Expand Down

0 comments on commit b99032d

Please sign in to comment.