cpython: 406ce103c170 (original) (raw)
Mercurial > cpython
changeset 84482:406ce103c170 3.3
Issue #18013: Fix cgi.FieldStorage to parse the W3C sample form. [#18013]
Florent Xicluna florent.xicluna@gmail.com | |
---|---|
date | Sun, 07 Jul 2013 12:44:28 +0200 |
parents | 738cba2bf849 |
children | 2ab2a2bfea49 65fce1dad331 |
files | Lib/cgi.py Lib/test/test_cgi.py Misc/NEWS |
diffstat | 3 files changed, 49 insertions(+), 1 deletions(-)[+] [-] Lib/cgi.py 2 Lib/test/test_cgi.py 46 Misc/NEWS 2 |
line wrap: on
line diff
--- a/Lib/cgi.py +++ b/Lib/cgi.py @@ -699,7 +699,7 @@ class FieldStorage: self.encoding, self.errors) self.bytes_read += part.bytes_read self.list.append(part)
if self.bytes_read >= self.length:[](#l1.7)
if part.done or self.bytes_read >= self.length > 0:[](#l1.8) break[](#l1.9) self.skip_lines()[](#l1.10)
--- a/Lib/test/test_cgi.py +++ b/Lib/test/test_cgi.py @@ -279,6 +279,27 @@ Content-Type: text/plain check('x' * (maxline - 1) + '\r') check('x' * (maxline - 1) + '\r' + 'y' * (maxline - 1))
- def test_fieldstorage_multipart_w3c(self):
# Test basic FieldStorage multipart parsing (W3C sample)[](#l2.8)
env = {[](#l2.9)
'REQUEST_METHOD': 'POST',[](#l2.10)
'CONTENT_TYPE': 'multipart/form-data; boundary={}'.format(BOUNDARY_W3),[](#l2.11)
'CONTENT_LENGTH': str(len(POSTDATA_W3))}[](#l2.12)
fp = BytesIO(POSTDATA_W3.encode('latin-1'))[](#l2.13)
fs = cgi.FieldStorage(fp, environ=env, encoding="latin-1")[](#l2.14)
self.assertEqual(len(fs.list), 2)[](#l2.15)
self.assertEqual(fs.list[0].name, 'submit-name')[](#l2.16)
self.assertEqual(fs.list[0].value, 'Larry')[](#l2.17)
self.assertEqual(fs.list[1].name, 'files')[](#l2.18)
files = fs.list[1].value[](#l2.19)
self.assertEqual(len(files), 2)[](#l2.20)
expect = [{'name': None, 'filename': 'file1.txt', 'value': b'... contents of file1.txt ...'},[](#l2.21)
{'name': None, 'filename': 'file2.gif', 'value': b'...contents of file2.gif...'}][](#l2.22)
for x in range(len(files)):[](#l2.23)
for k, exp in expect[x].items():[](#l2.24)
got = getattr(files[x], k)[](#l2.25)
self.assertEqual(got, exp)[](#l2.26)
+ _qs_result = { 'key1': 'value1', 'key2': ['value2x', 'value2y'], @@ -428,6 +449,31 @@ Content-Disposition: form-data; name="id -----------------------------721837373350705526688164684 """ +# http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4[](#l2.35) +BOUNDARY_W3 = "AaB03x" +POSTDATA_W3 = """--AaB03x +Content-Disposition: form-data; name="submit-name" + +Larry +--AaB03x +Content-Disposition: form-data; name="files" +Content-Type: multipart/mixed; boundary=BbC04y + +--BbC04y +Content-Disposition: file; filename="file1.txt" +Content-Type: text/plain + +... contents of file1.txt ... +--BbC04y +Content-Disposition: file; filename="file2.gif" +Content-Type: image/gif +Content-Transfer-Encoding: binary + +...contents of file2.gif... +--BbC04y-- +--AaB03x-- +""" + def test_main(): run_unittest(CgiTests)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -41,6 +41,8 @@ Core and Builtins Library ------- +- Issue #18013: Fix cgi.FieldStorage to parse the W3C sample form. +