forked from orbit-oss/flask
Fix send_file to work with non-ascii filenames
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
This commit is contained in:
parent
6efea346dd
commit
0049922f2e
4 changed files with 18 additions and 1 deletions
|
|
@ -41,6 +41,7 @@ from .signals import message_flashed
|
|||
from .globals import session, _request_ctx_stack, _app_ctx_stack, \
|
||||
current_app, request
|
||||
from ._compat import string_types, text_type
|
||||
from unidecode import unidecode
|
||||
|
||||
|
||||
# sentinel
|
||||
|
|
@ -534,8 +535,11 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
|
|||
if attachment_filename is None:
|
||||
raise TypeError('filename unavailable, required for '
|
||||
'sending as attachment')
|
||||
filename_dict = {
|
||||
'filename': unidecode(attachment_filename),
|
||||
'filename*': "UTF-8''%s" % url_quote(attachment_filename)}
|
||||
headers.add('Content-Disposition', 'attachment',
|
||||
filename=attachment_filename)
|
||||
**filename_dict)
|
||||
|
||||
if current_app.use_x_sendfile and filename:
|
||||
if file is not None:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue