[前][次][番号順一覧][スレッド一覧]

ruby-changes:40158

From: shugo <ko1@a...>
Date: Fri, 23 Oct 2015 15:23:57 +0900 (JST)
Subject: [ruby-changes:40158] shugo:r52239 (trunk): * lib/net/ftp.rb (gettextfile, getbinaryfile): use the safe

shugo	2015-10-23 15:23:16 +0900 (Fri, 23 Oct 2015)

  New Revision: 52239

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52239

  Log:
    * lib/net/ftp.rb (gettextfile, getbinaryfile): use the safe
      navigation operator.

  Modified files:
    trunk/ChangeLog
    trunk/lib/net/ftp.rb
    trunk/test/net/ftp/test_ftp.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 52238)
+++ ChangeLog	(revision 52239)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Oct 23 15:20:02 2015  Shugo Maeda  <shugo@r...>
+
+	* lib/net/ftp.rb (gettextfile, getbinaryfile): use the safe
+	  navigation operator.
+
 Fri Oct 23 13:51:33 2015  yui-knk  <spiketeika@g...>
 
 	* test_call.rb (test_safe_call): Add test cases for safe
Index: lib/net/ftp.rb
===================================================================
--- lib/net/ftp.rb	(revision 52238)
+++ lib/net/ftp.rb	(revision 52239)
@@ -1,5 +1,4 @@ https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L1
-#
-# -*- frozen_string_literal: true -*-
+# frozen_string_literal: true
 #
 # = net/ftp.rb - FTP Client Library
 #
@@ -601,7 +600,8 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L600
     # chunks.
     #
     def getbinaryfile(remotefile, localfile = File.basename(remotefile),
-                      blocksize = DEFAULT_BLOCKSIZE) # :yield: data
+                      blocksize = DEFAULT_BLOCKSIZE, &block) # :yield: data
+      f = nil
       result = nil
       if localfile
         if @resume
@@ -615,15 +615,15 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L615
         result = String.new
       end
       begin
-        f.binmode if localfile
+        f.?binmode
         retrbinary("RETR #{remotefile}", blocksize, rest_offset) do |data|
-          f.write(data) if localfile
-          yield(data) if block_given?
-          result.concat(data) if result
+          f.?write(data)
+          block.?(data)
+          result.?concat(data)
         end
         return result
       ensure
-        f.close if localfile
+        f.?close
       end
     end
 
@@ -634,7 +634,9 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L634
     # If a block is supplied, it is passed the retrieved data one
     # line at a time.
     #
-    def gettextfile(remotefile, localfile = File.basename(remotefile)) # :yield: line
+    def gettextfile(remotefile, localfile = File.basename(remotefile),
+                    &block) # :yield: line
+      f = nil
       result = nil
       if localfile
         f = open(localfile, "w")
@@ -644,13 +646,13 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L646
       begin
         retrlines("RETR #{remotefile}") do |line, newline|
           l = newline ? line + "\n" : line
-          f.print(l) if localfile
-          yield(line, newline) if block_given?
-          result.concat(l) if result
+          f.?print(l)
+          block.?(line, newline)
+          result.?concat(l)
         end
         return result
       ensure
-        f.close if localfile
+        f.?close
       end
     end
 
Index: test/net/ftp/test_ftp.rb
===================================================================
--- test/net/ftp/test_ftp.rb	(revision 52238)
+++ test/net/ftp/test_ftp.rb	(revision 52239)
@@ -1,10 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/test/net/ftp/test_ftp.rb#L1
-#
-# -*- frozen_string_literal: true -*-
+# frozen_string_literal: true
 
 require "net/ftp"
 require "test/unit"
 require "ostruct"
 require "stringio"
+require "tempfile"
 
 class FTPTest < Test::Unit::TestCase
   SERVER_ADDR = "127.0.0.1"
@@ -758,6 +758,64 @@ class FTPTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/net/ftp/test_ftp.rb#L758
     end
   end
 
+  def test_getbinaryfile_with_filename_and_block
+    commands = []
+    binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
+    server = create_ftp_server { |sock|
+      sock.print("220 (test_ftp).\r\n")
+      commands.push(sock.gets)
+      sock.print("331 Please specify the password.\r\n")
+      commands.push(sock.gets)
+      sock.print("230 Login successful.\r\n")
+      commands.push(sock.gets)
+      sock.print("200 Switching to Binary mode.\r\n")
+      line = sock.gets
+      commands.push(line)
+      port_args = line.slice(/\APORT (.*)/, 1).split(/,/)
+      host = port_args[0, 4].join(".")
+      port = port_args[4, 2].map(&:to_i).inject {|x, y| (x << 8) + y}
+      sock.print("200 PORT command successful.\r\n")
+      commands.push(sock.gets)
+      sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
+      conn = TCPSocket.new(host, port)
+      binary_data.scan(/.{1,1024}/nm) do |s|
+        conn.print(s)
+      end
+      conn.shutdown(Socket::SHUT_WR)
+      conn.read
+      conn.close
+      sock.print("226 Transfer complete.\r\n")
+    }
+    begin
+      begin
+        ftp = Net::FTP.new
+        ftp.read_timeout = 0.2
+        ftp.connect(SERVER_ADDR, server.port)
+        ftp.login
+        assert_match(/\AUSER /, commands.shift)
+        assert_match(/\APASS /, commands.shift)
+        assert_equal("TYPE I\r\n", commands.shift)
+        Tempfile.create("foo", external_encoding: "ASCII-8BIT") do |f|
+          buf = String.new
+          res = ftp.getbinaryfile("foo", f.path) { |s|
+            buf << s
+          }
+          assert_equal(nil, res)
+          assert_equal(binary_data, buf)
+          assert_equal(Encoding::ASCII_8BIT, buf.encoding)
+          assert_equal(binary_data, f.read)
+        end
+        assert_match(/\APORT /, commands.shift)
+        assert_equal("RETR foo\r\n", commands.shift)
+        assert_equal(nil, commands.shift)
+      ensure
+        ftp.close if ftp
+      end
+    ensure
+      server.close
+    end
+  end
+
   def test_storbinary
     commands = []
     binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
@@ -956,6 +1014,73 @@ EOF https://github.com/ruby/ruby/blob/trunk/test/net/ftp/test_ftp.rb#L1014
         assert_equal("TYPE A\r\n", commands.shift)
         assert_match(/\APORT /, commands.shift)
         assert_equal("RETR foo\r\n", commands.shift)
+        assert_equal("TYPE I\r\n", commands.shift)
+        assert_equal(nil, commands.shift)
+      ensure
+        ftp.close if ftp
+      end
+    ensure
+      server.close
+    end
+  end
+
+  def test_gettextfile_with_filename_and_block
+    commands = []
+    text_data = <<EOF.gsub(/\n/, "\r\n")
+foo
+bar
+baz
+EOF
+    server = create_ftp_server { |sock|
+      sock.print("220 (test_ftp).\r\n")
+      commands.push(sock.gets)
+      sock.print("331 Please specify the password.\r\n")
+      commands.push(sock.gets)
+      sock.print("230 Login successful.\r\n")
+      commands.push(sock.gets)
+      sock.print("200 Switching to Binary mode.\r\n")
+      commands.push(sock.gets)
+      sock.print("200 Switching to ASCII mode.\r\n")
+      line = sock.gets
+      commands.push(line)
+      port_args = line.slice(/\APORT (.*)/, 1).split(/,/)
+      host = port_args[0, 4].join(".")
+      port = port_args[4, 2].map(&:to_i).inject {|x, y| (x << 8) + y}
+      sock.print("200 PORT command successful.\r\n")
+      commands.push(sock.gets)
+      sock.print("150 Opening TEXT mode data connection for foo (#{text_data.size} bytes)\r\n")
+      conn = TCPSocket.new(host, port)
+      text_data.each_line do |line|
+        conn.print(line)
+      end
+      conn.shutdown(Socket::SHUT_WR)
+      conn.read
+      conn.close
+      sock.print("226 Transfer complete.\r\n")
+      commands.push(sock.gets)
+      sock.print("200 Switching to Binary mode.\r\n")
+    }
+    begin
+      begin
+        ftp = Net::FTP.new
+        ftp.connect(SERVER_ADDR, server.port)
+        ftp.login
+        assert_match(/\AUSER /, commands.shift)
+        assert_match(/\APASS /, commands.shift)
+        assert_equal("TYPE I\r\n", commands.shift)
+        Tempfile.create("foo", external_encoding: "ascii-8bit") do |f|
+          buf = String.new
+          res = ftp.gettextfile("foo", f.path) { |s|
+            buf << s << "\n"
+          }
+          assert_equal(nil, res)
+          assert_equal(text_data.gsub(/\r\n/, "\n"), buf)
+          assert_equal(Encoding::ASCII_8BIT, buf.encoding)
+          assert_equal(buf, f.read)
+        end
+        assert_equal("TYPE A\r\n", commands.shift)
+        assert_match(/\APORT /, commands.shift)
+        assert_equal("RETR foo\r\n", commands.shift)
         assert_equal("TYPE I\r\n", commands.shift)
         assert_equal(nil, commands.shift)
       ensure

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]