Skip to content

Commit

Permalink
pythongh-81403: Fix for CacheFTPHandler in urllib (python#13951)
Browse files Browse the repository at this point in the history
bpo-37222: Fix for CacheFTPHandler in urllib

A call to FTP.ntransfercmd must be followed by FTP.voidresp to clear
the "end transfer" message. Without this, the client and server get
out of sync, which will result in an error if the FTP instance is
reused to open a second URL. This scenario occurs for even the most
basic usage of CacheFTPHandler.

Reverts the patch merged as a resolution to bpo-16270 and adds a test
case for the CacheFTPHandler in test_urllib2net.py.

Co-authored-by: Senthil Kumaran <senthil@python.org>
  • Loading branch information
hemberger and orsenthil committed Apr 23, 2023
1 parent 0fd3891 commit e38bebb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Lib/test/test_urllib2net.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ def setUp(self):
# They do sometimes catch some major disasters, though.

def test_ftp(self):
# Testing the same URL twice exercises the caching in CacheFTPHandler
urls = [
'ftp://www.pythontest.net/README',
'ftp://www.pythontest.net/README',
('ftp://www.pythontest.net/non-existent-file',
None, urllib.error.URLError),
Expand Down
6 changes: 6 additions & 0 deletions Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2475,7 +2475,13 @@ def retrfile(self, file, type):
return (ftpobj, retrlen)

def endtransfer(self):
if not self.busy:
return
self.busy = 0
try:
self.ftp.voidresp()
except ftperrors():
pass

def close(self):
self.keepalive = False
Expand Down

0 comments on commit e38bebb

Please sign in to comment.