From 782e2148aff47d871e6079867fede4ea3f2526a1 Mon Sep 17 00:00:00 2001 From: shugo Date: Fri, 11 Sep 2015 02:23:31 +0000 Subject: * 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51824 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/net/ftp.rb | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) (limited to 'lib') diff --git a/lib/net/ftp.rb b/lib/net/ftp.rb index 3c3353e9fe..6aa102f6f0 100644 --- a/lib/net/ftp.rb +++ b/lib/net/ftp.rb @@ -337,7 +337,7 @@ module Net # 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 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 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 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 # 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 # 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 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 # 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 # 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 # # Returns host and port. def parse227(resp) # :nodoc: - if resp[0, 3] != "227" + if !resp.start_with?("227") raise FTPReplyError, resp end if m = /\((?\d+(,\d+){3}),(?\d+,\d+)\)/.match(resp) @@ -987,7 +992,7 @@ module Net # # 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,(?\d+(,\d+){3}),2,(?\d+,\d+)\)/.match(resp) @@ -1024,7 +1029,7 @@ module Net # # Returns host and port. def parse229(resp) # :nodoc: - if resp[0, 3] != "229" + if !resp.start_with?("229") raise FTPReplyError, resp end if m = /\((?[!-~])\k\k(?\d+)\k\)/.match(resp) @@ -1040,7 +1045,7 @@ module Net # # 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] != ' "' -- cgit v1.2.3