Skip to content

Commit 76fdeaf

Browse files
committed
[soc2009/http-wsgi-improvements] Remove setting the Content-Length header for HttpResponseSendFile from the handler, for compatibility, and add a content attribute. Refs django#2131.
Also adds a _charset class attribute to HttpResponse so the children all have it. git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/http-wsgi-improvements@11326 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent be96986 commit 76fdeaf

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

django/core/handlers/wsgi.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,7 @@ def __call__(self, environ, start_response):
251251
filelike = open(filename, 'rb')
252252
return environ['wsgi.file_wrapper'](filelike,
253253
response.block_size)
254-
else:
255-
import os.path
256-
if not os.path.exists(filename):
257-
raise Exception("Filename provided to HttpResponseSendFile does not exist.")
258-
response_headers.append(('Content-Length',
259-
str(os.path.getsize(filename))))
254+
260255
start_response(status, response_headers)
261256
return response
262257

django/http/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ class HttpResponse(object):
272272

273273
_status_code = 200
274274
_codec = None
275+
_charset = None
275276

276277
def __init__(self, content='', mimetype=None, status=None,
277278
content_type=None, request=None):
@@ -446,6 +447,8 @@ def __init__(self, path_to_file, content_type=None, block_size=8192):
446447
self.block_size = block_size
447448
self['Content-Disposition'] = ('attachment; filename=%s' %
448449
os.path.basename(path_to_file))
450+
if not settings.HTTPRESPONSE_SENDFILE_HEADER and os.path.exists(path_to_file):
451+
self['Content-Length'] = str(os.path.getsize(path_to_file))
449452
self._empty_content = False
450453

451454
def set_empty_content(self):
@@ -457,6 +460,11 @@ def __iter__(self):
457460
from django.core.servers.basehttp import FileWrapper
458461
return FileWrapper(self.get_file_handler(), self.block_size)
459462

463+
def _get_content(self):
464+
return "".join(self.iter())
465+
466+
content = property(_get_content)
467+
460468
def get_file_handler(self):
461469
if not self.sendfile_fh:
462470
self.sendfile_fh = open(self.sendfile_filename, 'rb')

0 commit comments

Comments
 (0)