Skip to content

Commit 9a0a65b

Browse files
committed
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. ........
1 parent c47408a commit 9a0a65b

3 files changed

Lines changed: 46 additions & 3 deletions

File tree

Lib/cgi.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,16 +272,28 @@ def parse_multipart(fp, pdict):
272272
return partdict
273273

274274

275+
def _parseparam(s):
276+
while s[:1] == ';':
277+
s = s[1:]
278+
end = s.find(';')
279+
while end > 0 and s.count('"', 0, end) % 2:
280+
end = s.find(';', end + 1)
281+
if end < 0:
282+
end = len(s)
283+
f = s[:end]
284+
yield f.strip()
285+
s = s[end:]
286+
275287
def parse_header(line):
276288
"""Parse a Content-type like header.
277289
278290
Return the main content-type and a dictionary of options.
279291
280292
"""
281-
plist = [x.strip() for x in line.split(';')]
282-
key = plist.pop(0).lower()
293+
parts = _parseparam(';' + line)
294+
key = parts.__next__()
283295
pdict = {}
284-
for p in plist:
296+
for p in parts:
285297
i = p.find('=')
286298
if i >= 0:
287299
name = p[:i].strip().lower()

Lib/test/test_cgi.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,33 @@ def test_deprecated_parse_qsl(self):
325325
self.assertEqual([('a', 'A1'), ('b', 'B2'), ('B', 'B3')],
326326
cgi.parse_qsl('a=A1&b=B2&B=B3'))
327327

328+
def test_parse_header(self):
329+
self.assertEqual(
330+
cgi.parse_header("text/plain"),
331+
("text/plain", {}))
332+
self.assertEqual(
333+
cgi.parse_header("text/vnd.just.made.this.up ; "),
334+
("text/vnd.just.made.this.up", {}))
335+
self.assertEqual(
336+
cgi.parse_header("text/plain;charset=us-ascii"),
337+
("text/plain", {"charset": "us-ascii"}))
338+
self.assertEqual(
339+
cgi.parse_header('text/plain ; charset="us-ascii"'),
340+
("text/plain", {"charset": "us-ascii"}))
341+
self.assertEqual(
342+
cgi.parse_header('text/plain ; charset="us-ascii"; another=opt'),
343+
("text/plain", {"charset": "us-ascii", "another": "opt"}))
344+
self.assertEqual(
345+
cgi.parse_header('attachment; filename="silly.txt"'),
346+
("attachment", {"filename": "silly.txt"}))
347+
self.assertEqual(
348+
cgi.parse_header('attachment; filename="strange;name"'),
349+
("attachment", {"filename": "strange;name"}))
350+
self.assertEqual(
351+
cgi.parse_header('attachment; filename="strange;name";size=123;'),
352+
("attachment", {"filename": "strange;name", "size": "123"}))
353+
354+
328355
def test_main():
329356
run_unittest(CgiTests)
330357

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Core and Builtins
1616
Library
1717
-------
1818

19+
- Issue #1055234: cgi.parse_header(): Fixed parsing of header parameters to
20+
support unusual filenames (such as those containing semi-colons) in
21+
Content-Disposition headers.
22+
1923

2024
Build
2125
-----

0 commit comments

Comments
 (0)