Code

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#15613 closed Bug (fixed)

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

Reported by: jaylett 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:

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

Attachments (0)

Change History (5)

comment:1 Changed 3 years ago by julien

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 3 years ago by lukeplant

  • Type set to Bug

comment:3 Changed 3 years ago by lukeplant

  • Severity set to Normal

comment:4 Changed 3 years ago by andrewgodwin

  • Resolution set to fixed
  • Status changed from new to closed

In [16014]:

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

comment:5 Changed 3 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.