Skip to content
Myr

Merge pull request #2957 from IgnasiBosch/2943-bytesio-partial-content

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix #2943: Allow bytesio partial content

authored by David Lord · committed by GitHub · parents 4f3dbb3, 366f3f4 · Browse files · 3 files+37 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+37 added−9 removed

  • tests:+14 added−0 removed
  • other:+23 added−9 removed

3 files+37 added−9 removed

modified:flask/helpers.py+12 added-2 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
494 unmodified lines
510 Filenames are encoded with ASCII instead of Latin-1 for broader511 compatibility with WSGI servers.512513 .. versionchanged:: 1.1514 Filenames may be a `PathLike` object.514 Filename may be a :class:`~os.PathLike` object.515516 .. versionadded:: 1.1517 Partial content supports :class:`~io.BytesIO`.518519 :param filename_or_fp: the filename of the file to send.520 This is relative to the :attr:`~Flask.root_path`521 if a relative path is specified.522 Alternatively a file object might be provided in
79 unmodified lines
602 file = open(filename, 'rb')603 mtime = os.path.getmtime(filename)604 fsize = os.path.getsize(filename)605 headers['Content-Length'] = fsize606 elif isinstance(file, io.BytesIO):607 try:608 fsize = file.getbuffer().nbytes609 except AttributeError:610 # Python 2 doesn't have getbuffer611 fsize = len(file.getvalue())612 headers['Content-Length'] = fsize613 data = wrap_file(request.environ, file)614615 rv = current_app.response_class(data, mimetype=mimetype, headers=headers,616 direct_passthrough=True)
451 unmodified lines
modified:tests/test_helpers.py+14 added
8 unmodified lines
9 :license: BSD, see LICENSE for more details.10"""1112import datetime13import io14import os15import uuid1617import pytest
590 unmodified lines
608 datetime.datetime(1999, 1, 1))})609 assert rv.status_code == 200610 rv.close()611612 def test_send_file_range_request_bytesio(self, app, client):613 @app.route('/')614 def index():615 file = io.BytesIO(b'somethingsomething')616 return flask.send_file(617 file, attachment_filename='filename', conditional=True618 )619620 rv = client.get('/', headers={'Range': 'bytes=4-15'})621 assert rv.status_code == 206622 assert rv.data == b'somethingsomething'[4:16]623 rv.close()624625 @pytest.mark.skipif(626 not callable(getattr(Range, 'to_content_range_header', None)),627 reason="not implemented within werkzeug"628 )
376 unmodified lines
modified:CHANGES.rst+11 added-7 removed
8 unmodified lines
910Unreleased1112- :meth:`flask.RequestContext.copy` includes the current session13 object in the request context copy. This prevents ``flask.session``14 pointing to an out-of-date object. (`#2935`)15- Using built-in RequestContext, unprintable Unicode characters in Host16 header will result in a HTTP 400 response and not HTTP 500 as previously.17 (`#2994`)18- :func:`send_file` supports ``PathLike`` objects as describe in19 PEP 0519, to support ``pathlib`` in Python 3. (`#3059`_)13 object in the request context copy. This prevents ``session``14 pointing to an out-of-date object. (`#2935`_)15- Using built-in RequestContext, unprintable Unicode characters in16 Host header will result in a HTTP 400 response and not HTTP 500 as17 previously. (`#2994`_)18- :func:`send_file` supports :class:`~os.PathLike` objects as19 described in PEP 0519, to support :mod:`pathlib` in Python 3.20 (`#3059`_)21- :func:`send_file` supports :class:`~io.BytesIO` partial content.22 (`#2957`_)2324.. _#2935: https://github.com/pallets/flask/issues/293525.. _#2957: https://github.com/pallets/flask/issues/295726.. _#2994: https://github.com/pallets/flask/pull/299427.. _#3059: https://github.com/pallets/flask/pull/30592829
931 unmodified lines