method
merge_path
v1_8_6_287 -
Show latest stable
- Class:
URI::Generic
merge_path(base, rel)private
No documentation available.
# File lib/uri/generic.rb, line 618
def merge_path(base, rel)
# RFC2396, Section 5.2, 5)
if rel[0] == ?/ #/
# RFC2396, Section 5.2, 5)
return rel
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
# 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
end
base_path.push('') if add_trailer_slash
return base_path.join('/')
end
end