Skip to content

Commit

Permalink
Set an explicit User-Agent and remove port out of Host.
Browse files Browse the repository at this point in the history
  • Loading branch information
ionelmc committed Apr 11, 2024
1 parent e3e18fb commit 9f2f3bd
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/holdup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@
from urllib.request import HTTPDigestAuthHandler
from urllib.request import HTTPPasswordMgrWithDefaultRealm
from urllib.request import HTTPSHandler
from urllib.request import Request
from urllib.request import build_opener

import holdup


class Check:
error = None
Expand Down Expand Up @@ -158,6 +161,7 @@ def __init__(self, url):
self.netloc = f"{url.hostname}:{url.port}"
else:
self.netloc = url.hostname
self.host = url.hostname

cleaned_url = urlunparse(url._replace(netloc=self.netloc))

Expand All @@ -180,8 +184,9 @@ def run(self, options):
handlers.append(HTTPSHandler(context=ssl_ctx))

opener = build_opener(*handlers)

with closing(opener.open(self.url, timeout=options.check_timeout)) as req:
opener.addheaders = [("User-Agent", f"python-holdup/{holdup.__version__}")]
request = Request(self.url, headers={"Host": self.host}) # noqa: S310
with closing(opener.open(request, timeout=options.check_timeout)) as req:
status = req.getcode()
if status != 200:
raise Exception(f"Expected status code 200, got {status!r}")
Expand Down Expand Up @@ -222,6 +227,8 @@ def __init__(self, path):
self.path = path

def run(self, _):
# necessary to check if it exists.
os.stat(self.path) # noqa: PTH116
if not os.access(self.path, os.R_OK):
raise Exception(f"Failed access({self.path!r}, R_OK) test")

Expand Down Expand Up @@ -379,8 +386,6 @@ def parse_value(value, proto):


def add_version_argument(parser):
import holdup

parser.add_argument(
"--version",
action="version",
Expand Down

0 comments on commit 9f2f3bd

Please sign in to comment.