From 74670498e902a0506e667cd21084c5e2eb71edfa Mon Sep 17 00:00:00 2001 From: harikrishnakanchi Date: Fri, 22 Jan 2016 00:53:51 +0530 Subject: [PATCH] Fixed #25971 -- Made BrokenLinkEmailsMiddleware ignore APPEND_SLASH redirects. If APPEND_SLASH=True and the referer is the URL without a trailing '/', then BrokenLinkEmailsMiddleware shouldn't send an email. --- django/middleware/common.py | 16 +++++++++++----- tests/middleware/tests.py | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/django/middleware/common.py b/django/middleware/common.py index 948b76c71b..70d8c57f7e 100644 --- a/django/middleware/common.py +++ b/django/middleware/common.py @@ -162,18 +162,24 @@ class BrokenLinkEmailsMiddleware(object): def is_ignorable_request(self, request, uri, domain, referer): """ Return True if the given request *shouldn't* notify the site managers - according to project settings or in three specific situations: - - If the referer is empty. - - If a '?' in referer is identified as a search engine source. - - If the referer is equal to the current URL, ignoring the scheme - (assumed to be a poorly implemented bot). + according to project settings or in situations outlined by the inline + comments. """ + # The referer is empty. if not referer: return True + # APPEND_SLASH is enabled and the referer is equal to the current URL + # without a trailing slash indicating an internal redirect. + if settings.APPEND_SLASH and uri.endswith('/') and referer == uri[:-1]: + return True + + # A '?' in referer is identified as a search engine source. if not self.is_internal_request(domain, referer) and '?' in referer: return True + # The referer is equal to the current URL, ignoring the scheme (assumed + # to be a poorly implemented bot). parsed_referer = urlparse(referer) if parsed_referer.netloc in ['', domain] and parsed_referer.path == uri: return True diff --git a/tests/middleware/tests.py b/tests/middleware/tests.py index 202f7511cd..dd26d68056 100644 --- a/tests/middleware/tests.py +++ b/tests/middleware/tests.py @@ -417,6 +417,20 @@ class BrokenLinkEmailsMiddlewareTest(SimpleTestCase): BrokenLinkEmailsMiddleware().process_response(self.req, self.resp) self.assertEqual(len(mail.outbox), 1) + @override_settings(APPEND_SLASH=True) + def test_referer_equal_to_requested_url_without_trailing_slash_when_append_slash_is_set(self): + self.req.path = self.req.path_info = '/regular_url/that/does/not/exist/' + self.req.META['HTTP_REFERER'] = self.req.path_info[:-1] + BrokenLinkEmailsMiddleware().process_response(self.req, self.resp) + self.assertEqual(len(mail.outbox), 0) + + @override_settings(APPEND_SLASH=False) + def test_referer_equal_to_requested_url_without_trailing_slash_when_append_slash_is_unset(self): + self.req.path = self.req.path_info = '/regular_url/that/does/not/exist/' + self.req.META['HTTP_REFERER'] = self.req.path_info[:-1] + BrokenLinkEmailsMiddleware().process_response(self.req, self.resp) + self.assertEqual(len(mail.outbox), 1) + @override_settings(ROOT_URLCONF='middleware.cond_get_urls') class ConditionalGetMiddlewareTest(SimpleTestCase):