2005-07-13 09:25:57 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.utils import httpwrappers
|
|
|
|
from django.core.mail import mail_managers
|
|
|
|
import md5, os
|
|
|
|
|
|
|
|
class CommonMiddleware:
|
|
|
|
"""
|
|
|
|
"Common" middleware for taking care of some basic operations:
|
|
|
|
|
|
|
|
- Forbids access to User-Agents in settings.DISALLOWED_USER_AGENTS
|
|
|
|
|
2005-11-07 05:55:57 +08:00
|
|
|
- URL rewriting: Based on the APPEND_SLASH and PREPEND_WWW settings,
|
|
|
|
this middleware appends missing slashes and/or prepends missing "www."s.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-07 05:55:57 +08:00
|
|
|
- ETags: If the USE_ETAGS setting is set, ETags will be calculated from
|
2005-07-13 09:25:57 +08:00
|
|
|
the entire page content and Not Modified responses will be returned
|
|
|
|
appropriately.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def process_request(self, request):
|
|
|
|
"""
|
|
|
|
Check for denied User-Agents and rewrite the URL based on
|
|
|
|
settings.APPEND_SLASH and settings.PREPEND_WWW
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Check for denied User-Agents
|
|
|
|
if request.META.has_key('HTTP_USER_AGENT'):
|
|
|
|
for user_agent_regex in settings.DISALLOWED_USER_AGENTS:
|
|
|
|
if user_agent_regex.search(request.META['HTTP_USER_AGENT']):
|
|
|
|
return httpwrappers.HttpResponseForbidden('<h1>Forbidden</h1>')
|
|
|
|
|
|
|
|
# Check for a redirect based on settings.APPEND_SLASH and settings.PREPEND_WWW
|
2005-12-05 23:25:55 +08:00
|
|
|
old_url = [request.META.get('HTTP_HOST', ''), request.path]
|
2005-07-13 09:25:57 +08:00
|
|
|
new_url = old_url[:]
|
2005-12-05 23:25:55 +08:00
|
|
|
if settings.PREPEND_WWW and old_url[0] and not old_url[0].startswith('www.'):
|
2005-07-13 09:25:57 +08:00
|
|
|
new_url[0] = 'www.' + old_url[0]
|
|
|
|
# Append a slash if append_slash is set and the URL doesn't have a
|
|
|
|
# trailing slash or a file extension.
|
|
|
|
if settings.APPEND_SLASH and (old_url[1][-1] != '/') and ('.' not in old_url[1].split('/')[-1]):
|
|
|
|
new_url[1] = new_url[1] + '/'
|
|
|
|
if new_url != old_url:
|
|
|
|
# Redirect
|
2005-12-05 23:25:55 +08:00
|
|
|
if new_url[0]:
|
|
|
|
newurl = "%s://%s%s" % (os.environ.get('HTTPS') == 'on' and 'https' or 'http', new_url[0], new_url[1])
|
|
|
|
else:
|
|
|
|
newurl = new_url[1]
|
2005-07-13 09:25:57 +08:00
|
|
|
if request.GET:
|
2005-09-03 03:39:47 +08:00
|
|
|
newurl += '?' + request.GET.urlencode()
|
2005-07-13 09:25:57 +08:00
|
|
|
return httpwrappers.HttpResponseRedirect(newurl)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def process_response(self, request, response):
|
2005-10-06 07:36:17 +08:00
|
|
|
"Check for a flat page (for 404s) and calculate the Etag, if needed."
|
2005-07-13 09:25:57 +08:00
|
|
|
if response.status_code == 404:
|
2005-10-06 07:36:17 +08:00
|
|
|
if settings.SEND_BROKEN_LINK_EMAILS:
|
2005-07-13 09:25:57 +08:00
|
|
|
# If the referrer was from an internal link or a non-search-engine site,
|
|
|
|
# send a note to the managers.
|
2005-10-06 07:36:17 +08:00
|
|
|
domain = request.META['HTTP_HOST']
|
|
|
|
referer = request.META.get('HTTP_REFERER', None)
|
|
|
|
is_internal = referer and (domain in referer)
|
|
|
|
path = request.get_full_path()
|
|
|
|
if referer and not _is_ignorable_404(path) and (is_internal or '?' not in referer):
|
|
|
|
mail_managers("Broken %slink on %s" % ((is_internal and 'INTERNAL ' or ''), domain),
|
|
|
|
"Referrer: %s\nRequested URL: %s\n" % (referer, request.get_full_path()))
|
2005-07-13 09:25:57 +08:00
|
|
|
return response
|
|
|
|
|
2005-10-06 07:36:17 +08:00
|
|
|
# Use ETags, if requested.
|
2005-07-13 09:25:57 +08:00
|
|
|
if settings.USE_ETAGS:
|
2005-10-06 10:27:08 +08:00
|
|
|
etag = md5.new(response.get_content_as_string(settings.DEFAULT_CHARSET)).hexdigest()
|
2005-07-13 09:25:57 +08:00
|
|
|
if request.META.get('HTTP_IF_NONE_MATCH') == etag:
|
|
|
|
response = httpwrappers.HttpResponseNotModified()
|
|
|
|
else:
|
|
|
|
response['ETag'] = etag
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
def _is_ignorable_404(uri):
|
|
|
|
"Returns True if a 404 at the given URL *shouldn't* notify the site managers"
|
|
|
|
for start in settings.IGNORABLE_404_STARTS:
|
|
|
|
if uri.startswith(start):
|
|
|
|
return True
|
|
|
|
for end in settings.IGNORABLE_404_ENDS:
|
|
|
|
if uri.endswith(end):
|
|
|
|
return True
|
|
|
|
return False
|