changeset 51830:208698117124

http: use urllib's cookie handler Split the logic for loading the cookies based on the configuration in a helper function and otherwise use the library implementation directly.
author Joerg Sonnenberger <joerg@bec.de>
date Sun, 30 Jun 2024 14:16:43 +0200
parents c1ed5ee2ad82
children 34eb3a711955
files mercurial/url.py mercurial/urllibcompat.py
diffstat 2 files changed, 20 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/url.py	Sun Jun 30 13:22:23 2024 +0200
+++ b/mercurial/url.py	Sun Jun 30 14:16:43 2024 +0200
@@ -455,41 +455,25 @@
             return None
 
 
-class cookiehandler(urlreq.basehandler):
-    def __init__(self, ui):
-        self.cookiejar = None
-
-        cookiefile = ui.config(b'auth', b'cookiefile')
-        if not cookiefile:
-            return
-
-        cookiefile = util.expandpath(cookiefile)
-        try:
-            cookiejar = util.cookielib.MozillaCookieJar(
-                pycompat.fsdecode(cookiefile)
+def load_cookiejar(ui):
+    cookiefile = ui.config(b'auth', b'cookiefile')
+    if not cookiefile:
+        return
+    cookiefile = util.expandpath(cookiefile)
+    try:
+        cookiejar = util.cookielib.MozillaCookieJar(
+            pycompat.fsdecode(cookiefile)
+        )
+        cookiejar.load()
+        return cookiejar
+    except util.cookielib.LoadError as e:
+        ui.warn(
+            _(
+                b'(error loading cookie file %s: %s; continuing without '
+                b'cookies)\n'
             )
-            cookiejar.load()
-            self.cookiejar = cookiejar
-        except util.cookielib.LoadError as e:
-            ui.warn(
-                _(
-                    b'(error loading cookie file %s: %s; continuing without '
-                    b'cookies)\n'
-                )
-                % (cookiefile, stringutil.forcebytestr(e))
-            )
-
-    def http_request(self, request):
-        if self.cookiejar:
-            self.cookiejar.add_cookie_header(request)
-
-        return request
-
-    def https_request(self, request):
-        if self.cookiejar:
-            self.cookiejar.add_cookie_header(request)
-
-        return request
+            % (cookiefile, stringutil.forcebytestr(e))
+        )
 
 
 class readlinehandler(urlreq.basehandler):
@@ -575,7 +559,7 @@
         (httpbasicauthhandler(passmgr), httpdigestauthhandler(passmgr))
     )
     handlers.extend([h(ui, passmgr) for h in handlerfuncs])
-    handlers.append(cookiehandler(ui))
+    handlers.append(urlreq.httpcookieprocessor(cookiejar=load_cookiejar(ui)))
     handlers.append(readlinehandler())
     opener = urlreq.buildopener(*handlers)
 
--- a/mercurial/urllibcompat.py	Sun Jun 30 13:22:23 2024 +0200
+++ b/mercurial/urllibcompat.py	Sun Jun 30 14:16:43 2024 +0200
@@ -68,6 +68,7 @@
         b"FileHandler",
         b"FTPHandler",
         b"ftpwrapper",
+        b"HTTPCookieProcessor",
         b"HTTPHandler",
         b"HTTPSHandler",
         b"install_opener",