summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authornagachika <nagachika@ruby-lang.org>2021-06-10 16:06:01 +0900
committernagachika <nagachika@ruby-lang.org>2021-06-10 16:06:01 +0900
commit6363492817d9dc73da836da852d7cb6a4ace3a8f (patch)
treeb512ea83ce9308bd1fcfe28aaba230e2dc6915ee /lib
parentced669aed0de19090d1ba85eb9881becb693a735 (diff)
merge revision(s) 990baec41174a0b4cf7e285cf3185b4ab444437e:
[ruby/net-ftp] Close the passive connection data socket if there is an error setting up the transfer Previously, the connection leaked in this case. This uses begin/ensure and checking for an error in the ensure block. An alternative approach would be to not even perform the connection until after the RETR (or other) command has been sent. However, I'm not sure all FTP servers support that. The current behavior is: * Send (PASV/EPSV) * Connect to the host/port returned in 227/229 reply * Send (RETR/other command) Changing it to connect after the RETR could break things. FTP servers might expect that the client has already connected before sending the RETR. The alternative approach is more likely to introduce backwards compatibility issues, compared to the begin/ensure approach taken here. Fixes Ruby Bug 17027 https://github.com/ruby/net-ftp/commit/6e8535f076 --- lib/net/ftp.rb | 24 ++++++++++++++---------- test/net/ftp/test_ftp.rb | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 12 deletions(-)
Diffstat (limited to 'lib')
-rw-r--r--lib/net/ftp.rb24
1 files changed, 14 insertions, 10 deletions
diff --git a/lib/net/ftp.rb b/lib/net/ftp.rb
index 9e57cb363f..3536e01ba3 100644
--- a/lib/net/ftp.rb
+++ b/lib/net/ftp.rb
@@ -547,18 +547,22 @@ module Net
def transfercmd(cmd, rest_offset = nil) # :nodoc:
if @passive
host, port = makepasv
- conn = open_socket(host, port)
- if @resume and rest_offset
- resp = sendcmd("REST " + rest_offset.to_s)
- if !resp.start_with?("3")
+ begin
+ conn = open_socket(host, port)
+ if @resume and rest_offset
+ resp = sendcmd("REST " + rest_offset.to_s)
+ if !resp.start_with?("3")
+ raise FTPReplyError, resp
+ end
+ end
+ resp = sendcmd(cmd)
+ # skip 2XX for some ftp servers
+ resp = getresp if resp.start_with?("2")
+ if !resp.start_with?("1")
raise FTPReplyError, resp
end
- end
- resp = sendcmd(cmd)
- # skip 2XX for some ftp servers
- resp = getresp if resp.start_with?("2")
- if !resp.start_with?("1")
- raise FTPReplyError, resp
+ ensure
+ conn.close if conn && $!
end
else
sock = makeport