Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit afde7e2

Browse files
committed
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
1 parent ce20967 commit afde7e2

3 files changed

Lines changed: 25 additions & 16 deletions

File tree

Lib/cgi.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -176,27 +176,26 @@ def parse_qs(qs, keep_blank_values=0, strict_parsing=0):
176176
def parse_qsl(qs, keep_blank_values=0, strict_parsing=0):
177177
"""Parse a query given as a string argument.
178178
179-
Arguments:
179+
Arguments:
180180
181-
qs: URL-encoded query string to be parsed
181+
qs: URL-encoded query string to be parsed
182182
183-
keep_blank_values: flag indicating whether blank values in
184-
URL encoded queries should be treated as blank strings.
185-
A true value indicates that blanks should be retained as
186-
blank strings. The default false value indicates that
187-
blank values are to be ignored and treated as if they were
188-
not included.
183+
keep_blank_values: flag indicating whether blank values in
184+
URL encoded queries should be treated as blank strings. A
185+
true value indicates that blanks should be retained as blank
186+
strings. The default false value indicates that blank values
187+
are to be ignored and treated as if they were not included.
189188
190-
strict_parsing: flag indicating what to do with parsing errors.
191-
If false (the default), errors are silently ignored.
192-
If true, errors raise a ValueError exception.
189+
strict_parsing: flag indicating what to do with parsing errors. If
190+
false (the default), errors are silently ignored. If true,
191+
errors raise a ValueError exception.
193192
194-
Returns a list, as God intended.
193+
Returns a list, as G-d intended.
195194
"""
196-
name_value_pairs = string.splitfields(qs, '&')
197-
r=[]
198-
for name_value in name_value_pairs:
199-
nv = string.splitfields(name_value, '=', 1)
195+
pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
196+
r = []
197+
for name_value in pairs:
198+
nv = name_value.split('=', 1)
200199
if len(nv) != 2:
201200
if strict_parsing:
202201
raise ValueError, "bad query field: %s" % `name_value`

Lib/test/output/test_cgi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ test_cgi
22
''
33
'&'
44
'&&'
5+
';'
6+
';&;'
57
'='
68
'=&='
9+
'=;='
710
'=a'
811
'&=a'
912
'=a&'
@@ -17,6 +20,8 @@ test_cgi
1720
'a=a+b&b=b+c'
1821
'a=a+b&a=b+a'
1922
'x=1&y=2.0&z=2-3.%2b0'
23+
'x=1;y=2.0&z=2-3.%2b0'
24+
'x=1;y=2.0;z=2-3.%2b0'
2025
'Hbc5161168c542333633315dee1182227:key_store_seqid=400006&cuyer=r&view=bustomer&order_id=0bb2e248638833d48cb7fed300000f1b&expire=964546263&lobale=en-US&kid=130003.300038&ss=env'
2126
'group_id=5470&set=custom&_assigned_to=31392&_status=1&_category=100&SUBMIT=Browse'
2227
Testing log

Lib/test/test_cgi.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@ def do_test(buf, method):
5858
("", ValueError("bad query field: ''")),
5959
("&", ValueError("bad query field: ''")),
6060
("&&", ValueError("bad query field: ''")),
61+
(";", ValueError("bad query field: ''")),
62+
(";&;", ValueError("bad query field: ''")),
6163
# Should the next few really be valid?
6264
("=", {}),
6365
("=&=", {}),
66+
("=;=", {}),
6467
# This rest seem to make sense
6568
("=a", {'': ['a']}),
6669
("&=a", ValueError("bad query field: ''")),
@@ -75,6 +78,8 @@ def do_test(buf, method):
7578
("a=a+b&b=b+c", {'a': ['a b'], 'b': ['b c']}),
7679
("a=a+b&a=b+a", {'a': ['a b', 'b a']}),
7780
("x=1&y=2.0&z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}),
81+
("x=1;y=2.0&z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}),
82+
("x=1;y=2.0;z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}),
7883
("Hbc5161168c542333633315dee1182227:key_store_seqid=400006&cuyer=r&view=bustomer&order_id=0bb2e248638833d48cb7fed300000f1b&expire=964546263&lobale=en-US&kid=130003.300038&ss=env",
7984
{'Hbc5161168c542333633315dee1182227:key_store_seqid': ['400006'],
8085
'cuyer': ['r'],

0 commit comments

Comments
 (0)