method
merge_path
v1_9_3_392 -
Show latest stable
- Class:
URI::Generic
merge_path(base, rel)private
Merges a base path base, with relative path rel, returns a modified base path.
# File lib/uri/generic.rb, line 1088
def merge_path(base, rel)
# RFC2396, Section 5.2, 5)
# 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 (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
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
end
base_path.push('') if add_trailer_slash
return base_path.join('/')
end Related methods
- Instance methods
- +
- -
- ==
- absolute
- absolute?
- coerce
- component
- default_port
- eql?
- find_proxy
- fragment=
- hash
- hierarchical?
- host=
- hostname
- hostname=
- inspect
- merge
- merge!
- normalize
- normalize!
- opaque=
- parser
- password
- password=
- path=
- port=
- query=
- registry=
- relative?
- route_from
- route_to
- scheme=
- select
- to_s
- user
- user=
- userinfo
- userinfo=
- Class methods
- build
- build2
- component
- default_port
- new
- use_registry
- Protected methods
-
component_ary -
set_fragment -
set_host -
set_opaque -
set_password -
set_path -
set_port -
set_query -
set_registry -
set_scheme -
set_user -
set_userinfo - Private methods
-
check_fragment -
check_host -
check_opaque -
check_password -
check_path -
check_port -
check_query -
check_registry -
check_scheme -
check_user -
check_userinfo -
escape_userpass -
merge0 -
merge_path -
path_query -
replace! -
split_path -
split_userinfo