annotate Lib/cgi.py @ 3216:d00a79ebb027 legacy-trunk

Added missing imgpbm and imgsgi modules
author Jack Jansen <jack.jansen@cwi.nl>
date Thu, 12 Oct 1995 10:23:27 +0000
parents c41a8e501973
children 6abc75ec6d29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 #!/usr/local/bin/python
3106
c41a8e501973 handle missing QUERY_STRING
Guido van Rossum <guido@python.org>
parents: 2918
diff changeset
2
c41a8e501973 handle missing QUERY_STRING
Guido van Rossum <guido@python.org>
parents: 2918
diff changeset
3 # XXX TODO
c41a8e501973 handle missing QUERY_STRING
Guido van Rossum <guido@python.org>
parents: 2918
diff changeset
4 # - proper doc strings instead of this rambling dialogue style
c41a8e501973 handle missing QUERY_STRING
Guido van Rossum <guido@python.org>
parents: 2918
diff changeset
5 # - more utilities, e.g.
c41a8e501973 handle missing QUERY_STRING
Guido van Rossum <guido@python.org>
parents: 2918
diff changeset
6 # - print_header(type="test/html", blankline=1) -- print MIME header
c41a8e501973 handle missing QUERY_STRING
Guido van Rossum <guido@python.org>
parents: 2918
diff changeset
7 # - utility to format a nice error message in HTML
c41a8e501973 handle missing QUERY_STRING
Guido van Rossum <guido@python.org>
parents: 2918
diff changeset
8 # - utility to format a Location: ... response, including HTML
c41a8e501973 handle missing QUERY_STRING
Guido van Rossum <guido@python.org>
parents: 2918
diff changeset
9 # - utility to catch errors and display traceback
c41a8e501973 handle missing QUERY_STRING
Guido van Rossum <guido@python.org>
parents: 2918
diff changeset
10
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 #
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # A class for wrapping the WWW Forms Common Gateway Interface (CGI)
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # Michael McLay, NIST mclay@eeel.nist.gov 6/14/94
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 #
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # modified by Steve Majewski <sdm7g@Virginia.EDU> 12/5/94
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 #
2918
cfa9b3a84872 added parse_qs(query_string)
Guido van Rossum <guido@python.org>
parents: 2538
diff changeset
17 # now maintained as part of the Python distribution
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # Several classes to parse the name/value pairs that are passed to
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # a server's CGI by GET, POST or PUT methods by a WWW FORM. This
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # module is based on Mike McLay's original cgi.py after discussing
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # changes with him and others on the comp.lang.python newsgroup, and
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # at the NIST Python workshop.
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 #
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # The rationale for changes was:
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # The original FormContent class was almost, but not quite like
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 # a dictionary object. Besides adding some extra access methods,
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # it had a values() method with different arguments and semantics
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 # from the standard values() method of a mapping object. Also,
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # it provided several different access methods that may be necessary
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # or useful, but made it a little more confusing to figure out how
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 # to use. Also, we wanted to make the most typical cases the simplest
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 # and most convenient access methods. ( Most form fields just return
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 # a single value, and in practice, a lot of code was just assuming
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 # a single value and ignoring all others. On the other hand, the
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 # protocol allows multiple values to be returned.
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 #
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 # The new base class (FormContentDict) is just like a dictionary.
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 # In fact, if you just want a dictionary, all of the stuff that was
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 # in __init__ has been extracted into a cgi.parse() function that will
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 # return the "raw" dictionary, but having a class allows you to customize
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 # it further.
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 # Mike McLay's original FormContent class is reimplemented as a
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 # subclass of FormContentDict.
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 # There are two additional sub-classes, but I'm not yet too sure
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 # whether they are what I want.
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 #
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 import string,regsub,sys,os,urllib
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # since os.environ may often be used in cgi code, we name it in this module.
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 from os import environ
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 def parse():
2918
cfa9b3a84872 added parse_qs(query_string)
Guido van Rossum <guido@python.org>
parents: 2538
diff changeset
55 """Parse the query passed in the environment or on stdin"""
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 if environ['REQUEST_METHOD'] == 'POST':
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 qs = sys.stdin.read(string.atoi(environ['CONTENT_LENGTH']))
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 environ['QUERY_STRING'] = qs
3106
c41a8e501973 handle missing QUERY_STRING
Guido van Rossum <guido@python.org>
parents: 2918
diff changeset
59 elif environ.has_key('QUERY_STRING'):
c41a8e501973 handle missing QUERY_STRING
Guido van Rossum <guido@python.org>
parents: 2918
diff changeset
60 qs = environ['QUERY_STRING']
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 else:
3106
c41a8e501973 handle missing QUERY_STRING
Guido van Rossum <guido@python.org>
parents: 2918
diff changeset
62 environ['QUERY_STRING'] = qs = ''
2918
cfa9b3a84872 added parse_qs(query_string)
Guido van Rossum <guido@python.org>
parents: 2538
diff changeset
63 return parse_qs(qs)
cfa9b3a84872 added parse_qs(query_string)
Guido van Rossum <guido@python.org>
parents: 2538
diff changeset
64
cfa9b3a84872 added parse_qs(query_string)
Guido van Rossum <guido@python.org>
parents: 2538
diff changeset
65
cfa9b3a84872 added parse_qs(query_string)
Guido van Rossum <guido@python.org>
parents: 2538
diff changeset
66 def parse_qs(qs):
cfa9b3a84872 added parse_qs(query_string)
Guido van Rossum <guido@python.org>
parents: 2538
diff changeset
67 """Parse a query given as a string argument"""
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 name_value_pairs = string.splitfields(qs, '&')
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 dict = {}
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 for name_value in name_value_pairs:
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 nv = string.splitfields(name_value, '=')
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 if len(nv) != 2:
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 continue
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 name = nv[0]
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 value = urllib.unquote(regsub.gsub('+',' ',nv[1]))
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 if len(value):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 if dict.has_key (name):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 dict[name].append(value)
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 else:
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 dict[name] = [value]
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 return dict
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 # The FormContent constructor creates a dictionary from the name/value pairs
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 # passed through the CGI interface.
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 #
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 # form['key']
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 # form.__getitem__('key')
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 # form.has_key('key')
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 # form.keys()
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 # form.values()
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 # form.items()
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 # form.dict
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 class FormContentDict:
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 def __init__( self ):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 self.dict = parse()
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 self.query_string = environ['QUERY_STRING']
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 def __getitem__(self,key):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 return self.dict[key]
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 def keys(self):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 return self.dict.keys()
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 def has_key(self, key):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 return self.dict.has_key(key)
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 def values(self):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 return self.dict.values()
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 def items(self):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 return self.dict.items()
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 def __len__( self ):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 return len(self.dict)
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 # This is the "strict" single-value expecting version.
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 # IF you only expect a single value for each field, then form[key]
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 # will return that single value ( the [0]-th ), and raise an
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 # IndexError if that expectation is not true.
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 # IF you expect a field to have possible multiple values, than you
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 # can use form.getlist( key ) to get all of the values.
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 # values() and items() are a compromise: they return single strings
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 # where there is a single value, and lists of strings otherwise.
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 class SvFormContentDict(FormContentDict):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 def __getitem__( self, key ):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 if len( self.dict[key] ) > 1 :
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 raise IndexError, 'expecting a single value'
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 return self.dict[key][0]
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 def getlist( self, key ):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 return self.dict[key]
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 def values( self ):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 lis = []
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 for each in self.dict.values() :
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 if len( each ) == 1 :
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 lis.append( each[0] )
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 else: lis.append( each )
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 return lis
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 def items( self ):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 lis = []
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 for key,value in self.dict.items():
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 if len(value) == 1 :
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 lis.append( (key,value[0]) )
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 else: lis.append( (key,value) )
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 return lis
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 # And this sub-class is similar to the above, but it will attempt to
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 # interpret numerical values. This is here as mostly as an example,
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 # but I think the real way to handle typed-data from a form may be
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 # to make an additional table driver parsing stage that has a table
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 # of allowed input patterns and the output conversion types - it
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 # would signal type-errors on parse, not on access.
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 class InterpFormContentDict(SvFormContentDict):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 def __getitem__( self, key ):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 v = SvFormContentDict.__getitem__( self, key )
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 if v[0] in string.digits+'+-.' :
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 try: return string.atoi( v )
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 except ValueError:
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 try: return string.atof( v )
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 except ValueError: pass
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 return string.strip(v)
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 def values( self ):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 lis = []
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 for key in self.keys():
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 try:
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 lis.append( self[key] )
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 except IndexError:
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 lis.append( self.dict[key] )
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 return lis
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 def items( self ):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 lis = []
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 for key in self.keys():
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 try:
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 lis.append( (key, self[key]) )
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 except IndexError:
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 lis.append( (key, self.dict[key]) )
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 return lis
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
179
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
180
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 # class FormContent parses the name/value pairs that are passed to a
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 # server's CGI by GET, POST, or PUT methods by a WWW FORM. several
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 # specialized FormContent dictionary access methods have been added
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 # for convenience.
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
185
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 # function return value
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 #
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 # form.keys() all keys in dictionary
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 # form.has_key('key') test keys existance
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 # form[key] returns list associated with key
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 # form.values('key') key's list (same as form.[key])
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 # form.indexed_value('key' index) nth element in key's value list
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 # form.value(key) key's unstripped value
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 # form.length(key) number of elements in key's list
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 # form.stripped(key) key's value with whitespace stripped
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 # form.pars() full dictionary
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
197
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
198
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
199
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 class FormContent(FormContentDict):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 # This is the original FormContent semantics of values,
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 # not the dictionary like semantics.
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 def values(self,key):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 if self.dict.has_key(key):return self.dict[key]
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 else: return None
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 def indexed_value(self,key, location):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 if self.dict.has_key(key):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 if len (self.dict[key]) > location:
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 return self.dict[key][location]
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 else: return None
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 else: return None
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 def value(self,key):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 if self.dict.has_key(key):return self.dict[key][0]
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 else: return None
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 def length(self,key):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 return len (self.dict[key])
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 def stripped(self,key):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 if self.dict.has_key(key):return string.strip(self.dict[key][0])
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 else: return None
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 def pars(self):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 return self.dict
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
223
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
224
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
225
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
227
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 def print_environ_usage():
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 print """
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 <H3>These operating system environment variables could have been
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 set:</H3> <UL>
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 <LI>AUTH_TYPE
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 <LI>CONTENT_LENGTH
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 <LI>CONTENT_TYPE
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 <LI>DATE_GMT
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 <LI>DATE_LOCAL
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 <LI>DOCUMENT_NAME
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 <LI>DOCUMENT_ROOT
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 <LI>DOCUMENT_URI
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 <LI>GATEWAY_INTERFACE
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 <LI>LAST_MODIFIED
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 <LI>PATH
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 <LI>PATH_INFO
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 <LI>PATH_TRANSLATED
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 <LI>QUERY_STRING
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 <LI>REMOTE_ADDR
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 <LI>REMOTE_HOST
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 <LI>REMOTE_IDENT
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 <LI>REMOTE_USER
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 <LI>REQUEST_METHOD
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 <LI>SCRIPT_NAME
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 <LI>SERVER_NAME
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 <LI>SERVER_PORT
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 <LI>SERVER_PROTOCOL
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 <LI>SERVER_ROOT
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 <LI>SERVER_SOFTWARE
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 </UL>
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 """
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
259
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 def print_environ():
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 skeys = environ.keys()
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 skeys.sort()
2441
2f89ec1a9c38 layout changes; quote & as well
Guido van Rossum <guido@python.org>
parents: 2135
diff changeset
263 print '<h3> The following environment variables ' \
2f89ec1a9c38 layout changes; quote & as well
Guido van Rossum <guido@python.org>
parents: 2135
diff changeset
264 'were set by the CGI script: </h3>'
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 print '<dl>'
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 for key in skeys:
2441
2f89ec1a9c38 layout changes; quote & as well
Guido van Rossum <guido@python.org>
parents: 2135
diff changeset
267 print '<dt>', escape(key), '<dd>', escape(environ[key])
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 print '</dl>'
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
269
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 def print_form( form ):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 skeys = form.keys()
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 skeys.sort()
2441
2f89ec1a9c38 layout changes; quote & as well
Guido van Rossum <guido@python.org>
parents: 2135
diff changeset
273 print '<h3> The following name/value pairs ' \
2f89ec1a9c38 layout changes; quote & as well
Guido van Rossum <guido@python.org>
parents: 2135
diff changeset
274 'were entered in the form: </h3>'
2f89ec1a9c38 layout changes; quote & as well
Guido van Rossum <guido@python.org>
parents: 2135
diff changeset
275 print '<dl>'
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 for key in skeys:
2441
2f89ec1a9c38 layout changes; quote & as well
Guido van Rossum <guido@python.org>
parents: 2135
diff changeset
277 print '<dt>', escape(key), ':',
2f89ec1a9c38 layout changes; quote & as well
Guido van Rossum <guido@python.org>
parents: 2135
diff changeset
278 print '<i>', escape(`type(form[key])`), '</i>',
2538
4df00b29477b fix typos in escape() and print_form()
Guido van Rossum <guido@python.org>
parents: 2441
diff changeset
279 print '<dd>', escape(`form[key]`)
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 print '</dl>'
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
281
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 def escape( s ):
2538
4df00b29477b fix typos in escape() and print_form()
Guido van Rossum <guido@python.org>
parents: 2441
diff changeset
283 s = regsub.gsub('&', '&amp;', s) # Must be done first
4df00b29477b fix typos in escape() and print_form()
Guido van Rossum <guido@python.org>
parents: 2441
diff changeset
284 s = regsub.gsub('<', '&lt;', s)
4df00b29477b fix typos in escape() and print_form()
Guido van Rossum <guido@python.org>
parents: 2441
diff changeset
285 s = regsub.gsub('>', '&gt;', s)
2441
2f89ec1a9c38 layout changes; quote & as well
Guido van Rossum <guido@python.org>
parents: 2135
diff changeset
286 return s
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
287
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 def test( what ):
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 label = escape(str(what))
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 print 'Content-type: text/html\n\n'
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 print '<HEADER>\n<TITLE>' + label + '</TITLE>\n</HEADER>\n'
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 print '<BODY>\n'
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 print "<H1>" + label +"</H1>\n"
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 form = what()
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 print_form( form )
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 print_environ()
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 print_environ_usage()
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 print '</body>'
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
299
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 if __name__ == '__main__' :
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 test_classes = ( FormContent, FormContentDict, SvFormContentDict, InterpFormContentDict )
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 test( test_classes[0] ) # by default, test compatibility with
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 # old version, change index to test others.