Python
Fonksiyonlar
Lisans: Belirtilmemis
Python — allowed_hosts.py
Python — Fonksiyonlar. Fonksiyonlar: normalise_host, is_local_host, host_matches, host_allowed.
"""One host policy for origins and request-derived authentication links."""
def normalise_host(host: str) -> str:
host = host.strip().lower()
if host.startswith("[") and "]" in host:
return host[1:host.index("]")]
if host.count(":") == 1:
return host.split(":", 1)[0]
return host
def is_local_host(host: str) -> bool:
return normalise_host(host) in {"localhost", "127.0.0.1", "::1"}
def host_matches(host: str, pattern: str) -> bool:
host = normalise_host(host)
pattern = pattern.strip().lower()
if not host or not pattern:
return False
if pattern == "*":
return True
# Like TrustedHostMiddleware, *.example.com means subdomains. The bare
# domain can be allowed separately and lookalike suffixes never match.
if pattern.startswith("*."):
return host.endswith(pattern[1:])
return host == normalise_host(pattern)
def host_allowed(host: str, patterns: list[str]) -> bool:
return any(host_matches(host, pattern) for pattern in patterns)