Handle the case of multiple headers when constructing root url (#7938)

* handle the case of multiple headers

* lint

* add changeset

* Update gradio/route_utils.py

* add changeset

* lint

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Abubakar Abid 2024-04-04 19:35:12 -07:00 committed by GitHub
parent 919afffcee
commit 8250a1a0df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 2 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": patch
---
feat:Handle the case of multiple headers when constructing root url

View File

@ -306,16 +306,24 @@ def get_root_url(
And if a relative `root_path` is provided, and it is not already the subpath of the URL, it is appended to the root url.
In cases (2) and (3), We also check to see if the x-forwarded-proto header is present, and if so, convert the root url to https.
And if there are multiple hosts in the x-forwarded-host or multiple protocols in the x-forwarded-proto, the first one is used.
"""
def get_first_header_value(header_name: str):
header_value = request.headers.get(header_name)
if header_value:
return header_value.split(",")[0].strip()
return None
if root_path and client_utils.is_http_url_like(root_path):
return root_path.rstrip("/")
x_forwarded_host = request.headers.get("x-forwarded-host")
x_forwarded_host = get_first_header_value("x-forwarded-host")
root_url = f"http://{x_forwarded_host}" if x_forwarded_host else str(request.url)
root_url = httpx.URL(root_url)
root_url = root_url.copy_with(query=None)
root_url = str(root_url).rstrip("/")
if request.headers.get("x-forwarded-proto") == "https":
if get_first_header_value("x-forwarded-proto") == "https":
root_url = root_url.replace("http://", "https://")
route_path = route_path.rstrip("/")

View File

@ -1095,6 +1095,15 @@ def test_get_root_url(
"/",
"https://gradio.dev",
),
(
{
"x-forwarded-host": "gradio.dev,internal.gradio.dev",
"x-forwarded-proto": "https,http",
},
"/",
"/",
"https://gradio.dev",
),
(
{"x-forwarded-host": "gradio.dev", "x-forwarded-proto": "https"},
"http://google.com",