Improved detection of whether a URL is internal or not for the purpose

of broken link e-mails (referred links from images.google were being
misclassified).


git-svn-id: http://code.djangoproject.com/svn/django/trunk@3870 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Luke Plant 2006-09-26 18:49:28 +00:00
parent 96f0b7bd21
commit 8b216eb865
1 changed files with 7 additions and 1 deletions

View File

@ -2,6 +2,7 @@ from django.conf import settings
from django import http
from django.core.mail import mail_managers
import md5
import re
class CommonMiddleware(object):
"""
@ -61,7 +62,7 @@ class CommonMiddleware(object):
# send a note to the managers.
domain = http.get_host(request)
referer = request.META.get('HTTP_REFERER', None)
is_internal = referer and (domain in referer)
is_internal = _is_internal_request(domain, referer)
path = request.get_full_path()
if referer and not _is_ignorable_404(path) and (is_internal or '?' not in referer):
ua = request.META.get('HTTP_USER_AGENT', '<none>')
@ -88,3 +89,8 @@ def _is_ignorable_404(uri):
if uri.endswith(end):
return True
return False
def _is_internal_request(domain, referer):
"Return true if the referring URL is the same domain as the current request"
# Different subdomains are treated as different domains.
return referer is not None and re.match("^https?://%s/" % re.escape(domain), referer)