diff options
| author | Sviatoslav Sydorenko <wk@sydorenko.org.ua> | 2020-04-17 19:49:39 +0200 |
|---|---|---|
| committer | Sviatoslav Sydorenko <wk@sydorenko.org.ua> | 2020-04-17 19:49:39 +0200 |
| commit | b2938bc92570a1a04aa4336412e1824b9fe455eb (patch) | |
| tree | c5fafefb4bdc8a555082eb4970b603263a8cfbfb | |
| parent | f17397ec648f48a6969268abb5486f727c7de98f (diff) | |
| parent | bc1dd5a5d9be7ad613a6b23ac83938936a4509a9 (diff) | |
| download | cherrypy-git-18.6.0.tar.gz | |
Merge remote-tracking branch 'origin/pr/1851'v18.6.0
This change implements serving files with Unicode in their names. This
is achieved adding extra `filename*` parameter in `Content-Disposition`
header when serving content with non-ASCII filenames. As per RFC 6266
recommendations in the Appendix D.
Refs:
* https://stackoverflow.com/a/8996249/2173868
* https://tools.ietf.org/html/rfc6266#appendix-D
Resolves #1776
| -rw-r--r-- | CHANGES.rst | 6 | ||||
| -rw-r--r-- | cherrypy/lib/static.py | 43 | ||||
| -rw-r--r-- | cherrypy/test/test_static.py | 30 |
3 files changed, 71 insertions, 8 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index 1697a91f..bc408c2f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,9 @@ +v18.6.0 +------- + +* :issue:`1776` via :pr:`1851`: Add support for UTF-8 encoded attachment + file names in ``Content-Disposition`` header via :rfc:`6266#appendix-D`. + v18.5.0 ------- diff --git a/cherrypy/lib/static.py b/cherrypy/lib/static.py index 9a3b8e83..66a5a947 100644 --- a/cherrypy/lib/static.py +++ b/cherrypy/lib/static.py @@ -6,6 +6,7 @@ import re import stat import mimetypes import urllib.parse +import unicodedata from email.generator import _make_boundary as make_boundary from io import UnsupportedOperation @@ -28,6 +29,30 @@ def _setup_mimetypes(): _setup_mimetypes() +def _make_content_disposition(disposition, file_name): + """Create HTTP header for downloading a file with a UTF-8 filename. + + This function implements the recommendations of :rfc:`6266#appendix-D`. + See this and related answers: https://stackoverflow.com/a/8996249/2173868. + """ + # As normalization algorithm for `unicodedata` is used composed form (NFC + # and NFKC) with compatibility equivalence criteria (NFK), so "NFKC" is the + # one. It first applies the compatibility decomposition, followed by the + # canonical composition. Should be displayed in the same manner, should be + # treated in the same way by applications such as alphabetizing names or + # searching, and may be substituted for each other. + # See: https://en.wikipedia.org/wiki/Unicode_equivalence. + ascii_name = ( + unicodedata.normalize('NFKC', file_name). + encode('ascii', errors='ignore').decode() + ) + header = '{}; filename="{}"'.format(disposition, ascii_name) + if ascii_name != file_name: + quoted_name = urllib.parse.quote(file_name) + header += '; filename*=UTF-8\'\'{}'.format(quoted_name) + return header + + def serve_file(path, content_type=None, disposition=None, name=None, debug=False): """Set status, headers, and body in order to serve the given path. @@ -37,9 +62,10 @@ def serve_file(path, content_type=None, disposition=None, name=None, of the 'path' argument. If disposition is not None, the Content-Disposition header will be set - to "<disposition>; filename=<name>". If name is None, it will be set - to the basename of path. If disposition is None, no Content-Disposition - header will be written. + to "<disposition>; filename=<name>; filename*=utf-8''<name>" + as described in :rfc:`6266#appendix-D`. + If name is None, it will be set to the basename of path. + If disposition is None, no Content-Disposition header will be written. """ response = cherrypy.serving.response @@ -92,7 +118,7 @@ def serve_file(path, content_type=None, disposition=None, name=None, if disposition is not None: if name is None: name = os.path.basename(path) - cd = '%s; filename="%s"' % (disposition, name) + cd = _make_content_disposition(disposition, name) response.headers['Content-Disposition'] = cd if debug: cherrypy.log('Content-Disposition: %r' % cd, 'TOOLS.STATIC') @@ -111,9 +137,10 @@ def serve_fileobj(fileobj, content_type=None, disposition=None, name=None, The Content-Type header will be set to the content_type arg, if provided. If disposition is not None, the Content-Disposition header will be set - to "<disposition>; filename=<name>". If name is None, 'filename' will - not be set. If disposition is None, no Content-Disposition header will - be written. + to "<disposition>; filename=<name>; filename*=utf-8''<name>" + as described in :rfc:`6266#appendix-D`. + If name is None, 'filename' will not be set. + If disposition is None, no Content-Disposition header will be written. CAUTION: If the request contains a 'Range' header, one or more seek()s will be performed on the file object. This may cause undesired behavior if @@ -149,7 +176,7 @@ def serve_fileobj(fileobj, content_type=None, disposition=None, name=None, if name is None: cd = disposition else: - cd = '%s; filename="%s"' % (disposition, name) + cd = _make_content_disposition(disposition, name) response.headers['Content-Disposition'] = cd if debug: cherrypy.log('Content-Disposition: %r' % cd, 'TOOLS.STATIC') diff --git a/cherrypy/test/test_static.py b/cherrypy/test/test_static.py index 3de658af..bfe2f40f 100644 --- a/cherrypy/test/test_static.py +++ b/cherrypy/test/test_static.py @@ -97,6 +97,20 @@ class StaticTest(helper.CPWebCase): f = io.BytesIO(b'Fee\nfie\nfo\nfum') return static.serve_fileobj(f, content_type='text/plain') + @cherrypy.expose + def serve_file_utf8_filename(self): + return static.serve_file( + __file__, + disposition='attachment', + name='has_utf-8_character_☃.html') + + @cherrypy.expose + def serve_fileobj_utf8_filename(self): + return static.serve_fileobj( + io.BytesIO('☃\nfie\nfo\nfum'.encode('utf-8')), + disposition='attachment', + name='has_utf-8_character_☃.html') + class Static: @cherrypy.expose @@ -193,6 +207,22 @@ class StaticTest(helper.CPWebCase): # we just check the content self.assertMatchesBody('^Dummy stylesheet') + # Check a filename with utf-8 characters in it + ascii_fn = 'has_utf-8_character_.html' + url_quote_fn = 'has_utf-8_character_%E2%98%83.html' # %E2%98%83 == ☃ + expected_content_disposition = ( + 'attachment; filename="{!s}"; filename*=UTF-8\'\'{!s}'. + format(ascii_fn, url_quote_fn) + ) + + self.getPage('/serve_file_utf8_filename') + self.assertStatus('200 OK') + self.assertHeader('Content-Disposition', expected_content_disposition) + + self.getPage('/serve_fileobj_utf8_filename') + self.assertStatus('200 OK') + self.assertHeader('Content-Disposition', expected_content_disposition) + @pytest.mark.skipif(platform.system() != 'Windows', reason='Windows only') def test_static_longpath(self): """Test serving of a file in subdir of a Windows long-path |
