diff --git a/ChangeLog b/ChangeLog index 57892f8fc9..58d3c6569d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Thu Sep 10 15:16:02 2015 Shugo Maeda + + * lib/net/ftp.rb (getmultiline): refactor. + Thu Sep 10 12:17:28 2015 Nobuyoshi Nakada * compile.c (iseq_build_from_ary_body): register cdhash to the diff --git a/lib/net/ftp.rb b/lib/net/ftp.rb index 724f4b85bd..3c3353e9fe 100644 --- a/lib/net/ftp.rb +++ b/lib/net/ftp.rb @@ -298,16 +298,16 @@ module Net # Receive a section of lines until the response code's match. def getmultiline # :nodoc: - line = getline - buff = line - if line[3] == ?- - code = line[0, 3] + lines = [] + lines << getline + code = lines.last.slice(/\A([0-9a-zA-Z]{3})-/, 1) + if code + delimiter = code + " " begin - line = getline - buff << "\n" << line - end until line[0, 3] == code and line[3] != ?- + lines << getline + end until lines.last.start_with?(delimiter) end - return buff << "\n" + return lines.join("\n") + "\n" end private :getmultiline diff --git a/test/net/ftp/test_ftp.rb b/test/net/ftp/test_ftp.rb index ad8ef5aa37..e42168e6dc 100644 --- a/test/net/ftp/test_ftp.rb +++ b/test/net/ftp/test_ftp.rb @@ -1025,6 +1025,34 @@ EOF end end + def test_getmultiline + server = create_ftp_server { |sock| + sock.print("220 (test_ftp).\r\n") + sock.print("123- foo\r\n") + sock.print("bar\r\n") + sock.print(" 123 baz\r\n") + sock.print("123 quux\r\n") + sock.print("123 foo\r\n") + sock.print("foo\r\n") + sock.print("\r\n") + } + begin + begin + ftp = Net::FTP.new + ftp.connect(SERVER_ADDR, server.port) + assert_equal("123- foo\nbar\n 123 baz\n123 quux\n", + ftp.send(:getmultiline)) + assert_equal("123 foo\n", ftp.send(:getmultiline)) + assert_equal("foo\n", ftp.send(:getmultiline)) + assert_equal("\n", ftp.send(:getmultiline)) + ensure + ftp.close if ftp + end + ensure + server.close + end + end + private