Opened 7 years ago

Closed 7 years ago

Last modified 3 weeks 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 (10)

comment:1 Changed 7 years ago by Julien Phalip

Triage Stage: UnreviewedAccepted

comment:2 Changed 7 years ago by Luke Plant

Type: Bug

comment:3 Changed 7 years ago by Luke Plant

Severity: Normal

comment:4 Changed 7 years ago by Andrew Godwin

Resolution: fixed
Status: newclosed

In [16014]:

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

comment:5 Changed 7 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

comment:6 Changed 3 weeks ago by Claude Paroz

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 Changed 3 weeks ago by James Aylett

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 Changed 3 weeks ago by Claude Paroz

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

comment:9 Changed 3 weeks ago by James Aylett

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 Changed 3 weeks ago by Claude Paroz

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

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