Changes by Jim Fulton: pass environ around as arg;

keep_blank_values option to parse().
This commit is contained in:
Guido van Rossum 1996-07-23 03:46:24 +00:00
parent a48bf79977
commit 773ab27f04
1 changed files with 53 additions and 20 deletions

View File

@ -407,16 +407,25 @@ import string
import sys import sys
import os import os
# A shorthand for os.environ
environ = os.environ
# Parsing functions # Parsing functions
# ================= # =================
def parse(fp=None): def parse(fp=None, environ=os.environ, keep_blank_values=None):
"""Parse a query in the environment or from a file (default stdin)""" """Parse a query in the environment or from a file (default stdin)
Arguments, all optional:
fp : file pointer; default: sys.stdin
environ : environment dictionary; default: os.environ
keep_blank_values: flag indicating whether blank values in
URL encoded forms should be treated as blank strings.
A true value inicates that blanks should be retained as
blank strings. The default false value indicates that
blank values are to be ignored and treated as if they were
not included.
"""
if not fp: if not fp:
fp = sys.stdin fp = sys.stdin
if not environ.has_key('REQUEST_METHOD'): if not environ.has_key('REQUEST_METHOD'):
@ -439,11 +448,23 @@ def parse(fp=None):
else: else:
qs = "" qs = ""
environ['QUERY_STRING'] = qs # XXX Shouldn't, really environ['QUERY_STRING'] = qs # XXX Shouldn't, really
return parse_qs(qs) return parse_qs(qs, keep_blank_values)
def parse_qs(qs): def parse_qs(qs, keep_blank_values=None):
"""Parse a query given as a string argument""" """Parse a query given as a string argumen
Arguments:
qs : URL-encoded query string to be parsed
keep_blank_values: flag indicating whether blank values in
URL encoded queries should be treated as blank strings.
A true value inicates that blanks should be retained as
blank strings. The default false value indicates that
blank values are to be ignored and treated as if they were
not included.
"""
import urllib, regsub import urllib, regsub
name_value_pairs = string.splitfields(qs, '&') name_value_pairs = string.splitfields(qs, '&')
dict = {} dict = {}
@ -453,7 +474,7 @@ def parse_qs(qs):
continue continue
name = nv[0] name = nv[0]
value = urllib.unquote(regsub.gsub('+', ' ', nv[1])) value = urllib.unquote(regsub.gsub('+', ' ', nv[1]))
if len(value): if len(value) or keep_blank_values:
if dict.has_key (name): if dict.has_key (name):
dict[name].append(value) dict[name].append(value)
else: else:
@ -579,6 +600,7 @@ class MiniFieldStorage:
filename = None filename = None
list = None list = None
type = None type = None
file = None
type_options = {} type_options = {}
disposition = None disposition = None
disposition_options = {} disposition_options = {}
@ -589,7 +611,7 @@ class MiniFieldStorage:
from StringIO import StringIO from StringIO import StringIO
self.name = name self.name = name
self.value = value self.value = value
self.file = StringIO(value) # self.file = StringIO(value)
def __repr__(self): def __repr__(self):
"""Return printable representation.""" """Return printable representation."""
@ -639,7 +661,8 @@ class FieldStorage:
""" """
def __init__(self, fp=None, headers=None, outerboundary=""): def __init__(self, fp=None, headers=None, outerboundary="",
environ=os.environ, keep_blank_values=None):
"""Constructor. Read multipart/* until last part. """Constructor. Read multipart/* until last part.
Arguments, all optional: Arguments, all optional:
@ -649,11 +672,21 @@ class FieldStorage:
headers : header dictionary-like object; default: headers : header dictionary-like object; default:
taken from environ as per CGI spec taken from environ as per CGI spec
outerboundary : optional terminating multipart boundary outerboundary : terminating multipart boundary
(for internal use only) (for internal use only)
environ : environment dictionary; default: os.environ
keep_blank_values: flag indicating whether blank values in
URL encoded forms should be treated as blank strings.
A true value inicates that blanks should be retained as
blank strings. The default false value indicates that
blank values are to be ignored and treated as if they were
not included.
""" """
method = None method = None
self.keep_blank_values = keep_blank_values
if environ.has_key('REQUEST_METHOD'): if environ.has_key('REQUEST_METHOD'):
method = string.upper(environ['REQUEST_METHOD']) method = string.upper(environ['REQUEST_METHOD'])
if not fp and method == 'GET': if not fp and method == 'GET':
@ -767,7 +800,7 @@ class FieldStorage:
def read_urlencoded(self): def read_urlencoded(self):
"""Internal: read data in query string format.""" """Internal: read data in query string format."""
qs = self.fp.read(self.length) qs = self.fp.read(self.length)
dict = parse_qs(qs) dict = parse_qs(qs, self.keep_blank_values)
self.list = [] self.list = []
for key, valuelist in dict.items(): for key, valuelist in dict.items():
for value in valuelist: for value in valuelist:
@ -922,8 +955,8 @@ class FormContentDict:
form.dict == {key: [val, val, ...], ...} form.dict == {key: [val, val, ...], ...}
""" """
def __init__( self ): def __init__(self, environ=os.environ):
self.dict = parse() self.dict = parse(environ)
self.query_string = environ['QUERY_STRING'] self.query_string = environ['QUERY_STRING']
def __getitem__(self,key): def __getitem__(self,key):
return self.dict[key] return self.dict[key]
@ -1027,7 +1060,7 @@ class FormContent(FormContentDict):
# Test/debug code # Test/debug code
# =============== # ===============
def test(): def test(environ=os.environ):
"""Robust test CGI script, usable as main program. """Robust test CGI script, usable as main program.
Write minimal HTTP headers and dump all information provided to Write minimal HTTP headers and dump all information provided to
@ -1041,7 +1074,7 @@ def test():
try: try:
form = FieldStorage() # Replace with other classes to test those form = FieldStorage() # Replace with other classes to test those
print_form(form) print_form(form)
print_environ() print_environ(environ)
print_directory() print_directory()
print_arguments() print_arguments()
print_environ_usage() print_environ_usage()
@ -1049,7 +1082,7 @@ def test():
print "\n\n<PRE>" # Turn off HTML word wrap print "\n\n<PRE>" # Turn off HTML word wrap
traceback.print_exc() traceback.print_exc()
def print_environ(): def print_environ(environ=os.environ):
"""Dump the shell environment as HTML.""" """Dump the shell environment as HTML."""
keys = environ.keys() keys = environ.keys()
keys.sort() keys.sort()