-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Introduce URL utilities for normalizing public URLs and paths #4494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| """Utilities for normalizing A2A public URLs and mount paths.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from urllib.parse import urlparse | ||
|
|
||
|
|
||
| def normalize_path(path: str) -> str: | ||
| """Normalize an application path to a canonical mount path.""" | ||
| path = (path or "/").strip() | ||
| if not path: | ||
| return "/" | ||
| if not path.startswith("/"): | ||
| path = f"/{path}" | ||
| if path != "/": | ||
| path = path.rstrip("/") | ||
| return path | ||
|
Comment on lines
+8
to
+17
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic here is a bit dense, especially def normalize_path(path: str) -> str:
"""Normalize an application path to a canonical mount path."""
path = (path or "").strip()
if not path or path == "/":
return "/"
if not path.startswith("/"):
path = f"/{path}"
return path.rstrip("/") |
||
|
|
||
|
|
||
| def normalize_public_url(url: str) -> str: | ||
| """Normalize a public URL and validate required URL components.""" | ||
| parsed = urlparse(url) | ||
| if not parsed.scheme or not parsed.netloc: | ||
| raise ValueError( | ||
| "http_url must include a scheme and host, for example " | ||
| "'https://example.com/analysis-agent'." | ||
| ) | ||
| normalized_path = normalize_path(parsed.path) | ||
| if normalized_path == "/": | ||
| return f"{parsed.scheme}://{parsed.netloc}" | ||
| return f"{parsed.scheme}://{parsed.netloc}{normalized_path}" | ||
|
|
||
|
|
||
| def build_public_url(protocol: str, host: str, port: int, path: str) -> str: | ||
| """Build a normalized public URL from host, port, protocol and path.""" | ||
| normalized_path = normalize_path(path) | ||
| base = f"{protocol}://{host}:{port}" | ||
| if normalized_path == "/": | ||
| return base | ||
| return f"{base}{normalized_path}" | ||
|
Comment on lines
+34
to
+40
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation always includes the port in the generated URL. For standard ports (80 for HTTP, 443 for HTTPS), it's common practice to omit them from the URL for better canonicalization. def build_public_url(protocol: str, host: str, port: int, path: str) -> str:
"""Build a normalized public URL from host, port, protocol and path."""
normalized_path = normalize_path(path)
port_str = f":{port}"
if (protocol.lower() == "http" and port == 80) or (
protocol.lower() == "https" and port == 443
):
port_str = ""
base = f"{protocol}://{host}{port_str}"
if normalized_path == "/":
return base
return f"{base}{normalized_path}" |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic can be simplified by using the
oroperator to provide the default URL, making the code more concise and readable.