Middleware using len() on response.content is gobbling generators
|Reported by:||FunkyBob||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
We recently found that a slow view (4 to 5 minutes) was made even worse because we couldn't give a generator to HttpResponse and have it still work.
This turned out to be because two of the built-in middlewares ( gzip and conditional-get ) assume the response.content is a string, and call len(response.content) -- which consumes the generator.
Once we bypassed these, the time to first response was seconds, and the entire process ran smoothly.
So, attached are two patches to work around this issue:
- In the case of conditional-get, it skips setting Content-Length header if not response._is_string.
- In the case of gzip, simply move the Content-Encoding test to the front, so the documentation saying gzip will be skipped on anything with a Content-Encoding header works as expected.