Skip to content
Myr

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

2 files+27 added−1 removed

modified:flask/helpers.py+9 added-1 removed
6 unmodified lines
78 :copyright: © 2010 by the Pallets team.9 :license: BSD, see LICENSE for more details.10"""1111import io12import os13import socket14import sys15import pkgutil
495 unmodified lines
511 compatibility with WSGI servers.512513 .. versionchanged:: 1.1514 Filenames may be a `PathLike` object.515 .. versionadded:: 1.1516 Partial content supports ``BytesIO``.517518 :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 lines
601 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)612613 rv = current_app.response_class(data, mimetype=mimetype, headers=headers,614 direct_passthrough=True)
451 unmodified lines
modified:tests/test_helpers.py+18 added
8 unmodified lines
9 :license: BSD, see LICENSE for more details.10"""1112import datetime13import io14import os15import uuid1617import pytest
594 unmodified lines
612 @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 )623624 rv = client.get('/', headers={'Range': 'bytes=4-15'})625 assert rv.status_code == 206626 assert rv.data == b'somethingsomething'[4:16]627 rv.close()628629 @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