"""
This module defines a subClass of the standard Python cgi.FieldStorage class
with an extra method that will allow a FieldStorage to parse a query string
even in a POST request.
"""
import cgi, os, urllib, string
class FieldStorage(cgi.FieldStorage):
def __init__(self, fp=None, headers=None, outerboundary="",
environ=os.environ, keep_blank_values=0, strict_parsing=0):
self._environ = environ
self._strict_parsing = strict_parsing
self._keep_blank_values = keep_blank_values
cgi.FieldStorage.__init__(self, fp, headers, outerboundary, environ, keep_blank_values, strict_parsing)
def parse_qs(self):
"""
Explicitly parse the query string, even if it's a POST request
"""
self._method = string.upper(self._environ['REQUEST_METHOD'])
if self._method == "GET" or self._method == "HEAD":
return
self._qs = self._environ.get('QUERY_STRING', None)
if not self._qs:
return
name_value_pairs = string.splitfields(self._qs, '&')
dict = {}
for name_value in name_value_pairs:
nv = string.splitfields(name_value, '=')
if len(nv) != 2:
if self._strict_parsing:
raise ValueError, "bad query field: %s" % `name_value`
continue
name = urllib.unquote(string.replace(nv[0], '+', ' '))
value = urllib.unquote(string.replace(nv[1], '+', ' '))
if len(value) or self._keep_blank_values:
if dict.has_key (name):
dict[name].append(value)
else:
dict[name] = [value]
if not self.list:
self.list = []
keys = self.keys()
for key, values in dict.items():
if key not in keys:
for value in values:
self.list.append(cgi.MiniFieldStorage(key,value))