Ticket #20356: 20356-1.diff

File 20356-1.diff, 2.2 KB (added by Claude Paroz, 11 years ago)
  • django/middleware/common.py

    diff --git a/django/middleware/common.py b/django/middleware/common.py
    index 92f8cb3..2507379 100644
    a b from django.conf import settings  
    77from django.core.mail import mail_managers
    88from django.core import urlresolvers
    99from django import http
     10from django.utils.encoding import force_text
    1011from django.utils.http import urlquote
    1112from django.utils import six
    1213
    class BrokenLinkEmailsMiddleware(object):  
    140141        if response.status_code == 404 and not settings.DEBUG:
    141142            domain = request.get_host()
    142143            path = request.get_full_path()
    143             referer = request.META.get('HTTP_REFERER', '')
     144            referer = force_text(request.META.get('HTTP_REFERER', ''), errors='replace')
    144145            is_internal = self.is_internal_request(domain, referer)
    145146            is_not_search_engine = '?' not in referer
    146147            is_ignorable = self.is_ignorable_404(path)
  • tests/middleware/tests.py

    diff --git a/tests/middleware/tests.py b/tests/middleware/tests.py
    index f2f7f4d..e526da4 100644
    a b from django.test.utils import override_settings  
    2222from django.utils import six
    2323from django.utils.encoding import force_str
    2424from django.utils.six.moves import xrange
    25 from django.utils.unittest import expectedFailure
     25from django.utils.unittest import expectedFailure, skipIf
    2626
    2727from transactions.tests import IgnorePendingDeprecationWarningsMixin
    2828
    class BrokenLinkEmailsMiddlewareTest(TestCase):  
    320320        BrokenLinkEmailsMiddleware().process_response(self.req, self.resp)
    321321        self.assertEqual(len(mail.outbox), 0)
    322322
     323    @skipIf(six.PY3, "HTTP_REFERER is str type on Python 3")
     324    def test_404_error_nonascii_referrer(self):
     325        # Such referer strings should not happen, but anyway, if it happens,
     326        # let's not crash
     327        self.req.META['HTTP_REFERER'] = b'http://testserver/c/\xd0\xbb\xd0\xb8/'
     328        BrokenLinkEmailsMiddleware().process_response(self.req, self.resp)
     329        self.assertEqual(len(mail.outbox), 1)
     330
    323331
    324332class ConditionalGetMiddlewareTest(TestCase):
    325333    urls = 'middleware.cond_get_urls'
Back to Top