attempt to fix issue 981

This commit is contained in:
mdipierro
2012-10-01 21:11:45 -05:00
parent 606daabdd3
commit f79660dd2a
3 changed files with 31 additions and 26 deletions
+1 -1
View File
@@ -1 +1 @@
Version 2.0.9 (2012-10-01 20:51:51) dev Version 2.0.9 (2012-10-01 21:11:37) dev
+2 -1
View File
@@ -329,7 +329,8 @@ class Response(Storage):
stream_file_or_304_or_206(stream, stream_file_or_304_or_206(stream,
chunk_size=chunk_size, chunk_size=chunk_size,
request=request, request=request,
headers=headers) headers=headers,
status=self.status)
# ## the following is for backward compatibility # ## the following is for backward compatibility
if hasattr(stream, 'name'): if hasattr(stream, 'name'):
+28 -24
View File
@@ -43,6 +43,7 @@ def stream_file_or_304_or_206(
chunk_size = DEFAULT_CHUNK_SIZE, chunk_size = DEFAULT_CHUNK_SIZE,
request = None, request = None,
headers = {}, headers = {},
status = 200,
error_message = None, error_message = None,
): ):
if error_message is None: if error_message is None:
@@ -67,30 +68,33 @@ def stream_file_or_304_or_206(
headers.setdefault('Pragma', 'cache') headers.setdefault('Pragma', 'cache')
headers.setdefault('Cache-Control', 'private') headers.setdefault('Cache-Control', 'private')
if request and request.env.http_if_modified_since == mtime: # if this is a normal response and not a respnse to an error page
raise HTTP(304, **{'Content-Type': headers['Content-Type']}) if status == 200:
if request and request.env.http_if_modified_since == mtime:
raise HTTP(304, **{'Content-Type': headers['Content-Type']})
elif request and request.env.http_range: elif request and request.env.http_range:
start_items = regex_start_range.findall(request.env.http_range) start_items = regex_start_range.findall(request.env.http_range)
if not start_items: if not start_items:
start_items = [0] start_items = [0]
stop_items = regex_stop_range.findall(request.env.http_range) stop_items = regex_stop_range.findall(request.env.http_range)
if not stop_items or int(stop_items[0]) > fsize - 1: if not stop_items or int(stop_items[0]) > fsize - 1:
stop_items = [fsize - 1] stop_items = [fsize - 1]
part = (int(start_items[0]), int(stop_items[0]), fsize) part = (int(start_items[0]), int(stop_items[0]), fsize)
bytes = part[1] - part[0] + 1 bytes = part[1] - part[0] + 1
try: try:
stream = open(static_file, 'rb') stream = open(static_file, 'rb')
except IOError, e: except IOError, e:
if e[0] in (errno.EISDIR, errno.EACCES): if e[0] in (errno.EISDIR, errno.EACCES):
raise HTTP(403) raise HTTP(403)
else: else:
raise HTTP(404) raise HTTP(404)
stream.seek(part[0]) stream.seek(part[0])
headers['Content-Range'] = 'bytes %i-%i/%i' % part headers['Content-Range'] = 'bytes %i-%i/%i' % part
headers['Content-Length'] = '%i' % bytes headers['Content-Length'] = '%i' % bytes
status = 206 status = 206
else: # in all the other cases (not 304, not 206, but 200 or error page)
if status != 206:
if 'gzip' in request.env.http_accept_encoding and\ if 'gzip' in request.env.http_accept_encoding and\
not 'Content-Encoding' in headers: not 'Content-Encoding' in headers:
gzipped = static_file + '.gz' gzipped = static_file + '.gz'
@@ -102,13 +106,13 @@ def stream_file_or_304_or_206(
try: try:
stream = open(static_file, 'rb') stream = open(static_file, 'rb')
except IOError, e: except IOError, e:
# this better does not happer when returning an error page ;-)
if e[0] in (errno.EISDIR, errno.EACCES): if e[0] in (errno.EISDIR, errno.EACCES):
raise HTTP(403) raise HTTP(403)
else: else:
raise HTTP(404) raise HTTP(404)
headers['Content-Length'] = fsize headers['Content-Length'] = fsize
bytes = None bytes = None
status = 200
if request and request.env.web2py_use_wsgi_file_wrapper: if request and request.env.web2py_use_wsgi_file_wrapper:
wrapped = request.env.wsgi_file_wrapper(stream, chunk_size) wrapped = request.env.wsgi_file_wrapper(stream, chunk_size)
else: else: