]> git.ipfire.org Git - pbs.git/commitdiff
httpclient: Automatically configure proxy for all requests
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 15 May 2023 15:07:42 +0000 (15:07 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 15 May 2023 15:07:42 +0000 (15:07 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/httpclient.py

index 9a626186750d94ff239f12a2553572d20816d9ca..d39b6b95e955c311c0236f19aaec6a15e7266067 100644 (file)
@@ -47,6 +47,28 @@ class HTTPClient(tornado.curl_httpclient.CurlAsyncHTTPClient):
                # Store a reference to the backend
                self.backend = backend
 
+       def _configure_proxy(self, request):
+               """
+                       Configures the proxy
+               """
+               proxy = self.backend.settings.get("proxy")
+               if not proxy:
+                       return
+
+               # Split the configuration value
+               host, delim, port = proxy.partition(":")
+
+               # Convert port to integer
+               # Set port
+               try:
+                       port = int(port)
+               except (TypeError, ValueError):
+                       log.error("Could not decode proxy setting: %s" % proxy)
+                       return
+
+               # Set values
+               request.proxy_host, request.proxy_port = host, port
+
        async def fetch(self, request, **kwargs):
                """
                        Sends a request
@@ -54,6 +76,9 @@ class HTTPClient(tornado.curl_httpclient.CurlAsyncHTTPClient):
                if not isinstance(request, HTTPRequest):
                        request = HTTPRequest(url=request, **kwargs)
 
+               # Configure the proxy
+               self._configure_proxy(request)
+
                # Set User-Agent
                request.user_agent = "PakfireBuildService/%s" % self.backend.version
 
@@ -73,7 +98,7 @@ class HTTPClient(tornado.curl_httpclient.CurlAsyncHTTPClient):
                        response = await super().fetch(request)
 
                # Log any errors
-               except torando.httpclient.HTTPError as e:
+               except tornado.httpclient.HTTPError as e:
                        log.error("Received status %s:" % e.code)
 
                        # Log the response body