Skip to content

Commit

Permalink
Merge pull request #7 from campanja-forks/rfc-compliant-at-sign-handling
Browse files Browse the repository at this point in the history
Rfc compliant at sign handling
  • Loading branch information
Evgeny Bob authored Mar 20, 2017
2 parents 93200b8 + 876007a commit a5397c8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/lhttpc_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -228,21 +228,26 @@ split_scheme("https://" ++ HostPortPath) ->
%% @end
%%------------------------------------------------------------------------------
split_credentials(CredsHostPortPath) ->
case string:tokens(CredsHostPortPath, "@") of
[HostPortPath] ->
{"", "", HostPortPath};
[Creds, HostPortPath] ->
[CredsHostPort | Path] = re:split(CredsHostPortPath, "/", [{return, list}]),
case string:tokens(CredsHostPort, "@") of
[HostPort] ->
{"", "", string:join([HostPort | Path], "/")};
[Creds, HostPort] ->
% RFC1738 (section 3.1) says:
% "The user name (and password), if present, are followed by a
% commercial at-sign "@". Within the user and password field, any ":",
% commercial at-sign "@", but it is only valid before the first
% "/".
% Within the user and password field, any ":",
% "@", or "/" must be encoded."
% The mentioned encoding is the "percent" encoding.
case string:tokens(Creds, ":") of
[User] ->
% RFC1738 says ":password" is optional
{http_uri:decode(User), "", HostPortPath};
{http_uri:decode(User), "",
string:join([HostPort | Path], "/")};
[User, Passwd] ->
{http_uri:decode(User), http_uri:decode(Passwd), HostPortPath}
{http_uri:decode(User), http_uri:decode(Passwd),
string:join([HostPort | Path], "/")}
end
end.

Expand Down
30 changes: 30 additions & 0 deletions test/lhttpc_lib_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ parse_url_test_() ->
},
lhttpc_lib:parse_url("https://host/")),

?_assertEqual(#lhttpc_url{
host = "host",
port = 443,
path = "/foo",
is_ssl = true,
user = "",
password = ""
},
lhttpc_lib:parse_url("https://host/foo")),

?_assertEqual(#lhttpc_url{
host = "host",
port = 443,
path = "/foo/",
is_ssl = true,
user = "",
password = ""
},
lhttpc_lib:parse_url("https://host/foo/")),

?_assertEqual(#lhttpc_url{
host = "host",
port = 180,
Expand Down Expand Up @@ -175,6 +195,16 @@ parse_url_test_() ->
},
lhttpc_lib:parse_url("http://joe%3aarm:erlang%2Fotp@host:180/foo/bar")),

?_assertEqual(#lhttpc_url{
host = "host",
port = 80,
path = "/[email protected]",
is_ssl = false,
user = "",
password = ""
},
lhttpc_lib:parse_url("http://host/[email protected]")),

?_assertEqual(#lhttpc_url{
host = "::1",
port = 80,
Expand Down

0 comments on commit a5397c8

Please sign in to comment.