From b53ec390d18e573a4c8192ab6615c881aed95574 Mon Sep 17 00:00:00 2001 From: knu Date: Thu, 14 Dec 2017 01:11:28 +0000 Subject: [PATCH] Allow empty path components in a URI [Bug #8352] * generic.rb (URI::Generic#merge, URI::Generic#route_to): Fix a bug where a sequence of slashes in the path part gets collapsed to a single slash. According to the relevant RFCs and WHATWG URL Standard, empty path components are simply valid and there is no special treatment defined for them, so we just keep them as they are. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61218 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- NEWS | 4 ++++ lib/uri/generic.rb | 6 +++--- test/uri/test_generic.rb | 6 ++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/NEWS b/NEWS index 12949c496c..b458b549a3 100644 --- a/NEWS +++ b/NEWS @@ -353,6 +353,10 @@ with all sufficient information, see the ChangeLog file or Redmine * StringScanner#size, StringScanner#captures, StringScanner#values_at [Feature #836] +* URI + + * Relative path operations no longer collapse consecutive slashes to a single slash. [Bug #8352] + * WEBrick * Add Server Name Indication (SNI) support [Feature #13729] diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index dc4f9c4c30..629323e80e 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -979,7 +979,7 @@ module URI # returns an Array of the path split on '/' # def split_path(path) - path.split(%r{/+}, -1) + path.split(%r{/}, -1) end private :split_path @@ -1154,8 +1154,8 @@ module URI return dst.dup end - src_path = src.scan(%r{(?:\A|[^/]+)/}) - dst_path = dst.scan(%r{(?:\A|[^/]+)/?}) + src_path = src.scan(%r{[^/]*/}) + dst_path = dst.scan(%r{[^/]*/?}) # discard same parts while !dst_path.empty? && dst_path.first == src_path.first diff --git a/test/uri/test_generic.rb b/test/uri/test_generic.rb index b77717f949..7e428aa849 100644 --- a/test/uri/test_generic.rb +++ b/test/uri/test_generic.rb @@ -208,6 +208,9 @@ class URI::TestGeneric < Test::Unit::TestCase assert(nil != u.merge!("../baz")) assert_equal('http://foo/baz', u.to_s) + url = URI.parse('http://a/b//c') + 'd//e' + assert_equal('http://a/b//d//e', 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]") @@ -265,6 +268,9 @@ class URI::TestGeneric < Test::Unit::TestCase url = URI.parse('http://hoge/b').route_to('http://hoge/b:c') assert_equal('./b:c', url.to_s) + url = URI.parse('http://hoge/b//c').route_to('http://hoge/b/c') + assert_equal('../c', url.to_s) + url = URI.parse('file:///a/b/').route_to('file:///a/b/') assert_equal('', url.to_s) url = URI.parse('file:///a/b/').route_to('file:///a/b')