[Python-checkins] cpython (3.2): Issue #12411: Fix to cgi.parse_multipart to correctly use bytes boundaries and

senthil.kumaran python-checkins at python.org
Wed Jan 23 12:01:37 CET 2013


http://hg.python.org/cpython/rev/a46a0dafcb7a
changeset:   81674:a46a0dafcb7a
branch:      3.2
parent:      81671:006d8740e14c
user:        Senthil Kumaran <senthil at uthcode.com>
date:        Wed Jan 23 02:50:15 2013 -0800
summary:
  Issue #12411: Fix to cgi.parse_multipart to correctly use bytes boundaries and
bytes data. Patch by Jonas Wagner.

files:
  Lib/cgi.py           |  18 +++++++++---------
  Lib/test/test_cgi.py |  21 ++++++++++++++++++++-
  Misc/NEWS            |   3 +++
  3 files changed, 32 insertions(+), 10 deletions(-)


diff --git a/Lib/cgi.py b/Lib/cgi.py
--- a/Lib/cgi.py
+++ b/Lib/cgi.py
@@ -214,17 +214,17 @@
     """
     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
@@ -243,7 +243,7 @@
                     raise ValueError('Maximum content length exceeded')
                 data = fp.read(bytes)
             else:
-                data = ""
+                data = b""
         # Read lines until end of part.
         lines = []
         while 1:
@@ -251,7 +251,7 @@
             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
@@ -263,12 +263,12 @@
             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
--- a/Lib/test/test_cgi.py
+++ b/Lib/test/test_cgi.py
@@ -4,6 +4,7 @@
 import sys
 import tempfile
 import unittest
+from collections import namedtuple
 from io import StringIO, BytesIO
 
 class HackedSysModule:
@@ -118,6 +119,23 @@
 
 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):
         self.assertEqual("test & string", cgi.escape("test & string"))
         self.assertEqual("<test string>", cgi.escape("<test string>"))
@@ -151,7 +169,8 @@
 
     def test_log(self):
         cgi.log("Testing")
-
+        cgi.logfile = "fail/"
+        cgi.initlog("%s", "Testing initlog")
         cgi.logfp = StringIO()
         cgi.initlog("%s", "Testing initlog 1")
         cgi.log("%s", "Testing log 2")
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -202,6 +202,9 @@
 Library
 -------
 
+- Issue #12411: Fix to cgi.parse_multipart to correctly use bytes boundaries
+  and bytes data. Patch by Jonas Wagner.
+
 - Issue #1159051: GzipFile now raises EOFError when reading a corrupted file
   with truncated header or footer.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list