import yarl


def forwarded_url(request) -> "yarl.URL":
    """Returns the URL with the correct protocol scheme.

    Looks for the X-Forwarded-Proto header and replaces the request URL's
    protocol scheme if applicable.

    :param request: The request needed to build the URL.
    :return: The corrected URL.
    """
    if forwarded_protocol := request.headers.get("X-Forwarded-Proto"):
        return request.url.with_scheme(forwarded_protocol)
    else:
        return request.url