ruby-changes:19249
From: knu <ko1@a...>
Date: Fri, 15 Apr 2011 17:04:28 +0900 (JST)
Subject: [ruby-changes:19249] Ruby:r31288 (trunk): * lib/uri/generic.rb (#route_from_path): Fix a bug where
knu 2011-04-15 15:13:08 +0900 (Fri, 15 Apr 2011) New Revision: 31288 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=31288 Log: * 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] Modified files: trunk/ChangeLog trunk/lib/uri/generic.rb trunk/test/uri/test_generic.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 31287) +++ ChangeLog (revision 31288) @@ -1,3 +1,9 @@ +Fri Apr 15 15:10:29 2011 Akinori MUSHA <knu@i...> + + * 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@i...> * lib/fileutils.rb (FileUtils#touch): Fix corrupted output when Index: lib/uri/generic.rb =================================================================== --- lib/uri/generic.rb (revision 31287) +++ lib/uri/generic.rb (revision 31288) @@ -862,30 +862,26 @@ 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? Index: test/uri/test_generic.rb =================================================================== --- test/uri/test_generic.rb (revision 31287) +++ test/uri/test_generic.rb (revision 31288) @@ -222,6 +222,9 @@ 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 @@ 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@e...').route_to('mailto:foo@e...#bar') assert_equal('#bar', url.to_s) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/