diff options
| author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-05-15 12:49:34 +0000 |
|---|---|---|
| committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-05-15 12:49:34 +0000 |
| commit | 408b181d2cec08ca3d492ecf2a10208aefd76c57 (patch) | |
| tree | 9604ae5d4eac83b4bc62743719c9031b56dd09eb | |
| parent | 8e2f38b88744560868d017e2c7bc1ddca44cc2ed (diff) | |
* lib/uri/generic.rb (#route_from_path): Fix a bug where
URI('http://h/b/').route_to('http://h/b') wrongly returned './'
(should be '../b'). [Bug #4476]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@31578 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
| -rw-r--r-- | ChangeLog | 6 | ||||
| -rw-r--r-- | lib/uri/generic.rb | 26 | ||||
| -rw-r--r-- | test/uri/test_generic.rb | 11 |
3 files changed, 28 insertions, 15 deletions
@@ -1,3 +1,9 @@ +Sun May 15 21:43:09 2011 Akinori MUSHA <knu@iDaemons.org> + + * lib/uri/generic.rb (#route_from_path): Fix a bug where + URI('http://h/b/').route_to('http://h/b') wrongly returned './' + (should be '../b'). [Bug #4476] + Sun May 15 21:37:54 2011 Akinori MUSHA <knu@iDaemons.org> * lib/fileutils.rb (FileUtils#touch): Fix corrupted output. diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index 4978816c94..fb81582278 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -799,30 +799,26 @@ module URI private :merge0 def route_from_path(src, dst) - # RFC2396, Section 4.2 - return '' if src == dst - - src_path = split_path(src) - dst_path = split_path(dst) - - # hmm... dst has abnormal absolute path, - # like "/./", "/../", "/x/../", ... - if dst_path.include?('..') || - dst_path.include?('.') + case dst + when src + # RFC2396, Section 4.2 + return '' + when %r{(?:\A|/)\.\.?(?:/|\z)} + # dst has abnormal absolute path, + # like "/./", "/../", "/x/../", ... return dst.dup end - src_path.pop + src_path = src.scan(%r{(?:\A|[^/]+)/}) + dst_path = dst.scan(%r{(?:\A|[^/]+)/?}) # discard same parts - while dst_path.first == src_path.first - break if dst_path.empty? - + while !dst_path.empty? && dst_path.first == src_path.first src_path.shift dst_path.shift end - tmp = dst_path.join('/') + tmp = dst_path.join # calculate if src_path.empty? diff --git a/test/uri/test_generic.rb b/test/uri/test_generic.rb index 1e535e5949..d3e691bcdb 100644 --- a/test/uri/test_generic.rb +++ b/test/uri/test_generic.rb @@ -231,8 +231,19 @@ class TestGeneric < Test::Unit::TestCase url = URI.parse('http://hoge/a/b/').route_to('http://MOGE/b/') assert_equal('//MOGE/b/', url.to_s) + url = URI.parse('http://hoge/b').route_to('http://hoge/b/') + assert_equal('b/', url.to_s) + url = URI.parse('http://hoge/b/a').route_to('http://hoge/b/') + assert_equal('./', url.to_s) + url = URI.parse('http://hoge/b/').route_to('http://hoge/b') + assert_equal('../b', url.to_s) + url = URI.parse('http://hoge/b').route_to('http://hoge/b:c') + assert_equal('./b: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') + assert_equal('../b', url.to_s) url = URI.parse('mailto:foo@example.com').route_to('mailto:foo@example.com#bar') assert_equal('#bar', url.to_s) |
