class URI::FTP
FTP
URI
语法由 RFC1738 第 3.2 节定义。
这个类将由于实现上的差异而重新设计;其路径的结构。 draft-hoffman-ftp-uri-04 是一个草案,但它是关于事实规范的一个很好的总结。datatracker.ietf.org/doc/html/draft-hoffman-ftp-uri-04
常量
属性
typecode 访问器。
请参阅 URI::FTP::COMPONENT
。
公共类方法
源代码
# File lib/uri/ftp.rb, line 96 def self.build(args) # Fix the incoming path to be generic URL syntax # FTP path -> URL path # foo/bar /foo/bar # /foo/bar /%2Ffoo/bar # if args.kind_of?(Array) args[3] = '/' + args[3].sub(/^\//, '%2F') else args[:path] = '/' + args[:path].sub(/^\//, '%2F') end tmp = Util::make_components_hash(self, args) if tmp[:typecode] if tmp[:typecode].size == 1 tmp[:typecode] = TYPECODE_PREFIX + tmp[:typecode] end tmp[:path] << tmp[:typecode] end return super(tmp) end
描述¶ ↑
使用组件创建一个新的 URI::FTP
对象,并进行语法检查。
接受的组件为 userinfo
、host
、port
、path
和 typecode
。
组件应以 Array
或 Hash
的形式提供,其中键是通过在组件名称前加上冒号形成的。
如果使用 Array
,则组件必须按 [userinfo, host, port, path, typecode]
的顺序传递。
如果提供的路径是绝对路径,则会对其进行转义,以便使其在 URI
中成为绝对路径。
示例
require 'uri' uri1 = URI::FTP.build(['user:password', 'ftp.example.com', nil, '/path/file.zip', 'i']) uri1.to_s # => "ftp://user:password@ftp.example.com/%2Fpath/file.zip;type=i" uri2 = URI::FTP.build({:host => 'ftp.example.com', :path => 'ruby/src'}) uri2.to_s # => "ftp://ftp.example.com/ruby/src"
源代码
# File lib/uri/ftp.rb, line 133 def initialize(scheme, userinfo, host, port, registry, path, opaque, query, fragment, parser = nil, arg_check = false) raise InvalidURIError unless path path = path.sub(/^\//,'') path.sub!(/^%2F/,'/') super(scheme, userinfo, host, port, registry, path, opaque, query, fragment, parser, arg_check) @typecode = nil if tmp = @path.index(TYPECODE_PREFIX) typecode = @path[tmp + TYPECODE_PREFIX.size..-1] @path = @path[0..tmp - 1] if arg_check self.typecode = typecode else self.set_typecode(typecode) end end end
描述¶ ↑
从通用 URL 组件创建一个新的 URI::FTP
对象,不进行语法检查。
与 build() 不同,此方法不会按照 RFC1738 的要求转义路径组件;而是按照 RFC2396 进行处理。
参数按顺序为 scheme
、userinfo
、host
、port
、registry
、path
、opaque
、query
和 fragment
。
公共实例方法
源代码
# File lib/uri/ftp.rb, line 240 def path return @path.sub(/^\//,'').sub(/^%2F/,'/') end
RFC 1738 特别指出,FTP
URI
的路径不包括将 URI
路径与 URI
主机分隔开的 /。示例
ftp://ftp.example.com/pub/ruby
上面的 URI
指示客户端应连接到 ftp.example.com,然后从初始登录目录 cd 到 pub/ruby。
如果要 cd 到绝对目录,则必须在路径中包含转义的 / (%2F)。示例
ftp://ftp.example.com/%2Fpub/ruby
此方法将返回 “/pub/ruby”。
源代码
源代码
# File lib/uri/ftp.rb, line 208 def typecode=(typecode) check_typecode(typecode) set_typecode(typecode) typecode end
参数¶ ↑
v
描述¶ ↑
类型代码 v
的公共设置器(带有验证)。
另请参阅 URI::FTP.check_typecode
。
用法¶ ↑
require 'uri' uri = URI.parse("ftp://john@ftp.example.com/my_file.img") #=> #<URI::FTP ftp://john@ftp.example.com/my_file.img> uri.typecode = "i" uri #=> #<URI::FTP ftp://john@ftp.example.com/my_file.img;type=i>
受保护的实例方法
源代码
# File lib/uri/ftp.rb, line 245 def set_path(v) super("/" + v.sub(/^\//, "%2F")) end
URI::FTP
路径的私有设置器。
源代码
# File lib/uri/ftp.rb, line 180 def set_typecode(v) @typecode = v end
类型代码 v
的私有设置器。
另请参阅 URI::FTP.typecode=
。
私有实例方法
源代码
# File lib/uri/ftp.rb, line 166 def check_typecode(v) if TYPECODE.include?(v) return true else raise InvalidComponentError, "bad typecode(expected #{TYPECODE.join(', ')}): #{v}" end end
验证类型代码 v
,返回 true
或 false
。