Skip to content
Myr

Fix send_file to work with non-ascii filenames

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

This commit implements https://tools.ietf.org/html/rfc2231#section-4 in order to support sending unicode characters. Tested on both Firefox and Chromium under Linux. This adds unidecode as a dependency, which might be relaxed by using .encode('latin-1', 'ignore') but wouldn't be as useful. Also, added a test for the correct headers to be added. Previously, using a filename parameter to send_file with unicode characters, it failed with the next error since HTTP headers don't allow non latin-1 characters. Error on request: Traceback (most recent call last): File "/usr/lib/python3.6/site-packages/werkzeug/serving.py", line 193, in run_wsgi execute(self.server.app) File "/usr/lib/python3.6/site-packages/werkzeug/serving.py", line 186, in execute write(b'') File "/usr/lib/python3.6/site-packages/werkzeug/serving.py", line 152, in write self.send_header(key, value) File "/usr/lib64/python3.6/http/server.py", line 509, in send_header ("%s: %s\r\n" % (keyword, value)).encode('latin-1', 'strict')) UnicodeEncodeError: 'latin-1' codec can't encode character '\uff0f' in position 58: ordinal not in range(256) Fixes #1286

authored by Antonio Larrosa · committed by Antonio Larrosa · parent 6efea34 · Browse files · 4 files+18 added−1 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+18 added−1 removed

  • tests:+11 added−0 removed
  • other:+7 added−1 removed

4 files+18 added−1 removed

modified:flask/helpers.py+5 added-1 removed
39 unmodified lines
40from .signals import message_flashed41from .globals import session, _request_ctx_stack, _app_ctx_stack, \42 current_app, request43from ._compat import string_types, text_type44from unidecode import unidecode454647# sentinel48_missing = object()
485 unmodified lines
534 if as_attachment:535 if attachment_filename is None:536 raise TypeError('filename unavailable, required for '537 'sending as attachment')538 filename_dict = {539 'filename': unidecode(attachment_filename),540 'filename*': "UTF-8''%s" % url_quote(attachment_filename)}541 headers.add('Content-Disposition', 'attachment',538 filename=attachment_filename)542 **filename_dict)543544 if current_app.use_x_sendfile and filename:545 if file is not None:546 file.close()
418 unmodified lines
modified:tests/test_helpers.py+11 added
558 unmodified lines
559 assert value == 'attachment'560 assert options['filename'] == 'index.txt'561 rv.close()562563 def test_attachment_with_utf8_filename(self):564 app = flask.Flask(__name__)565 with app.test_request_context():566 with open(os.path.join(app.root_path, 'static/index.html')) as f:567 rv = flask.send_file(f, as_attachment=True,568 attachment_filename='Ñandú/pingüino.txt')569 value, options = \570 parse_options_header(rv.headers['Content-Disposition'])571 assert options == {'filename': 'Nandu/pinguino.txt', 'filename*': "UTF-8''%C3%91and%C3%BA%EF%BC%8Fping%C3%BCino.txt"}572 rv.close()573574 def test_static_file(self):575 app = flask.Flask(__name__)576 # default cache timeout is 12 hours577 with app.test_request_context():
323 unmodified lines
modified:setup.py+1 added
73 unmodified lines
74 'Werkzeug>=0.7',75 'Jinja2>=2.4',76 'itsdangerous>=0.21',77 'click>=2.0',78 'unidecode',79 ],80 classifiers=[81 'Development Status :: 4 - Beta',82 'Environment :: Web Environment',
19 unmodified lines
modified:tox.ini+1 added
25 unmodified lines
26 devel: git+https://github.com/pallets/jinja.git27 devel: git+https://github.com/pallets/itsdangerous.git28 devel: git+https://github.com/jek/blinker.git29 simplejson: simplejson30 unidecode3132[testenv:docs]33deps = sphinx34commands = sphinx-build -W -b linkcheck -d {envtmpdir}/doctrees docs docs/_build/linkcheck