Skip to content

Commit b601b1c

Browse files
authored
Fix URI.merge/2 trailing slash handling at root path (#14346)
1 parent ba48980 commit b601b1c

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

lib/elixir/lib/uri.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,8 @@ defmodule URI do
962962
defp remove_dot_segments([".." | tail], [_ | acc]), do: remove_dot_segments(tail, acc)
963963
defp remove_dot_segments([head | tail], acc), do: remove_dot_segments(tail, [head | acc])
964964

965+
defp join_reversed_segments([:/]), do: "/"
966+
965967
defp join_reversed_segments(segments) do
966968
case Enum.reverse(segments) do
967969
[:/ | tail] -> ["" | tail]

lib/elixir/test/elixir/uri_test.exs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ defmodule URITest do
530530
#
531531
# https://w3c.github.io/json-ld-api/tests/toRdf-manifest#t0124
532532
# https://w3c.github.io/json-ld-api/tests/toRdf-manifest#t0125
533+
# https://w3c.github.io/json-ld-api/tests/toRdf-manifest#t0123
533534

534535
base1 = "http://a/bb/ccc/."
535536

@@ -632,6 +633,57 @@ defmodule URITest do
632633
for {rel, result} <- rel_and_result2 do
633634
assert URI.merge(base2, rel) |> URI.to_string() == result
634635
end
636+
637+
base3 = "http://a/bb/ccc/../d;p?q"
638+
639+
rel_and_result = %{
640+
"g:h" => "g:h",
641+
"g" => "http://a/bb/g",
642+
"./g" => "http://a/bb/g",
643+
"g/" => "http://a/bb/g/",
644+
"/g" => "http://a/g",
645+
"//g" => "http://g",
646+
"?y" => "http://a/bb/ccc/../d;p?y",
647+
"g?y" => "http://a/bb/g?y",
648+
"#s" => "http://a/bb/ccc/../d;p?q#s",
649+
"g#s" => "http://a/bb/g#s",
650+
"g?y#s" => "http://a/bb/g?y#s",
651+
";x" => "http://a/bb/;x",
652+
"g;x" => "http://a/bb/g;x",
653+
"g;x?y#s" => "http://a/bb/g;x?y#s",
654+
"" => "http://a/bb/ccc/../d;p?q",
655+
"." => "http://a/bb/",
656+
"./" => "http://a/bb/",
657+
".." => "http://a/",
658+
"../" => "http://a/",
659+
"../g" => "http://a/g",
660+
"../.." => "http://a/",
661+
"../../" => "http://a/",
662+
"../../g" => "http://a/g",
663+
"../../../g" => "http://a/g",
664+
"../../../../g" => "http://a/g",
665+
"/./g" => "http://a/g",
666+
"/../g" => "http://a/g",
667+
"g." => "http://a/bb/g.",
668+
".g" => "http://a/bb/.g",
669+
"g.." => "http://a/bb/g..",
670+
"..g" => "http://a/bb/..g",
671+
"./../g" => "http://a/g",
672+
"./g/." => "http://a/bb/g/",
673+
"g/./h" => "http://a/bb/g/h",
674+
"g/../h" => "http://a/bb/h",
675+
"g;x=1/./y" => "http://a/bb/g;x=1/y",
676+
"g;x=1/../y" => "http://a/bb/y",
677+
"g?y/./x" => "http://a/bb/g?y/./x",
678+
"g?y/../x" => "http://a/bb/g?y/../x",
679+
"g#s/./x" => "http://a/bb/g#s/./x",
680+
"g#s/../x" => "http://a/bb/g#s/../x",
681+
"http:g" => "http:g"
682+
}
683+
684+
for {rel, result} <- rel_and_result do
685+
assert URI.merge(base3, rel) |> URI.to_string() == result
686+
end
635687
end
636688

637689
test "append_query/2" do

0 commit comments

Comments
 (0)