Browse Source

Add separate HTTPProxyUnauthorized exception.

Jelmer Vernooij 3 years ago
parent
commit
40ba88eb9b
2 changed files with 14 additions and 2 deletions
  1. 3 0
      NEWS
  2. 11 2
      dulwich/client.py

+ 3 - 0
NEWS

@@ -5,6 +5,9 @@
 
  * Skip lines with spaces only in .gitignore. (Andrey Torsunov, #878)
 
+ * Add a separate HTTPProxyUnauthorized exception for 407 errors.
+   (Jelmer Vernooij, #822)
+
 0.20.23	2021-05-24
 
  * Fix installation of GPG during package publishing.

+ 11 - 2
dulwich/client.py

@@ -133,6 +133,15 @@ class HTTPUnauthorized(Exception):
         self.url = url
 
 
+class HTTPProxyUnauthorized(Exception):
+    """Raised when proxy authentication fails."""
+
+    def __init__(self, proxy_authenticate, url):
+        Exception.__init__(self, "No valid proxy credentials provided")
+        self.proxy_authenticate = proxy_authenticate
+        self.url = url
+
+
 def _fileno_can_read(fileno):
     """Check if a file descriptor is readable."""
     return len(select.select([fileno], [], [], 0)[0]) > 0
@@ -1759,8 +1768,6 @@ def default_urllib3_manager(   # noqa: C901
     if proxy_server is not None:
         if proxy_manager_cls is None:
             proxy_manager_cls = urllib3.ProxyManager
-        # `urllib3` requires a `str` object in both Python 2 and 3, while
-        # `ConfigDict` coerces entries to `bytes` on Python 3. Compensate.
         if not isinstance(proxy_server, str):
             proxy_server = proxy_server.decode()
         manager = proxy_manager_cls(proxy_server, headers=headers, **kwargs)
@@ -1873,6 +1880,8 @@ class HttpGitClient(GitClient):
             raise NotGitRepository()
         if resp.status == 401:
             raise HTTPUnauthorized(resp.getheader("WWW-Authenticate"), url)
+        if resp.status == 407:
+            raise HTTPProxyUnauthorized(resp.getheader("Proxy-Authenticate"), url)
         if resp.status != 200:
             raise GitProtocolError(
                 "unexpected http resp %d for %s" % (resp.status, url)