def get_request_origin(request: fastapi.Request, route_path: str) -> httpx.URL:
    """
    Examines the request headers to determine the origin of the request.
    If the request includes the x-forwarded-host header, it is used directly to determine the origin.
    Otherwise, the request url is used and the route path is stripped off.

    The returned URL is a httpx.URL object without a trailing slash, e.g. "https://example.com"
    """
    x_forwarded_host = get_first_header_value(request, "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 get_first_header_value(request, "x-forwarded-proto") == "https":
        root_url = root_url.replace("http://", "https://")

    route_path = route_path.rstrip("/")
    if len(route_path) > 0 and not x_forwarded_host:
        root_url = root_url[: -len(route_path)]
    root_url = root_url.rstrip("/")
    root_url = httpx.URL(root_url)
    return root_url

    Examines the request headers to determine the origin of the request.
    If the request includes the x-forwarded-host header, it is used directly to determine the origin.
    Otherwise, the request url is used and the route path is stripped off.

    The returned URL is a httpx.URL object without a trailing slash, e.g. "https://example.com"
    
gradio/route_utils.py