Code

Ticket #5686: commonmiddleware.diff

File commonmiddleware.diff, 4.1 KB (added by keturn, 7 years ago)
Line 
1Index: django/middleware/common.py
2===================================================================
3--- django/middleware/common.py (revision 6453)
4+++ django/middleware/common.py (working copy)
5@@ -57,37 +57,62 @@
6 
7     def process_response(self, request, response):
8         "Check for a flat page (for 404s) and calculate the Etag, if needed."
9-        if response.status_code == 404:
10-            if settings.SEND_BROKEN_LINK_EMAILS:
11-                # If the referrer was from an internal link or a non-search-engine site,
12-                # send a note to the managers.
13-                domain = request.get_host()
14-                referer = request.META.get('HTTP_REFERER', None)
15-                is_internal = _is_internal_request(domain, referer)
16-                path = request.get_full_path()
17-                if referer and not _is_ignorable_404(path) and (is_internal or '?' not in referer):
18-                    ua = request.META.get('HTTP_USER_AGENT', '<none>')
19-                    ip = request.META.get('REMOTE_ADDR', '<none>')
20-                    mail_managers("Broken %slink on %s" % ((is_internal and 'INTERNAL ' or ''), domain),
21-                        "Referrer: %s\nRequested URL: %s\nUser agent: %s\nIP address: %s\n" \
22-                                  % (referer, request.get_full_path(), ua, ip))
23-                return response
24+        if response.status_code == 404 and settings.SEND_BROKEN_LINK_EMAILS:
25+            return self.process_response_404(request, response)
26 
27         # Use ETags, if requested.
28         if settings.USE_ETAGS:
29-            if response.has_header('ETag'):
30-                etag = response['ETag']
31-            else:
32-                etag = md5.new(response.content).hexdigest()
33-            if response.status_code >= 200 and response.status_code < 300 and request.META.get('HTTP_IF_NONE_MATCH') == etag:
34-                cookies = response.cookies
35-                response = http.HttpResponseNotModified()
36-                response.cookies = cookies
37-            else:
38-                response['ETag'] = etag
39+            return self.process_response_etags(request, response)
40 
41         return response
42 
43+
44+    @staticmethod
45+    def process_response_404(request, response):
46+        """Sends mail to managers.
47+
48+        Respects settings IGONRABLE_404_STARTS and IGNORABLE_404_ENDS.
49+        Requests without referrers and those with search engine
50+        referrers (i.e. those with '?' in them) are excluded.
51+        """
52+        domain = request.get_host()
53+        referer = request.META.get('HTTP_REFERER', None)
54+        is_internal = _is_internal_request(domain, referer)
55+        path = request.get_full_path()
56+        if (referer
57+            and not _is_ignorable_404(path)
58+            and (is_internal or '?' not in referer)):
59+            ua = request.META.get('HTTP_USER_AGENT', '<none>')
60+            ip = request.META.get('REMOTE_ADDR', '<none>')
61+            mail_managers(
62+                "Broken %slink on %s" % ((is_internal and 'INTERNAL ' or ''),
63+                                         domain),
64+                "Referrer: %s\nRequested URL: %s\n"
65+                "User agent: %s\nIP address: %s\n"
66+                % (referer, path, ua, ip))
67+        return response
68+
69+
70+    @staticmethod
71+    def process_response_etags(request, response):
72+        """Adds ETags to responses that do not have them,
73+        and sends ResponseNotModified when appropriate.
74+        """
75+        if response.has_header('ETag'):
76+            etag = response['ETag']
77+        else:
78+            etag = md5.new(response.content).hexdigest()
79+        if (response.status_code >= 200
80+            and response.status_code < 300
81+            and request.META.get('HTTP_IF_NONE_MATCH') == etag):
82+            cookies = response.cookies
83+            response = http.HttpResponseNotModified()
84+            response.cookies = cookies
85+        else:
86+            response['ETag'] = etag
87+        return response
88+
89+
90 def _is_ignorable_404(uri):
91     "Returns True if a 404 at the given URL *shouldn't* notify the site managers"
92     for start in settings.IGNORABLE_404_STARTS: