Skip to content

Commit 8458cc5

Browse files
Dan Sullyuntitaker
Dan Sully
authored andcommitted
Remove deprecation warnings for add_etags & mimetype guessing for send_file()
Fix pallets#1849
1 parent 3d72099 commit 8458cc5

File tree

4 files changed

+14
-50
lines changed

4 files changed

+14
-50
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Patches and Suggestions
1515
- Chris Grindstaff
1616
- Christopher Grebs
1717
- Daniel Neuhäuser
18+
- Dan Sully
1819
- David Lord @davidism
1920
- Edmond Burnett
2021
- Florent Xicluna

CHANGES

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Version 0.12
77
------------
88

99
- the cli command now responds to `--version`.
10+
- Mimetype guessing for ``send_file`` has been removed, as per issue ``#104``.
11+
See pull request ``#1849``.
1012

1113
Version 0.11
1214
------------

flask/helpers.py

+9-22
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,7 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
437437
to ``True`` to directly emit an ``X-Sendfile`` header. This however
438438
requires support of the underlying webserver for ``X-Sendfile``.
439439
440-
By default it will try to guess the mimetype for you, but you can
441-
also explicitly provide one. For extra security you probably want
442-
to send certain files as attachment (HTML for instance). The mimetype
443-
guessing requires a `filename` or an `attachment_filename` to be
444-
provided.
440+
You must explicitly provide the mimetype for the filename or file object.
445441
446442
Please never pass filenames to this function from user sources;
447443
you should use :func:`send_from_directory` instead.
@@ -461,6 +457,11 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
461457
.. versionchanged:: 0.9
462458
cache_timeout pulls its default from application config, when None.
463459
460+
.. versionchanged:: 0.12
461+
mimetype guessing and etag support removed for file objects.
462+
If no mimetype or attachment_filename is provided, application/octet-stream
463+
will be used.
464+
464465
:param filename_or_fp: the filename of the file to send in `latin-1`.
465466
This is relative to the :attr:`~Flask.root_path`
466467
if a relative path is specified.
@@ -488,25 +489,9 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
488489
filename = filename_or_fp
489490
file = None
490491
else:
491-
from warnings import warn
492492
file = filename_or_fp
493493
filename = getattr(file, 'name', None)
494494

495-
# XXX: this behavior is now deprecated because it was unreliable.
496-
# removed in Flask 1.0
497-
if not attachment_filename and not mimetype \
498-
and isinstance(filename, string_types):
499-
warn(DeprecationWarning('The filename support for file objects '
500-
'passed to send_file is now deprecated. Pass an '
501-
'attach_filename if you want mimetypes to be guessed.'),
502-
stacklevel=2)
503-
if add_etags:
504-
warn(DeprecationWarning('In future flask releases etags will no '
505-
'longer be generated for file objects passed to the send_file '
506-
'function because this behavior was unreliable. Pass '
507-
'filenames instead if possible, otherwise attach an etag '
508-
'yourself based on another value'), stacklevel=2)
509-
510495
if filename is not None:
511496
if not os.path.isabs(filename):
512497
filename = os.path.join(current_app.root_path, filename)
@@ -553,7 +538,9 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
553538
rv.cache_control.max_age = cache_timeout
554539
rv.expires = int(time() + cache_timeout)
555540

556-
if add_etags and filename is not None:
541+
if add_etags and filename is not None and file is None:
542+
from warnings import warn
543+
557544
try:
558545
rv.set_etag('%s-%s-%s' % (
559546
os.path.getmtime(filename),

tests/test_helpers.py

+2-28
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def test_send_file_xsendfile(self, catch_deprecation_warnings):
349349
assert rv.mimetype == 'text/html'
350350
rv.close()
351351

352-
def test_send_file_object(self, recwarn):
352+
def test_send_file_object(self):
353353
app = flask.Flask(__name__)
354354

355355
with app.test_request_context():
@@ -361,10 +361,6 @@ def test_send_file_object(self, recwarn):
361361
assert rv.mimetype == 'text/html'
362362
rv.close()
363363

364-
# mimetypes + etag
365-
recwarn.pop(DeprecationWarning)
366-
recwarn.pop(DeprecationWarning)
367-
368364
app.use_x_sendfile = True
369365

370366
with app.test_request_context():
@@ -376,10 +372,6 @@ def test_send_file_object(self, recwarn):
376372
os.path.join(app.root_path, 'static/index.html')
377373
rv.close()
378374

379-
# mimetypes + etag
380-
recwarn.pop(DeprecationWarning)
381-
recwarn.pop(DeprecationWarning)
382-
383375
app.use_x_sendfile = False
384376
with app.test_request_context():
385377
f = StringIO('Test')
@@ -389,9 +381,6 @@ def test_send_file_object(self, recwarn):
389381
assert rv.mimetype == 'application/octet-stream'
390382
rv.close()
391383

392-
# etags
393-
recwarn.pop(DeprecationWarning)
394-
395384
class PyStringIO(object):
396385
def __init__(self, *args, **kwargs):
397386
self._io = StringIO(*args, **kwargs)
@@ -405,21 +394,13 @@ def __getattr__(self, name):
405394
assert rv.mimetype == 'text/plain'
406395
rv.close()
407396

408-
# attachment_filename and etags
409-
a = recwarn.pop(DeprecationWarning)
410-
b = recwarn.pop(DeprecationWarning)
411-
c = recwarn.pop(UserWarning) # file not found
412-
413397
f = StringIO('Test')
414398
rv = flask.send_file(f, mimetype='text/plain')
415399
rv.direct_passthrough = False
416400
assert rv.data == b'Test'
417401
assert rv.mimetype == 'text/plain'
418402
rv.close()
419403

420-
# etags
421-
recwarn.pop(DeprecationWarning)
422-
423404
app.use_x_sendfile = True
424405

425406
with app.test_request_context():
@@ -428,10 +409,7 @@ def __getattr__(self, name):
428409
assert 'x-sendfile' not in rv.headers
429410
rv.close()
430411

431-
# etags
432-
recwarn.pop(DeprecationWarning)
433-
434-
def test_attachment(self, recwarn):
412+
def test_attachment(self):
435413
app = flask.Flask(__name__)
436414
with app.test_request_context():
437415
with open(os.path.join(app.root_path, 'static/index.html')) as f:
@@ -441,10 +419,6 @@ def test_attachment(self, recwarn):
441419
assert value == 'attachment'
442420
rv.close()
443421

444-
# mimetypes + etag
445-
assert len(recwarn.list) == 2
446-
recwarn.clear()
447-
448422
with app.test_request_context():
449423
assert options['filename'] == 'index.html'
450424
rv = flask.send_file('static/index.html', as_attachment=True)

0 commit comments

Comments
 (0)