blob: caf54bdd9fd319381364dd7d28be9f68abae0550 [file] [log] [blame]
Guido van Rossum51914632000-10-03 13:51:09 +00001#! /usr/local/bin/python
Guido van Rossum1c9daa81995-09-18 21:52:37 +00002
Guido van Rossum467d7232001-02-13 13:13:33 +00003# NOTE: the above "/usr/local/bin/python" is NOT a mistake. It is
4# intentionally NOT "/usr/bin/env python". On many systems
5# (e.g. Solaris), /usr/local/bin is not in $PATH as passed to CGI
6# scripts, and /usr/local/bin is the default directory where Python is
7# installed, so /usr/bin/env would be unable to find python. Granted,
8# binary installations by Linux vendors often install Python in
9# /usr/bin. So let those vendors patch cgi.py to match their choice
10# of installation.
11
Guido van Rossum72755611996-03-06 07:20:06 +000012"""Support module for CGI (Common Gateway Interface) scripts.
Guido van Rossum1c9daa81995-09-18 21:52:37 +000013
Guido van Rossum7aee3841996-03-07 18:00:44 +000014This module defines a number of utilities for use by CGI scripts
15written in Python.
Guido van Rossum72755611996-03-06 07:20:06 +000016"""
17
Guido van Rossum98d9fd32000-02-28 15:12:25 +000018# History
19# -------
Tim Peters88869f92001-01-14 23:36:06 +000020#
Guido van Rossum98d9fd32000-02-28 15:12:25 +000021# Michael McLay started this module. Steve Majewski changed the
22# interface to SvFormContentDict and FormContentDict. The multipart
23# parsing was inspired by code submitted by Andreas Paepcke. Guido van
24# Rossum rewrote, reformatted and documented the module and is currently
25# responsible for its maintenance.
Tim Peters88869f92001-01-14 23:36:06 +000026#
Guido van Rossum98d9fd32000-02-28 15:12:25 +000027
Guido van Rossum52b8c292001-06-29 13:06:06 +000028__version__ = "2.6"
Guido van Rossum0147db01996-03-09 03:16:04 +000029
Guido van Rossum72755611996-03-06 07:20:06 +000030
31# Imports
32# =======
33
Raymond Hettingerf871d832004-12-31 21:59:02 +000034from operator import attrgetter
Barry Warsaw596097e2008-06-12 02:38:51 +000035from io import StringIO
Guido van Rossum72755611996-03-06 07:20:06 +000036import sys
37import os
Guido van Rossuma5e9fb61997-08-12 18:18:13 +000038import urllib
Armin Rigo3a703b62005-09-19 09:11:04 +000039import mimetools
Barry Warsaw596097e2008-06-12 02:38:51 +000040import email.parser
Guido van Rossum72755611996-03-06 07:20:06 +000041
Georg Brandl49d1b4f2008-05-11 21:42:51 +000042__all__ = ["MiniFieldStorage", "FieldStorage",
Guido van Rossuma8423a92001-03-19 13:40:44 +000043 "parse", "parse_qs", "parse_qsl", "parse_multipart",
44 "parse_header", "print_exception", "print_environ",
45 "print_form", "print_directory", "print_arguments",
46 "print_environ_usage", "escape"]
Guido van Rossumc204c701996-09-05 19:07:11 +000047
48# Logging support
49# ===============
50
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000051logfile = "" # Filename to log to, if not empty
52logfp = None # File object to log to, if not None
Guido van Rossumc204c701996-09-05 19:07:11 +000053
54def initlog(*allargs):
55 """Write a log message, if there is a log file.
56
57 Even though this function is called initlog(), you should always
58 use log(); log is a variable that is set either to initlog
59 (initially), to dolog (once the log file has been opened), or to
60 nolog (when logging is disabled).
61
62 The first argument is a format string; the remaining arguments (if
63 any) are arguments to the % operator, so e.g.
64 log("%s: %s", "a", "b")
65 will write "a: b" to the log file, followed by a newline.
66
67 If the global logfp is not None, it should be a file object to
68 which log data is written.
69
70 If the global logfp is None, the global logfile may be a string
71 giving a filename to open, in append mode. This file should be
72 world writable!!! If the file can't be opened, logging is
73 silently disabled (since there is no safe place where we could
74 send an error message).
75
76 """
77 global logfp, log
78 if logfile and not logfp:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000079 try:
80 logfp = open(logfile, "a")
81 except IOError:
82 pass
Guido van Rossumc204c701996-09-05 19:07:11 +000083 if not logfp:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000084 log = nolog
Guido van Rossumc204c701996-09-05 19:07:11 +000085 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000086 log = dolog
Guido van Rossum68468eb2003-02-27 20:14:51 +000087 log(*allargs)
Guido van Rossumc204c701996-09-05 19:07:11 +000088
89def dolog(fmt, *args):
90 """Write a log message to the log file. See initlog() for docs."""
91 logfp.write(fmt%args + "\n")
92
93def nolog(*allargs):
94 """Dummy function, assigned to log when logging is disabled."""
95 pass
96
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000097log = initlog # The current logging function
Guido van Rossumc204c701996-09-05 19:07:11 +000098
99
Guido van Rossum72755611996-03-06 07:20:06 +0000100# Parsing functions
101# =================
102
Guido van Rossumad164711997-05-13 19:03:23 +0000103# Maximum input we will accept when REQUEST_METHOD is POST
104# 0 ==> unlimited input
105maxlen = 0
106
Guido van Rossume08c04c1996-11-11 19:29:11 +0000107def parse(fp=None, environ=os.environ, keep_blank_values=0, strict_parsing=0):
Guido van Rossum773ab271996-07-23 03:46:24 +0000108 """Parse a query in the environment or from a file (default stdin)
109
110 Arguments, all optional:
111
112 fp : file pointer; default: sys.stdin
113
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000114 environ : environment dictionary; default: os.environ
Guido van Rossum773ab271996-07-23 03:46:24 +0000115
116 keep_blank_values: flag indicating whether blank values in
Tim Peters88869f92001-01-14 23:36:06 +0000117 URL encoded forms should be treated as blank strings.
118 A true value indicates that blanks should be retained as
Guido van Rossum773ab271996-07-23 03:46:24 +0000119 blank strings. The default false value indicates that
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000120 blank values are to be ignored and treated as if they were
121 not included.
Guido van Rossume08c04c1996-11-11 19:29:11 +0000122
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000123 strict_parsing: flag indicating what to do with parsing errors.
124 If false (the default), errors are silently ignored.
125 If true, errors raise a ValueError exception.
Guido van Rossum773ab271996-07-23 03:46:24 +0000126 """
Raymond Hettingera1449002002-05-31 23:54:44 +0000127 if fp is None:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000128 fp = sys.stdin
Raymond Hettinger54f02222002-06-01 14:18:47 +0000129 if not 'REQUEST_METHOD' in environ:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000130 environ['REQUEST_METHOD'] = 'GET' # For testing stand-alone
Guido van Rossum7aee3841996-03-07 18:00:44 +0000131 if environ['REQUEST_METHOD'] == 'POST':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000132 ctype, pdict = parse_header(environ['CONTENT_TYPE'])
133 if ctype == 'multipart/form-data':
134 return parse_multipart(fp, pdict)
135 elif ctype == 'application/x-www-form-urlencoded':
Eric S. Raymond7e9b4f52001-02-09 09:59:10 +0000136 clength = int(environ['CONTENT_LENGTH'])
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000137 if maxlen and clength > maxlen:
Collin Winterce36ad82007-08-30 01:19:48 +0000138 raise ValueError('Maximum content length exceeded')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000139 qs = fp.read(clength)
140 else:
141 qs = '' # Unknown content-type
Raymond Hettinger54f02222002-06-01 14:18:47 +0000142 if 'QUERY_STRING' in environ:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000143 if qs: qs = qs + '&'
144 qs = qs + environ['QUERY_STRING']
Tim Peters88869f92001-01-14 23:36:06 +0000145 elif sys.argv[1:]:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000146 if qs: qs = qs + '&'
147 qs = qs + sys.argv[1]
148 environ['QUERY_STRING'] = qs # XXX Shouldn't, really
Raymond Hettinger54f02222002-06-01 14:18:47 +0000149 elif 'QUERY_STRING' in environ:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000150 qs = environ['QUERY_STRING']
Guido van Rossum7aee3841996-03-07 18:00:44 +0000151 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000152 if sys.argv[1:]:
153 qs = sys.argv[1]
154 else:
155 qs = ""
156 environ['QUERY_STRING'] = qs # XXX Shouldn't, really
Guido van Rossume08c04c1996-11-11 19:29:11 +0000157 return parse_qs(qs, keep_blank_values, strict_parsing)
Guido van Rossume7808771995-08-07 20:12:09 +0000158
159
Guido van Rossume08c04c1996-11-11 19:29:11 +0000160def parse_qs(qs, keep_blank_values=0, strict_parsing=0):
161 """Parse a query given as a string argument.
Guido van Rossum773ab271996-07-23 03:46:24 +0000162
163 Arguments:
164
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000165 qs: URL-encoded query string to be parsed
Guido van Rossum773ab271996-07-23 03:46:24 +0000166
167 keep_blank_values: flag indicating whether blank values in
Tim Peters88869f92001-01-14 23:36:06 +0000168 URL encoded queries should be treated as blank strings.
169 A true value indicates that blanks should be retained as
Guido van Rossum773ab271996-07-23 03:46:24 +0000170 blank strings. The default false value indicates that
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000171 blank values are to be ignored and treated as if they were
172 not included.
Guido van Rossume08c04c1996-11-11 19:29:11 +0000173
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000174 strict_parsing: flag indicating what to do with parsing errors.
175 If false (the default), errors are silently ignored.
176 If true, errors raise a ValueError exception.
Guido van Rossum773ab271996-07-23 03:46:24 +0000177 """
Guido van Rossum7aee3841996-03-07 18:00:44 +0000178 dict = {}
Guido van Rossum1946f0d1999-06-04 17:54:39 +0000179 for name, value in parse_qsl(qs, keep_blank_values, strict_parsing):
Raymond Hettinger54f02222002-06-01 14:18:47 +0000180 if name in dict:
Moshe Zadkaa1a4b592000-08-25 21:47:56 +0000181 dict[name].append(value)
182 else:
183 dict[name] = [value]
Guido van Rossum1946f0d1999-06-04 17:54:39 +0000184 return dict
185
186def parse_qsl(qs, keep_blank_values=0, strict_parsing=0):
187 """Parse a query given as a string argument.
188
Jeremy Hyltonafde7e22000-09-15 20:06:57 +0000189 Arguments:
Guido van Rossum1946f0d1999-06-04 17:54:39 +0000190
Jeremy Hyltonafde7e22000-09-15 20:06:57 +0000191 qs: URL-encoded query string to be parsed
Guido van Rossum1946f0d1999-06-04 17:54:39 +0000192
Jeremy Hyltonafde7e22000-09-15 20:06:57 +0000193 keep_blank_values: flag indicating whether blank values in
194 URL encoded queries should be treated as blank strings. A
195 true value indicates that blanks should be retained as blank
196 strings. The default false value indicates that blank values
197 are to be ignored and treated as if they were not included.
Guido van Rossum1946f0d1999-06-04 17:54:39 +0000198
Jeremy Hyltonafde7e22000-09-15 20:06:57 +0000199 strict_parsing: flag indicating what to do with parsing errors. If
200 false (the default), errors are silently ignored. If true,
Tim Peters88869f92001-01-14 23:36:06 +0000201 errors raise a ValueError exception.
Guido van Rossum1946f0d1999-06-04 17:54:39 +0000202
Jeremy Hyltonafde7e22000-09-15 20:06:57 +0000203 Returns a list, as G-d intended.
Guido van Rossum1946f0d1999-06-04 17:54:39 +0000204 """
Jeremy Hyltonafde7e22000-09-15 20:06:57 +0000205 pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
206 r = []
207 for name_value in pairs:
Neil Schemenauer66edb622004-07-19 15:38:11 +0000208 if not name_value and not strict_parsing:
209 continue
Jeremy Hyltonafde7e22000-09-15 20:06:57 +0000210 nv = name_value.split('=', 1)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000211 if len(nv) != 2:
212 if strict_parsing:
Collin Winterce36ad82007-08-30 01:19:48 +0000213 raise ValueError("bad query field: %r" % (name_value,))
Brett Cannon8d9b60f2004-03-21 22:16:15 +0000214 # Handle case of a control-name with no equal sign
215 if keep_blank_values:
216 nv.append('')
217 else:
218 continue
Moshe Zadkaa1a4b592000-08-25 21:47:56 +0000219 if len(nv[1]) or keep_blank_values:
Eric S. Raymond7e9b4f52001-02-09 09:59:10 +0000220 name = urllib.unquote(nv[0].replace('+', ' '))
221 value = urllib.unquote(nv[1].replace('+', ' '))
Moshe Zadkaa1a4b592000-08-25 21:47:56 +0000222 r.append((name, value))
Guido van Rossum1946f0d1999-06-04 17:54:39 +0000223
224 return r
Guido van Rossum9a22de11995-01-12 12:29:47 +0000225
226
Guido van Rossum0147db01996-03-09 03:16:04 +0000227def parse_multipart(fp, pdict):
Guido van Rossum7aee3841996-03-07 18:00:44 +0000228 """Parse multipart input.
Guido van Rossum9a22de11995-01-12 12:29:47 +0000229
Guido van Rossum7aee3841996-03-07 18:00:44 +0000230 Arguments:
231 fp : input file
Johannes Gijsbersc7fc10a2005-01-08 13:56:36 +0000232 pdict: dictionary containing other parameters of content-type header
Guido van Rossum72755611996-03-06 07:20:06 +0000233
Tim Peters88869f92001-01-14 23:36:06 +0000234 Returns a dictionary just like parse_qs(): keys are the field names, each
235 value is a list of values for that field. This is easy to use but not
236 much good if you are expecting megabytes to be uploaded -- in that case,
237 use the FieldStorage class instead which is much more flexible. Note
238 that content-type is the raw, unparsed contents of the content-type
Guido van Rossum0147db01996-03-09 03:16:04 +0000239 header.
Tim Peters88869f92001-01-14 23:36:06 +0000240
241 XXX This does not parse nested multipart parts -- use FieldStorage for
Guido van Rossum0147db01996-03-09 03:16:04 +0000242 that.
Tim Peters88869f92001-01-14 23:36:06 +0000243
244 XXX This should really be subsumed by FieldStorage altogether -- no
Guido van Rossum0147db01996-03-09 03:16:04 +0000245 point in having two implementations of the same parsing algorithm.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000246 Also, FieldStorage protects itself better against certain DoS attacks
247 by limiting the size of the data read in one chunk. The API here
248 does not support that kind of protection. This also affects parse()
249 since it can call parse_multipart().
Guido van Rossum72755611996-03-06 07:20:06 +0000250
Guido van Rossum7aee3841996-03-07 18:00:44 +0000251 """
Guido van Rossum2e441f72001-07-25 21:00:19 +0000252 boundary = ""
Raymond Hettinger54f02222002-06-01 14:18:47 +0000253 if 'boundary' in pdict:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000254 boundary = pdict['boundary']
Guido van Rossum2e441f72001-07-25 21:00:19 +0000255 if not valid_boundary(boundary):
Collin Winterce36ad82007-08-30 01:19:48 +0000256 raise ValueError('Invalid boundary in multipart form: %r'
Walter Dörwald70a6b492004-02-12 17:35:32 +0000257 % (boundary,))
Tim Petersab9ba272001-08-09 21:40:30 +0000258
Guido van Rossum7aee3841996-03-07 18:00:44 +0000259 nextpart = "--" + boundary
260 lastpart = "--" + boundary + "--"
261 partdict = {}
262 terminator = ""
263
264 while terminator != lastpart:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000265 bytes = -1
266 data = None
267 if terminator:
268 # At start of next part. Read headers first.
Armin Rigo3a703b62005-09-19 09:11:04 +0000269 headers = mimetools.Message(fp)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000270 clength = headers.getheader('content-length')
271 if clength:
272 try:
Eric S. Raymond7e9b4f52001-02-09 09:59:10 +0000273 bytes = int(clength)
274 except ValueError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000275 pass
276 if bytes > 0:
277 if maxlen and bytes > maxlen:
Collin Winterce36ad82007-08-30 01:19:48 +0000278 raise ValueError('Maximum content length exceeded')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000279 data = fp.read(bytes)
280 else:
281 data = ""
282 # Read lines until end of part.
283 lines = []
284 while 1:
285 line = fp.readline()
286 if not line:
287 terminator = lastpart # End outer loop
288 break
289 if line[:2] == "--":
Eric S. Raymond7e9b4f52001-02-09 09:59:10 +0000290 terminator = line.strip()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000291 if terminator in (nextpart, lastpart):
292 break
293 lines.append(line)
294 # Done with part.
295 if data is None:
296 continue
297 if bytes < 0:
298 if lines:
299 # Strip final line terminator
300 line = lines[-1]
301 if line[-2:] == "\r\n":
302 line = line[:-2]
303 elif line[-1:] == "\n":
304 line = line[:-1]
305 lines[-1] = line
Eric S. Raymond7e9b4f52001-02-09 09:59:10 +0000306 data = "".join(lines)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000307 line = headers['content-disposition']
308 if not line:
309 continue
310 key, params = parse_header(line)
311 if key != 'form-data':
312 continue
Raymond Hettinger54f02222002-06-01 14:18:47 +0000313 if 'name' in params:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000314 name = params['name']
315 else:
316 continue
Raymond Hettinger54f02222002-06-01 14:18:47 +0000317 if name in partdict:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000318 partdict[name].append(data)
319 else:
320 partdict[name] = [data]
Guido van Rossum72755611996-03-06 07:20:06 +0000321
Guido van Rossum7aee3841996-03-07 18:00:44 +0000322 return partdict
Guido van Rossum9a22de11995-01-12 12:29:47 +0000323
324
Guido van Rossum72755611996-03-06 07:20:06 +0000325def parse_header(line):
Guido van Rossum7aee3841996-03-07 18:00:44 +0000326 """Parse a Content-type like header.
327
328 Return the main content-type and a dictionary of options.
329
330 """
Raymond Hettingerf871d832004-12-31 21:59:02 +0000331 plist = [x.strip() for x in line.split(';')]
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +0000332 key = plist.pop(0).lower()
Guido van Rossum7aee3841996-03-07 18:00:44 +0000333 pdict = {}
334 for p in plist:
Eric S. Raymond7e9b4f52001-02-09 09:59:10 +0000335 i = p.find('=')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000336 if i >= 0:
Eric S. Raymond7e9b4f52001-02-09 09:59:10 +0000337 name = p[:i].strip().lower()
338 value = p[i+1:].strip()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000339 if len(value) >= 2 and value[0] == value[-1] == '"':
340 value = value[1:-1]
Johannes Gijsbers9e15dd62004-08-14 15:39:34 +0000341 value = value.replace('\\\\', '\\').replace('\\"', '"')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000342 pdict[name] = value
Guido van Rossum7aee3841996-03-07 18:00:44 +0000343 return key, pdict
Guido van Rossum72755611996-03-06 07:20:06 +0000344
345
Guido van Rossum243ddcd1996-03-07 06:33:07 +0000346# Classes for field storage
347# =========================
348
349class MiniFieldStorage:
350
Guido van Rossum0147db01996-03-09 03:16:04 +0000351 """Like FieldStorage, for use when no file uploads are possible."""
Guido van Rossum243ddcd1996-03-07 06:33:07 +0000352
Guido van Rossum7aee3841996-03-07 18:00:44 +0000353 # Dummy attributes
354 filename = None
355 list = None
356 type = None
Guido van Rossum773ab271996-07-23 03:46:24 +0000357 file = None
Guido van Rossum4032c2c1996-03-09 04:04:35 +0000358 type_options = {}
Guido van Rossum7aee3841996-03-07 18:00:44 +0000359 disposition = None
360 disposition_options = {}
361 headers = {}
Guido van Rossum243ddcd1996-03-07 06:33:07 +0000362
Guido van Rossum7aee3841996-03-07 18:00:44 +0000363 def __init__(self, name, value):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000364 """Constructor from field name and value."""
365 self.name = name
366 self.value = value
Guido van Rossum773ab271996-07-23 03:46:24 +0000367 # self.file = StringIO(value)
Guido van Rossum7aee3841996-03-07 18:00:44 +0000368
369 def __repr__(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000370 """Return printable representation."""
Walter Dörwald70a6b492004-02-12 17:35:32 +0000371 return "MiniFieldStorage(%r, %r)" % (self.name, self.value)
Guido van Rossum243ddcd1996-03-07 06:33:07 +0000372
373
374class FieldStorage:
375
Guido van Rossum7aee3841996-03-07 18:00:44 +0000376 """Store a sequence of fields, reading multipart/form-data.
Guido van Rossum243ddcd1996-03-07 06:33:07 +0000377
Guido van Rossum7aee3841996-03-07 18:00:44 +0000378 This class provides naming, typing, files stored on disk, and
379 more. At the top level, it is accessible like a dictionary, whose
380 keys are the field names. (Note: None can occur as a field name.)
381 The items are either a Python list (if there's multiple values) or
382 another FieldStorage or MiniFieldStorage object. If it's a single
383 object, it has the following attributes:
Guido van Rossum243ddcd1996-03-07 06:33:07 +0000384
Guido van Rossum7aee3841996-03-07 18:00:44 +0000385 name: the field name, if specified; otherwise None
Guido van Rossum243ddcd1996-03-07 06:33:07 +0000386
Guido van Rossum7aee3841996-03-07 18:00:44 +0000387 filename: the filename, if specified; otherwise None; this is the
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000388 client side filename, *not* the file name on which it is
389 stored (that's a temporary file you don't deal with)
Guido van Rossum243ddcd1996-03-07 06:33:07 +0000390
Guido van Rossum7aee3841996-03-07 18:00:44 +0000391 value: the value as a *string*; for file uploads, this
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000392 transparently reads the file every time you request the value
Guido van Rossum7aee3841996-03-07 18:00:44 +0000393
394 file: the file(-like) object from which you can read the data;
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000395 None if the data is stored a simple string
Guido van Rossum7aee3841996-03-07 18:00:44 +0000396
397 type: the content-type, or None if not specified
398
399 type_options: dictionary of options specified on the content-type
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000400 line
Guido van Rossum7aee3841996-03-07 18:00:44 +0000401
402 disposition: content-disposition, or None if not specified
403
404 disposition_options: dictionary of corresponding options
405
Barry Warsaw596097e2008-06-12 02:38:51 +0000406 headers: a dictionary(-like) object (sometimes email.message.Message or a
Armin Rigo3a703b62005-09-19 09:11:04 +0000407 subclass thereof) containing *all* headers
Guido van Rossum7aee3841996-03-07 18:00:44 +0000408
409 The class is subclassable, mostly for the purpose of overriding
410 the make_file() method, which is called internally to come up with
411 a file open for reading and writing. This makes it possible to
412 override the default choice of storing all files in a temporary
413 directory and unlinking them as soon as they have been opened.
414
415 """
416
Guido van Rossum773ab271996-07-23 03:46:24 +0000417 def __init__(self, fp=None, headers=None, outerboundary="",
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000418 environ=os.environ, keep_blank_values=0, strict_parsing=0):
419 """Constructor. Read multipart/* until last part.
Guido van Rossum7aee3841996-03-07 18:00:44 +0000420
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000421 Arguments, all optional:
Guido van Rossum7aee3841996-03-07 18:00:44 +0000422
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000423 fp : file pointer; default: sys.stdin
Guido van Rossumb1b4f941998-05-08 19:55:51 +0000424 (not used when the request method is GET)
Guido van Rossum7aee3841996-03-07 18:00:44 +0000425
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000426 headers : header dictionary-like object; default:
427 taken from environ as per CGI spec
Guido van Rossum7aee3841996-03-07 18:00:44 +0000428
Guido van Rossum773ab271996-07-23 03:46:24 +0000429 outerboundary : terminating multipart boundary
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000430 (for internal use only)
Guido van Rossum7aee3841996-03-07 18:00:44 +0000431
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000432 environ : environment dictionary; default: os.environ
Guido van Rossum773ab271996-07-23 03:46:24 +0000433
434 keep_blank_values: flag indicating whether blank values in
Tim Peters88869f92001-01-14 23:36:06 +0000435 URL encoded forms should be treated as blank strings.
436 A true value indicates that blanks should be retained as
Guido van Rossum773ab271996-07-23 03:46:24 +0000437 blank strings. The default false value indicates that
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000438 blank values are to be ignored and treated as if they were
439 not included.
Guido van Rossum773ab271996-07-23 03:46:24 +0000440
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000441 strict_parsing: flag indicating what to do with parsing errors.
442 If false (the default), errors are silently ignored.
443 If true, errors raise a ValueError exception.
Guido van Rossume08c04c1996-11-11 19:29:11 +0000444
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000445 """
446 method = 'GET'
447 self.keep_blank_values = keep_blank_values
448 self.strict_parsing = strict_parsing
Raymond Hettinger54f02222002-06-01 14:18:47 +0000449 if 'REQUEST_METHOD' in environ:
Eric S. Raymond7e9b4f52001-02-09 09:59:10 +0000450 method = environ['REQUEST_METHOD'].upper()
Guido van Rossum01852831998-06-25 02:40:17 +0000451 if method == 'GET' or method == 'HEAD':
Raymond Hettinger54f02222002-06-01 14:18:47 +0000452 if 'QUERY_STRING' in environ:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000453 qs = environ['QUERY_STRING']
454 elif sys.argv[1:]:
455 qs = sys.argv[1]
456 else:
457 qs = ""
458 fp = StringIO(qs)
459 if headers is None:
460 headers = {'content-type':
461 "application/x-www-form-urlencoded"}
462 if headers is None:
Guido van Rossumcff311a1998-06-11 14:06:59 +0000463 headers = {}
464 if method == 'POST':
465 # Set default content-type for POST to what's traditional
466 headers['content-type'] = "application/x-www-form-urlencoded"
Raymond Hettinger54f02222002-06-01 14:18:47 +0000467 if 'CONTENT_TYPE' in environ:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000468 headers['content-type'] = environ['CONTENT_TYPE']
Raymond Hettinger54f02222002-06-01 14:18:47 +0000469 if 'CONTENT_LENGTH' in environ:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000470 headers['content-length'] = environ['CONTENT_LENGTH']
471 self.fp = fp or sys.stdin
472 self.headers = headers
473 self.outerboundary = outerboundary
Guido van Rossum7aee3841996-03-07 18:00:44 +0000474
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000475 # Process content-disposition header
476 cdisp, pdict = "", {}
Raymond Hettinger54f02222002-06-01 14:18:47 +0000477 if 'content-disposition' in self.headers:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000478 cdisp, pdict = parse_header(self.headers['content-disposition'])
479 self.disposition = cdisp
480 self.disposition_options = pdict
481 self.name = None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000482 if 'name' in pdict:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000483 self.name = pdict['name']
484 self.filename = None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000485 if 'filename' in pdict:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000486 self.filename = pdict['filename']
Guido van Rossum7aee3841996-03-07 18:00:44 +0000487
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000488 # Process content-type header
Barry Warsaw302331a1999-01-08 17:42:03 +0000489 #
490 # Honor any existing content-type header. But if there is no
491 # content-type header, use some sensible defaults. Assume
492 # outerboundary is "" at the outer level, but something non-false
493 # inside a multi-part. The default for an inner part is text/plain,
494 # but for an outer part it should be urlencoded. This should catch
495 # bogus clients which erroneously forget to include a content-type
496 # header.
497 #
498 # See below for what we do if there does exist a content-type header,
499 # but it happens to be something we don't understand.
Raymond Hettinger54f02222002-06-01 14:18:47 +0000500 if 'content-type' in self.headers:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000501 ctype, pdict = parse_header(self.headers['content-type'])
Guido van Rossumce900de1999-06-02 18:44:22 +0000502 elif self.outerboundary or method != 'POST':
Barry Warsaw302331a1999-01-08 17:42:03 +0000503 ctype, pdict = "text/plain", {}
504 else:
505 ctype, pdict = 'application/x-www-form-urlencoded', {}
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000506 self.type = ctype
507 self.type_options = pdict
508 self.innerboundary = ""
Raymond Hettinger54f02222002-06-01 14:18:47 +0000509 if 'boundary' in pdict:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000510 self.innerboundary = pdict['boundary']
511 clen = -1
Raymond Hettinger54f02222002-06-01 14:18:47 +0000512 if 'content-length' in self.headers:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000513 try:
Eric S. Raymond7e9b4f52001-02-09 09:59:10 +0000514 clen = int(self.headers['content-length'])
Skip Montanarodb5d1442002-03-23 05:50:17 +0000515 except ValueError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000516 pass
517 if maxlen and clen > maxlen:
Collin Winterce36ad82007-08-30 01:19:48 +0000518 raise ValueError('Maximum content length exceeded')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000519 self.length = clen
Guido van Rossum7aee3841996-03-07 18:00:44 +0000520
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000521 self.list = self.file = None
522 self.done = 0
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000523 if ctype == 'application/x-www-form-urlencoded':
524 self.read_urlencoded()
525 elif ctype[:10] == 'multipart/':
Guido van Rossumf5745001998-10-20 14:43:02 +0000526 self.read_multi(environ, keep_blank_values, strict_parsing)
Barry Warsaw302331a1999-01-08 17:42:03 +0000527 else:
Guido van Rossum60a3bd81999-06-11 18:26:09 +0000528 self.read_single()
Guido van Rossum7aee3841996-03-07 18:00:44 +0000529
530 def __repr__(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000531 """Return a printable representation."""
Walter Dörwald70a6b492004-02-12 17:35:32 +0000532 return "FieldStorage(%r, %r, %r)" % (
533 self.name, self.filename, self.value)
Guido van Rossum7aee3841996-03-07 18:00:44 +0000534
Guido van Rossum4061cbe2002-09-11 18:20:34 +0000535 def __iter__(self):
536 return iter(self.keys())
537
Guido van Rossum7aee3841996-03-07 18:00:44 +0000538 def __getattr__(self, name):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000539 if name != 'value':
Collin Winterce36ad82007-08-30 01:19:48 +0000540 raise AttributeError(name)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000541 if self.file:
542 self.file.seek(0)
543 value = self.file.read()
544 self.file.seek(0)
545 elif self.list is not None:
546 value = self.list
547 else:
548 value = None
549 return value
Guido van Rossum7aee3841996-03-07 18:00:44 +0000550
551 def __getitem__(self, key):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000552 """Dictionary style indexing."""
553 if self.list is None:
Collin Winterce36ad82007-08-30 01:19:48 +0000554 raise TypeError("not indexable")
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000555 found = []
556 for item in self.list:
557 if item.name == key: found.append(item)
558 if not found:
Collin Winterce36ad82007-08-30 01:19:48 +0000559 raise KeyError(key)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000560 if len(found) == 1:
561 return found[0]
562 else:
563 return found
Guido van Rossum7aee3841996-03-07 18:00:44 +0000564
Moshe Zadkaa1a4b592000-08-25 21:47:56 +0000565 def getvalue(self, key, default=None):
566 """Dictionary style get() method, including 'value' lookup."""
Raymond Hettinger54f02222002-06-01 14:18:47 +0000567 if key in self:
Moshe Zadkaa1a4b592000-08-25 21:47:56 +0000568 value = self[key]
569 if type(value) is type([]):
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000570 return [x.value for x in value]
Moshe Zadkaa1a4b592000-08-25 21:47:56 +0000571 else:
572 return value.value
573 else:
574 return default
575
Guido van Rossum1bfb3882001-09-05 19:45:34 +0000576 def getfirst(self, key, default=None):
577 """ Return the first value received."""
Raymond Hettinger54f02222002-06-01 14:18:47 +0000578 if key in self:
Guido van Rossum1bfb3882001-09-05 19:45:34 +0000579 value = self[key]
580 if type(value) is type([]):
581 return value[0].value
582 else:
583 return value.value
584 else:
585 return default
586
587 def getlist(self, key):
588 """ Return list of received values."""
Raymond Hettinger54f02222002-06-01 14:18:47 +0000589 if key in self:
Guido van Rossum1bfb3882001-09-05 19:45:34 +0000590 value = self[key]
591 if type(value) is type([]):
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000592 return [x.value for x in value]
Guido van Rossum1bfb3882001-09-05 19:45:34 +0000593 else:
594 return [value.value]
595 else:
596 return []
597
Guido van Rossum7aee3841996-03-07 18:00:44 +0000598 def keys(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000599 """Dictionary style keys() method."""
600 if self.list is None:
Collin Winterce36ad82007-08-30 01:19:48 +0000601 raise TypeError("not indexable")
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000602 return list(set(item.name for item in self.list))
Guido van Rossum7aee3841996-03-07 18:00:44 +0000603
Raymond Hettinger54f02222002-06-01 14:18:47 +0000604 def __contains__(self, key):
605 """Dictionary style __contains__ method."""
606 if self.list is None:
Collin Winterce36ad82007-08-30 01:19:48 +0000607 raise TypeError("not indexable")
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000608 return any(item.name == key for item in self.list)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000609
Guido van Rossum88b85d41997-01-11 19:21:33 +0000610 def __len__(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000611 """Dictionary style len(x) support."""
612 return len(self.keys())
Guido van Rossum88b85d41997-01-11 19:21:33 +0000613
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000614 def __nonzero__(self):
615 return bool(self.list)
616
Guido van Rossum7aee3841996-03-07 18:00:44 +0000617 def read_urlencoded(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000618 """Internal: read data in query string format."""
619 qs = self.fp.read(self.length)
Guido van Rossum1946f0d1999-06-04 17:54:39 +0000620 self.list = list = []
621 for key, value in parse_qsl(qs, self.keep_blank_values,
622 self.strict_parsing):
623 list.append(MiniFieldStorage(key, value))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000624 self.skip_lines()
Guido van Rossum7aee3841996-03-07 18:00:44 +0000625
Guido van Rossum030d2ec1998-12-09 22:16:46 +0000626 FieldStorageClass = None
627
Guido van Rossumf5745001998-10-20 14:43:02 +0000628 def read_multi(self, environ, keep_blank_values, strict_parsing):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000629 """Internal: read a part that is itself multipart."""
Guido van Rossum2e441f72001-07-25 21:00:19 +0000630 ib = self.innerboundary
631 if not valid_boundary(ib):
Collin Winterce36ad82007-08-30 01:19:48 +0000632 raise ValueError('Invalid boundary in multipart form: %r' % (ib,))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000633 self.list = []
Guido van Rossum030d2ec1998-12-09 22:16:46 +0000634 klass = self.FieldStorageClass or self.__class__
Barry Warsaw596097e2008-06-12 02:38:51 +0000635 parser = email.parser.FeedParser()
636 # Create bogus content-type header for proper multipart parsing
637 parser.feed('Content-Type: %s; boundary=%s\r\n\r\n' % (self.type, ib))
638 parser.feed(self.fp.read())
639 full_msg = parser.close()
640 # Get subparts
641 msgs = full_msg.get_payload()
642 for msg in msgs:
643 fp = StringIO(msg.get_payload())
644 part = klass(fp, msg, ib, environ, keep_blank_values,
645 strict_parsing)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000646 self.list.append(part)
647 self.skip_lines()
Guido van Rossum7aee3841996-03-07 18:00:44 +0000648
649 def read_single(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000650 """Internal: read an atomic part."""
651 if self.length >= 0:
652 self.read_binary()
653 self.skip_lines()
654 else:
655 self.read_lines()
656 self.file.seek(0)
Guido van Rossum7aee3841996-03-07 18:00:44 +0000657
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000658 bufsize = 8*1024 # I/O buffering size for copy to file
Guido van Rossum7aee3841996-03-07 18:00:44 +0000659
660 def read_binary(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000661 """Internal: read binary data."""
Guido van Rossuma1a68522007-08-28 03:11:34 +0000662 self.file = self.make_file()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000663 todo = self.length
664 if todo >= 0:
665 while todo > 0:
666 data = self.fp.read(min(todo, self.bufsize))
667 if not data:
668 self.done = -1
669 break
670 self.file.write(data)
671 todo = todo - len(data)
Guido van Rossum7aee3841996-03-07 18:00:44 +0000672
673 def read_lines(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000674 """Internal: read lines until EOF or outerboundary."""
Guido van Rossum52b8c292001-06-29 13:06:06 +0000675 self.file = self.__file = StringIO()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000676 if self.outerboundary:
677 self.read_lines_to_outerboundary()
678 else:
679 self.read_lines_to_eof()
Guido van Rossum7aee3841996-03-07 18:00:44 +0000680
Guido van Rossum52b8c292001-06-29 13:06:06 +0000681 def __write(self, line):
682 if self.__file is not None:
683 if self.__file.tell() + len(line) > 1000:
Guido van Rossuma1a68522007-08-28 03:11:34 +0000684 self.file = self.make_file()
685 data = self.__file.getvalue()
686 self.file.write(data)
Guido van Rossum52b8c292001-06-29 13:06:06 +0000687 self.__file = None
688 self.file.write(line)
689
Guido van Rossum7aee3841996-03-07 18:00:44 +0000690 def read_lines_to_eof(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000691 """Internal: read lines until EOF."""
692 while 1:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000693 line = self.fp.readline(1<<16)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000694 if not line:
695 self.done = -1
696 break
Guido van Rossum52b8c292001-06-29 13:06:06 +0000697 self.__write(line)
Guido van Rossum7aee3841996-03-07 18:00:44 +0000698
699 def read_lines_to_outerboundary(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000700 """Internal: read lines until outerboundary."""
701 next = "--" + self.outerboundary
702 last = next + "--"
703 delim = ""
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000704 last_line_lfend = True
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000705 while 1:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000706 line = self.fp.readline(1<<16)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000707 if not line:
708 self.done = -1
709 break
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000710 if line[:2] == "--" and last_line_lfend:
Eric S. Raymond7e9b4f52001-02-09 09:59:10 +0000711 strippedline = line.strip()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000712 if strippedline == next:
713 break
714 if strippedline == last:
715 self.done = 1
716 break
717 odelim = delim
718 if line[-2:] == "\r\n":
719 delim = "\r\n"
720 line = line[:-2]
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000721 last_line_lfend = True
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000722 elif line[-1] == "\n":
723 delim = "\n"
724 line = line[:-1]
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000725 last_line_lfend = True
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000726 else:
727 delim = ""
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000728 last_line_lfend = False
Guido van Rossum52b8c292001-06-29 13:06:06 +0000729 self.__write(odelim + line)
Guido van Rossum7aee3841996-03-07 18:00:44 +0000730
731 def skip_lines(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000732 """Internal: skip lines until outer boundary if defined."""
733 if not self.outerboundary or self.done:
734 return
735 next = "--" + self.outerboundary
736 last = next + "--"
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000737 last_line_lfend = True
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000738 while 1:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000739 line = self.fp.readline(1<<16)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000740 if not line:
741 self.done = -1
742 break
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000743 if line[:2] == "--" and last_line_lfend:
Eric S. Raymond7e9b4f52001-02-09 09:59:10 +0000744 strippedline = line.strip()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000745 if strippedline == next:
746 break
747 if strippedline == last:
748 self.done = 1
749 break
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000750 last_line_lfend = line.endswith('\n')
Guido van Rossum7aee3841996-03-07 18:00:44 +0000751
Guido van Rossuma1a68522007-08-28 03:11:34 +0000752 def make_file(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000753 """Overridable: return a readable & writable file.
Guido van Rossum7aee3841996-03-07 18:00:44 +0000754
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000755 The file will be used as follows:
756 - data is written to it
757 - seek(0)
758 - data is read from it
Guido van Rossum7aee3841996-03-07 18:00:44 +0000759
Guido van Rossuma1a68522007-08-28 03:11:34 +0000760 The file is always opened in text mode.
Guido van Rossum7aee3841996-03-07 18:00:44 +0000761
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000762 This version opens a temporary file for reading and writing,
763 and immediately deletes (unlinks) it. The trick (on Unix!) is
764 that the file can still be used, but it can't be opened by
765 another process, and it will automatically be deleted when it
766 is closed or when the current process terminates.
Guido van Rossum4032c2c1996-03-09 04:04:35 +0000767
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000768 If you want a more permanent file, you derive a class which
769 overrides this method. If you want a visible temporary file
770 that is nevertheless automatically deleted when the script
771 terminates, try defining a __del__ method in a derived class
772 which unlinks the temporary files you have created.
Guido van Rossum7aee3841996-03-07 18:00:44 +0000773
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000774 """
775 import tempfile
Guido van Rossum92bab812007-08-28 03:32:38 +0000776 return tempfile.TemporaryFile("w+", encoding="utf-8", newline="\n")
Tim Peters88869f92001-01-14 23:36:06 +0000777
Guido van Rossum243ddcd1996-03-07 06:33:07 +0000778
Guido van Rossum72755611996-03-06 07:20:06 +0000779# Test/debug code
780# ===============
Guido van Rossum9a22de11995-01-12 12:29:47 +0000781
Guido van Rossum773ab271996-07-23 03:46:24 +0000782def test(environ=os.environ):
Guido van Rossum7aee3841996-03-07 18:00:44 +0000783 """Robust test CGI script, usable as main program.
Guido van Rossum9a22de11995-01-12 12:29:47 +0000784
Guido van Rossum7aee3841996-03-07 18:00:44 +0000785 Write minimal HTTP headers and dump all information provided to
786 the script in HTML form.
787
788 """
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000789 print("Content-type: text/html")
790 print()
Guido van Rossum7aee3841996-03-07 18:00:44 +0000791 sys.stderr = sys.stdout
792 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000793 form = FieldStorage() # Replace with other classes to test those
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000794 print_directory()
795 print_arguments()
Guido van Rossuma3c6a8a2000-09-19 04:11:46 +0000796 print_form(form)
797 print_environ(environ)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000798 print_environ_usage()
799 def f():
Georg Brandl7cae87c2006-09-06 06:51:57 +0000800 exec("testing print_exception() -- <I>italics?</I>")
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000801 def g(f=f):
802 f()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000803 print("<H3>What follows is a test, not an actual exception:</H3>")
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000804 g()
Guido van Rossum7aee3841996-03-07 18:00:44 +0000805 except:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000806 print_exception()
Guido van Rossumf85de8a1996-08-20 20:22:39 +0000807
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000808 print("<H1>Second try with a small maxlen...</H1>")
Guido van Rossum57d51f22000-09-16 21:16:01 +0000809
Guido van Rossumad164711997-05-13 19:03:23 +0000810 global maxlen
811 maxlen = 50
812 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000813 form = FieldStorage() # Replace with other classes to test those
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000814 print_directory()
815 print_arguments()
Guido van Rossuma3c6a8a2000-09-19 04:11:46 +0000816 print_form(form)
817 print_environ(environ)
Guido van Rossumad164711997-05-13 19:03:23 +0000818 except:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000819 print_exception()
Guido van Rossumad164711997-05-13 19:03:23 +0000820
Guido van Rossumf85de8a1996-08-20 20:22:39 +0000821def print_exception(type=None, value=None, tb=None, limit=None):
822 if type is None:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000823 type, value, tb = sys.exc_info()
Guido van Rossumf85de8a1996-08-20 20:22:39 +0000824 import traceback
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000825 print()
826 print("<H3>Traceback (most recent call last):</H3>")
Guido van Rossumf85de8a1996-08-20 20:22:39 +0000827 list = traceback.format_tb(tb, limit) + \
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000828 traceback.format_exception_only(type, value)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000829 print("<PRE>%s<B>%s</B></PRE>" % (
Eric S. Raymond7e9b4f52001-02-09 09:59:10 +0000830 escape("".join(list[:-1])),
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000831 escape(list[-1]),
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000832 ))
Guido van Rossumf15d1591997-09-29 23:22:12 +0000833 del tb
Guido van Rossum9a22de11995-01-12 12:29:47 +0000834
Guido van Rossum773ab271996-07-23 03:46:24 +0000835def print_environ(environ=os.environ):
Guido van Rossum7aee3841996-03-07 18:00:44 +0000836 """Dump the shell environment as HTML."""
Guido van Rossuma1a68522007-08-28 03:11:34 +0000837 keys = sorted(environ.keys())
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000838 print()
839 print("<H3>Shell Environment:</H3>")
840 print("<DL>")
Guido van Rossum7aee3841996-03-07 18:00:44 +0000841 for key in keys:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000842 print("<DT>", escape(key), "<DD>", escape(environ[key]))
843 print("</DL>")
844 print()
Guido van Rossum72755611996-03-06 07:20:06 +0000845
846def print_form(form):
Guido van Rossum7aee3841996-03-07 18:00:44 +0000847 """Dump the contents of a form as HTML."""
Guido van Rossuma1a68522007-08-28 03:11:34 +0000848 keys = sorted(form.keys())
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000849 print()
850 print("<H3>Form Contents:</H3>")
Guido van Rossum57d51f22000-09-16 21:16:01 +0000851 if not keys:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000852 print("<P>No form fields.")
853 print("<DL>")
Guido van Rossum7aee3841996-03-07 18:00:44 +0000854 for key in keys:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000855 print("<DT>" + escape(key) + ":", end=' ')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000856 value = form[key]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000857 print("<i>" + escape(repr(type(value))) + "</i>")
858 print("<DD>" + escape(repr(value)))
859 print("</DL>")
860 print()
Guido van Rossum7aee3841996-03-07 18:00:44 +0000861
862def print_directory():
863 """Dump the current directory as HTML."""
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000864 print()
865 print("<H3>Current Working Directory:</H3>")
Guido van Rossum7aee3841996-03-07 18:00:44 +0000866 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000867 pwd = os.getcwd()
Guido van Rossumb940e112007-01-10 16:19:56 +0000868 except os.error as msg:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000869 print("os.error:", escape(str(msg)))
Guido van Rossum7aee3841996-03-07 18:00:44 +0000870 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000871 print(escape(pwd))
872 print()
Guido van Rossum9a22de11995-01-12 12:29:47 +0000873
Guido van Rossuma8738a51996-03-14 21:30:28 +0000874def print_arguments():
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000875 print()
876 print("<H3>Command Line Arguments:</H3>")
877 print()
878 print(sys.argv)
879 print()
Guido van Rossuma8738a51996-03-14 21:30:28 +0000880
Guido van Rossum9a22de11995-01-12 12:29:47 +0000881def print_environ_usage():
Guido van Rossum7aee3841996-03-07 18:00:44 +0000882 """Dump a list of environment variables used by CGI as HTML."""
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000883 print("""
Guido van Rossum72755611996-03-06 07:20:06 +0000884<H3>These environment variables could have been set:</H3>
885<UL>
Guido van Rossum9a22de11995-01-12 12:29:47 +0000886<LI>AUTH_TYPE
887<LI>CONTENT_LENGTH
888<LI>CONTENT_TYPE
889<LI>DATE_GMT
890<LI>DATE_LOCAL
891<LI>DOCUMENT_NAME
892<LI>DOCUMENT_ROOT
893<LI>DOCUMENT_URI
894<LI>GATEWAY_INTERFACE
895<LI>LAST_MODIFIED
896<LI>PATH
897<LI>PATH_INFO
898<LI>PATH_TRANSLATED
899<LI>QUERY_STRING
900<LI>REMOTE_ADDR
901<LI>REMOTE_HOST
902<LI>REMOTE_IDENT
903<LI>REMOTE_USER
904<LI>REQUEST_METHOD
905<LI>SCRIPT_NAME
906<LI>SERVER_NAME
907<LI>SERVER_PORT
908<LI>SERVER_PROTOCOL
909<LI>SERVER_ROOT
910<LI>SERVER_SOFTWARE
911</UL>
Guido van Rossum7aee3841996-03-07 18:00:44 +0000912In addition, HTTP headers sent by the server may be passed in the
913environment as well. Here are some common variable names:
914<UL>
915<LI>HTTP_ACCEPT
916<LI>HTTP_CONNECTION
917<LI>HTTP_HOST
918<LI>HTTP_PRAGMA
919<LI>HTTP_REFERER
920<LI>HTTP_USER_AGENT
921</UL>
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000922""")
Guido van Rossum9a22de11995-01-12 12:29:47 +0000923
Guido van Rossum9a22de11995-01-12 12:29:47 +0000924
Guido van Rossum72755611996-03-06 07:20:06 +0000925# Utilities
926# =========
Guido van Rossum9a22de11995-01-12 12:29:47 +0000927
Guido van Rossum64c66201997-07-19 20:11:53 +0000928def escape(s, quote=None):
Skip Montanaro97b2fa22005-08-02 02:50:25 +0000929 '''Replace special characters "&", "<" and ">" to HTML-safe sequences.
930 If the optional flag quote is true, the quotation mark character (")
931 is also translated.'''
Eric S. Raymond7e9b4f52001-02-09 09:59:10 +0000932 s = s.replace("&", "&amp;") # Must be done first!
933 s = s.replace("<", "&lt;")
934 s = s.replace(">", "&gt;")
Guido van Rossum64c66201997-07-19 20:11:53 +0000935 if quote:
Eric S. Raymond7e9b4f52001-02-09 09:59:10 +0000936 s = s.replace('"', "&quot;")
Guido van Rossum7aee3841996-03-07 18:00:44 +0000937 return s
Guido van Rossum9a22de11995-01-12 12:29:47 +0000938
Guido van Rossum2e441f72001-07-25 21:00:19 +0000939def valid_boundary(s, _vb_pattern="^[ -~]{0,200}[!-~]$"):
940 import re
941 return re.match(_vb_pattern, s)
Guido van Rossum9a22de11995-01-12 12:29:47 +0000942
Guido van Rossum72755611996-03-06 07:20:06 +0000943# Invoke mainline
944# ===============
945
946# Call test() when this file is run as a script (not imported as a module)
Tim Peters88869f92001-01-14 23:36:06 +0000947if __name__ == '__main__':
Guido van Rossum7aee3841996-03-07 18:00:44 +0000948 test()