This is for 1.8 middleware, same method can be applied to 1.10
It's not very clean, but from your middleware you can return a new
response object. The new object would also be a streaming response,
but for each chunk it emits (by calling the generator on the original
response), it would also count the size of the chunk and store a
rolling sum on the response object. After the final chunk is emitted,
your logging middleware can then be called with the resulting value.
Something like:
class WrappedStreamingResponse(StreamingHttpResponse):
def __init__(self, orig_response, middleware):
self.orig_response = orig_response
self.middleware = middleware
self.response_length = 0
super(WrappedStreamingResponse, self).__init__(self.chunks())
def chunks(self):
for chunk in self.orig_response.streaming_content:
self.response_length += len(chunk)
yield chunk
self.middleware.log_response_size(self.orig_response, self.response_length)
class MeasureMiddleware:
def process_response(self, request, response):
if response.streaming:
return WrappedStreamingResponse(response, self)
else:
self.log_response_size(response, len(response.content))
def log_response_size(self, response, length):
pass
Cheers
Tom
PS: 1 big caveat for this is that if a middleware class returns a new
response, middleware classes further down the chain are not called at
all, so StreamingHttpResponse would not get handled by the same
middleware as a regular HttpResponse.