From: Michael Tremer Date: Mon, 15 May 2023 15:07:42 +0000 (+0000) Subject: httpclient: Automatically configure proxy for all requests X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2af0750e138297a46b1144a84703bae9f5eb6155;p=pbs.git httpclient: Automatically configure proxy for all requests Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/httpclient.py b/src/buildservice/httpclient.py index 9a626186..d39b6b95 100644 --- a/src/buildservice/httpclient.py +++ b/src/buildservice/httpclient.py @@ -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