Browse Source

Remove deprecation warnings for add_etags & mimetype guessing for send_file()

Fix #1849
pull/1882/head
Dan Sully 9 years ago committed by Markus Unterwaditzer
parent
commit
8458cc5cd1
  1. 1
      AUTHORS
  2. 2
      CHANGES
  3. 31
      flask/helpers.py
  4. 30
      tests/test_helpers.py

1
AUTHORS

@ -15,6 +15,7 @@ Patches and Suggestions
- Chris Grindstaff - Chris Grindstaff
- Christopher Grebs - Christopher Grebs
- Daniel Neuhäuser - Daniel Neuhäuser
- Dan Sully
- David Lord @davidism - David Lord @davidism
- Edmond Burnett - Edmond Burnett
- Florent Xicluna - Florent Xicluna

2
CHANGES

@ -7,6 +7,8 @@ Version 0.12
------------ ------------
- the cli command now responds to `--version`. - the cli command now responds to `--version`.
- Mimetype guessing for ``send_file`` has been removed, as per issue ``#104``.
See pull request ``#1849``.
Version 0.11 Version 0.11
------------ ------------

31
flask/helpers.py

@ -437,11 +437,7 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
to ``True`` to directly emit an ``X-Sendfile`` header. This however to ``True`` to directly emit an ``X-Sendfile`` header. This however
requires support of the underlying webserver for ``X-Sendfile``. requires support of the underlying webserver for ``X-Sendfile``.
By default it will try to guess the mimetype for you, but you can You must explicitly provide the mimetype for the filename or file object.
also explicitly provide one. For extra security you probably want
to send certain files as attachment (HTML for instance). The mimetype
guessing requires a `filename` or an `attachment_filename` to be
provided.
Please never pass filenames to this function from user sources; Please never pass filenames to this function from user sources;
you should use :func:`send_from_directory` instead. you should use :func:`send_from_directory` instead.
@ -461,6 +457,11 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
.. versionchanged:: 0.9 .. versionchanged:: 0.9
cache_timeout pulls its default from application config, when None. cache_timeout pulls its default from application config, when None.
.. versionchanged:: 0.12
mimetype guessing and etag support removed for file objects.
If no mimetype or attachment_filename is provided, application/octet-stream
will be used.
:param filename_or_fp: the filename of the file to send in `latin-1`. :param filename_or_fp: the filename of the file to send in `latin-1`.
This is relative to the :attr:`~Flask.root_path` This is relative to the :attr:`~Flask.root_path`
if a relative path is specified. if a relative path is specified.
@ -488,25 +489,9 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
filename = filename_or_fp filename = filename_or_fp
file = None file = None
else: else:
from warnings import warn
file = filename_or_fp file = filename_or_fp
filename = getattr(file, 'name', None) filename = getattr(file, 'name', None)
# XXX: this behavior is now deprecated because it was unreliable.
# removed in Flask 1.0
if not attachment_filename and not mimetype \
and isinstance(filename, string_types):
warn(DeprecationWarning('The filename support for file objects '
'passed to send_file is now deprecated. Pass an '
'attach_filename if you want mimetypes to be guessed.'),
stacklevel=2)
if add_etags:
warn(DeprecationWarning('In future flask releases etags will no '
'longer be generated for file objects passed to the send_file '
'function because this behavior was unreliable. Pass '
'filenames instead if possible, otherwise attach an etag '
'yourself based on another value'), stacklevel=2)
if filename is not None: if filename is not None:
if not os.path.isabs(filename): if not os.path.isabs(filename):
filename = os.path.join(current_app.root_path, filename) filename = os.path.join(current_app.root_path, filename)
@ -553,7 +538,9 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
rv.cache_control.max_age = cache_timeout rv.cache_control.max_age = cache_timeout
rv.expires = int(time() + cache_timeout) rv.expires = int(time() + cache_timeout)
if add_etags and filename is not None: if add_etags and filename is not None and file is None:
from warnings import warn
try: try:
rv.set_etag('%s-%s-%s' % ( rv.set_etag('%s-%s-%s' % (
os.path.getmtime(filename), os.path.getmtime(filename),

30
tests/test_helpers.py

@ -349,7 +349,7 @@ class TestSendfile(object):
assert rv.mimetype == 'text/html' assert rv.mimetype == 'text/html'
rv.close() rv.close()
def test_send_file_object(self, recwarn): def test_send_file_object(self):
app = flask.Flask(__name__) app = flask.Flask(__name__)
with app.test_request_context(): with app.test_request_context():
@ -361,10 +361,6 @@ class TestSendfile(object):
assert rv.mimetype == 'text/html' assert rv.mimetype == 'text/html'
rv.close() rv.close()
# mimetypes + etag
recwarn.pop(DeprecationWarning)
recwarn.pop(DeprecationWarning)
app.use_x_sendfile = True app.use_x_sendfile = True
with app.test_request_context(): with app.test_request_context():
@ -376,10 +372,6 @@ class TestSendfile(object):
os.path.join(app.root_path, 'static/index.html') os.path.join(app.root_path, 'static/index.html')
rv.close() rv.close()
# mimetypes + etag
recwarn.pop(DeprecationWarning)
recwarn.pop(DeprecationWarning)
app.use_x_sendfile = False app.use_x_sendfile = False
with app.test_request_context(): with app.test_request_context():
f = StringIO('Test') f = StringIO('Test')
@ -389,9 +381,6 @@ class TestSendfile(object):
assert rv.mimetype == 'application/octet-stream' assert rv.mimetype == 'application/octet-stream'
rv.close() rv.close()
# etags
recwarn.pop(DeprecationWarning)
class PyStringIO(object): class PyStringIO(object):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self._io = StringIO(*args, **kwargs) self._io = StringIO(*args, **kwargs)
@ -405,11 +394,6 @@ class TestSendfile(object):
assert rv.mimetype == 'text/plain' assert rv.mimetype == 'text/plain'
rv.close() rv.close()
# attachment_filename and etags
a = recwarn.pop(DeprecationWarning)
b = recwarn.pop(DeprecationWarning)
c = recwarn.pop(UserWarning) # file not found
f = StringIO('Test') f = StringIO('Test')
rv = flask.send_file(f, mimetype='text/plain') rv = flask.send_file(f, mimetype='text/plain')
rv.direct_passthrough = False rv.direct_passthrough = False
@ -417,9 +401,6 @@ class TestSendfile(object):
assert rv.mimetype == 'text/plain' assert rv.mimetype == 'text/plain'
rv.close() rv.close()
# etags
recwarn.pop(DeprecationWarning)
app.use_x_sendfile = True app.use_x_sendfile = True
with app.test_request_context(): with app.test_request_context():
@ -428,10 +409,7 @@ class TestSendfile(object):
assert 'x-sendfile' not in rv.headers assert 'x-sendfile' not in rv.headers
rv.close() rv.close()
# etags def test_attachment(self):
recwarn.pop(DeprecationWarning)
def test_attachment(self, recwarn):
app = flask.Flask(__name__) app = flask.Flask(__name__)
with app.test_request_context(): with app.test_request_context():
with open(os.path.join(app.root_path, 'static/index.html')) as f: with open(os.path.join(app.root_path, 'static/index.html')) as f:
@ -441,10 +419,6 @@ class TestSendfile(object):
assert value == 'attachment' assert value == 'attachment'
rv.close() rv.close()
# mimetypes + etag
assert len(recwarn.list) == 2
recwarn.clear()
with app.test_request_context(): with app.test_request_context():
assert options['filename'] == 'index.html' assert options['filename'] == 'index.html'
rv = flask.send_file('static/index.html', as_attachment=True) rv = flask.send_file('static/index.html', as_attachment=True)

Loading…
Cancel
Save