new(host = nil, user_or_options = {}, passwd = nil, acct = nil)
public
Creates and returns a new
FTP object. If a host is given, a connection is made.
options is an option hash, each key of which is a symbol.
The available options are:
port |
Port number (default value is 21)
|
ssl |
If options[:ssl] is true, then an attempt will be made to use SSL (now TLS)
to connect to the server. For this to
work OpenSSL [OSSL] and the Ruby OpenSSL [RSSL] extensions need to be installed.
If options[:ssl] is a hash, it’s passed to OpenSSL::SSL::SSLContext#set_params
as parameters.
|
private_data_connection |
If true, TLS is used for data connections. Default: true when
options[:ssl] is true.
|
username |
Username for login. If
options[:username] is the string “anonymous” and the options[:password]
is nil, “anonymous@” is used as a password.
|
password |
Password for login.
|
account |
Account information for ACCT.
|
passive |
When true, the connection is in passive mode. Default:
true.
|
open_timeout |
Number of seconds to wait for the connection to open. See Net::FTP#open_timeout for
details. Default: nil.
|
read_timeout |
Number of seconds to wait for one block to be read. See
Net::FTP#read_timeout for details. Default: 60.
|
ssl_handshake_timeout |
Number of seconds to wait for the TLS handshake. See
Net::FTP#ssl_handshake_timeout for details. Default: nil.
|
debug_mode |
When true, all traffic to and from the server is written to
+$stdout+. Default: false.
|
Show source
def initialize(host = nil, user_or_options = {}, passwd = nil, acct = nil)
super()
begin
options = user_or_options.to_hash
rescue NoMethodError
options = {}
options[:username] = user_or_options
options[:password] = passwd
options[:account] = acct
end
@host = nil
if options[:ssl]
unless defined?(OpenSSL::SSL)
raise "SSL extension not installed"
end
ssl_params = options[:ssl] == true ? {} : options[:ssl]
@ssl_context = SSLContext.new
@ssl_context.set_params(ssl_params)
if defined?(VerifyCallbackProc)
@ssl_context.verify_callback = VerifyCallbackProc
end
@ssl_context.session_cache_mode =
OpenSSL::SSL::SSLContext::SESSION_CACHE_CLIENT |
OpenSSL::SSL::SSLContext::SESSION_CACHE_NO_INTERNAL_STORE
@ssl_context.session_new_cb = proc {|sock, sess| @ssl_session = sess }
@ssl_session = nil
if options[:private_data_connection].nil?
@private_data_connection = true
else
@private_data_connection = options[:private_data_connection]
end
else
@ssl_context = nil
if options[:private_data_connection]
raise ArgumentError,
"private_data_connection can be set to true only when ssl is enabled"
end
@private_data_connection = false
end
@binary = true
if options[:passive].nil?
@passive = @@default_passive
else
@passive = options[:passive]
end
if options[:debug_mode].nil?
@debug_mode = false
else
@debug_mode = options[:debug_mode]
end
@resume = false
@bare_sock = @sock = NullSocket.new
@logged_in = false
@open_timeout = options[:open_timeout]
@ssl_handshake_timeout = options[:ssl_handshake_timeout]
@read_timeout = options[:read_timeout] || 60
if host
connect(host, options[:port] || FTP_PORT)
if options[:username]
login(options[:username], options[:password], options[:account])
end
end
end