Ticket #5686: commonmiddleware.diff

File commonmiddleware.diff, 4.1 KB (added by keturn, 7 years ago)
  • django/middleware/common.py

     
    5757
    5858    def process_response(self, request, response):
    5959        "Check for a flat page (for 404s) and calculate the Etag, if needed."
    60         if response.status_code == 404:
    61             if settings.SEND_BROKEN_LINK_EMAILS:
    62                 # If the referrer was from an internal link or a non-search-engine site,
    63                 # send a note to the managers.
    64                 domain = request.get_host()
    65                 referer = request.META.get('HTTP_REFERER', None)
    66                 is_internal = _is_internal_request(domain, referer)
    67                 path = request.get_full_path()
    68                 if referer and not _is_ignorable_404(path) and (is_internal or '?' not in referer):
    69                     ua = request.META.get('HTTP_USER_AGENT', '<none>')
    70                     ip = request.META.get('REMOTE_ADDR', '<none>')
    71                     mail_managers("Broken %slink on %s" % ((is_internal and 'INTERNAL ' or ''), domain),
    72                         "Referrer: %s\nRequested URL: %s\nUser agent: %s\nIP address: %s\n" \
    73                                   % (referer, request.get_full_path(), ua, ip))
    74                 return response
     60        if response.status_code == 404 and settings.SEND_BROKEN_LINK_EMAILS:
     61            return self.process_response_404(request, response)
    7562
    7663        # Use ETags, if requested.
    7764        if settings.USE_ETAGS:
    78             if response.has_header('ETag'):
    79                 etag = response['ETag']
    80             else:
    81                 etag = md5.new(response.content).hexdigest()
    82             if response.status_code >= 200 and response.status_code < 300 and request.META.get('HTTP_IF_NONE_MATCH') == etag:
    83                 cookies = response.cookies
    84                 response = http.HttpResponseNotModified()
    85                 response.cookies = cookies
    86             else:
    87                 response['ETag'] = etag
     65            return self.process_response_etags(request, response)
    8866
    8967        return response
    9068
     69
     70    @staticmethod
     71    def process_response_404(request, response):
     72        """Sends mail to managers.
     73
     74        Respects settings IGONRABLE_404_STARTS and IGNORABLE_404_ENDS.
     75        Requests without referrers and those with search engine
     76        referrers (i.e. those with '?' in them) are excluded.
     77        """
     78        domain = request.get_host()
     79        referer = request.META.get('HTTP_REFERER', None)
     80        is_internal = _is_internal_request(domain, referer)
     81        path = request.get_full_path()
     82        if (referer
     83            and not _is_ignorable_404(path)
     84            and (is_internal or '?' not in referer)):
     85            ua = request.META.get('HTTP_USER_AGENT', '<none>')
     86            ip = request.META.get('REMOTE_ADDR', '<none>')
     87            mail_managers(
     88                "Broken %slink on %s" % ((is_internal and 'INTERNAL ' or ''),
     89                                         domain),
     90                "Referrer: %s\nRequested URL: %s\n"
     91                "User agent: %s\nIP address: %s\n"
     92                % (referer, path, ua, ip))
     93        return response
     94
     95
     96    @staticmethod
     97    def process_response_etags(request, response):
     98        """Adds ETags to responses that do not have them,
     99        and sends ResponseNotModified when appropriate.
     100        """
     101        if response.has_header('ETag'):
     102            etag = response['ETag']
     103        else:
     104            etag = md5.new(response.content).hexdigest()
     105        if (response.status_code >= 200
     106            and response.status_code < 300
     107            and request.META.get('HTTP_IF_NONE_MATCH') == etag):
     108            cookies = response.cookies
     109            response = http.HttpResponseNotModified()
     110            response.cookies = cookies
     111        else:
     112            response['ETag'] = etag
     113        return response
     114
     115
    91116def _is_ignorable_404(uri):
    92117    "Returns True if a 404 at the given URL *shouldn't* notify the site managers"
    93118    for start in settings.IGNORABLE_404_STARTS:
Back to Top