aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--lib/net/ftp.rb13
-rw-r--r--test/net/ftp/test_ftp.rb140
3 files changed, 152 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index a62145d4fc..a8665e2d88 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Tue Sep 8 15:01:19 2015 Shugo Maeda <shugo@ruby-lang.org>
+
+ * lib/net/ftp.rb (list): fetch all the lines before yielding a block
+ to allow other commands in the block. [Feature #11454]
+ Patched by Srikanth Shreenivas.
+
Tue Sep 8 12:05:00 2015 NAKAMURA Usaku <usa@ruby-lang.org>
* win32/win32.c (rb_w32_read_reparse_point): return correct required
diff --git a/lib/net/ftp.rb b/lib/net/ftp.rb
index 3eec86ae99..724f4b85bd 100644
--- a/lib/net/ftp.rb
+++ b/lib/net/ftp.rb
@@ -755,15 +755,14 @@ module Net
args.each do |arg|
cmd = "#{cmd} #{arg}"
end
+ lines = []
+ retrlines(cmd) do |line|
+ lines << line
+ end
if block
- retrlines(cmd, &block)
- else
- lines = []
- retrlines(cmd) do |line|
- lines << line
- end
- return lines
+ lines.each(&block)
end
+ return lines
end
alias ls list
alias dir list
diff --git a/test/net/ftp/test_ftp.rb b/test/net/ftp/test_ftp.rb
index 618a9da287..ad8ef5aa37 100644
--- a/test/net/ftp/test_ftp.rb
+++ b/test/net/ftp/test_ftp.rb
@@ -741,6 +741,146 @@ class FTPTest < Test::Unit::TestCase
end
end
+ def test_retrlines
+ 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)
+ buf = ""
+ ftp.retrlines("RETR foo") do |line|
+ buf << line + "\r\n"
+ end
+ assert_equal(text_data.bytesize, buf.bytesize)
+ assert_equal(text_data, buf)
+ 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_getbinaryfile_in_list
+ commands = []
+ binary_data = (0..0xff).map {|i| i.chr}.join
+ list_lines = [
+ "-rw-r--r-- 1 0 0 0 Mar 30 11:22 foo.txt",
+ "-rw-r--r-- 1 0 0 0 Mar 30 11:22 bar.txt",
+ "-rw-r--r-- 1 0 0 0 Mar 30 11:22 baz.bin"
+ ]
+ 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 Here comes the directory listing.\r\n")
+ conn = TCPSocket.new(host, port)
+ list_lines.each_with_index do |l, i|
+ conn.print(l, "\r\n")
+ end
+ conn.close
+ sock.print("226 Directory send OK.\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)
+ conn.print(binary_data)
+ conn.close
+ sock.print("226 Transfer complete.\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)
+ ftp.list do |line|
+ file = line.slice(/(\S*\.bin)\z/)
+ if file
+ data = ftp.getbinaryfile(file, nil)
+ assert_equal(binary_data, data)
+ end
+ end
+ assert_equal("TYPE A\r\n", commands.shift)
+ assert_match(/\APORT /, commands.shift)
+ assert_equal("LIST\r\n", commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_match(/\APORT /, commands.shift)
+ assert_equal("RETR baz.bin\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
def test_abort
commands = []
server = create_ftp_server { |sock|