def METHOD_NAME(request):
    referer = request.META.get("HTTP_REFERER")
    try:
        if not referer:
            raise ValueError()
        if not referer.startswith(request.scheme):
            raise ValueError()
        referer = referer[len(request.scheme) + 3 :]
        if not referer.startswith(request.META["HTTP_HOST"]):
            raise ValueError()
        referer = referer[len(request.META["HTTP_HOST"].rstrip("/")) :]
        if not referer.startswith("/"):
            raise ValueError()
        resolve(referer)
        return referer
    except (Http404, KeyError, ValueError):
        return None