Allow partial content on bytesio
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Ignasi Bosch · committed by David Lord · parent 4f3dbb3 · Browse files · 2 files·+27 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+27 added−1 removed
- tests:+18 added−0 removed
- other:+9 added−1 removed
Provenance
- Commit
b570bf6- Tree
9992125- Author
Ignasi Bosch <me@ignasibosch.com>- Committer
David Lord <davidism@gmail.com>- Parent
4f3dbb3
2 files changed+27 added−1 removed
modified:flask/helpers.py+9 added-1 removed
6 unmodified lines7 8 :copyright: © 2010 by the Pallets team.9 :license: BSD, see LICENSE for more details.10 """11-11+import io12 import os13 import socket14 import sys15 import pkgutil495 unmodified lines511 compatibility with WSGI servers.512 513 .. versionchanged:: 1.1514 Filenames may be a `PathLike` object.515+ .. versionadded:: 1.1516+ Partial content supports ``BytesIO``.517 518 :param filename_or_fp: the filename of the file to send.519 This is relative to the :attr:`~Flask.root_path`520 if a relative path is specified.80 unmodified lines601 file = open(filename, 'rb')602 mtime = os.path.getmtime(filename)603 fsize = os.path.getsize(filename)604 headers['Content-Length'] = fsize605+ elif isinstance(file, io.BytesIO):606+ try:607+ fsize = file.getbuffer().nbytes608+ except AttributeError:609+ fsize = len(file.getvalue())610+ headers['Content-Length'] = fsize611 data = wrap_file(request.environ, file)612 613 rv = current_app.response_class(data, mimetype=mimetype, headers=headers,614 direct_passthrough=True)451 unmodified linesmodified:tests/test_helpers.py+18 added
8 unmodified lines9 :license: BSD, see LICENSE for more details.10 """11 12 import datetime13+import io14 import os15 import uuid16 17 import pytest594 unmodified lines612 @pytest.mark.skipif(613 not callable(getattr(Range, 'to_content_range_header', None)),614 reason="not implemented within werkzeug"615 )616+ def test_send_file_range_request_bytesio(self, app, client):617+ @app.route('/')618+ def index():619+ file = io.BytesIO(b'somethingsomething')620+ return flask.send_file(621+ file, attachment_filename='filename', conditional=True622+ )623+624+ rv = client.get('/', headers={'Range': 'bytes=4-15'})625+ assert rv.status_code == 206626+ assert rv.data == b'somethingsomething'[4:16]627+ rv.close()628+629+ @pytest.mark.skipif(630+ not callable(getattr(Range, 'to_content_range_header', None)),631+ reason="not implemented within werkzeug"632+ )633 def test_send_file_range_request_xsendfile_invalid(self, app, client):634 # https://github.com/pallets/flask/issues/2526635 app.use_x_sendfile = True636 372 unmodified lines