]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-81403: Fix for CacheFTPHandler in urllib (#13951)
authorDan Hemberger <846186+hemberger@users.noreply.github.com>
Sun, 23 Apr 2023 04:41:23 +0000 (21:41 -0700)
committerGitHub <noreply@github.com>
Sun, 23 Apr 2023 04:41:23 +0000 (21:41 -0700)
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>
Lib/test/test_urllib2net.py
Lib/urllib/request.py

index 5da41c37bbfb8eec6844d1f937957a915083c81e..d8d882b2d33589b30ee05f81b4f3afd4ced3cfd2 100644 (file)
@@ -134,7 +134,9 @@ class OtherNetworkTests(unittest.TestCase):
     # 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),
index 19e2e5bd335627211e5a8185ed48e066b052c549..5314b3f26021eb6e3394ae2187da890c63986ae4 100644 (file)
@@ -2475,7 +2475,13 @@ class ftpwrapper:
         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