diff options
author | Senthil Kumaran <senthil@uthcode.com> | 2013-01-23 11:01:23 (GMT) |
---|---|---|
committer | Senthil Kumaran <senthil@uthcode.com> | 2013-01-23 11:01:23 (GMT) |
commit | 42d8773df4a2460080569d44949d644f602bd793 (patch) | |
tree | dbc519f7de8d9f4ee724bf5dc436c859f808db68 /Lib | |
parent | 560eff173c82ab57f88df06f63e4207280fd7ce6 (diff) | |
parent | c1a7c565aaf1dfa573337bbc9a5222097252cf3b (diff) | |
download | cpython-42d8773df4a2460080569d44949d644f602bd793.zip cpython-42d8773df4a2460080569d44949d644f602bd793.tar.gz cpython-42d8773df4a2460080569d44949d644f602bd793.tar.bz2 |
merge from 3.3
Issue #12411: Fix to cgi.parse_multipart to correctly use bytes boundaries and
bytes data. Patch by Jonas Wagner.
Diffstat (limited to 'Lib')
-rwxr-xr-x | Lib/cgi.py | 18 | ||||
-rw-r--r-- | Lib/test/test_cgi.py | 18 |
2 files changed, 27 insertions, 9 deletions
@@ -223,17 +223,17 @@ def parse_multipart(fp, pdict): """ import http.client - boundary = "" + boundary = b"" if 'boundary' in pdict: boundary = pdict['boundary'] if not valid_boundary(boundary): raise ValueError('Invalid boundary in multipart form: %r' % (boundary,)) - nextpart = "--" + boundary - lastpart = "--" + boundary + "--" + nextpart = b"--" + boundary + lastpart = b"--" + boundary + b"--" partdict = {} - terminator = "" + terminator = b"" 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 = "" + data = b"" # Read lines until end of part. lines = [] while 1: @@ -260,7 +260,7 @@ def parse_multipart(fp, pdict): if not line: terminator = lastpart # End outer loop break - if line.startswith("--"): + if line.startswith(b"--"): terminator = line.rstrip() if terminator in (nextpart, lastpart): break @@ -272,12 +272,12 @@ def parse_multipart(fp, pdict): if lines: # Strip final line terminator line = lines[-1] - if line[-2:] == "\r\n": + if line[-2:] == b"\r\n": line = line[:-2] - elif line[-1:] == "\n": + elif line[-1:] == b"\n": line = line[:-1] lines[-1] = line - data = "".join(lines) + data = b"".join(lines) line = headers['content-disposition'] if not line: continue diff --git a/Lib/test/test_cgi.py b/Lib/test/test_cgi.py index 5510a0d..cb28aa8 100644 --- 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')) + env = {'boundary': BOUNDARY.encode('latin1'), + 'CONTENT-LENGTH': '558'} + result = cgi.parse_multipart(fp, env) + expected = {'submit': [b' Add '], 'id': [b'1234'], + 'file': [b'Testing 123.\n'], 'title': [b'']} + self.assertEqual(result, expected) + + def test_fieldstorage_properties(self): + fs = cgi.FieldStorage() + self.assertFalse(fs) + self.assertIn("FieldStorage", repr(fs)) + self.assertEqual(list(fs), list(fs.keys())) + fs.list.append(namedtuple('MockFieldStorage', 'name')('fieldvalue')) + self.assertTrue(fs) + def test_escape(self): # cgi.escape() is deprecated. with warnings.catch_warnings(): |