diff --git a/src/aioweb/session.py b/src/aioweb/session.py index e0501f7..d1c725f 100644 --- a/src/aioweb/session.py +++ b/src/aioweb/session.py @@ -188,17 +188,19 @@ class ExtendedSession: def overwrite_domain(self, target, replacement): """register a host-substring rewrite (target -> replacement) - raises: - ValueError: if replacement contains a port (e.g. 'host:1234') - yarl's - with_host() rejects any ':' in a host at request time, deep inside - yarl with a stack trace far from this call; caught here instead + raises ValueError if the replacement is not a valid host for yarl's with_host() + (e.g. a 'host:port' string or a pre-bracketed '[::1]') - caught here rather than + deep in yarl at request time. a bare IPv6 literal ('::1', '2001:db8::1') is valid + and accepted: yarl brackets it itself. """ - if ":" in replacement: + try: + URL("http://placeholder").with_host(replacement) + except ValueError as exc: raise ValueError( - f"overwrite_domain replacement {replacement!r} contains a port - " - "yarl's URL.with_host() cannot take a host:port string; rewrite the " - "port separately or via a full URL override, not domain_overwrites" - ) + f"overwrite_domain replacement {replacement!r} is not a valid host: {exc}; " + "pass a bare hostname or IP (a bare IPv6 literal is fine), not a host:port or " + "bracketed form - rewrite a port via a full URL override, not domain_overwrites" + ) from exc self.domain_overwrites[target] = replacement def _apply_domain_overwrites(self, url: str) -> str: