summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-04-15 06:13:08 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-04-15 06:13:08 +0000
commit835693dac5bd2d38cac972adbfd7980c305435a4 (patch)
tree20ba6791478db81d8650400ea18ee8ce9285258f
parent6692ef3c54a6c2aaa01e122925f9fcc441bb3132 (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/trunk@31288 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--lib/uri/generic.rb26
-rw-r--r--test/uri/test_generic.rb5
3 files changed, 22 insertions, 15 deletions
diff --git a/ChangeLog b/ChangeLog
index f6d8907b17..1d9d6d6770 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Fri Apr 15 15:10:29 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]
+
Fri Apr 15 14:58:06 2011 Akinori MUSHA <knu@iDaemons.org>
* lib/fileutils.rb (FileUtils#touch): Fix corrupted output when
diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb
index f8354e1835..161d666ccb 100644
--- a/lib/uri/generic.rb
+++ b/lib/uri/generic.rb
@@ -862,30 +862,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 821b147fa0..1866b37f54 100644
--- a/test/uri/test_generic.rb
+++ b/test/uri/test_generic.rb
@@ -222,6 +222,9 @@ class URI::TestGeneric < Test::Unit::TestCase
url = URI.parse('http://hoge/a/b/').route_to('http://hoge/b/')
assert_equal('../../b/', url.to_s)
+ url = URI.parse('http://hoge/a/b/').route_to('http://hoge/a/b')
+ assert_equal('../b', url.to_s)
+
url = URI.parse('http://hoge/a/b/').route_to('http://HOGE/b/')
assert_equal('../../b/', url.to_s)
@@ -230,6 +233,8 @@ class URI::TestGeneric < Test::Unit::TestCase
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)