class URI::Generic
Constants
- COMPONENT
-
URI::Generic可用组件的Array。 - DEFAULT_PORT
-
URI::Generic的默认端口为 nil。
属性
返回 URI 的 fragment 组件。
URI("http://foo/bar/baz?search=FooBar#ponies").fragment #=> "ponies"
返回 URI 的 host 组件。
URI("http://foo/bar/baz").host #=> "foo"
如果没有 host 组件,则返回 nil。
URI("mailto:foo@example.org").host #=> nil
该组件不包含端口号。
URI("http://foo:8080/bar/baz").host #=> "foo"
由于 IPv6 地址在 URI 中用方括号括起来,此方法返回带方括号的 IPv6 地址。此格式不适用于传递给套接字方法(如 TCPSocket.open)。如果需要未加方括号的主机名,请使用 hostname 方法。
URI("http://[::1]/bar/baz").host #=> "[::1]" URI("http://[::1]/bar/baz").hostname #=> "::1"
返回 URI 的 opaque 部分。
URI("mailto:foo@example.org").opaque #=> "foo@example.org" URI("http://foo/bar/baz").opaque #=> nil
路径中不使用斜杠 '/' 的部分。路径通常指绝对路径或不透明部分。(参见 RFC2396 Section 3 和 5.2。)
返回 URI 的 port 组件。
URI("http://foo/bar/baz").port #=> 80 URI("http://foo:8080/bar/baz").port #=> 8080
返回 URI 的 scheme 组件。
URI("http://foo/bar/baz").scheme #=> "http"
Public Class Methods
Source
# File lib/uri/generic.rb, line 116 def self.build(args) if args.kind_of?(Array) && args.size == ::URI::Generic::COMPONENT.size tmp = args.dup elsif args.kind_of?(Hash) tmp = ::URI::Generic::COMPONENT.collect do |c| if args.include?(c) args[c] else nil end end else component = self.component rescue ::URI::Generic::COMPONENT raise ArgumentError, "expected Array of or Hash of components of #{self} (#{component.join(', ')})" end tmp << nil tmp << true return self.new(*tmp) end
摘要
参见 ::new。
描述
从 URI::Generic 的组件构建新的 URI::Generic 实例,并进行检查。组件包括:scheme、userinfo、host、port、registry、path、opaque、query 和 fragment。您可以通过 Array 或 Hash 提供参数。有关要使用的哈希键或数组项的顺序,请参见 ::new。
Source
# File lib/uri/generic.rb, line 78 def self.build2(args) begin return self.build(args) rescue InvalidComponentError if args.kind_of?(Array) return self.build(args.collect{|x| if x.is_a?(String) URI::RFC2396_PARSER.escape(x) else x end }) elsif args.kind_of?(Hash) tmp = {} args.each do |key, value| tmp[key] = if value URI::RFC2396_PARSER.escape(value) else value end end return self.build(tmp) end end end
摘要
参见 ::new。
描述
首先,尝试使用 URI::Generic::build 创建新的 URI::Generic 实例。但是,如果抛出 URI::InvalidComponentError 异常,则会对所有 URI 组件进行 URI::RFC2396_PARSER.escape,然后重试。
Source
# File lib/uri/generic.rb, line 32 def self.default_port self::DEFAULT_PORT end
返回默认端口。
Source
# File lib/uri/generic.rb, line 169 def initialize(scheme, userinfo, host, port, registry, path, opaque, query, fragment, parser = DEFAULT_PARSER, arg_check = false) @scheme = nil @user = nil @password = nil @host = nil @port = nil @path = nil @query = nil @opaque = nil @fragment = nil @parser = parser == DEFAULT_PARSER ? nil : parser if arg_check self.scheme = scheme self.hostname = host self.port = port self.userinfo = userinfo self.path = path self.query = query self.opaque = opaque self.fragment = fragment else self.set_scheme(scheme) self.set_host(host) self.set_port(port) self.set_userinfo(userinfo) self.set_path(path) self.query = query self.set_opaque(opaque) self.fragment=(fragment) end if registry raise InvalidURIError, "the scheme #{@scheme} does not accept registry part: #{registry} (or bad hostname?)" end @scheme&.freeze self.set_path('') if !@path && !@opaque # (see RFC2396 Section 5.2) self.set_port(self.default_port) if self.default_port && !@port end
参数
scheme-
协议方案,例如 ‘http’、‘ftp’、‘mailto’ 等。
userinfo-
用户名和密码,例如 ‘sdmitry:bla’。
host-
服务器主机名。
port-
服务器端口。
registry-
命名机构的注册表。
path-
服务器上的路径。
opaque-
不透明部分。
query-
查询数据。
fragment-
‘#’ 字符后面的
URI的一部分。 解析器-
用于内部使用的解析器 [默认为 URI::DEFAULT_PARSER]。
arg_check-
检查参数 [默认为 false]。
描述
从“通用”组件创建一个新的 URI::Generic 实例,不进行检查。
Public Instance Methods
Source
# File lib/uri/generic.rb, line 1399 def ==(oth) if self.class == oth.class self.normalize.component_ary == oth.normalize.component_ary else false end end
比较两个 URI。
Source
# File lib/uri/generic.rb, line 987 def absolute? if @scheme true else false end end
如果 URI 指定了 scheme(例如 http:// 或 https://),则返回 true。
Source
# File lib/uri/generic.rb, line 1478 def coerce(oth) case oth when String oth = parser.parse(oth) else super end return oth, self end
Source
# File lib/uri/generic.rb, line 589 def decoded_password URI.decode_uri_component(@password) if @password end
返回 URI 解码后的 password 组件。
Source
# File lib/uri/generic.rb, line 584 def decoded_user URI.decode_uri_component(@user) if @user end
返回 URI 解码后的 user 组件。
Source
# File lib/uri/generic.rb, line 39 def default_port self.class.default_port end
返回默认端口。
Source
# File lib/uri/generic.rb, line 1413 def eql?(oth) self.class == oth.class && parser == oth.parser && self.component_ary.eql?(oth.component_ary) end
与 oth 比较以用于 Hash。
Source
# File lib/uri/generic.rb, line 1504 def find_proxy(env=ENV) raise BadURIError, "relative URI: #{self}" if self.relative? name = self.scheme.downcase + '_proxy' proxy_uri = nil if name == 'http_proxy' && env.include?('REQUEST_METHOD') # CGI? # HTTP_PROXY conflicts with *_proxy for proxy settings and # HTTP_* for header information in CGI. # So it should be careful to use it. pairs = env.reject {|k, v| /\Ahttp_proxy\z/i !~ k } case pairs.length when 0 # no proxy setting anyway. proxy_uri = nil when 1 k, _ = pairs.shift if k == 'http_proxy' && env[k.upcase] == nil # http_proxy is safe to use because ENV is case sensitive. proxy_uri = env[name] else proxy_uri = nil end else # http_proxy is safe to use because ENV is case sensitive. proxy_uri = env.to_hash[name] end if !proxy_uri # Use CGI_HTTP_PROXY. cf. libwww-perl. proxy_uri = env["CGI_#{name.upcase}"] end elsif name == 'http_proxy' if RUBY_ENGINE == 'jruby' && p_addr = ENV_JAVA['http.proxyHost'] p_port = ENV_JAVA['http.proxyPort'] if p_user = ENV_JAVA['http.proxyUser'] p_pass = ENV_JAVA['http.proxyPass'] proxy_uri = "http://#{p_user}:#{p_pass}@#{p_addr}:#{p_port}" else proxy_uri = "http://#{p_addr}:#{p_port}" end else unless proxy_uri = env[name] if proxy_uri = env[name.upcase] warn 'The environment variable HTTP_PROXY is discouraged. Please use http_proxy instead.', uplevel: 1 end end end else proxy_uri = env[name] || env[name.upcase] end if proxy_uri.nil? || proxy_uri.empty? return nil end if self.hostname begin addr = IPSocket.getaddress(self.hostname) return nil if /\A127\.|\A::1\z/ =~ addr rescue SocketError end end name = 'no_proxy' if no_proxy = env[name] || env[name.upcase] return nil unless URI::Generic.use_proxy?(self.hostname, addr, self.port, no_proxy) end URI.parse(proxy_uri) end
返回一个代理 URI。代理 URI 从环境变量(如 http_proxy、ftp_proxy、no_proxy 等)中获取。如果没有合适的代理,则返回 nil。
如果指定了可选参数 env,则使用它代替 ENV。
请注意,大写变量(HTTP_PROXY、FTP_PROXY、NO_PROXY 等)也会被检查。
但在 CGI 环境下,http_proxy 和 HTTP_PROXY 被特殊处理。这是因为 HTTP_PROXY 可能由 Proxy: header 设置。因此不使用 HTTP_PROXY。如果变量不区分大小写,也不使用 http_proxy。可以使用 CGI_HTTP_PROXY 代替。
Source
# File lib/uri/generic.rb, line 944 def fragment=(v) return @fragment = nil unless v x = v.to_str v = x.dup if x.equal? v v.encode!(Encoding::UTF_8) rescue nil v.delete!("\t\r\n") v.force_encoding(Encoding::ASCII_8BIT) v.gsub!(/(?!%\h\h|[!-~])./n){'%%%02X' % $&.ord} v.force_encoding(Encoding::US_ASCII) @fragment = v end
Source
# File lib/uri/generic.rb, line 976 def hierarchical? if @path true else false end end
如果 URI 是层次化的,则返回 true。
描述
URI 的组件按从左到右的降序排列,参见 RFC3986 www.rfc-editor.org/rfc/rfc3986 1.2.3。
用法
require 'uri' uri = URI.parse("http://my.example.com/") uri.hierarchical? #=> true uri = URI.parse("mailto:joe@example.com") uri.hierarchical? #=> false
Source
# File lib/uri/generic.rb, line 652 def host=(v) check_host(v) set_host(v) set_userinfo(nil) v end
参数
v
描述
host 组件 v 的公共 setter(带验证)。
另请参见 URI::Generic.check_host。
用法
require 'uri' uri = URI.parse("http://my.example.com") uri.host = "foo.com" uri.to_s #=> "http://foo.com"
Source
# File lib/uri/generic.rb, line 668 def hostname v = self.host v&.start_with?('[') && v.end_with?(']') ? v[1..-2] : v end
提取 URI 的 host 部分,并移除 IPv6 地址的方括号。
此方法与 URI::Generic#host 相同,只是移除了 IPv6(及未来 IP)地址的方括号。
uri = URI("http://[::1]/bar") uri.hostname #=> "::1" uri.host #=> "[::1]"
Source
# File lib/uri/generic.rb, line 685 def hostname=(v) v = "[#{v}]" if !(v&.start_with?('[') && v&.end_with?(']')) && v&.index(':') self.host = v end
将 URI 的 host 部分设置为参数,并为 IPv6 地址添加方括号。
此方法与 URI::Generic#host= 相同,只是参数可以是裸露的 IPv6 地址。
uri = URI("http://foo/bar") uri.hostname = "::1" uri.to_s #=> "http://[::1]/bar"
如果参数看起来是 IPv6 地址,则会用方括号括起来。
Source
# File lib/uri/generic.rb, line 1124 def merge(oth) rel = parser.__send__(:convert_to_uri, oth) if rel.absolute? #raise BadURIError, "both URI are absolute" if absolute? # hmm... should return oth for usability? return rel end unless self.absolute? raise BadURIError, "both URI are relative" end base = self.dup authority = rel.authority # RFC2396, Section 5.2, 2) if (rel.path.nil? || rel.path.empty?) && !authority && !rel.query base.fragment=(rel.fragment) if rel.fragment return base end base.query = nil base.fragment=(nil) # RFC2396, Section 5.2, 4) if authority base.set_authority(*authority) base.set_path(rel.path) elsif base.path && rel.path base.set_path(merge_path(base.path, rel.path)) end # RFC2396, Section 5.2, 7) base.query = rel.query if rel.query base.fragment=(rel.fragment) if rel.fragment return base end
参数
描述
合并两个 URI。
用法
require 'uri' uri = URI.parse("http://my.example.com") uri.merge("/main.rbx?page=1") # => "http://my.example.com/main.rbx?page=1"
Source
# File lib/uri/generic.rb, line 1096 def merge!(oth) t = merge(oth) if self == t nil else replace!(t) self end end
Source
# File lib/uri/generic.rb, line 1331 def normalize uri = dup uri.normalize! uri end
返回规范化后的 URI。
require 'uri' URI("HTTP://my.EXAMPLE.com").normalize #=> #<URI::HTTP http://my.example.com/>
这里的规范化是指:
-
scheme 和 host 被转换为小写,
-
空的 path 组件被设置为 “/”。
Source
# File lib/uri/generic.rb, line 1340 def normalize! if path&.empty? set_path('/') end if scheme && scheme != scheme.downcase set_scheme(self.scheme.downcase) end if host && host != host.downcase set_host(self.host.downcase) end end
normalize 的破坏性版本。
Source
# File lib/uri/generic.rb, line 916 def opaque=(v) check_opaque(v) set_opaque(v) v end
Source
# File lib/uri/generic.rb, line 289 def parser if !defined?(@parser) || !@parser DEFAULT_PARSER else @parser || DEFAULT_PARSER end end
返回要使用的解析器。
如果未定义 parser,则使用 DEFAULT_PARSER。
Source
# File lib/uri/generic.rb, line 498 def password=(password) check_password(password) set_password(password) # returns password end
参数
v
描述
password 组件的公共 setter(带验证)。
另请参见 URI::Generic.check_password。
用法
require 'uri' uri = URI.parse("http://john:S3nsit1ve@my.example.com") uri.password = "V3ry_S3nsit1ve" uri.to_s #=> "http://john:V3ry_S3nsit1ve@my.example.com"
Source
# File lib/uri/generic.rb, line 830 def path=(v) check_path(v) set_path(v) v end
参数
v
描述
path 组件 v 的公共 setter(带验证)。
另请参见 URI::Generic.check_path。
用法
require 'uri' uri = URI.parse("http://my.example.com/pub/files") uri.path = "/faq/" uri.to_s #=> "http://my.example.com/faq/"
Source
# File lib/uri/generic.rb, line 743 def port=(v) check_port(v) set_port(v) set_userinfo(nil) port end
参数
v
描述
port 组件 v 的公共 setter(带验证)。
另请参见 URI::Generic.check_port。
用法
require 'uri' uri = URI.parse("http://my.example.com") uri.port = 8080 uri.to_s #=> "http://my.example.com:8080"
Source
# File lib/uri/generic.rb, line 854 def query=(v) return @query = nil unless v raise InvalidURIError, "query conflicts with opaque" if @opaque x = v.to_str v = x.dup if x.equal? v v.encode!(Encoding::UTF_8) rescue nil v.delete!("\t\r\n") v.force_encoding(Encoding::ASCII_8BIT) raise InvalidURIError, "invalid percent escape: #{$1}" if /(%\H\H)/n.match(v) v.gsub!(/(?!%\h\h|[!$-&(-;=?-_a-~])./n.freeze){'%%%02X' % $&.ord} v.force_encoding(Encoding::US_ASCII) @query = v end
Source
# File lib/uri/generic.rb, line 999 def relative? !absolute? end
如果 URI 未指定 scheme(例如 http:// 或 https://),则返回 true。
Source
# File lib/uri/generic.rb, line 1274 def route_from(oth) # you can modify `rel', but cannot `oth'. begin oth, rel = route_from0(oth) rescue raise $!.class, $!.message end if oth == rel return rel end rel.set_path(route_from_path(oth.path, self.path)) if rel.path == './' && self.query # "./?foo" -> "?foo" rel.set_path('') end return rel end
参数
描述
计算从 oth 到 self 的相对路径。
用法
require 'uri' uri = URI.parse('http://my.example.com/main.rbx?page=1') uri.route_from('http://my.example.com') #=> #<URI::Generic /main.rbx?page=1>
Source
# File lib/uri/generic.rb, line 1314 def route_to(oth) parser.__send__(:convert_to_uri, oth).route_from(self) end
Source
# File lib/uri/generic.rb, line 360 def scheme=(v) check_scheme(v) set_scheme(v) v end
参数
v
描述
scheme 组件 v 的公共 setter(带验证)。
另请参见 URI::Generic.check_scheme。
用法
require 'uri' uri = URI.parse("http://my.example.com") uri.scheme = "https" uri.to_s #=> "https://my.example.com"
Source
# File lib/uri/generic.rb, line 1444 def select(*components) components.collect do |c| if component.include?(c) self.__send__(c) else raise ArgumentError, "expected of components of #{self.class} (#{self.class.component.join(', ')})" end end end
Source
# File lib/uri/generic.rb, line 1355 def to_s str = ''.dup if @scheme str << @scheme str << ':' end if @opaque str << @opaque else if @host || %w[file postgres].include?(@scheme) str << '//' end if self.userinfo str << self.userinfo str << '@' end if @host str << @host end if @port && @port != self.default_port str << ':' str << @port.to_s end if (@host || @port) && !@path.empty? && !@path.start_with?('/') str << '/' end str << @path if @query str << '?' str << @query end end if @fragment str << '#' str << @fragment end str end
Source
# File lib/uri/generic.rb, line 471 def user=(user) check_user(user) set_user(user) # returns user end
参数
v
描述
user 组件的公共 setter(带验证)。
另请参见 URI::Generic.check_user。
用法
require 'uri' uri = URI.parse("http://john:S3nsit1ve@my.example.com") uri.user = "sam" uri.to_s #=> "http://sam@my.example.com"
Source
# File lib/uri/generic.rb, line 557 def userinfo if @user.nil? nil elsif @password.nil? @user else @user + ':' + @password end end
返回 userinfo,可以是“user”或“user:password”。
Source
# File lib/uri/generic.rb, line 441 def userinfo=(userinfo) if userinfo.nil? return nil end check_userinfo(*userinfo) set_userinfo(*userinfo) # returns userinfo end
设置 userinfo,参数是类似“name:pass”的字符串。
Protected Instance Methods
Source
# File lib/uri/generic.rb, line 1420 def component_ary component.collect do |x| self.__send__(x) end end
Source
# File lib/uri/generic.rb, line 619 def set_host(v) @host = v end
host 组件 v 的受保护 setter。
另请参见 URI::Generic.host=。
Source
# File lib/uri/generic.rb, line 898 def set_opaque(v) @opaque = v end
opaque 组件 v 的受保护 setter。
另请参见 URI::Generic.opaque=。
Source
# File lib/uri/generic.rb, line 534 def set_password(v) @password = v # returns v end
password 组件 v 的受保护 setter。
另请参见 URI::Generic.password=。
Source
# File lib/uri/generic.rb, line 804 def set_path(v) @path = v end
path 组件 v 的受保护 setter。
另请参见 URI::Generic.path=。
Source
# File lib/uri/generic.rb, line 716 def set_port(v) v = v.empty? ? nil : v.to_i unless !v || v.kind_of?(Integer) @port = v end
port 组件 v 的受保护 setter。
另请参见 URI::Generic.port=。
Source
# File lib/uri/generic.rb, line 334 def set_scheme(v) @scheme = v&.downcase end
scheme 组件 v 的受保护 setter。
另请参见 URI::Generic.scheme=。
Source
# File lib/uri/generic.rb, line 524 def set_user(v) set_userinfo(v, nil) v end
user 组件 v 的受保护 setter。
另请参见 URI::Generic.user=。
Source
# File lib/uri/generic.rb, line 509 def set_userinfo(user, password = nil) unless password user, password = split_userinfo(user) end @user = user @password = password [@user, @password] end
user 组件以及(如果可用)password 的受保护 setter(带验证)。
另请参见 URI::Generic.userinfo=。
私有实例方法
Source
# File lib/uri/generic.rb, line 600 def check_host(v) return v unless v if @opaque raise InvalidURIError, "cannot set host with registry or opaque" elsif parser.regexp[:HOST] !~ v raise InvalidComponentError, "bad component(expected host component): #{v}" end return true end
检查 host v 组件是否符合 RFC2396,并使用 parser Regexp 中的 :HOST 进行匹配。
在定义了 host 组件的情况下,不能同时定义 registry 或 opaque 组件。
Source
# File lib/uri/generic.rb, line 876 def check_opaque(v) return v unless v # raise if both hier and opaque are not nil, because: # absoluteURI = scheme ":" ( hier_part | opaque_part ) # hier_part = ( net_path | abs_path ) [ "?" query ] if @host || @port || @user || @path # userinfo = @user + ':' + @password raise InvalidURIError, "cannot set opaque with host, port, userinfo or path" elsif v && parser.regexp[:OPAQUE] !~ v raise InvalidComponentError, "bad component(expected opaque component): #{v}" end return true end
检查 opaque v 组件是否符合 RFC2396,并使用 parser Regexp 中的 :OPAQUE 进行匹配。
在定义了 opaque 组件的情况下,不能同时定义 host、port、user 或 path 组件。
Source
# File lib/uri/generic.rb, line 417 def check_password(v, user = @user) if @opaque raise InvalidURIError, "cannot set password with opaque" end return v unless v if !user raise InvalidURIError, "password component depends user component" end if parser.regexp[:USERINFO] !~ v raise InvalidComponentError, "bad password component" end return true end
检查 password v 组件是否符合 RFC2396,并使用 parser Regexp 中的 :USERINFO 进行匹配。
在定义了 user 组件的情况下,不能同时定义 registry 或 opaque 组件。
Source
# File lib/uri/generic.rb, line 772 def check_path(v) # raise if both hier and opaque are not nil, because: # absoluteURI = scheme ":" ( hier_part | opaque_part ) # hier_part = ( net_path | abs_path ) [ "?" query ] if v && @opaque raise InvalidURIError, "path conflicts with opaque" end # If scheme is ftp, path may be relative. # See RFC 1738 section 3.2.2, and RFC 2396. if @scheme && @scheme != "ftp" if v && v != '' && parser.regexp[:ABS_PATH] !~ v raise InvalidComponentError, "bad component(expected absolute path component): #{v}" end else if v && v != '' && parser.regexp[:ABS_PATH] !~ v && parser.regexp[:REL_PATH] !~ v raise InvalidComponentError, "bad component(expected relative path component): #{v}" end end return true end
检查 path v 组件是否符合 RFC2396,并使用 parser Regexp 中的 :ABS_PATH 和 :REL_PATH 进行匹配。
在定义了 opaque 组件的情况下,不能同时定义 path 组件。
Source
# File lib/uri/generic.rb, line 697 def check_port(v) return v unless v if @opaque raise InvalidURIError, "cannot set port with registry or opaque" elsif !v.kind_of?(Integer) && parser.regexp[:PORT] !~ v raise InvalidComponentError, "bad component(expected port component): #{v.inspect}" end return true end
检查 port v 组件是否符合 RFC2396,并使用 parser Regexp 中的 :PORT 进行匹配。
在定义了 port 组件的情况下,不能同时定义 registry 或 opaque 组件。
Source
# File lib/uri/generic.rb, line 320 def check_scheme(v) if v && parser.regexp[:SCHEME] !~ v raise InvalidComponentError, "bad component(expected scheme component): #{v}" end return true end
使用 parser Regexp 中的 :SCHEME 检查 scheme v 组件。
Source
# File lib/uri/generic.rb, line 393 def check_user(v) if @opaque raise InvalidURIError, "cannot set user with opaque" end return v unless v if parser.regexp[:USERINFO] !~ v raise InvalidComponentError, "bad component(expected userinfo component or user component): #{v}" end return true end
检查 user v 组件是否符合 RFC2396,并使用 parser Regexp 中的 :USERINFO 进行匹配。
在定义了 user 组件的情况下,不能同时定义 registry 或 opaque 组件。
Source
# File lib/uri/generic.rb, line 375 def check_userinfo(user, password = nil) if !password user, password = split_userinfo(user) end check_user(user) check_password(password, user) return true end
检查 user 和 password。
如果未提供 password,则使用 URI::Generic.split_userinfo 来分割 user,以提取 user 和 +password。
Source
# File lib/uri/generic.rb, line 551 def escape_userpass(v) parser.escape(v, /[@:\/]/o) # RFC 1738 section 3.1 #/ end
根据 RFC 1738 Section 3.1 转义“user:password”v。
Source
# File lib/uri/generic.rb, line 1015 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
合并基础路径 base 和相对路径 rel,返回修改后的基础路径。
Source
# File lib/uri/generic.rb, line 299 def replace!(oth) if self.class != oth.class raise ArgumentError, "expected #{self.class} object" end component.each do |c| self.__send__("#{c}=", oth.__send__(c)) end end
用另一个 URI 对象替换自身。
Source
# File lib/uri/generic.rb, line 1006 def split_path(path) path.split("/", -1) end
返回按“/”分割的路径的 Array。
Source
# File lib/uri/generic.rb, line 542 def split_userinfo(ui) return nil, nil unless ui user, password = ui.split(':', 2) return user, password end
如果 userinfo ui 格式正确(为“user:password”),则将其作为 [user, password] 返回。