-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement custom code for header creation
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
Showing
3 changed files
with
28 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters