Skip to content
Myr

Merge pull request #2223 from antlarr/master

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

Fix send_file's attachment_filename to work with non-ascii filenames

authored by David Lord · committed by GitHub · parents 00d6e33, f790ab7 · Browse files · 2 files+37 added−3 removed
This commit

State

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

Diff stats

2 files+37 added−3 removed

  • tests:+15 added−1 removed
  • other:+22 added−2 removed

2 files+37 added−3 removed

modified:flask/helpers.py+22 added-2 removed
15 unmodified lines
16import mimetypes17from time import time18from zlib import adler3219from threading import RLock20import unicodedata21from werkzeug.routing import BuildError22from functools import update_wrapper2324try:
452 unmodified lines
477478 .. versionchanged:: 0.12479 The `attachment_filename` is preferred over `filename` for MIME-type480 detection.481 482 .. versionchanged:: 0.13483 UTF-8 filenames, as specified in `RFC 2231`_, are supported.484 485 .. _RFC 2231: https://tools.ietf.org/html/rfc2231#section-4486487 :param filename_or_fp: the filename of the file to send in `latin-1`.488 This is relative to the :attr:`~Flask.root_path`489 if a relative path is specified.
49 unmodified lines
539 if as_attachment:540 if attachment_filename is None:541 raise TypeError('filename unavailable, required for '542 'sending as attachment')537 headers.add('Content-Disposition', 'attachment',538 filename=attachment_filename)543544 normalized = unicodedata.normalize(545 'NFKD', text_type(attachment_filename)546 )547548 try:549 normalized.encode('ascii')550 except UnicodeEncodeError:551 filenames = {552 'filename': normalized.encode('ascii', 'ignore'),553 'filename*': "UTF-8''%s" % url_quote(attachment_filename),554 }555 else:556 filenames = {'filename': attachment_filename}557558 headers.add('Content-Disposition', 'attachment', **filenames)559560 if current_app.use_x_sendfile and filename:561 if file is not None:562 file.close()563 headers['X-Sendfile'] = filename
417 unmodified lines
modified:tests/test_helpers.py+15 added-1 removed
538 unmodified lines
539 attachment_filename='index.html')540 value, options = \541 parse_options_header(rv.headers['Content-Disposition'])542 assert value == 'attachment'543 assert options['filename'] == 'index.html'544 assert 'filename*' not in rv.headers['Content-Disposition']545 rv.close()546547 with app.test_request_context():546 assert options['filename'] == 'index.html'548 rv = flask.send_file('static/index.html', as_attachment=True)549 value, options = parse_options_header(rv.headers['Content-Disposition'])550 assert value == 'attachment'551 assert options['filename'] == 'index.html'
8 unmodified lines
560 assert value == 'attachment'561 assert options['filename'] == 'index.txt'562 rv.close()563564 def test_attachment_with_utf8_filename(self):565 app = flask.Flask(__name__)566567 with app.test_request_context():568 rv = flask.send_file('static/index.html', as_attachment=True, attachment_filename=u'Ñandú/pingüino.txt')569 content_disposition = set(rv.headers['Content-Disposition'].split('; '))570 assert content_disposition == set((571 'attachment',572 'filename="Nandu/pinguino.txt"',573 "filename*=UTF-8''%C3%91and%C3%BA%EF%BC%8Fping%C3%BCino.txt"574 ))575 rv.close()576577 def test_static_file(self):578 app = flask.Flask(__name__)579 # default cache timeout is 12 hours580 with app.test_request_context():
323 unmodified lines