ruby-changes:39743
From: shugo <ko1@a...>
Date: Fri, 11 Sep 2015 11:23:51 +0900 (JST)
Subject: [ruby-changes:39743] shugo:r51824 (trunk): * lib/net/ftp.rb (size, mdtm, system): parse responses according to
shugo 2015-09-11 11:23:31 +0900 (Fri, 11 Sep 2015) New Revision: 51824 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51824 Log: * lib/net/ftp.rb (size, mdtm, system): parse responses according to RFC 959 and 3659, where reply codes must be followed by SP. * lib/net/ftp.rb (system): remove LF from the return value. Modified files: trunk/ChangeLog trunk/lib/net/ftp.rb trunk/test/net/ftp/test_ftp.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 51823) +++ ChangeLog (revision 51824) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Sep 11 11:15:12 2015 Shugo Maeda <shugo@r...> + + * lib/net/ftp.rb (size, mdtm, system): parse responses according to + RFC 959 and 3659, where reply codes must be followed by SP. + + * lib/net/ftp.rb (system): remove LF from the return value. + Thu Sep 10 22:48:49 2015 Nobuyoshi Nakada <nobu@r...> * parse.y (literal_concat_gen, evstr2dstr_gen): keep literal Index: lib/net/ftp.rb =================================================================== --- lib/net/ftp.rb (revision 51823) +++ lib/net/ftp.rb (revision 51824) @@ -337,7 +337,7 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L337 # equal 2. def voidresp # :nodoc: resp = getresp - if resp[0] != ?2 + if !resp.start_with?("2") raise FTPReplyError, resp end end @@ -402,14 +402,14 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L402 conn = open_socket(host, port) if @resume and rest_offset resp = sendcmd("REST " + rest_offset.to_s) - if resp[0] != ?3 + if !resp.start_with?("3") raise FTPReplyError, resp end end resp = sendcmd(cmd) # skip 2XX for some ftp servers - resp = getresp if resp[0] == ?2 - if resp[0] != ?1 + resp = getresp if resp.start_with?("2") + if !resp.start_with?("1") raise FTPReplyError, resp end else @@ -418,14 +418,14 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L418 sendport(sock.addr[3], sock.addr[1]) if @resume and rest_offset resp = sendcmd("REST " + rest_offset.to_s) - if resp[0] != ?3 + if !resp.start_with?("3") raise FTPReplyError, resp end end resp = sendcmd(cmd) # skip 2XX for some ftp servers - resp = getresp if resp[0] == ?2 - if resp[0] != ?1 + resp = getresp if resp.start_with?("2") + if !resp.start_with?("1") raise FTPReplyError, resp end conn = BufferedSocket.new(sock.accept) @@ -456,16 +456,16 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L456 resp = "" synchronize do resp = sendcmd('USER ' + user) - if resp[0] == ?3 + if resp.start_with?("3") raise FTPReplyError, resp if passwd.nil? resp = sendcmd('PASS ' + passwd) end - if resp[0] == ?3 + if resp.start_with?("3") raise FTPReplyError, resp if acct.nil? resp = sendcmd('ACCT ' + acct) end end - if resp[0] != ?2 + if !resp.start_with?("2") raise FTPReplyError, resp end @welcome = resp @@ -772,7 +772,7 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L772 # def rename(fromname, toname) resp = sendcmd("RNFR #{fromname}") - if resp[0] != ?3 + if !resp.start_with?("3") raise FTPReplyError, resp end voidcmd("RNTO #{toname}") @@ -783,9 +783,9 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L783 # def delete(filename) resp = sendcmd("DELE #{filename}") - if resp[0, 3] == "250" + if resp.start_with?("250") return - elsif resp[0] == ?5 + elsif resp.start_with?("5") raise FTPPermError, resp else raise FTPReplyError, resp @@ -810,16 +810,21 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L810 voidcmd(cmd) end + def get_body(resp) # :nodoc: + resp.slice(/\A[0-9a-zA-Z]{3} (.*)$/, 1) + end + private :get_body + # # Returns the size of the given (remote) filename. # def size(filename) with_binary(true) do resp = sendcmd("SIZE #{filename}") - if resp[0, 3] != "213" + if !resp.start_with?("213") raise FTPReplyError, resp end - return resp[3..-1].strip.to_i + return get_body(resp).to_i end end @@ -864,10 +869,10 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L869 # def system resp = sendcmd("SYST") - if resp[0, 3] != "215" + if !resp.start_with?("215") raise FTPReplyError, resp end - return resp[4 .. -1] + return get_body(resp) end # @@ -902,8 +907,8 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L907 # def mdtm(filename) resp = sendcmd("MDTM #{filename}") - if resp[0, 3] == "213" - return resp[3 .. -1].strip + if resp.start_with?("213") + return get_body(resp) end end @@ -971,7 +976,7 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L976 # # Returns host and port. def parse227(resp) # :nodoc: - if resp[0, 3] != "227" + if !resp.start_with?("227") raise FTPReplyError, resp end if m = /\((?<host>\d+(,\d+){3}),(?<port>\d+,\d+)\)/.match(resp) @@ -987,7 +992,7 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L992 # # Returns host and port. def parse228(resp) # :nodoc: - if resp[0, 3] != "228" + if !resp.start_with?("228") raise FTPReplyError, resp end if m = /\(4,4,(?<host>\d+(,\d+){3}),2,(?<port>\d+,\d+)\)/.match(resp) @@ -1024,7 +1029,7 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L1029 # # Returns host and port. def parse229(resp) # :nodoc: - if resp[0, 3] != "229" + if !resp.start_with?("229") raise FTPReplyError, resp end if m = /\((?<d>[!-~])\k<d>\k<d>(?<port>\d+)\k<d>\)/.match(resp) @@ -1040,7 +1045,7 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L1045 # # Returns host and port. def parse257(resp) # :nodoc: - if resp[0, 3] != "257" + if !resp.start_with?("257") raise FTPReplyError, resp end if resp[3, 2] != ' "' Index: test/net/ftp/test_ftp.rb =================================================================== --- test/net/ftp/test_ftp.rb (revision 51823) +++ test/net/ftp/test_ftp.rb (revision 51824) @@ -1053,6 +1053,72 @@ EOF https://github.com/ruby/ruby/blob/trunk/test/net/ftp/test_ftp.rb#L1053 end end + def test_size + commands = [] + server = create_ftp_server { |sock| + sock.print("220 (test_ftp).\r\n") + commands.push(sock.gets) + sock.print("213 12345\r\n") + } + begin + begin + ftp = Net::FTP.new + ftp.connect(SERVER_ADDR, server.port) + assert_equal(12345, ftp.size("foo.txt")) + assert_match("SIZE foo.txt\r\n", commands.shift) + assert_equal(nil, commands.shift) + ensure + ftp.close if ftp + end + ensure + server.close + end + end + + def test_mdtm + commands = [] + server = create_ftp_server { |sock| + sock.print("220 (test_ftp).\r\n") + commands.push(sock.gets) + sock.print("213 20150910161739\r\n") + } + begin + begin + ftp = Net::FTP.new + ftp.connect(SERVER_ADDR, server.port) + assert_equal("20150910161739", ftp.mdtm("foo.txt")) + assert_match("MDTM foo.txt\r\n", commands.shift) + assert_equal(nil, commands.shift) + ensure + ftp.close if ftp + end + ensure + server.close + end + end + + def test_system + commands = [] + server = create_ftp_server { |sock| + sock.print("220 (test_ftp).\r\n") + commands.push(sock.gets) + sock.print("215 UNIX Type: L8\r\n") + } + begin + begin + ftp = Net::FTP.new + ftp.connect(SERVER_ADDR, server.port) + assert_equal("UNIX Type: L8", ftp.system) + assert_match("SYST\r\n", commands.shift) + assert_equal(nil, commands.shift) + ensure + ftp.close if ftp + end + ensure + server.close + end + end + private -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/