[Python-bugs-list] [ python-Bugs-508665 ] Improvement of cgi.parse_qsl function

noreply@sourceforge.net noreply@sourceforge.net
Fri, 25 Jan 2002 12:41:25 -0800


Bugs item #508665, was opened at 2002-01-25 12:23
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=508665&group_id=5470

Category: Python Library
Group: Python 2.2
Status: Open
Resolution: None
Priority: 5
Submitted By: Christoph Zwerschke (cito)
Assigned to: Nobody/Anonymous (nobody)
Summary: Improvement of cgi.parse_qsl function

Initial Comment:
I found the parsing function "parse_qsl" in the 
module "cgi" to have some flaws. Especially, empty 
names are allowed, even if empty values are explicitly 
disallowed. If the latter are allowed, "?name=" is 
accepted, while "?name" is ignored. Often you want to 
use links like "?logout" or "?help". This is not 
possible, even if empty values are explicitly allowed. 
Also, "strict parsing" objects to "?name=", while it 
ignores "?name=a=b=c". My improvement suggestion:

------------- use ----------

for name_value in pairs:
    if strict_parsing:
        nv = name_value.split('=', 2)
        if len(nv) != 2 or not len(nv[0]):
            raise ValueError, "bad query field: %s" % 
`name_value`
    else:
        nv = name_value.split('=', 1).append('')
        if not len(nv[0]):
            continue
    if len(nv[1]) or keep_blank_values:
        name = urllib.unquote(nv[0].replace('+', ' '))
        value = urllib.unquote(nv[1].replace('+', ' '))
        r.append((name, value))

----------- instead of --------

for name_value in pairs:
    nv = name_value.split('=', 1)
    if len(nv) != 2:
        if strict_parsing:
            raise ValueError, "bad query field: %s" % 
`name_value`
        continue
    if len(nv[1]) or keep_blank_values:
        name = urllib.unquote(nv[0].replace('+', ' '))
        value = urllib.unquote(nv[1].replace('+', ' '))
        r.append((name, value))


----------------------------------------------------------------------

>Comment By: Christoph Zwerschke (cito)
Date: 2002-01-25 12:41

Message:
Logged In: YES 
user_id=193957

-------- better use: ---------- 

<pre>

for name_value in pairs:
    if strict_parsing:
        nv = name_value.split('=', 2)
        if len(nv) != 2 or not len(nv[0]):
            raise ValueError, "bad query field: %s" % 
`name_value`
    else:
        nv = name_value.split('=', 1)
        if not len(nv[0]):
            continue
        if len(nv) != 2:
            nv.append('')
    if len(nv[1]) or keep_blank_values:
        name = urllib.unquote(nv[0].replace('+', ' '))
        value = urllib.unquote(nv[1].replace('+', ' '))
        r.append((name, value))

</pre>

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=508665&group_id=5470