From 12b1e59f2eacd346da550bbc30cc3644909cb314 Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 1 Aug 2007 03:40:08 +0000 Subject: * generic.rb (URI::Generic::merge_path): behave as RFC 3986. [ruby-talk:252052] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/uri/generic.rb | 104 ++++++++++++++++++++++++++--------------------------- 1 file changed, 52 insertions(+), 52 deletions(-) (limited to 'lib/uri') diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index 2b66adeb93..bcf293ec0a 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -616,65 +616,65 @@ module URI private :split_path def merge_path(base, rel) + # RFC2396, Section 5.2, 5) - if rel[0] == ?/ #/ - # RFC2396, Section 5.2, 5) - return rel + # RFC2396, Section 5.2, 6) + base_path = split_path(base) + rel_path = split_path(rel) + + # RFC2396, Section 5.2, 6), a) + base_path << '' if base_path.last == '..' + while i = base_path.index('..') + base_path.slice!(i - 1, 2) + end - else - # RFC2396, Section 5.2, 6) - base_path = split_path(base) - rel_path = split_path(rel) - - # RFC2396, Section 5.2, 6), a) - base_path << '' if base_path.last == '..' - while i = base_path.index('..') - base_path.slice!(i - 1, 2) - end - if base_path.empty? - base_path = [''] # keep '/' for root directory - else - base_path.pop - end + if (first = rel_path.first) and first.empty? + base_path.clear + rel_path.shift + end - # RFC2396, Section 5.2, 6), c) - # RFC2396, Section 5.2, 6), d) - rel_path.push('') if rel_path.last == '.' || rel_path.last == '..' - rel_path.delete('.') - - # RFC2396, Section 5.2, 6), e) - tmp = [] - rel_path.each do |x| - if x == '..' && - !(tmp.empty? || tmp.last == '..') - tmp.pop - else - tmp << x - end + # RFC2396, Section 5.2, 6), c) + # RFC2396, Section 5.2, 6), d) + rel_path.push('') if rel_path.last == '.' || rel_path.last == '..' + rel_path.delete('.') + + # RFC2396, Section 5.2, 6), e) + tmp = [] + rel_path.each do |x| + if x == '..' && + !(tmp.empty? || tmp.last == '..') + tmp.pop + else + tmp << x end + end - add_trailer_slash = true - while x = tmp.shift - if x == '..' && base_path.size > 1 - # RFC2396, Section 4 - # a .. or . in an absolute path has no special meaning - base_path.pop - else - # if x == '..' - # valid absolute (but abnormal) path "/../..." - # else - # valid absolute path - # end - base_path << x - tmp.each {|t| base_path << t} - add_trailer_slash = false - break - end + add_trailer_slash = !tmp.empty? + if base_path.empty? + base_path = [''] # keep '/' for root directory + elsif add_trailer_slash + base_path.pop + end + while x = tmp.shift + if x == '..' + # RFC2396, Section 4 + # a .. or . in an absolute path has no special meaning + base_path.pop if base_path.size > 1 + else + # if x == '..' + # valid absolute (but abnormal) path "/../..." + # else + # valid absolute path + # end + base_path << x + tmp.each {|t| base_path << t} + add_trailer_slash = false + break end - base_path.push('') if add_trailer_slash - - return base_path.join('/') end + base_path.push('') if add_trailer_slash + + return base_path.join('/') end private :merge_path -- cgit v1.2.3