Class: URI::FTP

Inherits:
Object
  • Object
show all
Includes:
OpenURI::OpenRead
Defined in:
lib/open-uri.rb

Instance Method Summary collapse

Methods included from OpenURI::OpenRead

#open, #read

Instance Method Details

#buffer_open(buf, proxy, options) ⇒ Object

:nodoc:



777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
# File 'lib/open-uri.rb', line 777

def buffer_open(buf, proxy, options) # :nodoc:
  if proxy
    OpenURI.open_http(buf, self, proxy, options)
    return
  end

  begin
    require 'net/ftp'
  rescue LoadError
    abort "net/ftp is not found. You may need to `gem install net-ftp` to install net/ftp."
  end

  path = self.path
  path = path.sub(%r{\A/}, '%2F') # re-encode the beginning slash because uri library decodes it.
  directories = path.split(%r{/}, -1)
  directories.each {|d|
    d.gsub!(/%([0-9A-Fa-f][0-9A-Fa-f])/) { [$1].pack("H2") }
  }
  unless filename = directories.pop
    raise ArgumentError, "no filename: #{self.inspect}"
  end
  directories.each {|d|
    if /[\r\n]/ =~ d
      raise ArgumentError, "invalid directory: #{d.inspect}"
    end
  }
  if /[\r\n]/ =~ filename
    raise ArgumentError, "invalid filename: #{filename.inspect}"
  end
  typecode = self.typecode
  if typecode && /\A[aid]\z/ !~ typecode
    raise ArgumentError, "invalid typecode: #{typecode.inspect}"
  end

  # The access sequence is defined by RFC 1738
  ftp = Net::FTP.new
  ftp.connect(self.hostname, self.port)
  ftp.passive = !options[:ftp_active_mode]
  # todo: extract user/passwd from .netrc.
  user = 'anonymous'
  passwd = nil
  user, passwd = self.userinfo.split(/:/) if self.userinfo
  ftp.(user, passwd)
  directories.each {|cwd|
    ftp.voidcmd("CWD #{cwd}")
  }
  if typecode
    # xxx: typecode D is not handled.
    ftp.voidcmd("TYPE #{typecode.upcase}")
  end
  if options[:content_length_proc]
    options[:content_length_proc].call(ftp.size(filename))
  end
  ftp.retrbinary("RETR #{filename}", 4096) { |str|
    buf << str
    options[:progress_proc].call(buf.size) if options[:progress_proc]
  }
  ftp.close
  buf.io.rewind
end