1
0
mirror of https://github.com/django/django.git synced 2025-07-03 17:29:12 +00:00

[soc2009/http-wsgi-improvements] Changes for `HttpResponseSendFile` support in FastCGI. Refs #2131.

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/http-wsgi-improvements@11319 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Chris Cahoon 2009-07-23 20:17:30 +00:00
parent c6322f0096
commit 9ac495af44
6 changed files with 26 additions and 12 deletions

View File

@ -240,9 +240,9 @@ MEDIA_URL = ''
# file with efficient handler-specific routines. None causes HttpResponseSendFile
# to fall back to, first, mechanisms in the handler (wsgi.filewrapper and
# req.sendfile.
# Examples: 'X-Sendfile' (FastCGI, lighttpd, Apache with mod_xsendfile),
# Examples: 'X-Sendfile' (lighttpd & Cherokee with FastCGI/SCGI, Apache with mod_xsendfile),
# 'X-Accel-Redirect' (nginx)
HTTPRESPONSE_SENDFILE_METHOD = None
HTTPRESPONSE_SENDFILE_HEADER = None
# List of upload handler classes to be applied in order.
FILE_UPLOAD_HANDLERS = (

View File

@ -204,9 +204,9 @@ class ModPythonHandler(BaseHandler):
req.sendfile(response.sendfile_filename)
else:
# If we are using a header to do sendfile, set the header and send empty content
if settings.RESPONSE_SENDFILE_METHOD:
if settings.RESPONSE_SENDFILE_HEADER:
response.set_empty_content()
response[settings.HTTPRESPONSE_SENDFILE_METHOD] = response.sendfile_filename
response[settings.HTTPRESPONSE_SENDFILE_HEADER] = response.sendfile_filename
for chunk in response:
req.write(chunk)
return 0 # mod_python.apache.OK

View File

@ -242,14 +242,21 @@ class WSGIHandler(base.BaseHandler):
response_headers.append(('Set-Cookie', str(c.output(header=''))))
if isinstance(response, http.HttpResponseSendFile):
if settings.HTTPRESPONSE_SENDFILE_METHOD:
response_headers.append((settings.HTTPRESPONSE_SENDFILE_METHOD,
response.sendfile_filename))
filename = response.sendfile_filename
if settings.HTTPRESPONSE_SENDFILE_HEADER:
response.set_empty_content()
response_headers.append((settings.HTTPRESPONSE_SENDFILE_HEADER,
filename))
elif 'wsgi.file_wrapper' in environ:
filelike = open(response.sendfile_filename, 'rb')
filelike = open(filename, 'rb')
return environ['wsgi.file_wrapper'](filelike,
response.block_size)
else:
import os.path
if not os.path.exists(filename):
raise Exception("Filename provided to HttpResponseSendFile does not exist.")
response_headers.append(('Content-Length',
str(os.path.getsize(filename))))
start_response(status, response_headers)
return response

View File

@ -444,7 +444,6 @@ class HttpResponseSendFile(HttpResponse):
super(HttpResponseSendFile, self).__init__('', content_type=content_type)
self.sendfile_filename = path_to_file
self.block_size = block_size
self['Content-Length'] = os.path.getsize(path_to_file)
self['Content-Disposition'] = ('attachment; filename=%s' %
os.path.basename(path_to_file))
self._empty_content = False

View File

@ -585,6 +585,16 @@ live in :mod:`django.http`.
optionally, the file's content type and block size hint for handlers that
need it.
If the setting ``HTTPRESPONSE_SENDFILE_HEADER`` is overridden (default None),
HttpResponseSendFile will return that response header set as the file name given.
If the file is unavailable, no content will be returned. Since certain servers
do not allow direct access to the file system, it is not feasible to verify
the file's existence beforehand.
Be very careful with this method. It provides access to the filesystem that
must be controlled, and performs no verification of a file's existence in most
cases.
Note that response middleware will be bypassed if you use
:class:`HttpResponseSendFile`.

View File

@ -18,8 +18,6 @@ class SendFileTests(TestCase):
urllib.quote(file1.name))
self.assertEqual(response.status_code, 200)
#self.assertEqual(response[settings.HTTPRESPONSE_SENDFILE_METHOD],
# file1.name)
self.assertEqual(response['Content-Disposition'],
'attachment; filename=%s' % os.path.basename(file1.name))
self.assertEqual(response['Content-Length'], str(FILE_SIZE))