Commit df0523de authored by Berker Peksag's avatar Berker Peksag Committed by Tim Graham
Browse files

Fixed #23531 -- Added CommonMiddleware.response_redirect_class.

parent 83daf536
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -30,11 +30,16 @@ class CommonMiddleware(object):
              urlpatterns, then an HTTP-redirect is returned to this new URL;
              otherwise the initial URL is processed as usual.

          This behavior can be customized by subclassing CommonMiddleware and
          overriding the response_redirect_class attribute.

        - ETags: If the USE_ETAGS setting is set, ETags will be calculated from
          the entire page content and Not Modified responses will be returned
          appropriately.
    """

    response_redirect_class = http.HttpResponsePermanentRedirect

    def process_request(self, request):
        """
        Check for denied User-Agents and rewrite the URL based on
@@ -100,7 +105,7 @@ class CommonMiddleware(object):
                    newurl += '?' + request.META['QUERY_STRING'].decode()
                except UnicodeDecodeError:
                    pass
        return http.HttpResponsePermanentRedirect(newurl)
        return self.response_redirect_class(newurl)

    def process_response(self, request, response):
        """
+8 −0
Original line number Diff line number Diff line
@@ -66,6 +66,14 @@ Adds a few conveniences for perfectionists:
  for each request by MD5-hashing the page content, and it'll take care of
  sending ``Not Modified`` responses, if appropriate.

.. attribute:: CommonMiddleware.response_redirect_class

.. versionadded:: 1.8

Defaults to :class:`~django.http.HttpResponsePermanentRedirect`. Subclass
``CommonMiddleware`` and override the attribute to customize the redirects
issued by the middleware.

.. class:: BrokenLinkEmailsMiddleware

* Sends broken link notification emails to :setting:`MANAGERS` (see
+7 −0
Original line number Diff line number Diff line
@@ -304,6 +304,13 @@ Management Commands
  :setting:`FIXTURE_DIRS` contains duplicates or a default fixture directory
  path (``app_name/fixtures``), an exception is raised.

Middleware
^^^^^^^^^^

* The :attr:`CommonMiddleware.response_redirect_class
  <django.middleware.common.CommonMiddleware.response_redirect_class>`
  attribute allows you to customize the redirects issued by the middleware.

Migrations
^^^^^^^^^^

+21 −1
Original line number Diff line number Diff line
@@ -9,7 +9,10 @@ from unittest import skipIf

from django.conf import settings
from django.core import mail
from django.http import HttpRequest, HttpResponse, StreamingHttpResponse
from django.http import (
    HttpRequest, HttpResponse, StreamingHttpResponse, HttpResponsePermanentRedirect,
    HttpResponseRedirect,
)
from django.middleware.clickjacking import XFrameOptionsMiddleware
from django.middleware.common import CommonMiddleware, BrokenLinkEmailsMiddleware
from django.middleware.http import ConditionalGetMiddleware
@@ -242,6 +245,23 @@ class CommonMiddlewareTest(TestCase):
        response = CommonMiddleware().process_request(request)
        self.assertEqual(response.status_code, 301)

    def test_response_redirect_class(self):
        request = self._get_request('slash')
        r = CommonMiddleware().process_request(request)
        self.assertEqual(r.status_code, 301)
        self.assertEqual(r.url, 'http://testserver/slash/')
        self.assertIsInstance(r, HttpResponsePermanentRedirect)

    def test_response_redirect_class_subclass(self):
        class MyCommonMiddleware(CommonMiddleware):
            response_redirect_class = HttpResponseRedirect

        request = self._get_request('slash')
        r = MyCommonMiddleware().process_request(request)
        self.assertEqual(r.status_code, 302)
        self.assertEqual(r.url, 'http://testserver/slash/')
        self.assertIsInstance(r, HttpResponseRedirect)


@override_settings(
    IGNORABLE_404_URLS=(re.compile(r'foo'),),