Skip to content
Myr

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

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

Fix #1849

authored by Dan Sully · committed by Markus Unterwaditzer · parent 3d72099 · Browse files · 4 files+14 added−50 removed
This commit

State

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

Diff stats

4 files+14 added−50 removed

  • tests:+2 added−28 removed
  • other:+12 added−22 removed

4 files+14 added−50 removed

modified:flask/helpers.py+9 added-22 removed
435 unmodified lines
436 you can set the application's :attr:`~Flask.use_x_sendfile` attribute437 to ``True`` to directly emit an ``X-Sendfile`` header. This however438 requires support of the underlying webserver for ``X-Sendfile``.439440 By default it will try to guess the mimetype for you, but you can441 also explicitly provide one. For extra security you probably want442 to send certain files as attachment (HTML for instance). The mimetype443 guessing requires a `filename` or an `attachment_filename` to be444 provided.440 You must explicitly provide the mimetype for the filename or file object.441442 Please never pass filenames to this function from user sources;443 you should use :func:`send_from_directory` instead.444
11 unmodified lines
456457 .. versionchanged:: 0.9458 cache_timeout pulls its default from application config, when None.459460 .. versionchanged:: 0.12461 mimetype guessing and etag support removed for file objects.462 If no mimetype or attachment_filename is provided, application/octet-stream463 will be used.464465 :param filename_or_fp: the filename of the file to send in `latin-1`.466 This is relative to the :attr:`~Flask.root_path`467 if a relative path is specified.468 Alternatively a file object might be provided in
19 unmodified lines
488 if isinstance(filename_or_fp, string_types):489 filename = filename_or_fp490 file = None491 else:491 from warnings import warn492 file = filename_or_fp493 filename = getattr(file, 'name', None)494495 # XXX: this behavior is now deprecated because it was unreliable.496 # removed in Flask 1.0497 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)509495 if filename is not None:496 if not os.path.isabs(filename):497 filename = os.path.join(current_app.root_path, filename)498 if mimetype is None and (filename or attachment_filename):
38 unmodified lines
537 if cache_timeout is not None:538 rv.cache_control.max_age = cache_timeout539 rv.expires = int(time() + cache_timeout)540556 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 warn543544 try:545 rv.set_etag('%s-%s-%s' % (546 os.path.getmtime(filename),547 os.path.getsize(filename),
370 unmodified lines
modified:tests/test_helpers.py+2 added-28 removed
347 unmodified lines
348 os.path.join(app.root_path, 'static/index.html')349 assert rv.mimetype == 'text/html'350 rv.close()351352 def test_send_file_object(self, recwarn):352 def test_send_file_object(self):353 app = flask.Flask(__name__)354355 with app.test_request_context():356 with open(os.path.join(app.root_path, 'static/index.html'), mode='rb') as f:
3 unmodified lines
360 assert rv.data == f.read()361 assert rv.mimetype == 'text/html'362 rv.close()363364 # mimetypes + etag365 recwarn.pop(DeprecationWarning)366 recwarn.pop(DeprecationWarning)367364 app.use_x_sendfile = True365366 with app.test_request_context():367 with open(os.path.join(app.root_path, 'static/index.html')) as f:
3 unmodified lines
371 assert rv.headers['x-sendfile'] == \372 os.path.join(app.root_path, 'static/index.html')373 rv.close()374379 # mimetypes + etag380 recwarn.pop(DeprecationWarning)381 recwarn.pop(DeprecationWarning)382375 app.use_x_sendfile = False376 with app.test_request_context():377 f = StringIO('Test')378 rv = flask.send_file(f)
1 unmodified line
380 assert rv.data == b'Test'381 assert rv.mimetype == 'application/octet-stream'382 rv.close()383392 # etags393 recwarn.pop(DeprecationWarning)394384 class PyStringIO(object):385 def __init__(self, *args, **kwargs):386 self._io = StringIO(*args, **kwargs)387 def __getattr__(self, name):
5 unmodified lines
393 assert rv.data == b'Test'394 assert rv.mimetype == 'text/plain'395 rv.close()396408 # attachment_filename and etags409 a = recwarn.pop(DeprecationWarning)410 b = recwarn.pop(DeprecationWarning)411 c = recwarn.pop(UserWarning) # file not found412397 f = StringIO('Test')398 rv = flask.send_file(f, mimetype='text/plain')399 rv.direct_passthrough = False400 assert rv.data == b'Test'401 assert rv.mimetype == 'text/plain'402 rv.close()403420 # etags421 recwarn.pop(DeprecationWarning)422404 app.use_x_sendfile = True405406 with app.test_request_context():407 f = StringIO('Test')408 rv = flask.send_file(f)409 assert 'x-sendfile' not in rv.headers410 rv.close()411431 # etags432 recwarn.pop(DeprecationWarning)433434 def test_attachment(self, recwarn):412 def test_attachment(self):413 app = flask.Flask(__name__)414 with app.test_request_context():415 with open(os.path.join(app.root_path, 'static/index.html')) as f:416 rv = flask.send_file(f, as_attachment=True)
1 unmodified line
418 parse_options_header(rv.headers['Content-Disposition'])419 assert value == 'attachment'420 rv.close()421444 # mimetypes + etag445 assert len(recwarn.list) == 2446 recwarn.clear()447422 with app.test_request_context():423 assert options['filename'] == 'index.html'424 rv = flask.send_file('static/index.html', as_attachment=True)425 value, options = parse_options_header(rv.headers['Content-Disposition'])
299 unmodified lines
modified:AUTHORS+1 added
13 unmodified lines
14- Chris Edgemon15- Chris Grindstaff16- Christopher Grebs17- Daniel Neuhäuser18- Dan Sully19- David Lord @davidism20- Edmond Burnett21- Florent Xicluna22- Georg Brandl
15 unmodified lines
modified:CHANGES+2 added
5 unmodified lines
6Version 0.127------------89- 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``.1213Version 0.1114------------15
557 unmodified lines