Skip to content

Commit e38bebb

Browse files
hembergerorsenthil
andauthored
gh-81403: Fix for CacheFTPHandler in urllib (#13951)
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 <[email protected]>
1 parent 0fd3891 commit e38bebb

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

Lib/test/test_urllib2net.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ def setUp(self):
134134
# They do sometimes catch some major disasters, though.
135135

136136
def test_ftp(self):
137+
# Testing the same URL twice exercises the caching in CacheFTPHandler
137138
urls = [
139+
'ftp://www.pythontest.net/README',
138140
'ftp://www.pythontest.net/README',
139141
('ftp://www.pythontest.net/non-existent-file',
140142
None, urllib.error.URLError),

Lib/urllib/request.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2475,7 +2475,13 @@ def retrfile(self, file, type):
24752475
return (ftpobj, retrlen)
24762476

24772477
def endtransfer(self):
2478+
if not self.busy:
2479+
return
24782480
self.busy = 0
2481+
try:
2482+
self.ftp.voidresp()
2483+
except ftperrors():
2484+
pass
24792485

24802486
def close(self):
24812487
self.keepalive = False

0 commit comments

Comments
 (0)