diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index fce8a8a..112d7bf 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -1020,8 +1020,9 @@ def merge_path(base, rel) rel_path = split_path(rel) # RFC2396, Section 5.2, 6), a) - base_path << '' if base_path.last == '..' - # Remove "/.." pairs in a single left-to-right pass (O(n)). + base_path << '' if base_path.last == '..' || base_path.last == '.' + # Remove "." segments and "/.." pairs in a single + # left-to-right pass (O(n)). # This deliberately differs from the relative-path stack handling # below: a leading ".." (one with no preceding segment left to # cancel) discards the whole base path, reproducing the previous @@ -1031,7 +1032,7 @@ def merge_path(base, rel) if seg == '..' break if reduced.empty? reduced.pop - else + elsif seg != '.' reduced << seg end end diff --git a/test/uri/test_generic.rb b/test/uri/test_generic.rb index 9006125..fe0bd5f 100644 --- a/test/uri/test_generic.rb +++ b/test/uri/test_generic.rb @@ -248,6 +248,14 @@ def test_merge url = URI.parse('http://a/b//c') + 'd//e' assert_equal('http://a/b//d//e', url.to_s) + # "." segments in the base path are removed as well (RFC 3986, 5.2.4) + url = URI.parse('http://a/b/./c/') + 'g' + assert_equal('http://a/b/c/g', url.to_s) + url = URI.parse('http://a/b/./../c/') + 'g' + assert_equal('http://a/c/g', url.to_s) + url = URI.parse('http://a/b/.') + 'g' + assert_equal('http://a/b/g', url.to_s) + u0 = URI.parse('mailto:foo@example.com') u1 = URI.parse('mailto:foo@example.com#bar') assert_equal(uri_to_ary(u0 + '#bar'), uri_to_ary(u1), "[ruby-dev:23628]")