cpython: 3d7000549eb1 (original) (raw)
Mercurial > cpython
changeset 81676:3d7000549eb1
merge from 3.3 Issue #12411: Fix to cgi.parse_multipart to correctly use bytes boundaries and bytes data. Patch by Jonas Wagner. [#12411]
Senthil Kumaran senthil@uthcode.com | |
---|---|
date | Wed, 23 Jan 2013 03:01:23 -0800 |
parents | 02e7da4c4fe3(current diff)59ea872d8b6b(diff) |
children | cd87afe18ff8 |
files | Lib/cgi.py Misc/NEWS |
diffstat | 3 files changed, 30 insertions(+), 9 deletions(-)[+] [-] Lib/cgi.py 18 Lib/test/test_cgi.py 18 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/cgi.py +++ b/Lib/cgi.py @@ -223,17 +223,17 @@ def parse_multipart(fp, pdict): """ import http.client
- boundary = b"" if 'boundary' in pdict: boundary = pdict['boundary'] if not valid_boundary(boundary): raise ValueError('Invalid boundary in multipart form: %r' % (boundary,))
while terminator != lastpart: bytes = -1 @@ -252,7 +252,7 @@ def parse_multipart(fp, pdict): raise ValueError('Maximum content length exceeded') data = fp.read(bytes) else:
data = ""[](#l1.29)
data = b""[](#l1.30) # Read lines until end of part.[](#l1.31) lines = [][](#l1.32) while 1:[](#l1.33)
@@ -260,7 +260,7 @@ def parse_multipart(fp, pdict): if not line: terminator = lastpart # End outer loop break
if line.startswith("--"):[](#l1.38)
if line.startswith(b"--"):[](#l1.39) terminator = line.rstrip()[](#l1.40) if terminator in (nextpart, lastpart):[](#l1.41) break[](#l1.42)
@@ -272,12 +272,12 @@ def parse_multipart(fp, pdict): if lines: # Strip final line terminator line = lines[-1]
if line[-2:] == "\r\n":[](#l1.47)
if line[-2:] == b"\r\n":[](#l1.48) line = line[:-2][](#l1.49)
elif line[-1:] == "\n":[](#l1.50)
elif line[-1:] == b"\n":[](#l1.51) line = line[:-1][](#l1.52) lines[-1] = line[](#l1.53)
data = "".join(lines)[](#l1.54)
data = b"".join(lines)[](#l1.55) line = headers['content-disposition'][](#l1.56) if not line:[](#l1.57) continue[](#l1.58)
--- a/Lib/test/test_cgi.py +++ b/Lib/test/test_cgi.py @@ -5,6 +5,7 @@ import sys import tempfile import unittest import warnings +from collections import namedtuple from io import StringIO, BytesIO class HackedSysModule: @@ -119,6 +120,23 @@ def gen_result(data, environ): class CgiTests(unittest.TestCase):
- def test_parse_multipart(self):
fp = BytesIO(POSTDATA.encode('latin1'))[](#l2.16)
env = {'boundary': BOUNDARY.encode('latin1'),[](#l2.17)
'CONTENT-LENGTH': '558'}[](#l2.18)
result = cgi.parse_multipart(fp, env)[](#l2.19)
expected = {'submit': [b' Add '], 'id': [b'1234'],[](#l2.20)
'file': [b'Testing 123.\n'], 'title': [b'']}[](#l2.21)
self.assertEqual(result, expected)[](#l2.22)
- def test_fieldstorage_properties(self):
fs = cgi.FieldStorage()[](#l2.25)
self.assertFalse(fs)[](#l2.26)
self.assertIn("FieldStorage", repr(fs))[](#l2.27)
self.assertEqual(list(fs), list(fs.keys()))[](#l2.28)
fs.list.append(namedtuple('MockFieldStorage', 'name')('fieldvalue'))[](#l2.29)
self.assertTrue(fs)[](#l2.30)
+ def test_escape(self): # cgi.escape() is deprecated. with warnings.catch_warnings():
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -220,6 +220,9 @@ Core and Builtins Library ------- +- Issue #12411: Fix to cgi.parse_multipart to correctly use bytes boundaries