url_utils.py 620 B

123456789101112131415161718
  1. """Small helpers for composing URLs from configured network hosts."""
  2. def format_url_host(host: str) -> str:
  3. """Return *host* in HTTP URL-authority form.
  4. IPv6 literals require brackets when followed by a port. Already-bracketed
  5. values are preserved so callers can safely pass either representation.
  6. """
  7. host = host.strip()
  8. if host.startswith("[") and host.endswith("]"):
  9. inner_host = host[1:-1]
  10. if ":" in inner_host:
  11. return f"[{inner_host.replace('%', '%25')}]"
  12. return host
  13. if ":" in host:
  14. return f"[{host.replace('%', '%25')}]"
  15. return host