2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2004-03-24 06:53:31 -05:00
|
|
|
# = uri/ftp.rb
|
2002-01-10 03:00:51 -05:00
|
|
|
#
|
2004-03-24 06:53:31 -05:00
|
|
|
# Author:: Akira Yamada <akira@ruby-lang.org>
|
|
|
|
# License:: You can redistribute it and/or modify it under the same term as Ruby.
|
2002-01-10 03:00:51 -05:00
|
|
|
#
|
2011-05-13 16:03:21 -04:00
|
|
|
# See URI for general documentation
|
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
|
2018-11-02 13:52:33 -04:00
|
|
|
require_relative 'generic'
|
2002-01-10 03:00:51 -05:00
|
|
|
|
|
|
|
module URI
|
|
|
|
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
2005-08-24 01:08:00 -04:00
|
|
|
# FTP URI syntax is defined by RFC1738 section 3.2.
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
2010-04-09 04:15:33 -04:00
|
|
|
# This class will be redesigned because of difference of implementations;
|
|
|
|
# the structure of its path. draft-hoffman-ftp-uri-04 is a draft but it
|
|
|
|
# is a good summary about the de facto spec.
|
|
|
|
# http://tools.ietf.org/html/draft-hoffman-ftp-uri-04
|
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
class FTP < Generic
|
2018-04-21 16:04:05 -04:00
|
|
|
# A Default port of 21 for URI::FTP.
|
2002-01-10 03:00:51 -05:00
|
|
|
DEFAULT_PORT = 21
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# An Array of the available components for URI::FTP.
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
COMPONENT = [
|
2009-03-05 22:56:38 -05:00
|
|
|
:scheme,
|
2002-01-10 03:00:51 -05:00
|
|
|
:userinfo, :host, :port,
|
|
|
|
:path, :typecode
|
|
|
|
].freeze
|
2011-05-12 16:39:11 -04:00
|
|
|
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# Typecode is "a", "i", or "d".
|
2005-08-24 01:08:00 -04:00
|
|
|
#
|
|
|
|
# * "a" indicates a text file (the FTP command was ASCII)
|
|
|
|
# * "i" indicates a binary file (FTP command IMAGE)
|
|
|
|
# * "d" indicates the contents of a directory should be displayed
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
TYPECODE = ['a', 'i', 'd'].freeze
|
2011-05-12 16:39:11 -04:00
|
|
|
|
2018-04-21 16:04:05 -04:00
|
|
|
# Typecode prefix ";type=".
|
2002-01-10 03:00:51 -05:00
|
|
|
TYPECODE_PREFIX = ';type='.freeze
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
def self.new2(user, password, host, port, path,
|
2013-02-01 22:31:56 -05:00
|
|
|
typecode = nil, arg_check = true) # :nodoc:
|
2013-02-01 22:18:40 -05:00
|
|
|
# Do not use this method! Not tested. [Bug #7301]
|
|
|
|
# This methods remains just for compatibility,
|
|
|
|
# Keep it undocumented until the active maintainer is assigned.
|
2002-01-10 03:00:51 -05:00
|
|
|
typecode = nil if typecode.size == 0
|
|
|
|
if typecode && !TYPECODE.include?(typecode)
|
2004-03-24 06:53:31 -05:00
|
|
|
raise ArgumentError,
|
|
|
|
"bad typecode is specified: #{typecode}"
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# do escape
|
|
|
|
|
|
|
|
self.new('ftp',
|
2009-03-05 22:56:38 -05:00
|
|
|
[user, password],
|
|
|
|
host, port, nil,
|
|
|
|
typecode ? path + TYPECODE_PREFIX + typecode : path,
|
2004-03-24 06:53:31 -05:00
|
|
|
nil, nil, nil, arg_check)
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
|
|
|
# == Description
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# Creates a new URI::FTP object from components, with syntax checking.
|
2005-08-24 01:08:00 -04:00
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# The components accepted are +userinfo+, +host+, +port+, +path+, and
|
2005-08-24 01:08:00 -04:00
|
|
|
# +typecode+.
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# The components should be provided either as an Array, or as a Hash
|
|
|
|
# with keys formed by preceding the component names with a colon.
|
2005-08-24 01:08:00 -04:00
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# If an Array is used, the components must be passed in the
|
|
|
|
# order <code>[userinfo, host, port, path, typecode]</code>.
|
2005-08-24 01:08:00 -04:00
|
|
|
#
|
|
|
|
# If the path supplied is absolute, it will be escaped in order to
|
2018-04-21 16:04:05 -04:00
|
|
|
# make it absolute in the URI.
|
|
|
|
#
|
|
|
|
# Examples:
|
2005-08-24 01:08:00 -04:00
|
|
|
#
|
|
|
|
# require 'uri'
|
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# uri1 = URI::FTP.build(['user:password', 'ftp.example.com', nil,
|
2015-11-07 20:38:06 -05:00
|
|
|
# '/path/file.zip', 'i'])
|
2018-04-21 16:04:05 -04:00
|
|
|
# uri1.to_s # => "ftp://user:password@ftp.example.com/%2Fpath/file.zip;type=i"
|
2005-08-24 01:08:00 -04:00
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# uri2 = URI::FTP.build({:host => 'ftp.example.com',
|
2005-08-24 01:08:00 -04:00
|
|
|
# :path => 'ruby/src'})
|
2018-04-21 16:04:05 -04:00
|
|
|
# uri2.to_s # => "ftp://ftp.example.com/ruby/src"
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
def self.build(args)
|
2005-08-24 01:08:00 -04:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2002-01-10 03:00:51 -05:00
|
|
|
tmp = Util::make_components_hash(self, args)
|
|
|
|
|
|
|
|
if tmp[:typecode]
|
2004-03-24 06:53:31 -05:00
|
|
|
if tmp[:typecode].size == 1
|
2009-03-05 22:56:38 -05:00
|
|
|
tmp[:typecode] = TYPECODE_PREFIX + tmp[:typecode]
|
2004-03-24 06:53:31 -05:00
|
|
|
end
|
|
|
|
tmp[:path] << tmp[:typecode]
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
return super(tmp)
|
|
|
|
end
|
|
|
|
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
|
|
|
# == Description
|
|
|
|
#
|
2005-08-24 01:08:00 -04:00
|
|
|
# Creates a new URI::FTP object from generic URL components with no
|
|
|
|
# syntax checking.
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
2005-08-24 01:08:00 -04:00
|
|
|
# Unlike build(), this method does not escape the path component as
|
|
|
|
# required by RFC1738; instead it is treated as per RFC2396.
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# Arguments are +scheme+, +userinfo+, +host+, +port+, +registry+, +path+,
|
2018-04-21 16:04:05 -04:00
|
|
|
# +opaque+, +query+, and +fragment+, in that order.
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
2014-06-21 20:21:57 -04:00
|
|
|
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)
|
2002-01-10 03:00:51 -05:00
|
|
|
@typecode = nil
|
2014-06-21 20:21:57 -04:00
|
|
|
if tmp = @path.index(TYPECODE_PREFIX)
|
2004-03-24 06:53:31 -05:00
|
|
|
typecode = @path[tmp + TYPECODE_PREFIX.size..-1]
|
2010-04-15 09:41:19 -04:00
|
|
|
@path = @path[0..tmp - 1]
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2014-06-21 20:21:57 -04:00
|
|
|
if arg_check
|
2004-03-24 06:53:31 -05:00
|
|
|
self.typecode = typecode
|
|
|
|
else
|
|
|
|
self.set_typecode(typecode)
|
|
|
|
end
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
end
|
2011-05-12 16:39:11 -04:00
|
|
|
|
2018-04-21 16:04:05 -04:00
|
|
|
# typecode accessor.
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# See URI::FTP::COMPONENT.
|
2002-01-10 03:00:51 -05:00
|
|
|
attr_reader :typecode
|
|
|
|
|
2018-04-21 16:04:05 -04:00
|
|
|
# Validates typecode +v+,
|
|
|
|
# returns +true+ or +false+.
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
def check_typecode(v)
|
|
|
|
if TYPECODE.include?(v)
|
2004-03-24 06:53:31 -05:00
|
|
|
return true
|
2002-01-10 03:00:51 -05:00
|
|
|
else
|
2004-03-24 06:53:31 -05:00
|
|
|
raise InvalidComponentError,
|
|
|
|
"bad typecode(expected #{TYPECODE.join(', ')}): #{v}"
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
private :check_typecode
|
|
|
|
|
2018-04-21 16:04:05 -04:00
|
|
|
# Private setter for the typecode +v+.
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# See also URI::FTP.typecode=.
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
def set_typecode(v)
|
|
|
|
@typecode = v
|
|
|
|
end
|
|
|
|
protected :set_typecode
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
|
|
|
# == Args
|
|
|
|
#
|
|
|
|
# +v+::
|
|
|
|
# String
|
|
|
|
#
|
|
|
|
# == Description
|
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# Public setter for the typecode +v+
|
|
|
|
# (with validation).
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# See also URI::FTP.check_typecode.
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
|
|
|
# == Usage
|
|
|
|
#
|
|
|
|
# require 'uri'
|
|
|
|
#
|
|
|
|
# uri = URI.parse("ftp://john@ftp.example.com/my_file.img")
|
2018-04-21 16:04:05 -04:00
|
|
|
# #=> #<URI::FTP ftp://john@ftp.example.com/my_file.img>
|
2011-05-12 16:39:11 -04:00
|
|
|
# uri.typecode = "i"
|
|
|
|
# uri
|
2018-04-21 16:04:05 -04:00
|
|
|
# #=> #<URI::FTP ftp://john@ftp.example.com/my_file.img;type=i>
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
def typecode=(typecode)
|
|
|
|
check_typecode(typecode)
|
|
|
|
set_typecode(typecode)
|
2003-02-14 00:32:48 -05:00
|
|
|
typecode
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
2004-03-24 06:53:31 -05:00
|
|
|
def merge(oth) # :nodoc:
|
2002-01-10 03:00:51 -05:00
|
|
|
tmp = super(oth)
|
|
|
|
if self != tmp
|
2004-03-24 06:53:31 -05:00
|
|
|
tmp.set_typecode(oth.typecode)
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
return tmp
|
|
|
|
end
|
|
|
|
|
2005-08-24 01:08:00 -04:00
|
|
|
# Returns the path from an FTP URI.
|
|
|
|
#
|
|
|
|
# RFC 1738 specifically states that the path for an FTP URI does not
|
|
|
|
# include the / which separates the URI path from the URI host. Example:
|
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# <code>ftp://ftp.example.com/pub/ruby</code>
|
2005-08-24 01:08:00 -04:00
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# The above URI indicates that the client should connect to
|
2018-04-21 16:04:05 -04:00
|
|
|
# ftp.example.com then cd to pub/ruby from the initial login directory.
|
2005-08-24 01:08:00 -04:00
|
|
|
#
|
|
|
|
# If you want to cd to an absolute directory, you must include an
|
|
|
|
# escaped / (%2F) in the path. Example:
|
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# <code>ftp://ftp.example.com/%2Fpub/ruby</code>
|
2005-08-24 01:08:00 -04:00
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# This method will then return "/pub/ruby".
|
2005-08-24 01:08:00 -04:00
|
|
|
#
|
|
|
|
def path
|
|
|
|
return @path.sub(/^\//,'').sub(/^%2F/,'/')
|
|
|
|
end
|
|
|
|
|
2018-04-21 16:04:05 -04:00
|
|
|
# Private setter for the path of the URI::FTP.
|
2010-04-15 09:41:19 -04:00
|
|
|
def set_path(v)
|
|
|
|
super("/" + v.sub(/^\//, "%2F"))
|
|
|
|
end
|
|
|
|
protected :set_path
|
|
|
|
|
2018-04-21 16:04:05 -04:00
|
|
|
# Returns a String representation of the URI::FTP.
|
2003-10-03 23:49:01 -04:00
|
|
|
def to_s
|
2002-01-10 03:00:51 -05:00
|
|
|
save_path = nil
|
|
|
|
if @typecode
|
2004-03-24 06:53:31 -05:00
|
|
|
save_path = @path
|
|
|
|
@path = @path + TYPECODE_PREFIX + @typecode
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
str = super
|
|
|
|
if @typecode
|
2004-03-24 06:53:31 -05:00
|
|
|
@path = save_path
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
return str
|
|
|
|
end
|
2004-03-24 06:53:31 -05:00
|
|
|
end
|
2021-06-25 07:38:01 -04:00
|
|
|
|
|
|
|
register_scheme 'FTP', FTP
|
2004-03-24 06:53:31 -05:00
|
|
|
end
|