mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
fix bug #110661 (PR#356) -- accept either & or ; as separator for CGI
query string also some doc string reformatting and use of string methods instead of older string.splitfields
This commit is contained in:
parent
ce20967c2c
commit
afde7e24b6
3 changed files with 25 additions and 16 deletions
31
Lib/cgi.py
31
Lib/cgi.py
|
@ -176,27 +176,26 @@ def parse_qs(qs, keep_blank_values=0, strict_parsing=0):
|
||||||
def parse_qsl(qs, keep_blank_values=0, strict_parsing=0):
|
def parse_qsl(qs, keep_blank_values=0, strict_parsing=0):
|
||||||
"""Parse a query given as a string argument.
|
"""Parse a query given as a string argument.
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
|
|
||||||
qs: URL-encoded query string to be parsed
|
qs: URL-encoded query string to be parsed
|
||||||
|
|
||||||
keep_blank_values: flag indicating whether blank values in
|
keep_blank_values: flag indicating whether blank values in
|
||||||
URL encoded queries should be treated as blank strings.
|
URL encoded queries should be treated as blank strings. A
|
||||||
A true value indicates that blanks should be retained as
|
true value indicates that blanks should be retained as blank
|
||||||
blank strings. The default false value indicates that
|
strings. The default false value indicates that blank values
|
||||||
blank values are to be ignored and treated as if they were
|
are to be ignored and treated as if they were not included.
|
||||||
not included.
|
|
||||||
|
|
||||||
strict_parsing: flag indicating what to do with parsing errors.
|
strict_parsing: flag indicating what to do with parsing errors. If
|
||||||
If false (the default), errors are silently ignored.
|
false (the default), errors are silently ignored. If true,
|
||||||
If true, errors raise a ValueError exception.
|
errors raise a ValueError exception.
|
||||||
|
|
||||||
Returns a list, as God intended.
|
Returns a list, as G-d intended.
|
||||||
"""
|
"""
|
||||||
name_value_pairs = string.splitfields(qs, '&')
|
pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
|
||||||
r=[]
|
r = []
|
||||||
for name_value in name_value_pairs:
|
for name_value in pairs:
|
||||||
nv = string.splitfields(name_value, '=', 1)
|
nv = name_value.split('=', 1)
|
||||||
if len(nv) != 2:
|
if len(nv) != 2:
|
||||||
if strict_parsing:
|
if strict_parsing:
|
||||||
raise ValueError, "bad query field: %s" % `name_value`
|
raise ValueError, "bad query field: %s" % `name_value`
|
||||||
|
|
|
@ -2,8 +2,11 @@ test_cgi
|
||||||
''
|
''
|
||||||
'&'
|
'&'
|
||||||
'&&'
|
'&&'
|
||||||
|
';'
|
||||||
|
';&;'
|
||||||
'='
|
'='
|
||||||
'=&='
|
'=&='
|
||||||
|
'=;='
|
||||||
'=a'
|
'=a'
|
||||||
'&=a'
|
'&=a'
|
||||||
'=a&'
|
'=a&'
|
||||||
|
@ -17,6 +20,8 @@ test_cgi
|
||||||
'a=a+b&b=b+c'
|
'a=a+b&b=b+c'
|
||||||
'a=a+b&a=b+a'
|
'a=a+b&a=b+a'
|
||||||
'x=1&y=2.0&z=2-3.%2b0'
|
'x=1&y=2.0&z=2-3.%2b0'
|
||||||
|
'x=1;y=2.0&z=2-3.%2b0'
|
||||||
|
'x=1;y=2.0;z=2-3.%2b0'
|
||||||
'Hbc5161168c542333633315dee1182227:key_store_seqid=400006&cuyer=r&view=bustomer&order_id=0bb2e248638833d48cb7fed300000f1b&expire=964546263&lobale=en-US&kid=130003.300038&ss=env'
|
'Hbc5161168c542333633315dee1182227:key_store_seqid=400006&cuyer=r&view=bustomer&order_id=0bb2e248638833d48cb7fed300000f1b&expire=964546263&lobale=en-US&kid=130003.300038&ss=env'
|
||||||
'group_id=5470&set=custom&_assigned_to=31392&_status=1&_category=100&SUBMIT=Browse'
|
'group_id=5470&set=custom&_assigned_to=31392&_status=1&_category=100&SUBMIT=Browse'
|
||||||
Testing log
|
Testing log
|
||||||
|
|
|
@ -58,9 +58,12 @@ parse_test_cases = [
|
||||||
("", ValueError("bad query field: ''")),
|
("", ValueError("bad query field: ''")),
|
||||||
("&", ValueError("bad query field: ''")),
|
("&", ValueError("bad query field: ''")),
|
||||||
("&&", ValueError("bad query field: ''")),
|
("&&", ValueError("bad query field: ''")),
|
||||||
|
(";", ValueError("bad query field: ''")),
|
||||||
|
(";&;", ValueError("bad query field: ''")),
|
||||||
# Should the next few really be valid?
|
# Should the next few really be valid?
|
||||||
("=", {}),
|
("=", {}),
|
||||||
("=&=", {}),
|
("=&=", {}),
|
||||||
|
("=;=", {}),
|
||||||
# This rest seem to make sense
|
# This rest seem to make sense
|
||||||
("=a", {'': ['a']}),
|
("=a", {'': ['a']}),
|
||||||
("&=a", ValueError("bad query field: ''")),
|
("&=a", ValueError("bad query field: ''")),
|
||||||
|
@ -75,6 +78,8 @@ parse_test_cases = [
|
||||||
("a=a+b&b=b+c", {'a': ['a b'], 'b': ['b c']}),
|
("a=a+b&b=b+c", {'a': ['a b'], 'b': ['b c']}),
|
||||||
("a=a+b&a=b+a", {'a': ['a b', 'b a']}),
|
("a=a+b&a=b+a", {'a': ['a b', 'b a']}),
|
||||||
("x=1&y=2.0&z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}),
|
("x=1&y=2.0&z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}),
|
||||||
|
("x=1;y=2.0&z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}),
|
||||||
|
("x=1;y=2.0;z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}),
|
||||||
("Hbc5161168c542333633315dee1182227:key_store_seqid=400006&cuyer=r&view=bustomer&order_id=0bb2e248638833d48cb7fed300000f1b&expire=964546263&lobale=en-US&kid=130003.300038&ss=env",
|
("Hbc5161168c542333633315dee1182227:key_store_seqid=400006&cuyer=r&view=bustomer&order_id=0bb2e248638833d48cb7fed300000f1b&expire=964546263&lobale=en-US&kid=130003.300038&ss=env",
|
||||||
{'Hbc5161168c542333633315dee1182227:key_store_seqid': ['400006'],
|
{'Hbc5161168c542333633315dee1182227:key_store_seqid': ['400006'],
|
||||||
'cuyer': ['r'],
|
'cuyer': ['r'],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue