Skip to content

Commit

Permalink
Merge pull request #75 from zopefoundation/fix-for-multipart-1.0.0
Browse files Browse the repository at this point in the history
Fixes for multipart 1.0.0
  • Loading branch information
mgedmin authored Sep 25, 2024
2 parents 023e142 + 6b9710a commit 7f077e9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
10 changes: 9 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
7.1 (unreleased)
================

- Nothing changed yet.
- Fix test suite to use proper line endings (\r\n) in raw multipart/form-data
HTTP requests, because multipart 1.0.0 is stricter about line endings.
Fixes `issue <https://github.com/zopefoundation/zope.publisher/issues/74>`_.

- ``FileUpload`` objects now implement a fallback ``seekable()`` method on
Python 3.7 through 3.10, where tempfile.SpooledTemporaryFile lacks it.
Fixes `issue 44 <https://github.com/zopefoundation/zope.publisher/issues/44>`_
again, which had regressed due to certain assumptions that were no longer
true after the multipart 1.0.0 release.


7.0 (2023-08-29)
Expand Down
7 changes: 7 additions & 0 deletions src/zope/publisher/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
packaged into a nice, Python-friendly 'FileUpload' object.
"""
import re
import tempfile
from email.message import Message
from urllib.parse import parse_qsl

Expand Down Expand Up @@ -676,6 +677,12 @@ def __init__(self, aFieldStorage):
if hasattr(file, m):
d[m] = getattr(file, m)

if 'seekable' not in d and isinstance(
file, tempfile.SpooledTemporaryFile
): # Python 3.7 to 3.10
# NB: can't assign file._file.seekable, file._file might roll over
d['seekable'] = lambda: file._file.seekable()

self.headers = aFieldStorage.headers
filename = aFieldStorage.filename
if filename is not None:
Expand Down
18 changes: 9 additions & 9 deletions src/zope/publisher/tests/test_browserrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,30 @@
Content-Type: application/octet-stream
-----------------------------1--
"""
""".replace(b'\n', b'\r\n')

LARGE_FILE_BODY = b''.join([b"""-----------------------------1
Content-Disposition: form-data; name="upload"; filename="test"
Content-Type: text/plain
Here comes some text! """, (b'test' * 1000), b"""
-----------------------------1--
"""])
"""]).replace(b'\n', b'\r\n')

LARGE_POSTED_VALUE = b''.join([b"""-----------------------------1
Content-Disposition: form-data; name="upload"
Here comes some text! """, (b'test' * 1000), b"""
-----------------------------1--
"""])
"""]).replace(b'\n', b'\r\n')

IE_FILE_BODY = b"""-----------------------------1
Content-Disposition: form-data; name="upload"; filename="C:\\Windows\\notepad.exe"
Content-Type: text/plain
Some data
-----------------------------1--
""" # noqa: E501 line too long
""".replace(b'\n', b'\r\n') # noqa: E501 line too long


def publish(request):
Expand Down Expand Up @@ -307,7 +307,7 @@ def testFormMultipartUTF8(self):
'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': 'multipart/form_data; boundary=-123',
}
body = b'\n'.join([
body = b'\r\n'.join([
b'---123',
b'Content-Disposition: form-data; name="a"',
b'',
Expand All @@ -333,7 +333,7 @@ def testFormMultipartFilenameUTF8(self):
'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': 'multipart/form_data; boundary=-123',
}
body = b'\n'.join([
body = b'\r\n'.join([
b'---123',
b'Content-Disposition: form-data; name="upload";'
b' filename="\xe2\x98\x83"',
Expand All @@ -355,7 +355,7 @@ def testFormMultipartFilenameLatin7(self):
'CONTENT_TYPE': (
'multipart/form_data; boundary=-123; charset=ISO-8859-13'),
}
body = b'\n'.join([
body = b'\r\n'.join([
b'---123',
b'Content-Disposition: form-data; name="upload";'
b' filename="\xc0\xfeuolyno"',
Expand All @@ -376,7 +376,7 @@ def testFormMultipartFilenameLatin7DefaultCharset(self):
'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': 'multipart/form_data; boundary=-123',
}
body = b'\n'.join([
body = b'\r\n'.join([
b'---123',
b'Content-Disposition: form-data; name="upload";'
b' filename="\xc0\xfeuolyno"',
Expand Down Expand Up @@ -681,7 +681,7 @@ def testFormMultipartDuplicateFieldNames(self):
'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': 'multipart/form_data; boundary=-123',
}
body = b'\n'.join([
body = b'\r\n'.join([
b'---123',
b'Content-Disposition: form-data; name="a"',
b'',
Expand Down

0 comments on commit 7f077e9

Please sign in to comment.