Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

1.7 KB
Raw Blame
"""Host membership shared by URL routing and source labels.

Kept free of project imports so any module can use it: ``utilities/arxiv.py``
and the content fetcher's ``URLClassifier`` both do, and a helper in
``url_utils.py`` would have pulled the security package into their import graph.
"""

from urllib.parse import ParseResult


def http_authority_host(parsed: ParseResult) -> str | None:
    """Return the host an HTTP client would connect to, or ``None``.

    The host is lower-cased and loses the root dot of a fully qualified name.
    ``None`` means urllib and the HTTP stack disagree about the authority, so
    no host can be trusted. That happens in two cases. urllib keeps a
    backslash inside the authority, while browsers and requests end the
    authority there, so ``https://evil.example\\@doi.org/`` has the host
    ``doi.org`` for urllib and connects to ``evil.example``. And a port that
    is not a number (``host:443.evil.example``) hides the rest of the
    authority from ``hostname``, while requests refuses the URL.
    """
    if "\\" in parsed.netloc:
        return None
    try:
        _ = parsed.port
    except ValueError:
        return None
    host = parsed.hostname
    return host.rstrip(".") if host else None


def is_host_or_subdomain(hostname: str, domain: str) -> bool:
    """Return whether ``hostname`` is ``domain`` itself or a subdomain of it.

    Both arguments are expected lower-cased, without port, userinfo or a
    trailing root dot. The dot boundary is the point of the check: a
    substring test also accepts ``doi.org.evil.example`` as ``doi.org``, and
    a suffix test without the dot accepts ``notdoi.org``.
    """
    return hostname == domain or hostname.endswith(f".{domain}")