mirror of
https://github.com/django/django.git
synced 2025-07-04 01:39:20 +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:
parent
c6322f0096
commit
9ac495af44
@ -240,9 +240,9 @@ MEDIA_URL = ''
|
|||||||
# file with efficient handler-specific routines. None causes HttpResponseSendFile
|
# file with efficient handler-specific routines. None causes HttpResponseSendFile
|
||||||
# to fall back to, first, mechanisms in the handler (wsgi.filewrapper and
|
# to fall back to, first, mechanisms in the handler (wsgi.filewrapper and
|
||||||
# req.sendfile.
|
# 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)
|
# 'X-Accel-Redirect' (nginx)
|
||||||
HTTPRESPONSE_SENDFILE_METHOD = None
|
HTTPRESPONSE_SENDFILE_HEADER = None
|
||||||
|
|
||||||
# List of upload handler classes to be applied in order.
|
# List of upload handler classes to be applied in order.
|
||||||
FILE_UPLOAD_HANDLERS = (
|
FILE_UPLOAD_HANDLERS = (
|
||||||
|
@ -204,9 +204,9 @@ class ModPythonHandler(BaseHandler):
|
|||||||
req.sendfile(response.sendfile_filename)
|
req.sendfile(response.sendfile_filename)
|
||||||
else:
|
else:
|
||||||
# If we are using a header to do sendfile, set the header and send empty content
|
# 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.set_empty_content()
|
||||||
response[settings.HTTPRESPONSE_SENDFILE_METHOD] = response.sendfile_filename
|
response[settings.HTTPRESPONSE_SENDFILE_HEADER] = response.sendfile_filename
|
||||||
for chunk in response:
|
for chunk in response:
|
||||||
req.write(chunk)
|
req.write(chunk)
|
||||||
return 0 # mod_python.apache.OK
|
return 0 # mod_python.apache.OK
|
||||||
|
@ -242,14 +242,21 @@ class WSGIHandler(base.BaseHandler):
|
|||||||
response_headers.append(('Set-Cookie', str(c.output(header=''))))
|
response_headers.append(('Set-Cookie', str(c.output(header=''))))
|
||||||
|
|
||||||
if isinstance(response, http.HttpResponseSendFile):
|
if isinstance(response, http.HttpResponseSendFile):
|
||||||
if settings.HTTPRESPONSE_SENDFILE_METHOD:
|
filename = response.sendfile_filename
|
||||||
response_headers.append((settings.HTTPRESPONSE_SENDFILE_METHOD,
|
if settings.HTTPRESPONSE_SENDFILE_HEADER:
|
||||||
response.sendfile_filename))
|
response.set_empty_content()
|
||||||
|
response_headers.append((settings.HTTPRESPONSE_SENDFILE_HEADER,
|
||||||
|
filename))
|
||||||
elif 'wsgi.file_wrapper' in environ:
|
elif 'wsgi.file_wrapper' in environ:
|
||||||
filelike = open(response.sendfile_filename, 'rb')
|
filelike = open(filename, 'rb')
|
||||||
return environ['wsgi.file_wrapper'](filelike,
|
return environ['wsgi.file_wrapper'](filelike,
|
||||||
response.block_size)
|
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)
|
start_response(status, response_headers)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
@ -444,7 +444,6 @@ class HttpResponseSendFile(HttpResponse):
|
|||||||
super(HttpResponseSendFile, self).__init__('', content_type=content_type)
|
super(HttpResponseSendFile, self).__init__('', content_type=content_type)
|
||||||
self.sendfile_filename = path_to_file
|
self.sendfile_filename = path_to_file
|
||||||
self.block_size = block_size
|
self.block_size = block_size
|
||||||
self['Content-Length'] = os.path.getsize(path_to_file)
|
|
||||||
self['Content-Disposition'] = ('attachment; filename=%s' %
|
self['Content-Disposition'] = ('attachment; filename=%s' %
|
||||||
os.path.basename(path_to_file))
|
os.path.basename(path_to_file))
|
||||||
self._empty_content = False
|
self._empty_content = False
|
||||||
|
@ -585,6 +585,16 @@ live in :mod:`django.http`.
|
|||||||
optionally, the file's content type and block size hint for handlers that
|
optionally, the file's content type and block size hint for handlers that
|
||||||
need it.
|
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
|
Note that response middleware will be bypassed if you use
|
||||||
:class:`HttpResponseSendFile`.
|
:class:`HttpResponseSendFile`.
|
||||||
|
|
||||||
|
@ -18,8 +18,6 @@ class SendFileTests(TestCase):
|
|||||||
urllib.quote(file1.name))
|
urllib.quote(file1.name))
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
#self.assertEqual(response[settings.HTTPRESPONSE_SENDFILE_METHOD],
|
|
||||||
# file1.name)
|
|
||||||
self.assertEqual(response['Content-Disposition'],
|
self.assertEqual(response['Content-Disposition'],
|
||||||
'attachment; filename=%s' % os.path.basename(file1.name))
|
'attachment; filename=%s' % os.path.basename(file1.name))
|
||||||
self.assertEqual(response['Content-Length'], str(FILE_SIZE))
|
self.assertEqual(response['Content-Length'], str(FILE_SIZE))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user