attempt to fix issue 981

This commit is contained in:
mdipierro
2012-10-01 21:11:45 -05:00
parent f4595ced2d
commit e8702e757e
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,
chunk_size=chunk_size,
request=request,
headers=headers)
headers=headers,
status=self.status)
# ## the following is for backward compatibility
if hasattr(stream, 'name'):
+28 -24
View File
@@ -43,6 +43,7 @@ def stream_file_or_304_or_206(
chunk_size = DEFAULT_CHUNK_SIZE,
request = None,
headers = {},
status = 200,
error_message = None,
):
if error_message is None:
@@ -67,30 +68,33 @@ def stream_file_or_304_or_206(
headers.setdefault('Pragma', 'cache')
headers.setdefault('Cache-Control', 'private')
if request and request.env.http_if_modified_since == mtime:
raise HTTP(304, **{'Content-Type': headers['Content-Type']})
# if this is a normal response and not a respnse to an error page
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:
start_items = regex_start_range.findall(request.env.http_range)
if not start_items:
start_items = [0]
stop_items = regex_stop_range.findall(request.env.http_range)
if not stop_items or int(stop_items[0]) > fsize - 1:
stop_items = [fsize - 1]
part = (int(start_items[0]), int(stop_items[0]), fsize)
bytes = part[1] - part[0] + 1
try:
stream = open(static_file, 'rb')
except IOError, e:
if e[0] in (errno.EISDIR, errno.EACCES):
raise HTTP(403)
else:
raise HTTP(404)
stream.seek(part[0])
headers['Content-Range'] = 'bytes %i-%i/%i' % part
headers['Content-Length'] = '%i' % bytes
status = 206
else:
elif request and request.env.http_range:
start_items = regex_start_range.findall(request.env.http_range)
if not start_items:
start_items = [0]
stop_items = regex_stop_range.findall(request.env.http_range)
if not stop_items or int(stop_items[0]) > fsize - 1:
stop_items = [fsize - 1]
part = (int(start_items[0]), int(stop_items[0]), fsize)
bytes = part[1] - part[0] + 1
try:
stream = open(static_file, 'rb')
except IOError, e:
if e[0] in (errno.EISDIR, errno.EACCES):
raise HTTP(403)
else:
raise HTTP(404)
stream.seek(part[0])
headers['Content-Range'] = 'bytes %i-%i/%i' % part
headers['Content-Length'] = '%i' % bytes
status = 206
# 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\
not 'Content-Encoding' in headers:
gzipped = static_file + '.gz'
@@ -102,13 +106,13 @@ def stream_file_or_304_or_206(
try:
stream = open(static_file, 'rb')
except IOError, e:
# this better does not happer when returning an error page ;-)
if e[0] in (errno.EISDIR, errno.EACCES):
raise HTTP(403)
else:
raise HTTP(404)
headers['Content-Length'] = fsize
bytes = None
status = 200
if request and request.env.web2py_use_wsgi_file_wrapper:
wrapped = request.env.wsgi_file_wrapper(stream, chunk_size)
else: