首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

URI::FTP

Parent:GenericIncluded modules:OpenURI::OpenRead

FTP URI语法由RFC1738第3.2节定义。

由于实现的不同,这个类将被重新设计; 它的路径结构。draft-hoffman-ftp-uri-04是一个草稿,但它是对事实规范的一个很好的总结。tools.ietf.org/html/draft-hoffman-ftp-uri-04

常量

COMPONENT

URI::FTP可用组件的数组

DEFAULT_PORT

A Default port of 21 for URI::FTP

TYPECODE

Typecode是“a”,“i”或“d”。

  • “a”表示一个文本文件(FTP命令是ASCII)
  • “i”表示二进制文件(FTP命令IMAGE)
  • “d”表示应显示目录的内容

TYPECODE_PREFIX

Typecode prefix

代码语言:javascript
复制
';type='

属性

typecodeR

typecode accessor

see URI::FTP::COMPONENT

公共类别方法

build(args) Show source

描述

从组件创建一个新的URI::FTP对象,并进行语法检查。

接受的成分是userinfohostportpathtypecode

这些组件应该以数组的形式提供,或者以组件名称前面用冒号形成的哈希形式提供。

如果使用数组,则必须按顺序传递组件

userinfo, host, port, path, typecode

如果提供的路径是绝对路径,它将被转义以便使其在URI中绝对。例子:

代码语言:javascript
复制
require 'uri'

uri = URI::FTP.build(['user:password', 'ftp.example.com', nil,
  '/path/file.zip', 'i'])
puts uri.to_s  ->  ftp://user:password@ftp.example.com/%2Fpath/file.zip;type=i

uri2 = URI::FTP.build({:host => 'ftp.example.com',
  :path => 'ruby/src'})
puts uri2.to_s  ->  ftp://ftp.example.com/ruby/src

调用超类方法

代码语言:javascript
复制
# File lib/uri/ftp.rb, line 95
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

new(scheme, userinfo, host, port, registry, path, opaque, query, fragment, parser = nil, arg_check = false) Show source

描述

从通用URL组件创建新的URI0::FTP对象,不进行语法检查。

与build()不同,此方法不会像RFC1738所要求的那样转义路径组件;相反,它按照RFC2396进行处理。

参数是schemeuserinfohostportregistrypathopaquequeryfragment按此顺序。

调用超类方法

代码语言:javascript
复制
# File lib/uri/ftp.rb, line 132
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

公共实例方法

path() Show source

返回来自FTP URI的路径。

RFC 1738明确指出,FTP URI的路径不包括将URI路径与URI主机分开的/。例:

代码语言:javascript
复制
ftp://ftp.example.com/pub/ruby

上面的URI表示客户端应该连接到ftp.example.com,然后从初始登录目录cd pub / ruby​​。

如果您想要cd到绝对目录,则必须在路径中包含转义的/(%2F)。例:

代码语言:javascript
复制
ftp://ftp.example.com/%2Fpub/ruby

此方法将返回“/pub/ruby​​”

代码语言:javascript
复制
# File lib/uri/ftp.rb, line 240
def path
  return @path.sub(/^\//,'').sub(/^%2F/,'/')
end

to_s() Show source

返回URI::FTP的字符串表示形式

调用超类方法

代码语言:javascript
复制
# File lib/uri/ftp.rb, line 251
def to_s
  save_path = nil
  if @typecode
    save_path = @path
    @path = @path + TYPECODE_PREFIX + @typecode
  end
  str = super
  if @typecode
    @path = save_path
  end

  return str
end

typecode=(typecode) Show source

Args

v

String

描述

typecode的公共setter v.(有验证)

see also #check_typecode

用法

代码语言:javascript
复制
require 'uri'

uri = URI.parse("ftp://john@ftp.example.com/my_file.img")
#=> #<URI::FTP:0x00000000923650 URL:ftp://john@ftp.example.com/my_file.img>
uri.typecode = "i"
# =>  "i"
uri
#=> #<URI::FTP:0x00000000923650 URL:ftp://john@ftp.example.com/my_file.img;type=i>
代码语言:javascript
复制
# File lib/uri/ftp.rb, line 208
def typecode=(typecode)
  check_typecode(typecode)
  set_typecode(typecode)
  typecode
end

受保护的实例方法

set_path(v) Show source

URI::FTP路径的私有setter

调用超类方法

代码语言:javascript
复制
# File lib/uri/ftp.rb, line 245
def set_path(v)
  super("/" + v.sub(/^\//, "%2F"))
end

set_typecode(v) Show source

typecode的私人二传手 v

另见#typecode =

代码语言:javascript
复制
# File lib/uri/ftp.rb, line 179
def set_typecode(v)
  @typecode = v
end

私有实例方法

check_typecode(v) Show source

验证typecode v,返回一个truefalse布尔值

代码语言:javascript
复制
# File lib/uri/ftp.rb, line 165
def check_typecode(v)
  if TYPECODE.include?(v)
    return true
  else
    raise InvalidComponentError,
      "bad typecode(expected #{TYPECODE.join(', ')}): #{v}"
  end
end

扫码关注腾讯云开发者

领取腾讯云代金券