Fixed #26293 -- Fixed CommonMiddleware to process PREPEND_WWW and APPEND_SLASH independently.
This commit is contained in:
parent
107165c4b0
commit
9390da7fb6
|
@ -54,18 +54,19 @@ class CommonMiddleware(object):
|
||||||
|
|
||||||
# Check for a redirect based on settings.PREPEND_WWW
|
# Check for a redirect based on settings.PREPEND_WWW
|
||||||
host = request.get_host()
|
host = request.get_host()
|
||||||
|
must_prepend = settings.PREPEND_WWW and host and not host.startswith('www.')
|
||||||
|
redirect_url = ('%s://www.%s' % (request.scheme, host)) if must_prepend else ''
|
||||||
|
|
||||||
if settings.PREPEND_WWW and host and not host.startswith('www.'):
|
# Check if a slash should be appended
|
||||||
host = 'www.' + host
|
|
||||||
|
|
||||||
# Check if we also need to append a slash so we can do it all
|
|
||||||
# with a single redirect.
|
|
||||||
if self.should_redirect_with_slash(request):
|
if self.should_redirect_with_slash(request):
|
||||||
path = self.get_full_path_with_slash(request)
|
path = self.get_full_path_with_slash(request)
|
||||||
else:
|
else:
|
||||||
path = request.get_full_path()
|
path = request.get_full_path()
|
||||||
|
|
||||||
return self.response_redirect_class('%s://%s%s' % (request.scheme, host, path))
|
# Return a redirect if necessary
|
||||||
|
if redirect_url or path != request.get_full_path():
|
||||||
|
redirect_url += path
|
||||||
|
return self.response_redirect_class(redirect_url)
|
||||||
|
|
||||||
def should_redirect_with_slash(self, request):
|
def should_redirect_with_slash(self, request):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -34,3 +34,6 @@ Bugfixes
|
||||||
* Fixed a crash when using a reverse lookup with a subquery when a
|
* Fixed a crash when using a reverse lookup with a subquery when a
|
||||||
``ForeignKey`` has a ``to_field`` set to something other than the primary key
|
``ForeignKey`` has a ``to_field`` set to something other than the primary key
|
||||||
(:ticket:`26373`).
|
(:ticket:`26373`).
|
||||||
|
|
||||||
|
* Fixed a regression in ``CommonMiddleware`` that caused spurious warnings in
|
||||||
|
logs on requests missing a trailing slash (:ticket:`26293`).
|
||||||
|
|
|
@ -67,10 +67,8 @@ class CommonMiddlewareTest(SimpleTestCase):
|
||||||
APPEND_SLASH should redirect slashless URLs to a valid pattern.
|
APPEND_SLASH should redirect slashless URLs to a valid pattern.
|
||||||
"""
|
"""
|
||||||
request = self.rf.get('/slash')
|
request = self.rf.get('/slash')
|
||||||
response = HttpResponseNotFound()
|
r = CommonMiddleware().process_request(request)
|
||||||
r = CommonMiddleware().process_response(request, response)
|
|
||||||
self.assertEqual(r.status_code, 301)
|
self.assertEqual(r.status_code, 301)
|
||||||
self.assertEqual(r.url, '/slash/')
|
|
||||||
|
|
||||||
@override_settings(APPEND_SLASH=True)
|
@override_settings(APPEND_SLASH=True)
|
||||||
def test_append_slash_redirect_querystring(self):
|
def test_append_slash_redirect_querystring(self):
|
||||||
|
@ -301,9 +299,6 @@ class CommonMiddlewareTest(SimpleTestCase):
|
||||||
request = self.rf.get('/slash')
|
request = self.rf.get('/slash')
|
||||||
request.META['QUERY_STRING'] = force_str('drink=café')
|
request.META['QUERY_STRING'] = force_str('drink=café')
|
||||||
r = CommonMiddleware().process_request(request)
|
r = CommonMiddleware().process_request(request)
|
||||||
self.assertIsNone(r)
|
|
||||||
response = HttpResponseNotFound()
|
|
||||||
r = CommonMiddleware().process_response(request, response)
|
|
||||||
self.assertEqual(r.status_code, 301)
|
self.assertEqual(r.status_code, 301)
|
||||||
|
|
||||||
def test_response_redirect_class(self):
|
def test_response_redirect_class(self):
|
||||||
|
|
Loading…
Reference in New Issue