mirror of
https://github.com/gradio-app/gradio.git
synced 2025-04-06 12:30:29 +08:00
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:
parent
919afffcee
commit
8250a1a0df
5
.changeset/little-breads-allow.md
Normal file
5
.changeset/little-breads-allow.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"gradio": patch
|
||||
---
|
||||
|
||||
feat:Handle the case of multiple headers when constructing root url
|
@ -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("/")
|
||||
|
@ -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",
|
||||
|
Loading…
x
Reference in New Issue
Block a user