Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1a7c56

Browse files
committed
merge from 3.2
Issue #12411: Fix to cgi.parse_multipart to correctly use bytes boundaries and bytes data. Patch by Jonas Wagner.
2 parents 9427b03 + 6b102f2 commit c1a7c56

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

Lib/cgi.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,17 @@ def parse_multipart(fp, pdict):
223223
"""
224224
import http.client
225225

226-
boundary = ""
226+
boundary = b""
227227
if 'boundary' in pdict:
228228
boundary = pdict['boundary']
229229
if not valid_boundary(boundary):
230230
raise ValueError('Invalid boundary in multipart form: %r'
231231
% (boundary,))
232232

233-
nextpart = "--" + boundary
234-
lastpart = "--" + boundary + "--"
233+
nextpart = b"--" + boundary
234+
lastpart = b"--" + boundary + b"--"
235235
partdict = {}
236-
terminator = ""
236+
terminator = b""
237237

238238
while terminator != lastpart:
239239
bytes = -1
@@ -252,15 +252,15 @@ def parse_multipart(fp, pdict):
252252
raise ValueError('Maximum content length exceeded')
253253
data = fp.read(bytes)
254254
else:
255-
data = ""
255+
data = b""
256256
# Read lines until end of part.
257257
lines = []
258258
while 1:
259259
line = fp.readline()
260260
if not line:
261261
terminator = lastpart # End outer loop
262262
break
263-
if line.startswith("--"):
263+
if line.startswith(b"--"):
264264
terminator = line.rstrip()
265265
if terminator in (nextpart, lastpart):
266266
break
@@ -272,12 +272,12 @@ def parse_multipart(fp, pdict):
272272
if lines:
273273
# Strip final line terminator
274274
line = lines[-1]
275-
if line[-2:] == "\r\n":
275+
if line[-2:] == b"\r\n":
276276
line = line[:-2]
277-
elif line[-1:] == "\n":
277+
elif line[-1:] == b"\n":
278278
line = line[:-1]
279279
lines[-1] = line
280-
data = "".join(lines)
280+
data = b"".join(lines)
281281
line = headers['content-disposition']
282282
if not line:
283283
continue

Lib/test/test_cgi.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import tempfile
66
import unittest
77
import warnings
8+
from collections import namedtuple
89
from io import StringIO, BytesIO
910

1011
class HackedSysModule:
@@ -119,6 +120,23 @@ def gen_result(data, environ):
119120

120121
class CgiTests(unittest.TestCase):
121122

123+
def test_parse_multipart(self):
124+
fp = BytesIO(POSTDATA.encode('latin1'))
125+
env = {'boundary': BOUNDARY.encode('latin1'),
126+
'CONTENT-LENGTH': '558'}
127+
result = cgi.parse_multipart(fp, env)
128+
expected = {'submit': [b' Add '], 'id': [b'1234'],
129+
'file': [b'Testing 123.\n'], 'title': [b'']}
130+
self.assertEqual(result, expected)
131+
132+
def test_fieldstorage_properties(self):
133+
fs = cgi.FieldStorage()
134+
self.assertFalse(fs)
135+
self.assertIn("FieldStorage", repr(fs))
136+
self.assertEqual(list(fs), list(fs.keys()))
137+
fs.list.append(namedtuple('MockFieldStorage', 'name')('fieldvalue'))
138+
self.assertTrue(fs)
139+
122140
def test_escape(self):
123141
# cgi.escape() is deprecated.
124142
with warnings.catch_warnings():

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ Core and Builtins
150150
Library
151151
-------
152152

153+
- Issue #12411: Fix to cgi.parse_multipart to correctly use bytes boundaries
154+
and bytes data. Patch by Jonas Wagner.
155+
153156
- Issue #16957: shutil.which() no longer searches a bare file name in the
154157
current directory on Unix and no longer searches a relative file path with
155158
a directory part in PATH directories. Patch by Thomas Kluyver.

0 commit comments

Comments
 (0)