Commit e1b13d20 authored by Georg Brandl's avatar Georg Brandl

Bug #735248: Fix urllib2.parse_http_list.

parent 256372c8
...@@ -45,6 +45,14 @@ class TrivialTests(unittest.TestCase): ...@@ -45,6 +45,14 @@ class TrivialTests(unittest.TestCase):
# test the new-in-2.5 httpresponses dictionary # test the new-in-2.5 httpresponses dictionary
self.assertEquals(urllib2.httpresponses[404], "Not Found") self.assertEquals(urllib2.httpresponses[404], "Not Found")
def test_parse_http_list(self):
tests = [('a,b,c', ['a', 'b', 'c']),
('path"o,l"og"i"cal, example', ['path"o,l"og"i"cal', 'example']),
('a, b, "c", "d", "e,f", g, h', ['a', 'b', '"c"', '"d"', '"e,f"', 'g', 'h']),
('a="b\\"c", d="e\\,f", g="h\\\\i"', ['a="b"c"', 'd="e,f"', 'g="h\\i"'])]
for string, list in tests:
self.assertEquals(urllib2.parse_http_list(string), list)
class MockOpener: class MockOpener:
addheaders = [] addheaders = []
......
...@@ -1069,49 +1069,46 @@ def parse_keqv_list(l): ...@@ -1069,49 +1069,46 @@ def parse_keqv_list(l):
def parse_http_list(s): def parse_http_list(s):
"""Parse lists as described by RFC 2068 Section 2. """Parse lists as described by RFC 2068 Section 2.
In particular, parse comma-separated lists where the elements of In particular, parse comma-separated lists where the elements of
the list may include quoted-strings. A quoted-string could the list may include quoted-strings. A quoted-string could
contain a comma. contain a comma. A non-quoted string could have quotes in the
middle. Neither commas nor quotes count if they are escaped.
Only double-quotes count, not single-quotes.
""" """
# XXX this function could probably use more testing res = []
part = ''
list = []
end = len(s) escape = quote = False
i = 0 for cur in s:
inquote = 0 if escape:
start = 0 part += cur
while i < end: escape = False
cur = s[i:] continue
c = cur.find(',') if quote:
q = cur.find('"') if cur == '\\':
if c == -1: escape = True
list.append(s[start:])
break
if q == -1:
if inquote:
raise ValueError, "unbalanced quotes"
else:
list.append(s[start:i+c])
i = i + c + 1
continue continue
if inquote: elif cur == '"':
if q < c: quote = False
list.append(s[start:i+c]) part += cur
i = i + c + 1 continue
start = i
inquote = 0 if cur == ',':
else: res.append(part)
i = i + q part = ''
else: continue
if c < q:
list.append(s[start:i+c]) if cur == '"':
i = i + c + 1 quote = True
start = i
else: part += cur
inquote = 1
i = i + q + 1 # append last part
return map(lambda x: x.strip(), list) if part:
res.append(part)
return [part.strip() for part in res]
class FileHandler(BaseHandler): class FileHandler(BaseHandler):
# Use local file or FTP depending on form of URL # Use local file or FTP depending on form of URL
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment