Browse Source

Keep using only filename if it's valid ascii

pull/2223/head
Antonio Larrosa 8 years ago
parent
commit
d50a5db5ed
  1. 23
      flask/helpers.py

23
flask/helpers.py

@ -14,7 +14,7 @@ import sys
import pkgutil import pkgutil
import posixpath import posixpath
import mimetypes import mimetypes
import unicodedata from unicodedata import normalize
from time import time from time import time
from zlib import adler32 from zlib import adler32
from threading import RLock from threading import RLock
@ -535,13 +535,22 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
if attachment_filename is None: if attachment_filename is None:
raise TypeError('filename unavailable, required for ' raise TypeError('filename unavailable, required for '
'sending as attachment') 'sending as attachment')
filename_dict = { normalized = normalize('NFKD', text_type(attachment_filename))
'filename': (unicodedata.normalize('NFKD',
text_type(attachment_filename)).encode('ascii', try:
'ignore')), normalized.encode('ascii')
'filename*': "UTF-8''%s" % url_quote(attachment_filename)} except UnicodeEncodeError:
filenames = {
'filename': normalized.encode('ascii', 'ignore'),
'filename*': "UTF-8''%s" % url_quote(attachment_filename),
}
else:
filenames = {
'filename': attachment_filename,
}
headers.add('Content-Disposition', 'attachment', headers.add('Content-Disposition', 'attachment',
**filename_dict) **filenames)
if current_app.use_x_sendfile and filename: if current_app.use_x_sendfile and filename:
if file is not None: if file is not None:

Loading…
Cancel
Save