From ed5599adaa4c57e59f6d099f0a54edf72256bd3a Mon Sep 17 00:00:00 2001 From: Arnaud Veron Date: Thu, 11 May 2023 10:26:26 +0200 Subject: [PATCH] Add http Host header --- opensvc/core/node/node.py | 4 ++++ opensvc/utilities/uri/__init__.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/opensvc/core/node/node.py b/opensvc/core/node/node.py index e00907767..219d030cc 100644 --- a/opensvc/core/node/node.py +++ b/opensvc/core/node/node.py @@ -2708,6 +2708,8 @@ def collector_api(self, path=None): data["url"] = self.collector_env.dbopensvc.replace("/feed/default/call/xmlrpc", "/init/rest/api") if not data["url"].startswith("http"): data["url"] = "https://%s" % data["url"] + from utilities.uri import Uri + data["hosthdr"] = Uri(data["url"]).host_header() return data def collector_auth_node(self): @@ -2750,6 +2752,8 @@ def collector_request(self, rpath, path=None): base64string = base64encode(auth_string) base64string = base64string.replace('\n', '') request.add_header("Authorization", "Basic %s" % base64string) + if api["hosthdr"]: + request.add_header("Host", api["hosthdr"]) return request def collector_rest_get(self, rpath, data=None, path=None): diff --git a/opensvc/utilities/uri/__init__.py b/opensvc/utilities/uri/__init__.py index 471549aa9..0264190a9 100644 --- a/opensvc/utilities/uri/__init__.py +++ b/opensvc/utilities/uri/__init__.py @@ -5,6 +5,7 @@ try: from foreign.six.moves.urllib.request import Request, urlopen + from foreign.six.moves.urllib.parse import urlparse except ImportError: # pylint false positive pass @@ -59,6 +60,19 @@ def _set_ssl_context(self, kwargs=None): kwargs.update(ssl_context_kwargs()) return kwargs + def host_header(self): + """ + Format http Host header + """ + hdr = None + if not self.uri: + return hdr + parsed = urlparse(self.uri) + hdr = parsed.hostname + if parsed.port: + hdr = hdr + ':' + str(parsed.port) + return hdr + def ssl_context_kwargs(): kwargs = {} try: