Skip to content

Commit 801c902

Browse files
committed
Handle URLs where the query part follows the hostname without a slash in between
For example, in http://www.example.com?a=b the hostname is "www.example.com", not "www.example.com?a=b". See RFC 3986, section 3.
1 parent abf356e commit 801c902

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/lhttpc_lib.erl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ split_host([$: | PortPath], Host) ->
254254
{lists:reverse(Host), PortPath};
255255
split_host([$/ | _] = PortPath, Host) ->
256256
{lists:reverse(Host), PortPath};
257+
split_host([$? | _] = Query, Host) ->
258+
%% The query string follows the hostname, without a slash. The
259+
%% path is empty, but for HTTP an empty path is equivalent to "/"
260+
%% (RFC 3986, section 6.2.3), so let's add the slash ourselves.
261+
{lists:reverse(Host), "/" ++ Query};
257262
split_host([H | T], Host) ->
258263
split_host(T, [H | Host]);
259264
split_host([], Host) ->

test/lhttpc_lib_tests.erl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,5 +212,15 @@ parse_url_test_() ->
212212
user = "joe",
213213
password = "erlang"
214214
},
215-
lhttpc_lib:parse_url("http://joe:erlang@[1080:0:0:0:8:800:200C:417A]:180/foo/bar"))
215+
lhttpc_lib:parse_url("http://joe:erlang@[1080:0:0:0:8:800:200C:417A]:180/foo/bar")),
216+
217+
?_assertEqual(#lhttpc_url{
218+
host = "www.example.com",
219+
port = 80,
220+
path = "/?a=b",
221+
is_ssl = false,
222+
user = "",
223+
password = ""
224+
},
225+
lhttpc_lib:parse_url("http://www.example.com?a=b"))
216226
].

0 commit comments

Comments
 (0)