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.
This commit is contained in:
harikrishnakanchi 2016-01-22 00:53:51 +05:30 committed by Tim Graham
parent 6c33e73333
commit 74670498e9
2 changed files with 25 additions and 5 deletions

View File

@ -162,18 +162,24 @@ class BrokenLinkEmailsMiddleware(object):
def is_ignorable_request(self, request, uri, domain, referer): def is_ignorable_request(self, request, uri, domain, referer):
""" """
Return True if the given request *shouldn't* notify the site managers Return True if the given request *shouldn't* notify the site managers
according to project settings or in three specific situations: according to project settings or in situations outlined by the inline
- If the referer is empty. comments.
- 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).
""" """
# The referer is empty.
if not referer: if not referer:
return True 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: if not self.is_internal_request(domain, referer) and '?' in referer:
return True return True
# The referer is equal to the current URL, ignoring the scheme (assumed
# to be a poorly implemented bot).
parsed_referer = urlparse(referer) parsed_referer = urlparse(referer)
if parsed_referer.netloc in ['', domain] and parsed_referer.path == uri: if parsed_referer.netloc in ['', domain] and parsed_referer.path == uri:
return True return True

View File

@ -417,6 +417,20 @@ class BrokenLinkEmailsMiddlewareTest(SimpleTestCase):
BrokenLinkEmailsMiddleware().process_response(self.req, self.resp) BrokenLinkEmailsMiddleware().process_response(self.req, self.resp)
self.assertEqual(len(mail.outbox), 1) 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') @override_settings(ROOT_URLCONF='middleware.cond_get_urls')
class ConditionalGetMiddlewareTest(SimpleTestCase): class ConditionalGetMiddlewareTest(SimpleTestCase):