Skip to content

Commit

Permalink
Handle the case of multiple headers when constructing root url (#7938)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
abidlabs and gradio-pr-bot committed Apr 5, 2024
1 parent 919afff commit 8250a1a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-breads-allow.md
@@ -0,0 +1,5 @@
---
"gradio": patch
---

feat:Handle the case of multiple headers when constructing root url
12 changes: 10 additions & 2 deletions gradio/route_utils.py
Expand Up @@ -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("/")
Expand Down
9 changes: 9 additions & 0 deletions test/test_routes.py
Expand Up @@ -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",
Expand Down

0 comments on commit 8250a1a

Please sign in to comment.