summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2008-12-04 19:01:28 (GMT)
committerFred Drake <fdrake@acm.org>2008-12-04 19:01:28 (GMT)
commit0c7dfa3e12650c06c3fc49804442d44448a04cfe (patch)
tree02fe1d3480b9757ade807e176baf0cad007dd5bf
parent8ad4f8018a83a220c7dab77cf8bf58b101d82fd9 (diff)
downloadcpython-0c7dfa3e12650c06c3fc49804442d44448a04cfe.zip
cpython-0c7dfa3e12650c06c3fc49804442d44448a04cfe.tar.gz
cpython-0c7dfa3e12650c06c3fc49804442d44448a04cfe.tar.bz2
Merged revisions 67528 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r67528 | fred.drake | 2008-12-04 13:25:17 -0500 (Thu, 04 Dec 2008) | 4 lines Issue #1055234: cgi.parse_header(): Fixed parsing of header parameters to support unusual filenames (such as those containing semi-colons) in Content-Disposition headers. ........
-rwxr-xr-xLib/cgi.py18
-rw-r--r--Lib/test/test_cgi.py26
-rw-r--r--Misc/NEWS4
3 files changed, 45 insertions, 3 deletions
diff --git a/Lib/cgi.py b/Lib/cgi.py
index 33b91bf..0bb5b8ea 100755
--- a/Lib/cgi.py
+++ b/Lib/cgi.py
@@ -289,16 +289,28 @@ def parse_multipart(fp, pdict):
return partdict
+def _parseparam(s):
+ while s[:1] == ';':
+ s = s[1:]
+ end = s.find(';')
+ while end > 0 and s.count('"', 0, end) % 2:
+ end = s.find(';', end + 1)
+ if end < 0:
+ end = len(s)
+ f = s[:end]
+ yield f.strip()
+ s = s[end:]
+
def parse_header(line):
"""Parse a Content-type like header.
Return the main content-type and a dictionary of options.
"""
- plist = [x.strip() for x in line.split(';')]
- key = plist.pop(0).lower()
+ parts = _parseparam(';' + line)
+ key = parts.next()
pdict = {}
- for p in plist:
+ for p in parts:
i = p.find('=')
if i >= 0:
name = p[:i].strip().lower()
diff --git a/Lib/test/test_cgi.py b/Lib/test/test_cgi.py
index fa1d37f..800f629 100644
--- a/Lib/test/test_cgi.py
+++ b/Lib/test/test_cgi.py
@@ -354,6 +354,32 @@ this is the content of the fake file
self.assertEqual([('a', 'A1'), ('b', 'B2'), ('B', 'B3')],
cgi.parse_qsl('a=A1&b=B2&B=B3'))
+ def test_parse_header(self):
+ self.assertEqual(
+ cgi.parse_header("text/plain"),
+ ("text/plain", {}))
+ self.assertEqual(
+ cgi.parse_header("text/vnd.just.made.this.up ; "),
+ ("text/vnd.just.made.this.up", {}))
+ self.assertEqual(
+ cgi.parse_header("text/plain;charset=us-ascii"),
+ ("text/plain", {"charset": "us-ascii"}))
+ self.assertEqual(
+ cgi.parse_header('text/plain ; charset="us-ascii"'),
+ ("text/plain", {"charset": "us-ascii"}))
+ self.assertEqual(
+ cgi.parse_header('text/plain ; charset="us-ascii"; another=opt'),
+ ("text/plain", {"charset": "us-ascii", "another": "opt"}))
+ self.assertEqual(
+ cgi.parse_header('attachment; filename="silly.txt"'),
+ ("attachment", {"filename": "silly.txt"}))
+ self.assertEqual(
+ cgi.parse_header('attachment; filename="strange;name"'),
+ ("attachment", {"filename": "strange;name"}))
+ self.assertEqual(
+ cgi.parse_header('attachment; filename="strange;name";size=123;'),
+ ("attachment", {"filename": "strange;name", "size": "123"}))
+
def test_main():
run_unittest(CgiTests)
diff --git a/Misc/NEWS b/Misc/NEWS
index abe8630..653a39d 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -52,6 +52,10 @@ Core and Builtins
Library
-------
+- Issue #1055234: cgi.parse_header(): Fixed parsing of header parameters to
+ support unusual filenames (such as those containing semi-colons) in
+ Content-Disposition headers.
+
- Issue #3741: DISTUTILS_USE_SDK set causes msvc9compiler.py to raise an
exception.