ruby-changes:19537
From: knu <ko1@a...>
Date: Sun, 15 May 2011 21:49:42 +0900 (JST)
Subject: [ruby-changes:19537] knu:r31578 (ruby_1_8): * lib/uri/generic.rb (#route_from_path): Fix a bug where
knu 2011-05-15 21:49:34 +0900 (Sun, 15 May 2011) New Revision: 31578 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=31578 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: branches/ruby_1_8/ChangeLog branches/ruby_1_8/lib/uri/generic.rb branches/ruby_1_8/test/uri/test_generic.rb Index: ruby_1_8/ChangeLog =================================================================== --- ruby_1_8/ChangeLog (revision 31577) +++ ruby_1_8/ChangeLog (revision 31578) @@ -1,3 +1,9 @@ +Sun May 15 21:43:09 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] + Sun May 15 21:37:54 2011 Akinori MUSHA <knu@i...> * lib/fileutils.rb (FileUtils#touch): Fix corrupted output. Index: ruby_1_8/lib/uri/generic.rb =================================================================== --- ruby_1_8/lib/uri/generic.rb (revision 31577) +++ ruby_1_8/lib/uri/generic.rb (revision 31578) @@ -799,30 +799,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: ruby_1_8/test/uri/test_generic.rb =================================================================== --- ruby_1_8/test/uri/test_generic.rb (revision 31577) +++ ruby_1_8/test/uri/test_generic.rb (revision 31578) @@ -231,8 +231,19 @@ 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@e...').route_to('mailto:foo@e...#bar') assert_equal('#bar', url.to_s) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/