summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcel Hellkamp <marc@gsites.de>2018-02-14 12:16:06 +0100
committerMarcel Hellkamp <marc@gsites.de>2018-02-14 12:16:06 +0100
commit063b8e89465ade5c056fcde34315aa12b766b177 (patch)
treeb5be955331a82ea0c91ccd0bde1182f0b4a4a947
parent245386e4cb45320047f2e3f9f1912d3f7e1eef42 (diff)
downloadbottle-063b8e89465ade5c056fcde34315aa12b766b177.tar.gz
Make closing the file in _file_iter_range() optional.
-rwxr-xr-xbottle.py10
1 files changed, 6 insertions, 4 deletions
diff --git a/bottle.py b/bottle.py
index 96e94c3..7ef0ff8 100755
--- a/bottle.py
+++ b/bottle.py
@@ -2808,8 +2808,9 @@ def redirect(url, code=None):
raise res
-def _file_iter_range(fp, offset, bytes, maxread=1024 * 1024):
- """ Yield chunks from a range in a file. No chunk is bigger than maxread."""
+def _file_iter_range(fp, offset, bytes, maxread=1024 * 1024, close=False):
+ """ Yield chunks from a range in a file, optionally closing it at the end.
+ No chunk is bigger than maxread. """
fp.seek(offset)
while bytes > 0:
part = fp.read(min(bytes, maxread))
@@ -2817,7 +2818,8 @@ def _file_iter_range(fp, offset, bytes, maxread=1024 * 1024):
break
bytes -= len(part)
yield part
- fp.close()
+ if close:
+ fp.close()
def static_file(filename, root,
@@ -2920,7 +2922,7 @@ def static_file(filename, root,
offset, end = ranges[0]
headers["Content-Range"] = "bytes %d-%d/%d" % (offset, end - 1, clen)
headers["Content-Length"] = str(end - offset)
- if body: body = _file_iter_range(body, offset, end - offset)
+ if body: body = _file_iter_range(body, offset, end - offset, close=True)
return HTTPResponse(body, status=206, **headers)
return HTTPResponse(body, **headers)