Skip to content

Commit

Permalink
Merge pull request #615 from arnaudveron/add-http-host-header
Browse files Browse the repository at this point in the history
Add http Host header
  • Loading branch information
arnaudveron committed May 11, 2023
2 parents 3488ad7 + ed5599a commit c758e29
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions opensvc/core/node/node.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
14 changes: 14 additions & 0 deletions opensvc/utilities/uri/__init__.py
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit c758e29

Please sign in to comment.