22import logging
33from collections .abc import Callable , Iterator
44from typing import Any , Generic , Literal , TypeAlias , TypeVar
5- from urllib .parse import urlparse
65
76from aiohttp import web
87from aiohttp .web_exceptions import HTTPError , HTTPException
@@ -132,7 +131,7 @@ class NextPage(BaseModel, Generic[PageParameters]):
132131 parameters : PageParameters | None = None
133132
134133
135- def iter_origins (request : web .Request ) -> Iterator [str ]:
134+ def iter_origins (request : web .Request ) -> Iterator [tuple [ str , str ] ]:
136135 #
137136 # SEE https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Host
138137 # SEE https://doc.traefik.io/traefik/getting-started/faq/#what-are-the-forwarded-headers-when-proxying-http-requests
@@ -152,21 +151,16 @@ def iter_origins(request: web.Request) -> Iterator[str]:
152151 ]
153152
154153 if fwd_protos and fwd_hosts :
155- fwd_origins = [
156- f"{ proto } ://{ host } "
157- for proto , host in zip (fwd_protos , fwd_hosts , strict = False )
158- ]
159- for origin in fwd_origins :
160- if origin and origin not in seen :
161- seen .add (origin )
162- yield origin
154+ for proto , host in zip (fwd_protos , fwd_hosts , strict = False ):
155+ if (proto , host ) not in seen :
156+ seen .add ((proto , host ))
157+ yield (proto , host )
163158
164159 # Fallback to request.host
165- yield f" { request .url .scheme } :// { request .url .host } "
160+ yield request .url .scheme , f" { request .url .host } "
166161
167162
168163def get_api_base_url (request : web .Request ) -> str :
169- origin = next (iter_origins (request ))
170- hostname = urlparse (origin ).hostname or "localhost"
171- api_host = f"api.{ hostname } " if not is_ip_address (hostname ) else hostname
172- return f"{ request .url .with_host (api_host ).with_port (None ).with_path ('' )} "
164+ scheme , host = next (iter_origins (request ))
165+ api_host = f"api.{ host } " if not is_ip_address (host ) else host
166+ return f"{ scheme } ://{ api_host } "
0 commit comments