Opened 13 years ago

Closed 13 years ago

Last modified 6 years ago

#15613 closed Bug (fixed)

django.views.static.serve gives incorrect Content-Length with non-regular files

Reported by: James Aylett Owned by: nobody
Component: Uncategorized Version: 1.3-beta
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Because of changes introduced related to #15281, Content-Length is currently calculated using statobj.st_size. This fails if serving from eg a named pipe, since st_size will be 0; user agents may then close the connection while Django is still sending the actual response data, causing a broken pipe in runserver and of course no useful data being provided to the user agent.

(Named pipes are useful during development if you're using anything that automatically preprocesses CSS or Javascript, for instance.)

The following tiny patch fixes things:

--- Django-1.3-rc-1/django/views/static.py      2011-03-02 10:40:48.000000000 +0000
+++ ENV/lib/python2.6/site-packages/django/views/static.py      2011-03-14 14:27:25.000000000 +0000
@@ -5,6 +5,7 @@
 
 import mimetypes
 import os
+import stat
 import posixpath
 import re
 import urllib
@@ -58,7 +59,8 @@
         return HttpResponseNotModified(mimetype=mimetype)
     response = HttpResponse(open(fullpath, 'rb').read(), mimetype=mimetype)
     response["Last-Modified"] = http_date(statobj.st_mtime)
-    response["Content-Length"] = statobj.st_size
+    if stat.S_ISREG(statobj.st_mode):
+        response["Content-Length"] = statobj.st_size
     if encoding:
         response["Content-Encoding"] = encoding
     return response

Change History (12)

comment:1 by Julien Phalip, 13 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Luke Plant, 13 years ago

Type: Bug

comment:3 by Luke Plant, 13 years ago

Severity: Normal

comment:4 by Andrew Godwin, 13 years ago

Resolution: fixed
Status: newclosed

In [16014]:

Fixed #15613: Don't send content-length headers for non-regular files. Thanks to jaylett.

comment:5 by Jacob, 12 years ago

milestone: 1.3

Milestone 1.3 deleted

comment:6 by Claude Paroz, 6 years ago

Easy pickings: unset
UI/UX: unset

Would have been great to have a test for that use case, so we'd know how to reproduce the issue. If anyone have an idea...

comment:7 by James Aylett, 6 years ago

For any Unixoid, you should be create a named pipe somewhere in a temporary location (although I suspect it won't be able to use secure filename creation), shove some data into it, then it should be pretty easy to drive serve() appropriately. I've no idea if Windows has an equivalent non-regular file that we'd want to test.

comment:8 by Claude Paroz, 6 years ago

Do you think you would be able to write a test for that, even if we skip it on Windows?

comment:9 by James Aylett, 6 years ago

Yes; I don't think it's particularly hard — but I can't guarantee when I'll have time to do it, sadly.

comment:10 by Claude Paroz, 6 years ago

I think I managed to create a test in that pull request.

comment:11 by James Aylett, 6 years ago

That's mostly good, except for a typo. I've opened a small PR to fix that.

in reply to:  11 comment:12 by Claude Paroz, 6 years ago

Replying to James Aylett:

That's mostly good, except for a typo. I've opened a small PR to fix that.

Thanks James. Pushed, but referenced #16470 instead.

Note: See TracTickets for help on using tickets.
Back to Top