--- a/app/django/middleware/common.py Tue Oct 14 12:36:55 2008 +0000
+++ b/app/django/middleware/common.py Tue Oct 14 16:00:59 2008 +0000
@@ -1,4 +1,3 @@
-import md5
import re
from django.conf import settings
@@ -6,6 +5,7 @@
from django.core.mail import mail_managers
from django.utils.http import urlquote
from django.core import urlresolvers
+from django.utils.hashcompat import md5_constructor
class CommonMiddleware(object):
"""
@@ -21,7 +21,7 @@
slash, and it is not found in urlpatterns, a new URL is formed by
appending a slash at the end. If this new URL is found in
urlpatterns, then an HTTP-redirect is returned to this new URL;
- otherwise the initial URL is processed as usual.
+ otherwise the initial URL is processed as usual.
- ETags: If the USE_ETAGS setting is set, ETags will be calculated from
the entire page content and Not Modified responses will be returned
@@ -53,9 +53,8 @@
# Append a slash if APPEND_SLASH is set and the URL doesn't have a
# trailing slash and there is no pattern for the current path
if settings.APPEND_SLASH and (not old_url[1].endswith('/')):
- try:
- urlresolvers.resolve(request.path)
- except urlresolvers.Resolver404:
+ if (not _is_valid_path(request.path_info) and
+ _is_valid_path("%s/" % request.path_info)):
new_url[1] = new_url[1] + '/'
if settings.DEBUG and request.method == 'POST':
raise RuntimeError, (""
@@ -66,24 +65,18 @@
"slash), or set APPEND_SLASH=False in your Django "
"settings.") % (new_url[0], new_url[1])
- if new_url != old_url:
- # Redirect if the target url exists
- try:
- urlresolvers.resolve(new_url[1])
- except urlresolvers.Resolver404:
- pass
- else:
- 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.GET.urlencode()
- return http.HttpResponsePermanentRedirect(newurl)
-
- return None
+ if new_url == old_url:
+ # No redirects required.
+ 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)
def process_response(self, request, response):
"Check for a flat page (for 404s) and calculate the Etag, if needed."
@@ -108,7 +101,7 @@
if response.has_header('ETag'):
etag = response['ETag']
else:
- etag = md5.new(response.content).hexdigest()
+ etag = '"%s"' % md5_constructor(response.content).hexdigest()
if response.status_code >= 200 and response.status_code < 300 and request.META.get('HTTP_IF_NONE_MATCH') == etag:
cookies = response.cookies
response = http.HttpResponseNotModified()
@@ -119,7 +112,9 @@
return response
def _is_ignorable_404(uri):
- "Returns True if a 404 at the given URL *shouldn't* notify the site managers"
+ """
+ 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
@@ -129,6 +124,23 @@
return False
def _is_internal_request(domain, referer):
- "Return true if the referring URL is the same domain as the current request"
+ """
+ Returns 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)
+
+def _is_valid_path(path):
+ """
+ Returns True if the given path resolves against the default URL resolver,
+ False otherwise.
+
+ This is a convenience method to make working with "is this a match?" cases
+ easier, avoiding unnecessarily indented try...except blocks.
+ """
+ try:
+ urlresolvers.resolve(path)
+ return True
+ except urlresolvers.Resolver404:
+ return False
+