"""One small request to an authorized public HTTPS page. No automatic retry."""
import os
from urllib.parse import quote, urlsplit
import requests


def main():
    target = os.environ["AUTHORIZED_HTTPS_URL"]
    host = os.environ["PROXY_HOST"]
    port = os.environ["PROXY_HTTP_PORT"]
    parsed = urlsplit(target)
    if (parsed.scheme != "https" or not parsed.hostname or parsed.username
            or parsed.password or parsed.fragment):
        raise ValueError("Use a public HTTPS URL you are authorized to check.")
    if not host or any(char not in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-" for char in host):
        raise ValueError("Copy the gateway hostname from your active package.")
    if not port.isdigit() or not 1 <= int(port) <= 65535:
        raise ValueError("Use the HTTP proxy port shown in your dashboard.")
    login = quote(os.environ["PROXY_LOGIN"], safe="")
    password = quote(os.environ["PROXY_PASSWORD"], safe="")
    proxy = f"http://{login}:{password}@{host}:{port}"
    with requests.Session() as session:
        session.trust_env = False
        session.proxies = {"https": proxy}
        # Target TLS verification remains enabled. Do not follow redirects.
        with session.get(target, timeout=(10, 15), allow_redirects=False,
                         stream=True, verify=True) as response:
            print("HTTP status:", response.status_code)
            # Do not download or print the body. Stop on a challenge or denial.


if __name__ == "__main__":
    try:
        main()
    except (KeyError, ValueError, requests.RequestException):
        print("Connection not confirmed. Check your private settings or contact Support.")
        raise SystemExit(1)
