Reproducible by adding
{{{
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
}}}
to the urls.py an call a file in that path.
The Response status is 200 and the content lenght is transmitted
correctly.
In fact, only 0 bytes transferred.
A possible fix is, to change the line to
{{{
response = HttpResponse(''.join(iter(lambda:
f.read(STREAM_CHUNK_SIZE), '')), mimetype=mimetype)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24158>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* type: Uncategorized => Bug
* needs_tests: => 0
* needs_docs: => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/24158#comment:1>
* component: Generic views => Core (Other)
Comment:
I can't reproduce this problem on Python 2.7; serving files seems to work
just fine. What version of Python are you using? Are you using a browser
to request the files or something else?
--
Ticket URL: <https://code.djangoproject.com/ticket/24158#comment:2>
* type: Bug => Cleanup/optimization
* component: Core (Other) => Documentation
* stage: Unreviewed => Accepted
Comment:
Actually, it seems likely that this is caused by one of your middlewares
consuming the iterator prematurely, see the
[https://docs.djangoproject.com/en/dev/releases/1.5/#explicit-support-for-
streaming-responses 1.5 release notes]. We could document this caveat in
the 1.4.18 release notes if it seems appropriate. Your proposed change
would negate the benefit of the security fix.
--
Ticket URL: <https://code.djangoproject.com/ticket/24158#comment:3>
Comment (by TheRealEckat):
Replying to [comment:2 timgraham]:
> I can't reproduce this problem on Python 2.7; serving files seems to
work just fine. What version of Python are you using? Are you using a
browser to request the files or something else?
I use python 2.7.
A colleague can reproduce this problem.
I located a potential problem with the middlewares, but I commented them
one by one out.
After first middleware, the length od the response is always zero.
The first two one in my settings are custom middlewares, but the third is
the django CsrfViewMiddleware. This middleware does the same, the response
length is then zero.
Thanks for the hint with the documentation of django 1.5, I'll check it on
monday.
--
Ticket URL: <https://code.djangoproject.com/ticket/24158#comment:4>
Comment (by TheRealEckat):
UPDATE:
The problem is located in django.middleware.gzip.GZipMiddleware.
If I comment out the gzip middleware, the output is looking good.
--
Ticket URL: <https://code.djangoproject.com/ticket/24158#comment:5>
Comment (by kilrogg):
I can reproduce this.
Using the GZipMiddleware on 1.4.18 and the static.serve view results in
all served files having empty content.
Curiously it works for serving static files via static files contrib app
which uses said view as well.
In 1.5 GZipMiddleware was changed to check for Streaming response, same
could be backported to 1.4 checking response content for GeneratorType.
--
Ticket URL: <https://code.djangoproject.com/ticket/24158#comment:6>
* type: Cleanup/optimization => Bug
* component: Documentation => HTTP handling
Comment:
I opened Pull request #3987 with a fix for GZipMiddleware:
https://github.com/django/django/pull/3987
I backported django.utils.text.compress_sequence and some of the newer
GZipMiddleware code to 1.4 to check for response._base_content_is_iter
before consuming response.content and added handling of iterables in
responses.
This bug also happens when returning HTTPResponse(iter(...)) in any view.
Is this a feasible solution? Was it okay to recategorize this ticket?
This is my first django ticket I'm working on, so any feedback is very
welcome.
--
Ticket URL: <https://code.djangoproject.com/ticket/24158#comment:7>
* owner: nobody => timgraham
* status: new => assigned
* has_patch: 0 => 1
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/24158#comment:8>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"1e39d0f6280abf34c7719db5e7ed1c333f5e5919"]:
{{{
#!CommitTicketReference repository=""
revision="1e39d0f6280abf34c7719db5e7ed1c333f5e5919"
[1.4.x] Fixed #24158 -- Allowed GZipMiddleware to work with streaming
responses
Backport of django.utils.text.compress_sequence and fix for
django.middleware.gzip.GZipMiddleware when using iterators as
response.content.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24158#comment:9>