Skip to content

Commit a8a662d

Browse files
marcelottojosevalim
authored andcommitted
Add missing calls of remove_dot_segments to URI.merge/2 (#6208)
1 parent d3b6f80 commit a8a662d

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/elixir/lib/uri.ex

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,10 @@ defmodule URI do
481481
raise ArgumentError, "you must merge onto an absolute URI"
482482
end
483483
def merge(_base, %URI{scheme: rel_scheme} = rel) when rel_scheme != nil do
484-
rel
484+
%{rel | path: remove_dot_segments_from_path(rel.path)}
485485
end
486486
def merge(base, %URI{authority: authority} = rel) when authority != nil do
487-
%{rel | scheme: base.scheme}
487+
%{rel | scheme: base.scheme, path: remove_dot_segments_from_path(rel.path)}
488488
end
489489
def merge(%URI{} = base, %URI{path: rel_path} = rel) when rel_path in ["", nil] do
490490
%{base | query: rel.query || base.query, fragment: rel.fragment}
@@ -500,7 +500,7 @@ defmodule URI do
500500
defp merge_paths(nil, rel_path),
501501
do: merge_paths("/", rel_path)
502502
defp merge_paths(_, "/" <> _ = rel_path),
503-
do: rel_path
503+
do: remove_dot_segments_from_path(rel_path)
504504
defp merge_paths(base_path, rel_path) do
505505
[_ | base_segments] = path_to_segments(base_path)
506506
path_to_segments(rel_path)
@@ -509,6 +509,17 @@ defmodule URI do
509509
|> Enum.join("/")
510510
end
511511

512+
defp remove_dot_segments_from_path(nil) do
513+
nil
514+
end
515+
516+
defp remove_dot_segments_from_path(path) do
517+
path
518+
|> path_to_segments()
519+
|> remove_dot_segments([])
520+
|> Enum.join("/")
521+
end
522+
512523
defp remove_dot_segments([], [head, ".." | acc]),
513524
do: remove_dot_segments([], [head | acc])
514525
defp remove_dot_segments([], acc),

lib/elixir/test/elixir/uri_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,13 @@ defmodule URITest do
240240
end
241241

242242
assert URI.merge("http://google.com/foo", "http://example.com/baz") |> to_string == "http://example.com/baz"
243+
assert URI.merge("http://google.com/foo", "http://example.com/.././bar/../../baz") |> to_string == "http://example.com/baz"
244+
243245
assert URI.merge("http://google.com/foo", "//example.com/baz") |> to_string == "http://example.com/baz"
246+
assert URI.merge("http://google.com/foo", "//example.com/.././bar/../../../baz") |> to_string == "http://example.com/baz"
244247

245248
assert URI.merge("http://example.com", URI.parse("/foo")) |> to_string == "http://example.com/foo"
249+
assert URI.merge("http://example.com", URI.parse("/.././bar/../../../baz")) |> to_string == "http://example.com/baz"
246250

247251
base = URI.parse("http://example.com/foo/bar")
248252
assert URI.merge(base, "") |> to_string == "http://example.com/foo/bar"

0 commit comments

Comments
 (0)