mirror of https://github.com/python/cpython.git
Bug #735248: Fix urllib2.parse_http_list.
This commit is contained in:
parent
256372c88c
commit
e1b13d2019
|
@ -45,6 +45,14 @@ def test_statudict(self):
|
||||||
# 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 = []
|
||||||
|
|
|
@ -1072,46 +1072,43 @@ def parse_http_list(s):
|
||||||
|
|
||||||
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 = []
|
escape = quote = False
|
||||||
end = len(s)
|
for cur in s:
|
||||||
i = 0
|
if escape:
|
||||||
inquote = 0
|
part += cur
|
||||||
start = 0
|
escape = False
|
||||||
while i < end:
|
continue
|
||||||
cur = s[i:]
|
if quote:
|
||||||
c = cur.find(',')
|
if cur == '\\':
|
||||||
q = cur.find('"')
|
escape = True
|
||||||
if c == -1:
|
|
||||||
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
|
||||||
|
|
Loading…
Reference in New Issue