* 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
This commit is contained in:
shugo 2015-09-11 02:23:31 +00:00
Родитель a7727448a1
Коммит 782e2148af
3 изменённых файлов: 101 добавлений и 23 удалений

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

@ -1,3 +1,10 @@
Fri Sep 11 11:15:12 2015 Shugo Maeda <shugo@ruby-lang.org>
* 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@ruby-lang.org>
* parse.y (literal_concat_gen, evstr2dstr_gen): keep literal

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

@ -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] != ' "'

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

@ -1053,6 +1053,72 @@ EOF
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