Ticket #15954: 15954.patch
File 15954.patch, 10.9 KB (added by , 14 years ago) |
---|
-
docs/howto/error-reporting.txt
66 66 those are usually just people typing in broken URLs or broken Web 'bots). 67 67 68 68 You can tell Django to stop reporting particular 404s by tweaking the 69 :setting:`IGNORABLE_404_ ENDS` and :setting:`IGNORABLE_404_STARTS` settings. Both70 should be a tuple of strings. For example::69 :setting:`IGNORABLE_404_URLS` setting. It should be a tuple of compiled 70 regular expression objects. For example:: 71 71 72 IGNORABLE_404_ENDS = ('.php', '.cgi') 73 IGNORABLE_404_STARTS = ('/phpmyadmin/',) 72 import re 73 IGNORABLE_404_URLS = ( 74 re.compile(r'\.(php|cgi)$'), 75 re.compile(r'^/phpmyadmin/'), 76 ) 74 77 75 78 In this example, a 404 to any URL ending with ``.php`` or ``.cgi`` will *not* be 76 79 reported. Neither will any URL starting with ``/phpmyadmin/``. 77 80 81 The following example shows how to exclude some conventional URLs that browsers and 82 crawlers often request:: 83 84 import re 85 IGNORABLE_404_URLS = ( 86 re.compile(r'^/apple-touch-icon.*\.png$'), 87 re.compile(r'^/favicon.ico$), 88 re.compile(r'^/robots.txt$), 89 ) 90 91 78 92 The best way to disable this behavior is to set 79 93 :setting:`SEND_BROKEN_LINK_EMAILS` to ``False``. 80 94 … … 93 107 records are ignored, but you can use them for error reporting by writing a 94 108 handler and :doc:`configuring logging </topics/logging>` appropriately. 95 109 110 .. seealso:: 111 112 .. versionchanged:: 1.4 113 114 Previously, two settings were used to control which URLs not to report: 115 :setting:`IGNORABLE_404_STARTS` and :setting:`IGNORABLE_404_ENDS`. They 116 were replaced by :setting:`IGNORABLE_404_URLS`. -
docs/releases/1.4.txt
176 176 177 177 For more details see the docs about 178 178 :doc:`customizing the comments framework </ref/contrib/comments/custom>`. 179 180 `IGNORABLE_404_STARTS` and `IGNORABLE_404_ENDS` settings 181 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 182 183 Django can report 404 errors: see :doc:`/howto/error-reporting`. 184 Until Django 1.3, it was possible to exclude some URLs from the reporting 185 by adding prefixes to :setting:`IGNORABLE_404_STARTS` and suffixes to 186 :setting:`IGNORABLE_404_ENDS`. 187 188 In Django 1.4, these two settings are superseded by 189 :setting:`IGNORABLE_404_URLS`, which is a list of compiled regular expressions. 190 Django won't send an email for 404 errors on URLs that match any of them. 191 192 Furthermore, the previous settings had some rather arbitrary default values:: 193 194 IGNORABLE_404_STARTS = ('/cgi-bin/', '/_vti_bin', '/_vti_inf') 195 IGNORABLE_404_ENDS = ('mail.pl', 'mailform.pl', 'mail.cgi', 'mailform.cgi', 196 'favicon.ico', '.php') 197 198 It's not Django's role to decide if your website has a legacy ``/cgi-bin/`` 199 section or a ``favicon.ico``. As a consequence, the default value of 200 :setting:`IGNORABLE_404_URLS` is now empty. 201 202 If you have customized :setting:`IGNORABLE_404_STARTS` and 203 :setting:`IGNORABLE_404_ENDS`, of if you want to keep the old default value, 204 you should add the following lines in your settings file:: 205 206 import re 207 IGNORABLE_404_URLS = ( 208 # for each <prefix> in IGNORABLE_404_STARTS 209 re.compile(r'^<prefix>'), 210 # for each <suffix> in IGNORABLE_404_ENDS 211 re.compile(r'<suffix>$'), 212 ) 213 214 Don't forget to escape characters that have a special meaning in a regular 215 expression. -
docs/ref/settings.txt
1020 1020 ``SHORT_DATETIME_FORMAT``, ``FIRST_DAY_OF_WEEK``, ``DECIMAL_SEPARATOR``, 1021 1021 ``THOUSAND_SEPARATOR`` and ``NUMBER_GROUPING``. 1022 1022 1023 .. setting:: IGNORABLE_404_ ENDS1023 .. setting:: IGNORABLE_404_URLS 1024 1024 1025 IGNORABLE_404_ ENDS1025 IGNORABLE_404_URLS 1026 1026 ------------------ 1027 1027 1028 Default: ``('mail.pl', 'mailform.pl', 'mail.cgi', 'mailform.cgi', 'favicon.ico', '.php')`` 1028 .. versionadded:: 1.4 1029 1029 1030 See also ``IGNORABLE_404_STARTS`` and ``Error reporting via email``. 1030 Default: ``()`` 1031 1031 1032 .. setting:: IGNORABLE_404_STARTS 1032 List of compiled regular expression objects describing URLs that should be 1033 ignored when reporting HTTP 404 errors via email (see 1034 :doc:`/howto/error-reporting`). Use this if your site does not provide a 1035 commonly requested file such as ``favicon.ico`` or ``robots.txt``, or if it 1036 gets hammered by script kiddies. 1033 1037 1034 IGNORABLE_404_STARTS 1035 -------------------- 1038 This is only used if :setting:`SEND_BROKEN_LINK_EMAILS` is set to ``True`` and 1039 ``CommonMiddleware`` is installed (see :doc:`/topics/http/middleware`). 1036 1040 1037 Default: ``('/cgi-bin/', '/_vti_bin', '/_vti_inf')``1038 1039 A tuple of strings that specify beginnings of URLs that should be ignored by1040 the 404 emailer. See ``SEND_BROKEN_LINK_EMAILS``, ``IGNORABLE_404_ENDS`` and1041 the :doc:`/howto/error-reporting`.1042 1043 1041 .. setting:: INSTALLED_APPS 1044 1042 1045 1043 INSTALLED_APPS … … 1435 1433 Whether to send an email to the ``MANAGERS`` each time somebody visits a 1436 1434 Django-powered page that is 404ed with a non-empty referer (i.e., a broken 1437 1435 link). This is only used if ``CommonMiddleware`` is installed (see 1438 :doc:`/topics/http/middleware` . See also ``IGNORABLE_404_STARTS``,1439 ``IGNORABLE_404_ENDS`` and:doc:`/howto/error-reporting`.1436 :doc:`/topics/http/middleware`). See also ``IGNORABLE_404_URLS`` and 1437 :doc:`/howto/error-reporting`. 1440 1438 1441 1439 .. setting:: SERIALIZATION_MODULES 1442 1440 … … 2045 2043 This setting has been replaced by :setting:`USER` in 2046 2044 :setting:`DATABASES`. 2047 2045 2046 .. setting:: IGNORABLE_404_ENDS 2047 2048 IGNORABLE_404_ENDS 2049 ------------------ 2050 2051 .. deprecated:: 1.4 2052 This setting has been superseded by :setting:`IGNORABLE_404_URLS`. 2053 2054 .. setting:: IGNORABLE_404_STARTS 2055 2056 IGNORABLE_404_STARTS 2057 -------------------- 2058 2059 .. deprecated:: 1.4 2060 This setting has been superseded by :setting:`IGNORABLE_404_URLS`. 2061 2048 2062 .. setting:: TEST_DATABASE_CHARSET 2049 2063 2050 2064 TEST_DATABASE_CHARSET … … 2071 2085 .. deprecated:: 1.2 2072 2086 This setting has been replaced by :setting:`TEST_NAME` in 2073 2087 :setting:`DATABASES`. 2074 -
tests/regressiontests/middleware/tests.py
1 1 # -*- coding: utf-8 -*- 2 2 3 import re 4 3 5 from django.conf import settings 6 from django.core import mail 4 7 from django.http import HttpRequest 5 8 from django.middleware.common import CommonMiddleware 6 9 from django.middleware.http import ConditionalGetMiddleware … … 9 12 10 13 class CommonMiddlewareTest(TestCase): 11 14 def setUp(self): 12 self.slash = settings.APPEND_SLASH 13 self.www = settings.PREPEND_WWW 15 self.append_slash = settings.APPEND_SLASH 16 self.prepend_www = settings.PREPEND_WWW 17 self.ignorable_404_urls = settings.IGNORABLE_404_URLS 18 self.send_broken_email_links = settings.SEND_BROKEN_LINK_EMAILS 14 19 15 20 def tearDown(self): 16 settings.APPEND_SLASH = self.slash 17 settings.PREPEND_WWW = self.www 21 settings.APPEND_SLASH = self.append_slash 22 settings.PREPEND_WWW = self.prepend_www 23 settings.IGNORABLE_404_URLS = self.ignorable_404_urls 24 settings.SEND_BROKEN_LINK_EMAILS = self.send_broken_email_links 18 25 19 26 def _get_request(self, path): 20 27 request = HttpRequest() … … 249 256 self.assertEqual(r['Location'], 250 257 'http://www.testserver/middleware/customurlconf/slash/') 251 258 259 # Tests for the 404 error reporting via email 260 261 def test_404_error_reporting(self): 262 settings.IGNORABLE_404_URLS = (re.compile(r'foo'),) 263 settings.SEND_BROKEN_LINK_EMAILS = True 264 request = self._get_request('regular_url/that/does/not/exist') 265 request.META['HTTP_REFERER'] = '/another/url/' 266 response = self.client.get(request.path) 267 CommonMiddleware().process_response(request, response) 268 self.assertEqual(len(mail.outbox), 1) 269 self.assertIn('Broken', mail.outbox[0].subject) 270 271 def test_404_error_reporting_no_referer(self): 272 settings.IGNORABLE_404_URLS = (re.compile(r'foo'),) 273 settings.SEND_BROKEN_LINK_EMAILS = True 274 request = self._get_request('regular_url/that/does/not/exist') 275 response = self.client.get(request.path) 276 CommonMiddleware().process_response(request, response) 277 self.assertEqual(len(mail.outbox), 0) 278 279 def test_404_error_reporting_ignored_url(self): 280 settings.IGNORABLE_404_URLS = (re.compile(r'foo'),) 281 settings.SEND_BROKEN_LINK_EMAILS = True 282 request = self._get_request('foo_url/that/does/not/exist/either') 283 request.META['HTTP_REFERER'] = '/another/url/' 284 response = self.client.get(request.path) 285 CommonMiddleware().process_response(request, response) 286 self.assertEqual(len(mail.outbox), 0) 287 288 252 289 class ConditionalGetMiddlewareTest(TestCase): 253 290 urls = 'regressiontests.middleware.cond_get_urls' 254 291 def setUp(self): -
django/conf/global_settings.py
246 246 # is an admin. 247 247 ADMIN_FOR = () 248 248 249 # 404s that may be ignored. 250 IGNORABLE_404_STARTS = ('/cgi-bin/', '/_vti_bin', '/_vti_inf') 251 IGNORABLE_404_ENDS = ('mail.pl', 'mailform.pl', 'mail.cgi', 'mailform.cgi', 'favicon.ico', '.php') 249 # List of compiled regular expression objects representing URLs that need not 250 # be reported when SEND_BROKEN_LINK_EMAILS is True. Here is are a few examples: 251 # import re 252 # IGNORABLE_404_URLS = ( 253 # re.compile(r'^/apple-touch-icon.*\.png$'), 254 # re.compile(r'^/favicon.ico$), 255 # re.compile(r'^/robots.txt$), 256 # re.compile(r'^/phpmyadmin/), 257 # re.compile(r'\.(cgi|php|pl)$'), 258 # ) 259 IGNORABLE_404_URLS = () 252 260 253 261 # A secret key for this particular Django installation. Used in secret-key 254 262 # hashing algorithms. Set this in your settings, or Django will complain -
django/middleware/common.py
127 127 """ 128 128 Returns True if a 404 at the given URL *shouldn't* notify the site managers. 129 129 """ 130 for start in settings.IGNORABLE_404_STARTS: 131 if uri.startswith(start): 132 return True 133 for end in settings.IGNORABLE_404_ENDS: 134 if uri.endswith(end): 135 return True 136 return False 130 return any(pattern.search(uri) for pattern in settings.IGNORABLE_404_URLS) 137 131 138 132 def _is_internal_request(domain, referer): 139 133 """