Skip to content

Commit

Permalink
Implement custom code for header creation
Browse files Browse the repository at this point in the history
The ZPL does not allow to vendor third party code except the original
author would sign the contributor agreement.

As this is not realistic in this case, a custom solution was implemented
to handle non-latin-1 compatible headers.

modified:   src/OFS/ObjectManager.py
modified:   src/ZPublisher/http_header_utils.py
modified:   src/ZPublisher/tests/test_http_header_utils.py
  • Loading branch information
jugmac00 committed Sep 28, 2020
1 parent c2b2d5c commit a8031e2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 53 deletions.
3 changes: 1 addition & 2 deletions src/OFS/ObjectManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,7 @@ def manage_exportObject(
RESPONSE.setHeader('Content-type', 'application/data')
RESPONSE.setHeader(
'Content-Disposition',
make_content_disposition(
'inline', f'filename={id}.{suffix}')
make_content_disposition('inline', f'{id}.{suffix}')
)
return result

Expand Down
76 changes: 26 additions & 50 deletions src/ZPublisher/http_header_utils.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,37 @@
"""This module offers helpers to handle HTTP headers."""

# The function `make_content_disposition` was vendored from our
# friends from `CherryPy` - thank you!
#
# Copyright © 2004-2019, CherryPy Team (team@cherrypy.org)
#
# All rights reserved.
#
# * * *
##############################################################################
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# Copyright (c) 2020 Zope Foundation and Contributors.
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of CherryPy nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # noqa: E501
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
##############################################################################
"""This module offers helpers to handle HTTP headers."""

import unicodedata
import urllib


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
header = f'{disposition}'
try:
file_name.encode("latin-1")
except UnicodeEncodeError:
# the file cannot be encoded using the `latin-1` encoding
# which is required for HTTP headers
#
# a special header has to be crafted
# also see https://tools.ietf.org/html/rfc6266#appendix-D
quoted_file_name = urllib.parse.quote(file_name)
header += f'; filename*=UTF-8\'\'{quoted_file_name}'
return header
else:
header += f'; filename="{file_name}"'
return header
2 changes: 1 addition & 1 deletion src/ZPublisher/tests/test_http_header_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ def test_unicode(self):
"""
self.assertEqual(
make_content_disposition("inline", "ıq.png"),
"""inline; filename="q.png"; filename*=UTF-8''%C4%B1q.png"""
"""inline; filename*=UTF-8''%C4%B1q.png"""
)

0 comments on commit a8031e2

Please sign in to comment.