Fixing root path issue with subpath being repeated twice (#7624)

* fixing root path

* add changeset

* add changeset

* fix

* add changeset

* replace tests

* prints

* fix

* add test

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Abubakar Abid 2024-03-06 12:52:01 -08:00 committed by GitHub
parent ba8cc48b13
commit a22f3e062d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 3 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": patch
---
fix:Fixing root path issue with subpath being repeated twice

View File

@ -289,8 +289,8 @@ def get_root_url(
) -> str:
"""
Gets the root url of the request, stripping off any query parameters, the route_path, and trailing slashes.
Also ensures that the root url is https if the request is https. If root_path is provided, it is appended to the root url.
The final root url will not have a trailing slash.
Also ensures that the root url is https if the request is https. If root_path is provided, and it is not already
the subpath of the URL, it is appended to the root url. The final root url will not have a trailing slash.
"""
root_url = str(request.url)
root_url = httpx.URL(root_url)
@ -298,10 +298,17 @@ def get_root_url(
root_url = str(root_url).rstrip("/")
if request.headers.get("x-forwarded-proto") == "https":
root_url = root_url.replace("http://", "https://")
route_path = route_path.rstrip("/")
if len(route_path) > 0:
root_url = root_url[: -len(route_path)]
return (root_url.rstrip("/") + (root_path or "")).rstrip("/")
root_url = root_url.rstrip("/")
root_url = httpx.URL(root_url)
if root_path and root_url.path != root_path:
root_url = root_url.copy_with(path=root_path)
return str(root_url).rstrip("/")
def _user_safe_decode(src: bytes, codec: str) -> str:

View File

@ -1017,6 +1017,24 @@ def test_component_server_endpoints(connect):
"/gradio/",
"https://localhost:7860/gradio",
),
(
"https://www.gradio.app/playground/",
"/",
"/playground",
"https://www.gradio.app/playground",
),
(
"https://www.gradio.app/playground/",
"/",
"/playground",
"https://www.gradio.app/playground",
),
(
"https://www.gradio.app/playground/",
"/",
"",
"https://www.gradio.app/playground",
),
],
)
def test_get_root_url(request_url, route_path, root_path, expected_root_url):