forked from orbit-oss/flask
style cleanup
break out header parts in test test for no filename* parameter for ascii header
This commit is contained in:
parent
d50a5db5ed
commit
c1973016ea
2 changed files with 28 additions and 16 deletions
|
|
@ -14,10 +14,10 @@ import sys
|
||||||
import pkgutil
|
import pkgutil
|
||||||
import posixpath
|
import posixpath
|
||||||
import mimetypes
|
import mimetypes
|
||||||
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
|
||||||
|
import unicodedata
|
||||||
from werkzeug.routing import BuildError
|
from werkzeug.routing import BuildError
|
||||||
from functools import update_wrapper
|
from functools import update_wrapper
|
||||||
|
|
||||||
|
|
@ -479,6 +479,11 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
|
||||||
The `attachment_filename` is preferred over `filename` for MIME-type
|
The `attachment_filename` is preferred over `filename` for MIME-type
|
||||||
detection.
|
detection.
|
||||||
|
|
||||||
|
.. versionchanged:: 0.13
|
||||||
|
UTF-8 filenames, as specified in `RFC 2231`_, are supported.
|
||||||
|
|
||||||
|
.. _RFC 2231: https://tools.ietf.org/html/rfc2231#section-4
|
||||||
|
|
||||||
:param filename_or_fp: the filename of the file to send in `latin-1`.
|
:param filename_or_fp: the filename of the file to send in `latin-1`.
|
||||||
This is relative to the :attr:`~Flask.root_path`
|
This is relative to the :attr:`~Flask.root_path`
|
||||||
if a relative path is specified.
|
if a relative path is specified.
|
||||||
|
|
@ -535,7 +540,10 @@ 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')
|
||||||
normalized = normalize('NFKD', text_type(attachment_filename))
|
|
||||||
|
normalized = unicodedata.normalize(
|
||||||
|
'NFKD', text_type(attachment_filename)
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
normalized.encode('ascii')
|
normalized.encode('ascii')
|
||||||
|
|
@ -545,12 +553,9 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
|
||||||
'filename*': "UTF-8''%s" % url_quote(attachment_filename),
|
'filename*': "UTF-8''%s" % url_quote(attachment_filename),
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
filenames = {
|
filenames = {'filename': attachment_filename}
|
||||||
'filename': attachment_filename,
|
|
||||||
}
|
|
||||||
|
|
||||||
headers.add('Content-Disposition', 'attachment',
|
headers.add('Content-Disposition', 'attachment', **filenames)
|
||||||
**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:
|
||||||
|
|
|
||||||
|
|
@ -540,10 +540,11 @@ class TestSendfile(object):
|
||||||
value, options = \
|
value, options = \
|
||||||
parse_options_header(rv.headers['Content-Disposition'])
|
parse_options_header(rv.headers['Content-Disposition'])
|
||||||
assert value == 'attachment'
|
assert value == 'attachment'
|
||||||
|
assert options['filename'] == 'index.html'
|
||||||
|
assert 'filename*' not in options
|
||||||
rv.close()
|
rv.close()
|
||||||
|
|
||||||
with app.test_request_context():
|
with app.test_request_context():
|
||||||
assert options['filename'] == 'index.html'
|
|
||||||
rv = flask.send_file('static/index.html', as_attachment=True)
|
rv = flask.send_file('static/index.html', as_attachment=True)
|
||||||
value, options = parse_options_header(rv.headers['Content-Disposition'])
|
value, options = parse_options_header(rv.headers['Content-Disposition'])
|
||||||
assert value == 'attachment'
|
assert value == 'attachment'
|
||||||
|
|
@ -562,15 +563,21 @@ class TestSendfile(object):
|
||||||
|
|
||||||
def test_attachment_with_utf8_filename(self):
|
def test_attachment_with_utf8_filename(self):
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
|
|
||||||
with app.test_request_context():
|
with app.test_request_context():
|
||||||
with open(os.path.join(app.root_path, 'static/index.html')) as f:
|
rv = flask.send_file(
|
||||||
rv = flask.send_file(f, as_attachment=True,
|
'static/index.html', as_attachment=True,
|
||||||
attachment_filename=u'Ñandú/pingüino.txt')
|
attachment_filename=u'Ñandú/pingüino.txt'
|
||||||
content_disposition = set(rv.headers['Content-Disposition'].split(';'))
|
)
|
||||||
assert content_disposition == set(['attachment',
|
value, options = parse_options_header(
|
||||||
' filename="Nandu/pinguino.txt"',
|
rv.headers['Content-Disposition']
|
||||||
" filename*=UTF-8''%C3%91and%C3%BA%EF%BC%8Fping%C3%BCino.txt"])
|
)
|
||||||
rv.close()
|
rv.close()
|
||||||
|
|
||||||
|
assert value == 'attachment'
|
||||||
|
assert sorted(options.keys()) == ('filename', 'filename*')
|
||||||
|
assert options['filename'] == 'Nandu/pinguino.txt'
|
||||||
|
assert options['filename*'] == 'UTF-8''%C3%91and%C3%BA%EF%BC%8Fping%C3%BCino.txt'
|
||||||
|
|
||||||
def test_static_file(self):
|
def test_static_file(self):
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue