1
0
mirror of https://github.com/django/django.git synced 2025-06-05 11:39:13 +00:00

Fixed #9199 -- We were erroneously only prepending "www" to the domain if we

also needed to append a slash (when PREPEND_WWW=True).

Based on a patch and tests from gonz. Thanks.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@9184 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-10-07 08:22:50 +00:00
parent a928c563e9
commit 30b568226f
2 changed files with 63 additions and 23 deletions

View File

@ -53,9 +53,8 @@ class CommonMiddleware(object):
# Append a slash if APPEND_SLASH is set and the URL doesn't have a # 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 # trailing slash and there is no pattern for the current path
if settings.APPEND_SLASH and (not old_url[1].endswith('/')): if settings.APPEND_SLASH and (not old_url[1].endswith('/')):
try: if (not _is_valid_path(request.path_info) and
urlresolvers.resolve(request.path_info) _is_valid_path("%s/" % request.path_info)):
except urlresolvers.Resolver404:
new_url[1] = new_url[1] + '/' new_url[1] = new_url[1] + '/'
if settings.DEBUG and request.method == 'POST': if settings.DEBUG and request.method == 'POST':
raise RuntimeError, ("" raise RuntimeError, (""
@ -66,24 +65,18 @@ class CommonMiddleware(object):
"slash), or set APPEND_SLASH=False in your Django " "slash), or set APPEND_SLASH=False in your Django "
"settings.") % (new_url[0], new_url[1]) "settings.") % (new_url[0], new_url[1])
if new_url != old_url: if new_url == old_url:
# Redirect if the target url exists # No redirects required.
try: return
urlresolvers.resolve("%s/" % request.path_info) if new_url[0]:
except urlresolvers.Resolver404: newurl = "%s://%s%s" % (
pass request.is_secure() and 'https' or 'http',
else: new_url[0], urlquote(new_url[1]))
if new_url[0]: else:
newurl = "%s://%s%s" % ( newurl = urlquote(new_url[1])
request.is_secure() and 'https' or 'http', if request.GET:
new_url[0], urlquote(new_url[1])) newurl += '?' + request.META['QUERY_STRING']
else: return http.HttpResponsePermanentRedirect(newurl)
newurl = urlquote(new_url[1])
if request.GET:
newurl += '?' + request.META['QUERY_STRING']
return http.HttpResponsePermanentRedirect(newurl)
return None
def process_response(self, request, response): def process_response(self, request, response):
"Check for a flat page (for 404s) and calculate the Etag, if needed." "Check for a flat page (for 404s) and calculate the Etag, if needed."
@ -119,7 +112,9 @@ class CommonMiddleware(object):
return response return response
def _is_ignorable_404(uri): 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: for start in settings.IGNORABLE_404_STARTS:
if uri.startswith(start): if uri.startswith(start):
return True return True
@ -129,6 +124,23 @@ def _is_ignorable_404(uri):
return False return False
def _is_internal_request(domain, referer): 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. # Different subdomains are treated as different domains.
return referer is not None and re.match("^https?://%s/" % re.escape(domain), referer) 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

View File

@ -89,3 +89,31 @@ class CommonMiddlewareTest(TestCase):
self.assertEquals( self.assertEquals(
r['Location'], r['Location'],
'http://testserver/middleware/needsquoting%23/') 'http://testserver/middleware/needsquoting%23/')
def test_prepend_www(self):
settings.PREPEND_WWW = True
settings.APPEND_SLASH = False
request = self._get_request('path/')
r = CommonMiddleware().process_request(request)
self.assertEquals(r.status_code, 301)
self.assertEquals(
r['Location'],
'http://www.testserver/middleware/path/')
def test_prepend_www_append_slash_have_slash(self):
settings.PREPEND_WWW = True
settings.APPEND_SLASH = True
request = self._get_request('slash/')
r = CommonMiddleware().process_request(request)
self.assertEquals(r.status_code, 301)
self.assertEquals(r['Location'],
'http://www.testserver/middleware/slash/')
def test_prepend_www_append_slash_slashless(self):
settings.PREPEND_WWW = True
settings.APPEND_SLASH = True
request = self._get_request('slash')
r = CommonMiddleware().process_request(request)
self.assertEquals(r.status_code, 301)
self.assertEquals(r['Location'],
'http://www.testserver/middleware/slash/')