Opened 7 years ago

Closed 7 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: UI/UX:


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/      2011-03-02 10:40:48.000000000 +0000
+++ ENV/lib/python2.6/site-packages/django/views/      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 (5)

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 6 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

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