1 | #
|
---|
2 | # = uri/ftp.rb
|
---|
3 | #
|
---|
4 | # Author:: Akira Yamada <[email protected]>
|
---|
5 | # License:: You can redistribute it and/or modify it under the same term as Ruby.
|
---|
6 | # Revision:: $Id: ftp.rb 11757 2007-02-15 11:36:28Z knu $
|
---|
7 | #
|
---|
8 |
|
---|
9 | require 'uri/generic'
|
---|
10 |
|
---|
11 | module URI
|
---|
12 |
|
---|
13 | #
|
---|
14 | # RFC1738 section 3.2.
|
---|
15 | #
|
---|
16 | class FTP < Generic
|
---|
17 | DEFAULT_PORT = 21
|
---|
18 |
|
---|
19 | COMPONENT = [
|
---|
20 | :scheme,
|
---|
21 | :userinfo, :host, :port,
|
---|
22 | :path, :typecode
|
---|
23 | ].freeze
|
---|
24 | #
|
---|
25 | # Typecode is, "a", "i" or "d".
|
---|
26 | # As for "a" the text, as for "i" binary,
|
---|
27 | # as for "d" the directory is displayed.
|
---|
28 | # "A" with the text, as for "i" being binary,
|
---|
29 | # is because the respective data type was called ASCII and
|
---|
30 | # IMAGE with the protocol of FTP.
|
---|
31 | #
|
---|
32 | TYPECODE = ['a', 'i', 'd'].freeze
|
---|
33 | TYPECODE_PREFIX = ';type='.freeze
|
---|
34 |
|
---|
35 | def self.new2(user, password, host, port, path,
|
---|
36 | typecode = nil, arg_check = true)
|
---|
37 | typecode = nil if typecode.size == 0
|
---|
38 | if typecode && !TYPECODE.include?(typecode)
|
---|
39 | raise ArgumentError,
|
---|
40 | "bad typecode is specified: #{typecode}"
|
---|
41 | end
|
---|
42 |
|
---|
43 | # do escape
|
---|
44 |
|
---|
45 | self.new('ftp',
|
---|
46 | [user, password],
|
---|
47 | host, port, nil,
|
---|
48 | typecode ? path + TYPECODE_PREFIX + typecode : path,
|
---|
49 | nil, nil, nil, arg_check)
|
---|
50 | end
|
---|
51 |
|
---|
52 | #
|
---|
53 | # == Description
|
---|
54 | #
|
---|
55 | # Creates a new URI::FTP object from components of URI::FTP with
|
---|
56 | # check. It is scheme, userinfo, host, port, path and typecode. It
|
---|
57 | # provided by an Array or a Hash. typecode is "a", "i" or "d".
|
---|
58 | #
|
---|
59 | def self.build(args)
|
---|
60 | tmp = Util::make_components_hash(self, args)
|
---|
61 |
|
---|
62 | if tmp[:typecode]
|
---|
63 | if tmp[:typecode].size == 1
|
---|
64 | tmp[:typecode] = TYPECODE_PREFIX + tmp[:typecode]
|
---|
65 | end
|
---|
66 | tmp[:path] << tmp[:typecode]
|
---|
67 | end
|
---|
68 |
|
---|
69 | return super(tmp)
|
---|
70 | end
|
---|
71 |
|
---|
72 | #
|
---|
73 | # == Description
|
---|
74 | #
|
---|
75 | # Create a new URI::FTP object from ``generic'' components with no
|
---|
76 | # check.
|
---|
77 | #
|
---|
78 | # == Usage
|
---|
79 | #
|
---|
80 | # require 'uri'
|
---|
81 | # p ftp = URI.parse("ftp://ftp.ruby-lang.org/pub/ruby/;type=d")
|
---|
82 | # # => #<URI::FTP:0x201fad08 URL:ftp://ftp.ruby-lang.org/pub/ruby/;type=d>
|
---|
83 | # p ftp.typecode
|
---|
84 | # # => "d"
|
---|
85 | #
|
---|
86 | def initialize(*arg)
|
---|
87 | super(*arg)
|
---|
88 | @typecode = nil
|
---|
89 | tmp = @path.index(TYPECODE_PREFIX)
|
---|
90 | if tmp
|
---|
91 | typecode = @path[tmp + TYPECODE_PREFIX.size..-1]
|
---|
92 | self.set_path(@path[0..tmp - 1])
|
---|
93 |
|
---|
94 | if arg[-1]
|
---|
95 | self.typecode = typecode
|
---|
96 | else
|
---|
97 | self.set_typecode(typecode)
|
---|
98 | end
|
---|
99 | end
|
---|
100 | end
|
---|
101 | attr_reader :typecode
|
---|
102 |
|
---|
103 | def check_typecode(v)
|
---|
104 | if TYPECODE.include?(v)
|
---|
105 | return true
|
---|
106 | else
|
---|
107 | raise InvalidComponentError,
|
---|
108 | "bad typecode(expected #{TYPECODE.join(', ')}): #{v}"
|
---|
109 | end
|
---|
110 | end
|
---|
111 | private :check_typecode
|
---|
112 |
|
---|
113 | def set_typecode(v)
|
---|
114 | @typecode = v
|
---|
115 | end
|
---|
116 | protected :set_typecode
|
---|
117 |
|
---|
118 | def typecode=(typecode)
|
---|
119 | check_typecode(typecode)
|
---|
120 | set_typecode(typecode)
|
---|
121 | typecode
|
---|
122 | end
|
---|
123 |
|
---|
124 | def merge(oth) # :nodoc:
|
---|
125 | tmp = super(oth)
|
---|
126 | if self != tmp
|
---|
127 | tmp.set_typecode(oth.typecode)
|
---|
128 | end
|
---|
129 |
|
---|
130 | return tmp
|
---|
131 | end
|
---|
132 |
|
---|
133 | def to_s
|
---|
134 | save_path = nil
|
---|
135 | if @typecode
|
---|
136 | save_path = @path
|
---|
137 | @path = @path + TYPECODE_PREFIX + @typecode
|
---|
138 | end
|
---|
139 | str = super
|
---|
140 | if @typecode
|
---|
141 | @path = save_path
|
---|
142 | end
|
---|
143 |
|
---|
144 | return str
|
---|
145 | end
|
---|
146 | end
|
---|
147 | @@schemes['FTP'] = FTP
|
---|
148 | end
|
---|