Skip to content

Commit 65f1b2d

Browse files
whatyouhideJosé Valim
authored and
José Valim
committed
Fix parsing of queries with just "?" in URI.parse/1 (#7565)
Closes #7563 Signed-off-by: José Valim <[email protected]>
1 parent 479e46f commit 65f1b2d

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lib/elixir/lib/uri.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,13 @@ defmodule URI do
422422

423423
parts = Regex.run(regex, string)
424424

425-
destructure [_, _, scheme, _, authority, path, _, query, _, fragment], parts
425+
destructure [_, _, scheme, _, authority, path, query_with_question_mark, _, _, fragment],
426+
parts
427+
426428
scheme = nillify(scheme)
427429
authority = nillify(authority)
428430
path = nillify(path)
429-
query = nillify(query)
431+
query = nillify_query(query_with_question_mark)
430432
{userinfo, host, port} = split_authority(authority)
431433

432434
scheme = scheme && String.downcase(scheme)
@@ -444,6 +446,10 @@ defmodule URI do
444446
}
445447
end
446448

449+
defp nillify_query("?"), do: ""
450+
defp nillify_query("?" <> query), do: query
451+
defp nillify_query(_other), do: nil
452+
447453
# Split an authority into its userinfo, host and port parts.
448454
defp split_authority(string) do
449455
regex = Regex.recompile!(~r/(^(.*)@)?(\[[a-zA-Z0-9:.]*\]|[^:]*)(:(\d*))?/)

lib/elixir/test/elixir/uri_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ defmodule URITest do
306306
assert URI.parse("http://example.com/#").fragment == ""
307307
assert URI.parse("http://example.com/test#").fragment == ""
308308
end
309+
310+
test "preserves an empty query" do
311+
assert URI.parse("http://foo.com/?").query == ""
312+
end
309313
end
310314

311315
test "default_port/1,2" do

0 commit comments

Comments
 (0)