demo/sverigekartan/origin.py
"""Which map server URL the BROWSER gets: the page's own public https origin.
The front (rpxy) serves the page and the map server's /v1 under every public name,
so a page opened as https://<name>/ fetches the map same-origin from https://<name>.
Only names in the allow-list are followed (the Host header is the client's word);
anything else (localhost, a bare IP, an unknown name) gets the first public name.
SVERIGEKARTAN_MAP_SERVER, when set, wins outright.
"""
from __future__ import annotations
DEFAULT_PUBLIC_HOSTS = "karta.gunnar.rs,sverigekartan.gunnar.rs,83-190-65-118.sslip.io"
def public_hosts(env_value: str | None) -> list[str]:
hosts = [h.strip().lower().rstrip(".") for h in (env_value or DEFAULT_PUBLIC_HOSTS).split(",")]
return [h for h in hosts if h] or DEFAULT_PUBLIC_HOSTS.split(",")
def map_server_for(host_header: str | None, fixed: str | None, hosts: list[str]) -> str:
if fixed:
return fixed.rstrip("/")
host = (host_header or "").strip().lower()
# Drop a port; an IPv6 literal ("[::1]:8501") is never in the list anyway.
if host.count(":") == 1:
host = host.split(":", 1)[0]
host = host.rstrip(".")
return f"https://{host}" if host in hosts else f"https://{hosts[0]}"