Ticket #5898: http_requirements.diff

File http_requirements.diff, 4.2 KB (added by arien <regexbot@…>, 17 years ago)

Try to ensure responses meet basic HTTP requirements.

  • django/core/handlers/base.py

     
    5050
    5151    def get_response(self, request):
    5252        "Returns an HttpResponse object for the given HttpRequest"
    53         response = self._real_get_response(request)
    54         return fix_location_header(request, response)
    55 
    56     def _real_get_response(self, request):
    5753        from django.core import exceptions, urlresolvers
    5854        from django.core.mail import mail_admins
    5955        from django.conf import settings
     
    129125                callback, param_dict = resolver.resolve500()
    130126                return callback(request, **param_dict)
    131127
     128    def fix_up_response(self, request, response):
     129        "Fix up a response to try and meet basic HTTP requirements."
     130        fixes = (fix_location_header, remove_unallowed_response_content)
     131        for fix in fixes:
     132            response = fix(request, response)
     133        return response
     134
    132135    def _get_traceback(self, exc_info=None):
    133136        "Helper function to return the traceback as a string"
    134137        import traceback
     
    146149        response['Location'] = request.build_absolute_uri(response['Location'])
    147150    return response
    148151
     152def remove_unallowed_response_content(request, response):
     153    """
     154    Removes the content of responses to HEAD requests, and of responses with
     155    status codes 1xx (Informational), 204 (No Content), and 304 (Not Modified)
     156    that must not have content.  This is required by RFC 2616, section 4.3.
     157    """
     158    if 100 <= response.status_code < 200 or response.status_code in (204, 304):
     159        response.content = ''
     160        response['Content-Length'] = 0
     161    if request.method == 'HEAD':
     162        response.content = ''
     163    return response
  • django/core/handlers/modpython.py

     
    162162                # Apply response middleware
    163163                for middleware_method in self._response_middleware:
    164164                    response = middleware_method(request, response)
     165                response = self.fix_up_response(request, response)
    165166        finally:
    166167            dispatcher.send(signal=signals.request_finished)
    167168
  • django/core/handlers/wsgi.py

     
    207207                # Apply response middleware
    208208                for middleware_method in self._response_middleware:
    209209                    response = middleware_method(request, response)
     210                response = self.fix_up_response(request, response)
    210211        finally:
    211212            dispatcher.send(signal=signals.request_finished)
    212213
  • django/middleware/http.py

     
    66    Last-Modified header, and the request has If-None-Match or
    77    If-Modified-Since, the response is replaced by an HttpNotModified.
    88
    9     Removes the content from any response to a HEAD request.
    10 
    119    Also sets the Date and Content-Length response-headers.
    1210    """
    1311    def process_response(self, request, response):
     
    1917            if_none_match = request.META.get('HTTP_IF_NONE_MATCH', None)
    2018            if if_none_match == response['ETag']:
    2119                response.status_code = 304
    22                 response.content = ''
    23                 response['Content-Length'] = '0'
    2420
    2521        if response.has_header('Last-Modified'):
    2622            if_modified_since = request.META.get('HTTP_IF_MODIFIED_SINCE', None)
    2723            if if_modified_since == response['Last-Modified']:
    2824                response.status_code = 304
    29                 response.content = ''
    30                 response['Content-Length'] = '0'
    3125
    32         if request.method == 'HEAD':
    33             response.content = ''
    34 
    3526        return response
    3627
    3728class SetRemoteAddrFromForwardedFor(object):
Back to Top