* 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.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51791 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
shugo 2015-09-08 06:08:20 +00:00
Родитель 49cbfc8359
Коммит d5d6b149df
3 изменённых файлов: 153 добавлений и 8 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -755,15 +755,14 @@ module Net
args.each do |arg|
cmd = "#{cmd} #{arg}"
end
if block
retrlines(cmd, &block)
else
lines = []
retrlines(cmd) do |line|
lines << line
end
return lines
lines = []
retrlines(cmd) do |line|
lines << line
end
if block
lines.each(&block)
end
return lines
end
alias ls list
alias dir list

Просмотреть файл

@ -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|