diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/net/ftp.rb | 51 |
1 files changed, 28 insertions, 23 deletions
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 = /\((?<host>\d+(,\d+){3}),(?<port>\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,(?<host>\d+(,\d+){3}),2,(?<port>\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 = /\((?<d>[!-~])\k<d>\k<d>(?<port>\d+)\k<d>\)/.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] != ' "' |