Ticket #4650: response-wrapping.patch

File response-wrapping.patch, 1.7 KB (added by (removed), 17 years ago)

Wrap the response object, firing the signal once the response object is fully consumed

  • django/core/handlers/wsgi.py

    === modified file 'django/core/handlers/wsgi.py'
     
    174174    REQUEST = property(_get_request)
    175175    raw_post_data = property(_get_raw_post_data)
    176176
     177class IterConsumedSignal(object):
     178
     179    def __iter__(self):
     180        return self
     181
     182    def next(self):
     183        dispatcher.send(signal=signals.request_finished)
     184        raise StopIteration()
     185IterConsumedSignal = IterConsumedSignal()
     186
     187class ResponseWrapper(object):
     188
     189    def __init__(self, response):
     190        self._response = response
     191        if hasattr(self._response, 'close'):
     192            self.close = self._response.close
     193
     194    def __iter__(self):
     195        assert self._response is not None
     196        try:
     197            if isinstance(self._response, basestring):
     198                return chain([self._response], IterConsumedSignal)
     199            return chain(self._response, IterConsumedSignal)
     200        finally:
     201            self._response = None
     202
     203
    177204class WSGIHandler(BaseHandler):
    178205    def __call__(self, environ, start_response):
    179206        from django.conf import settings
     
    192219            for middleware_method in self._response_middleware:
    193220                response = middleware_method(request, response)
    194221
    195         finally:
     222        except:
    196223            dispatcher.send(signal=signals.request_finished)
     224            raise
    197225
    198226        try:
    199227            status_text = STATUS_CODE_TEXT[response.status_code]
     
    204232        for c in response.cookies.values():
    205233            response_headers.append(('Set-Cookie', c.output(header='')))
    206234        start_response(status, response_headers)
    207         return response
     235        return ResponseWrapper(response)
Back to Top