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
Provenance
- Commit
8b45009- Tree
c251a22- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
00d6e33- Parent 2
f790ab7
2 files changed+37 added−3 removed
modified:flask/helpers.py+22 added-2 removed
15 unmodified lines16 import mimetypes17 from time import time18 from zlib import adler3219 from threading import RLock20+import unicodedata21 from werkzeug.routing import BuildError22 from functools import update_wrapper23 24 try:452 unmodified lines477 478 .. 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-4486 487 :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 lines539 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)543 544+ normalized = unicodedata.normalize(545+ 'NFKD', text_type(attachment_filename)546+ )547+548+ 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}557+558+ headers.add('Content-Disposition', 'attachment', **filenames)559+560 if current_app.use_x_sendfile and filename:561 if file is not None:562 file.close()563 headers['X-Sendfile'] = filename417 unmodified linesmodified:tests/test_helpers.py+15 added-1 removed
538 unmodified lines539 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()546 547 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 lines560 assert value == 'attachment'561 assert options['filename'] == 'index.txt'562 rv.close()563 564+ def test_attachment_with_utf8_filename(self):565+ app = flask.Flask(__name__)566+567+ 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()576+577 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