Skip to content

Commit

Permalink
Use latin-1 decoded filename in tests with FileUpload.
Browse files Browse the repository at this point in the history
This seems to be what browsers actually use, or how the filename is at this point in the zope publishing machinery.
See zopefoundation/Zope#1094 (comment)
mauritsvanrees committed Mar 8, 2023
1 parent 244293b commit 26c5285
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 2 additions & 0 deletions news/1094.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Test fix: use latin-1 decoded filename in tests with FileUpload.
[maurits]
2 changes: 1 addition & 1 deletion plone/formwidget/namedfile/converter.py
Original file line number Diff line number Diff line change
@@ -71,7 +71,7 @@ def b64encode_file(filename, data):

def b64decode_file(value):
if isinstance(value, str):
value = value.encode("utf8")
value = value.encode("utf-8")
filename, data = value.split(b";")

filename = filename.split(b":")[1]
8 changes: 5 additions & 3 deletions plone/formwidget/namedfile/widget.rst
Original file line number Diff line number Diff line change
@@ -496,12 +496,13 @@ content type::

>>> myfile = six.BytesIO(b'File upload contents.')
>>> # \xc3\xb8 is UTF-8 for a small letter o with slash
>>> aFieldStorage = FieldStorageStub(myfile, filename=b'rand\xc3\xb8m.txt'.decode('utf8'),
>>> # Still, we must decode it using latin-1 according to HTTP/1.1.
>>> aFieldStorage = FieldStorageStub(myfile, filename=b'rand\xc3\xb8m.txt'.decode('latin-1'),
... headers={'Content-Type': 'text/x-dummy'})
>>> file_obj = file_converter.toFieldValue(FileUpload(aFieldStorage))
>>> file_obj.data
b'File upload contents.'
>>> file_obj.filename.encode('utf8')
>>> file_obj.filename.encode('utf-8')
b'rand\xc3\xb8m.txt'

Content type from headers sent by browser should be ignored::
@@ -640,7 +641,8 @@ filename too::

>>> myfile = six.BytesIO(b'File upload contents.')
>>> # \xc3\xb8 is UTF-8 for a small letter o with slash
>>> aFieldStorage = FieldStorageStub(myfile, filename=b'rand\xc3\xb8m.txt'.decode('utf8'),
>>> # Still, we must decode it using latin-1 according to HTTP/1.1.
>>> aFieldStorage = FieldStorageStub(myfile, filename=b'rand\xc3\xb8m.txt'.decode('latin-1'),
... headers={'Content-Type': 'text/x-dummy'})
>>> bytes_file_converter.toFieldValue(FileUpload(aFieldStorage))
b'filenameb64:cmFuZMO4bS50eHQ=;datab64:RmlsZSB1cGxvYWQgY29udGVudHMu'

0 comments on commit 26c5285

Please sign in to comment.