'Check for denied User-Agents and rewrite the URL based on
settings.APPEND_SLASH and settings.PREPEND_WWW'

def process_request(self, request):

    if ('HTTP_USER_AGENT' in request.META):
        for user_agent_regex in settings.DISALLOWED_USER_AGENTS:
            if user_agent_regex.search(request.META['HTTP_USER_AGENT']):
                logger.warning(('Forbidden    (User    agent):    %s' % request.path), extra={'status_code': 403, 'request': request})
                return http.HttpResponseForbidden('<h1>Forbidden</h1>')
    host = request.get_host()
    old_url = [host, request.path]
    new_url = old_url[:]
    if (settings.PREPEND_WWW and old_url[0] and (not old_url[0].startswith('www.'))):
        new_url[0] = ('www.' + old_url[0])
    if (settings.APPEND_SLASH and (not old_url[1].endswith('/'))):
        urlconf = getattr(request, 'urlconf', None)
        if ((not _is_valid_path(request.path_info, urlconf)) and _is_valid_path(('%s/' % request.path_info), urlconf)):
            new_url[1] = (new_url[1] + '/')
            if (settings.DEBUG and (request.method == 'POST')):
                raise RuntimeError, ("You    called    this    URL    via    POST,    but    the    URL    doesn't    end    in    a    slash    and    you    have    APPEND_SLASH    set.    Django    can't    redirect    to    the    slash    URL    while    maintaining    POST    data.    Change    your    form    to    point    to    %s%s    (note    the    trailing    slash),    or    set    APPEND_SLASH=False    in    your    Django    settings." % (new_url[0], new_url[1]))
    if (new_url == old_url):
        return
    if new_url[0]:
        newurl = ('%s://%s%s' % (((request.is_secure() and 'https') or 'http'), new_url[0], urlquote(new_url[1])))
    else:
        newurl = urlquote(new_url[1])
    if request.GET:
        newurl += ('?' + request.META['QUERY_STRING'])
    return http.HttpResponsePermanentRedirect(newurl)
