From ad4a9f1b8df29473118a5be3b772e6f05d0527a0 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Mon, 7 Mar 2011 17:36:07 +0900 Subject: [PATCH] * lib/uri/generic.rb (#route_from_path): Fix a bug in relative route computation. URI.parse('http://hoge/b/').route_to('http://hoge/b') should return '../b' instead of './'. --- ChangeLog | 7 +++++++ lib/uri/generic.rb | 26 +++++++++++--------------- test/uri/test_generic.rb | 9 +++++++++ 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index da102dc..b9c84ac 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Mon Mar 7 17:30:28 2011 Akinori MUSHA + + * lib/uri/generic.rb (#route_from_path): Fix a bug in relative + route computation. + URI.parse('http://hoge/b/').route_to('http://hoge/b') should + return '../b' instead of './'. + Mon Mar 7 09:05:18 2011 Nobuyoshi Nakada * process.c: NUM2RLIM is defined but no getrlimit and setrlimit on diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index f8354e1..161d666 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 821b147..de6b3f0 100644 --- a/test/uri/test_generic.rb +++ b/test/uri/test_generic.rb @@ -228,6 +228,15 @@ class URI::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) -- 1.7.4.1