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

Fix #2943: Allow bytesio partial content
This commit is contained in:
David Lord 2019-01-07 07:02:02 -08:00 committed by GitHub
commit 4f32c6d4e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 9 deletions

View file

@ -10,15 +10,19 @@ Version 1.1
Unreleased Unreleased
- :meth:`flask.RequestContext.copy` includes the current session - :meth:`flask.RequestContext.copy` includes the current session
object in the request context copy. This prevents ``flask.session`` object in the request context copy. This prevents ``session``
pointing to an out-of-date object. (`#2935`) pointing to an out-of-date object. (`#2935`_)
- Using built-in RequestContext, unprintable Unicode characters in Host - Using built-in RequestContext, unprintable Unicode characters in
header will result in a HTTP 400 response and not HTTP 500 as previously. Host header will result in a HTTP 400 response and not HTTP 500 as
(`#2994`) previously. (`#2994`_)
- :func:`send_file` supports ``PathLike`` objects as describe in - :func:`send_file` supports :class:`~os.PathLike` objects as
PEP 0519, to support ``pathlib`` in Python 3. (`#3059`_) described in PEP 0519, to support :mod:`pathlib` in Python 3.
(`#3059`_)
- :func:`send_file` supports :class:`~io.BytesIO` partial content.
(`#2957`_)
.. _#2935: https://github.com/pallets/flask/issues/2935 .. _#2935: https://github.com/pallets/flask/issues/2935
.. _#2957: https://github.com/pallets/flask/issues/2957
.. _#2994: https://github.com/pallets/flask/pull/2994 .. _#2994: https://github.com/pallets/flask/pull/2994
.. _#3059: https://github.com/pallets/flask/pull/3059 .. _#3059: https://github.com/pallets/flask/pull/3059

View file

@ -8,7 +8,7 @@
:copyright: © 2010 by the Pallets team. :copyright: © 2010 by the Pallets team.
:license: BSD, see LICENSE for more details. :license: BSD, see LICENSE for more details.
""" """
import io
import os import os
import socket import socket
import sys import sys
@ -511,7 +511,10 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
compatibility with WSGI servers. compatibility with WSGI servers.
.. versionchanged:: 1.1 .. versionchanged:: 1.1
Filenames may be a `PathLike` object. Filename may be a :class:`~os.PathLike` object.
.. versionadded:: 1.1
Partial content supports :class:`~io.BytesIO`.
:param filename_or_fp: the filename of the file to send. :param filename_or_fp: the filename of the file to send.
This is relative to the :attr:`~Flask.root_path` This is relative to the :attr:`~Flask.root_path`
@ -600,6 +603,13 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
mtime = os.path.getmtime(filename) mtime = os.path.getmtime(filename)
fsize = os.path.getsize(filename) fsize = os.path.getsize(filename)
headers['Content-Length'] = fsize headers['Content-Length'] = fsize
elif isinstance(file, io.BytesIO):
try:
fsize = file.getbuffer().nbytes
except AttributeError:
# Python 2 doesn't have getbuffer
fsize = len(file.getvalue())
headers['Content-Length'] = fsize
data = wrap_file(request.environ, file) data = wrap_file(request.environ, file)
rv = current_app.response_class(data, mimetype=mimetype, headers=headers, rv = current_app.response_class(data, mimetype=mimetype, headers=headers,

View file

@ -10,6 +10,7 @@
""" """
import datetime import datetime
import io
import os import os
import uuid import uuid
@ -608,6 +609,19 @@ class TestSendfile(object):
assert rv.status_code == 200 assert rv.status_code == 200
rv.close() rv.close()
def test_send_file_range_request_bytesio(self, app, client):
@app.route('/')
def index():
file = io.BytesIO(b'somethingsomething')
return flask.send_file(
file, attachment_filename='filename', conditional=True
)
rv = client.get('/', headers={'Range': 'bytes=4-15'})
assert rv.status_code == 206
assert rv.data == b'somethingsomething'[4:16]
rv.close()
@pytest.mark.skipif( @pytest.mark.skipif(
not callable(getattr(Range, 'to_content_range_header', None)), not callable(getattr(Range, 'to_content_range_header', None)),
reason="not implemented within werkzeug" reason="not implemented within werkzeug"