Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/hackney.erl
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ request(Method, #hackney_url{}=URL0, Headers0, Body, Options0) ->
Error
end;
request(Method, URL, Headers, Body, Options)
when is_binary(URL) orelse is_list(URL) ->
when is_binary(URL) orelse is_list(URL) orelse is_tuple(URL) ->
request(Method, hackney_url:parse_url(URL), Headers, Body, Options).


Expand Down
39 changes: 38 additions & 1 deletion src/hackney_url.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
%%%

%% @doc module to manage URLs.
%%
%% TODO: add support of ssl on unix sockets

-module(hackney_url).

Expand All @@ -32,7 +34,7 @@
-type qs_opt() :: noplus | upper.

%% @doc Parse an URL and return a #hackney_url record.
-spec parse_url(URL::binary()|list()) -> hackney_url().
-spec parse_url(URL::binary()|list()|tuple()) -> hackney_url().
parse_url(URL) when is_list(URL) ->
case unicode:characters_to_binary(URL) of
URL1 when is_binary(URL1) ->
Expand All @@ -48,9 +50,15 @@ parse_url(<<"https://", Rest/binary>>) ->
scheme=https});
parse_url(<<"http+unix://", Rest/binary>>) ->
parse_url(Rest, #hackney_url{transport=hackney_local_tcp, scheme=http_unix});
parse_url({<<"unix:", SocketPath/binary>>, <<"http:/", Rest/binary >>}) ->
parse_unix_url(SocketPath, Rest, #hackney_url{transport=hackney_local_tcp, scheme=http_unix});

parse_url(URL) when is_tuple(URL) ->
erlang:error({bad_url, URL});
parse_url(URL) ->
parse_url(URL, #hackney_url{transport=hackney_tcp, scheme=http}).


parse_url(URL, S) ->
{URL1, Fragment} = parse_fragment(URL),
case binary:split(URL1, <<"/">>) of
Expand All @@ -73,6 +81,35 @@ parse_url(URL, S) ->
end.


parse_unix_url(SocketPath, URL, S) ->
{URL1, Fragment} = parse_fragment(URL),
{Path, Query} = parse_path(URL1),

case binary:split(Path, <<"@">>) of
[Path1] ->
S#hackney_url{host=unicode:characters_to_list(urldecode(SocketPath)),
port=0,
raw_path = URL,
path = Path1,
qs = Query,
fragment = Fragment};
[Credentials, Path1] ->
{User, Password}  = case binary:split(Credentials, <<":">>) of
[User1, Password1] -> {User1, Password1};
[User1] -> {User1, <<>>}
end,
S#hackney_url{host=unicode:characters_to_list(urldecode(SocketPath)),
port=0,
raw_path = URL,
path = Path1,
qs = Query,
fragment = Fragment,
user=urldecode(User),
password=urldecode(Password)}

end.


raw_fragment(<<"">>) -> <<"">>;
raw_fragment(Fragment) -> <<"#", Fragment/binary>>.

Expand Down
24 changes: 24 additions & 0 deletions test/hackney_url_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,34 @@ parse_and_unparse_url_test_() ->
user = <<"">>,
password = <<"">>}
}

],
[{V, fun() -> R = hackney_url:parse_url(V) end} || {V, R} <- Tests] ++
[{V, fun() -> V = hackney_url:unparse_url(R) end} || {V, R} <- Tests].



parse_unix_socket_test_() ->
Tests = [
{{<<"unix:/var/run/test.sock">>, <<"http:user@/path?key=value#Section%205">>},
#hackney_url{transport =hackney_local_tcp,
scheme = http_unix,
netloc = <<"%2Fvar%2Frun%2Ftest.sock">>,
raw_path = <<"/path?key=value#Section%205">>,
path = <<"/path">>,
qs = <<"key=value">>,
fragment = <<"Section%205">>,
host = "/var/run/test.sock",
port = 0,
user = <<"user">>,
password = <<"">>}
}
],

[{V, fun() -> R = hackney_url:parse_url(V) end} || {_, {V, R}} <- Tests].



parse_url_test_() ->
%% {Value, Result}.
Tests = [
Expand Down