source file: /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/cgi.py
file stats: 562 lines, 160 executed: 28.5% covered
   1. #! /usr/bin/env python
   2. 
   3. """Support module for CGI (Common Gateway Interface) scripts.
   4. 
   5. This module defines a number of utilities for use by CGI scripts
   6. written in Python.
   7. """
   8. 
   9. # XXX Perhaps there should be a slimmed version that doesn't contain
  10. # all those backwards compatible and debugging classes and functions?
  11. 
  12. # History
  13. # -------
  14. #
  15. # Michael McLay started this module.  Steve Majewski changed the
  16. # interface to SvFormContentDict and FormContentDict.  The multipart
  17. # parsing was inspired by code submitted by Andreas Paepcke.  Guido van
  18. # Rossum rewrote, reformatted and documented the module and is currently
  19. # responsible for its maintenance.
  20. #
  21. 
  22. __version__ = "2.6"
  23. 
  24. 
  25. # Imports
  26. # =======
  27. 
  28. import sys
  29. import os
  30. import urllib
  31. import mimetools
  32. import rfc822
  33. import UserDict
  34. from StringIO import StringIO
  35. 
  36. __all__ = ["MiniFieldStorage", "FieldStorage", "FormContentDict",
  37.            "SvFormContentDict", "InterpFormContentDict", "FormContent",
  38.            "parse", "parse_qs", "parse_qsl", "parse_multipart",
  39.            "parse_header", "print_exception", "print_environ",
  40.            "print_form", "print_directory", "print_arguments",
  41.            "print_environ_usage", "escape"]
  42. 
  43. # Logging support
  44. # ===============
  45. 
  46. logfile = ""            # Filename to log to, if not empty
  47. logfp = None            # File object to log to, if not None
  48. 
  49. def initlog(*allargs):
  50.     """Write a log message, if there is a log file.
  51. 
  52.     Even though this function is called initlog(), you should always
  53.     use log(); log is a variable that is set either to initlog
  54.     (initially), to dolog (once the log file has been opened), or to
  55.     nolog (when logging is disabled).
  56. 
  57.     The first argument is a format string; the remaining arguments (if
  58.     any) are arguments to the % operator, so e.g.
  59.         log("%s: %s", "a", "b")
  60.     will write "a: b" to the log file, followed by a newline.
  61. 
  62.     If the global logfp is not None, it should be a file object to
  63.     which log data is written.
  64. 
  65.     If the global logfp is None, the global logfile may be a string
  66.     giving a filename to open, in append mode.  This file should be
  67.     world writable!!!  If the file can't be opened, logging is
  68.     silently disabled (since there is no safe place where we could
  69.     send an error message).
  70. 
  71.     """
  72.     global logfp, log
  73.     if logfile and not logfp:
  74.         try:
  75.             logfp = open(logfile, "a")
  76.         except IOError:
  77.             pass
  78.     if not logfp:
  79.         log = nolog
  80.     else:
  81.         log = dolog
  82.     log(*allargs)
  83. 
  84. def dolog(fmt, *args):
  85.     """Write a log message to the log file.  See initlog() for docs."""
  86.     logfp.write(fmt%args + "\n")
  87. 
  88. def nolog(*allargs):
  89.     """Dummy function, assigned to log when logging is disabled."""
  90.     pass
  91. 
  92. log = initlog           # The current logging function
  93. 
  94. 
  95. # Parsing functions
  96. # =================
  97. 
  98. # Maximum input we will accept when REQUEST_METHOD is POST
  99. # 0 ==> unlimited input
 100. maxlen = 0
 101. 
 102. def parse(fp=None, environ=os.environ, keep_blank_values=0, strict_parsing=0):
 103.     """Parse a query in the environment or from a file (default stdin)
 104. 
 105.         Arguments, all optional:
 106. 
 107.         fp              : file pointer; default: sys.stdin
 108. 
 109.         environ         : environment dictionary; default: os.environ
 110. 
 111.         keep_blank_values: flag indicating whether blank values in
 112.             URL encoded forms should be treated as blank strings.
 113.             A true value indicates that blanks should be retained as
 114.             blank strings.  The default false value indicates that
 115.             blank values are to be ignored and treated as if they were
 116.             not included.
 117. 
 118.         strict_parsing: flag indicating what to do with parsing errors.
 119.             If false (the default), errors are silently ignored.
 120.             If true, errors raise a ValueError exception.
 121.     """
 122.     if fp is None:
 123.         fp = sys.stdin
 124.     if not 'REQUEST_METHOD' in environ:
 125.         environ['REQUEST_METHOD'] = 'GET'       # For testing stand-alone
 126.     if environ['REQUEST_METHOD'] == 'POST':
 127.         ctype, pdict = parse_header(environ['CONTENT_TYPE'])
 128.         if ctype == 'multipart/form-data':
 129.             return parse_multipart(fp, pdict)
 130.         elif ctype == 'application/x-www-form-urlencoded':
 131.             clength = int(environ['CONTENT_LENGTH'])
 132.             if maxlen and clength > maxlen:
 133.                 raise ValueError, 'Maximum content length exceeded'
 134.             qs = fp.read(clength)
 135.         else:
 136.             qs = ''                     # Unknown content-type
 137.         if 'QUERY_STRING' in environ:
 138.             if qs: qs = qs + '&'
 139.             qs = qs + environ['QUERY_STRING']
 140.         elif sys.argv[1:]:
 141.             if qs: qs = qs + '&'
 142.             qs = qs + sys.argv[1]
 143.         environ['QUERY_STRING'] = qs    # XXX Shouldn't, really
 144.     elif 'QUERY_STRING' in environ:
 145.         qs = environ['QUERY_STRING']
 146.     else:
 147.         if sys.argv[1:]:
 148.             qs = sys.argv[1]
 149.         else:
 150.             qs = ""
 151.         environ['QUERY_STRING'] = qs    # XXX Shouldn't, really
 152.     return parse_qs(qs, keep_blank_values, strict_parsing)
 153. 
 154. 
 155. def parse_qs(qs, keep_blank_values=0, strict_parsing=0):
 156.     """Parse a query given as a string argument.
 157. 
 158.         Arguments:
 159. 
 160.         qs: URL-encoded query string to be parsed
 161. 
 162.         keep_blank_values: flag indicating whether blank values in
 163.             URL encoded queries should be treated as blank strings.
 164.             A true value indicates that blanks should be retained as
 165.             blank strings.  The default false value indicates that
 166.             blank values are to be ignored and treated as if they were
 167.             not included.
 168. 
 169.         strict_parsing: flag indicating what to do with parsing errors.
 170.             If false (the default), errors are silently ignored.
 171.             If true, errors raise a ValueError exception.
 172.     """
 173.     dict = {}
 174.     for name, value in parse_qsl(qs, keep_blank_values, strict_parsing):
 175.         if name in dict:
 176.             dict[name].append(value)
 177.         else:
 178.             dict[name] = [value]
 179.     return dict
 180. 
 181. def parse_qsl(qs, keep_blank_values=0, strict_parsing=0):
 182.     """Parse a query given as a string argument.
 183. 
 184.     Arguments:
 185. 
 186.     qs: URL-encoded query string to be parsed
 187. 
 188.     keep_blank_values: flag indicating whether blank values in
 189.         URL encoded queries should be treated as blank strings.  A
 190.         true value indicates that blanks should be retained as blank
 191.         strings.  The default false value indicates that blank values
 192.         are to be ignored and treated as if they were  not included.
 193. 
 194.     strict_parsing: flag indicating what to do with parsing errors. If
 195.         false (the default), errors are silently ignored. If true,
 196.         errors raise a ValueError exception.
 197. 
 198.     Returns a list, as G-d intended.
 199.     """
 200.     pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
 201.     r = []
 202.     for name_value in pairs:
 203.         nv = name_value.split('=', 1)
 204.         if len(nv) != 2:
 205.             if strict_parsing:
 206.                 raise ValueError, "bad query field: %s" % `name_value`
 207.             continue
 208.         if len(nv[1]) or keep_blank_values:
 209.             name = urllib.unquote(nv[0].replace('+', ' '))
 210.             value = urllib.unquote(nv[1].replace('+', ' '))
 211.             r.append((name, value))
 212. 
 213.     return r
 214. 
 215. 
 216. def parse_multipart(fp, pdict):
 217.     """Parse multipart input.
 218. 
 219.     Arguments:
 220.     fp   : input file
 221.     pdict: dictionary containing other parameters of conten-type header
 222. 
 223.     Returns a dictionary just like parse_qs(): keys are the field names, each
 224.     value is a list of values for that field.  This is easy to use but not
 225.     much good if you are expecting megabytes to be uploaded -- in that case,
 226.     use the FieldStorage class instead which is much more flexible.  Note
 227.     that content-type is the raw, unparsed contents of the content-type
 228.     header.
 229. 
 230.     XXX This does not parse nested multipart parts -- use FieldStorage for
 231.     that.
 232. 
 233.     XXX This should really be subsumed by FieldStorage altogether -- no
 234.     point in having two implementations of the same parsing algorithm.
 235. 
 236.     """
 237.     boundary = ""
 238.     if 'boundary' in pdict:
 239.         boundary = pdict['boundary']
 240.     if not valid_boundary(boundary):
 241.         raise ValueError,  ('Invalid boundary in multipart form: %s'
 242.                             % `boundary`)
 243. 
 244.     nextpart = "--" + boundary
 245.     lastpart = "--" + boundary + "--"
 246.     partdict = {}
 247.     terminator = ""
 248. 
 249.     while terminator != lastpart:
 250.         bytes = -1
 251.         data = None
 252.         if terminator:
 253.             # At start of next part.  Read headers first.
 254.             headers = mimetools.Message(fp)
 255.             clength = headers.getheader('content-length')
 256.             if clength:
 257.                 try:
 258.                     bytes = int(clength)
 259.                 except ValueError:
 260.                     pass
 261.             if bytes > 0:
 262.                 if maxlen and bytes > maxlen:
 263.                     raise ValueError, 'Maximum content length exceeded'
 264.                 data = fp.read(bytes)
 265.             else:
 266.                 data = ""
 267.         # Read lines until end of part.
 268.         lines = []
 269.         while 1:
 270.             line = fp.readline()
 271.             if not line:
 272.                 terminator = lastpart # End outer loop
 273.                 break
 274.             if line[:2] == "--":
 275.                 terminator = line.strip()
 276.                 if terminator in (nextpart, lastpart):
 277.                     break
 278.             lines.append(line)
 279.         # Done with part.
 280.         if data is None:
 281.             continue
 282.         if bytes < 0:
 283.             if lines:
 284.                 # Strip final line terminator
 285.                 line = lines[-1]
 286.                 if line[-2:] == "\r\n":
 287.                     line = line[:-2]
 288.                 elif line[-1:] == "\n":
 289.                     line = line[:-1]
 290.                 lines[-1] = line
 291.                 data = "".join(lines)
 292.         line = headers['content-disposition']
 293.         if not line:
 294.             continue
 295.         key, params = parse_header(line)
 296.         if key != 'form-data':
 297.             continue
 298.         if 'name' in params:
 299.             name = params['name']
 300.         else:
 301.             continue
 302.         if name in partdict:
 303.             partdict[name].append(data)
 304.         else:
 305.             partdict[name] = [data]
 306. 
 307.     return partdict
 308. 
 309. 
 310. def parse_header(line):
 311.     """Parse a Content-type like header.
 312. 
 313.     Return the main content-type and a dictionary of options.
 314. 
 315.     """
 316.     plist = map(lambda x: x.strip(), line.split(';'))
 317.     key = plist.pop(0).lower()
 318.     pdict = {}
 319.     for p in plist:
 320.         i = p.find('=')
 321.         if i >= 0:
 322.             name = p[:i].strip().lower()
 323.             value = p[i+1:].strip()
 324.             if len(value) >= 2 and value[0] == value[-1] == '"':
 325.                 value = value[1:-1]
 326.             pdict[name] = value
 327.     return key, pdict
 328. 
 329. 
 330. # Classes for field storage
 331. # =========================
 332. 
 333. class MiniFieldStorage:
 334. 
 335.     """Like FieldStorage, for use when no file uploads are possible."""
 336. 
 337.     # Dummy attributes
 338.     filename = None
 339.     list = None
 340.     type = None
 341.     file = None
 342.     type_options = {}
 343.     disposition = None
 344.     disposition_options = {}
 345.     headers = {}
 346. 
 347.     def __init__(self, name, value):
 348.         """Constructor from field name and value."""
 349.         self.name = name
 350.         self.value = value
 351.         # self.file = StringIO(value)
 352. 
 353.     def __repr__(self):
 354.         """Return printable representation."""
 355.         return "MiniFieldStorage(%s, %s)" % (`self.name`, `self.value`)
 356. 
 357. 
 358. class FieldStorage:
 359. 
 360.     """Store a sequence of fields, reading multipart/form-data.
 361. 
 362.     This class provides naming, typing, files stored on disk, and
 363.     more.  At the top level, it is accessible like a dictionary, whose
 364.     keys are the field names.  (Note: None can occur as a field name.)
 365.     The items are either a Python list (if there's multiple values) or
 366.     another FieldStorage or MiniFieldStorage object.  If it's a single
 367.     object, it has the following attributes:
 368. 
 369.     name: the field name, if specified; otherwise None
 370. 
 371.     filename: the filename, if specified; otherwise None; this is the
 372.         client side filename, *not* the file name on which it is
 373.         stored (that's a temporary file you don't deal with)
 374. 
 375.     value: the value as a *string*; for file uploads, this
 376.         transparently reads the file every time you request the value
 377. 
 378.     file: the file(-like) object from which you can read the data;
 379.         None if the data is stored a simple string
 380. 
 381.     type: the content-type, or None if not specified
 382. 
 383.     type_options: dictionary of options specified on the content-type
 384.         line
 385. 
 386.     disposition: content-disposition, or None if not specified
 387. 
 388.     disposition_options: dictionary of corresponding options
 389. 
 390.     headers: a dictionary(-like) object (sometimes rfc822.Message or a
 391.         subclass thereof) containing *all* headers
 392. 
 393.     The class is subclassable, mostly for the purpose of overriding
 394.     the make_file() method, which is called internally to come up with
 395.     a file open for reading and writing.  This makes it possible to
 396.     override the default choice of storing all files in a temporary
 397.     directory and unlinking them as soon as they have been opened.
 398. 
 399.     """
 400. 
 401.     def __init__(self, fp=None, headers=None, outerboundary="",
 402.                  environ=os.environ, keep_blank_values=0, strict_parsing=0):
 403.         """Constructor.  Read multipart/* until last part.
 404. 
 405.         Arguments, all optional:
 406. 
 407.         fp              : file pointer; default: sys.stdin
 408.             (not used when the request method is GET)
 409. 
 410.         headers         : header dictionary-like object; default:
 411.             taken from environ as per CGI spec
 412. 
 413.         outerboundary   : terminating multipart boundary
 414.             (for internal use only)
 415. 
 416.         environ         : environment dictionary; default: os.environ
 417. 
 418.         keep_blank_values: flag indicating whether blank values in
 419.             URL encoded forms should be treated as blank strings.
 420.             A true value indicates that blanks should be retained as
 421.             blank strings.  The default false value indicates that
 422.             blank values are to be ignored and treated as if they were
 423.             not included.
 424. 
 425.         strict_parsing: flag indicating what to do with parsing errors.
 426.             If false (the default), errors are silently ignored.
 427.             If true, errors raise a ValueError exception.
 428. 
 429.         """
 430.         method = 'GET'
 431.         self.keep_blank_values = keep_blank_values
 432.         self.strict_parsing = strict_parsing
 433.         if 'REQUEST_METHOD' in environ:
 434.             method = environ['REQUEST_METHOD'].upper()
 435.         if method == 'GET' or method == 'HEAD':
 436.             if 'QUERY_STRING' in environ:
 437.                 qs = environ['QUERY_STRING']
 438.             elif sys.argv[1:]:
 439.                 qs = sys.argv[1]
 440.             else:
 441.                 qs = ""
 442.             fp = StringIO(qs)
 443.             if headers is None:
 444.                 headers = {'content-type':
 445.                            "application/x-www-form-urlencoded"}
 446.         if headers is None:
 447.             headers = {}
 448.             if method == 'POST':
 449.                 # Set default content-type for POST to what's traditional
 450.                 headers['content-type'] = "application/x-www-form-urlencoded"
 451.             if 'CONTENT_TYPE' in environ:
 452.                 headers['content-type'] = environ['CONTENT_TYPE']
 453.             if 'CONTENT_LENGTH' in environ:
 454.                 headers['content-length'] = environ['CONTENT_LENGTH']
 455.         self.fp = fp or sys.stdin
 456.         self.headers = headers
 457.         self.outerboundary = outerboundary
 458. 
 459.         # Process content-disposition header
 460.         cdisp, pdict = "", {}
 461.         if 'content-disposition' in self.headers:
 462.             cdisp, pdict = parse_header(self.headers['content-disposition'])
 463.         self.disposition = cdisp
 464.         self.disposition_options = pdict
 465.         self.name = None
 466.         if 'name' in pdict:
 467.             self.name = pdict['name']
 468.         self.filename = None
 469.         if 'filename' in pdict:
 470.             self.filename = pdict['filename']
 471. 
 472.         # Process content-type header
 473.         #
 474.         # Honor any existing content-type header.  But if there is no
 475.         # content-type header, use some sensible defaults.  Assume
 476.         # outerboundary is "" at the outer level, but something non-false
 477.         # inside a multi-part.  The default for an inner part is text/plain,
 478.         # but for an outer part it should be urlencoded.  This should catch
 479.         # bogus clients which erroneously forget to include a content-type
 480.         # header.
 481.         #
 482.         # See below for what we do if there does exist a content-type header,
 483.         # but it happens to be something we don't understand.
 484.         if 'content-type' in self.headers:
 485.             ctype, pdict = parse_header(self.headers['content-type'])
 486.         elif self.outerboundary or method != 'POST':
 487.             ctype, pdict = "text/plain", {}
 488.         else:
 489.             ctype, pdict = 'application/x-www-form-urlencoded', {}
 490.         self.type = ctype
 491.         self.type_options = pdict
 492.         self.innerboundary = ""
 493.         if 'boundary' in pdict:
 494.             self.innerboundary = pdict['boundary']
 495.         clen = -1
 496.         if 'content-length' in self.headers:
 497.             try:
 498.                 clen = int(self.headers['content-length'])
 499.             except ValueError:
 500.                 pass
 501.             if maxlen and clen > maxlen:
 502.                 raise ValueError, 'Maximum content length exceeded'
 503.         self.length = clen
 504. 
 505.         self.list = self.file = None
 506.         self.done = 0
 507.         if ctype == 'application/x-www-form-urlencoded':
 508.             self.read_urlencoded()
 509.         elif ctype[:10] == 'multipart/':
 510.             self.read_multi(environ, keep_blank_values, strict_parsing)
 511.         else:
 512.             self.read_single()
 513. 
 514.     def __repr__(self):
 515.         """Return a printable representation."""
 516.         return "FieldStorage(%s, %s, %s)" % (
 517.                 `self.name`, `self.filename`, `self.value`)
 518. 
 519.     def __iter__(self):
 520.         return iter(self.keys())
 521. 
 522.     def __getattr__(self, name):
 523.         if name != 'value':
 524.             raise AttributeError, name
 525.         if self.file:
 526.             self.file.seek(0)
 527.             value = self.file.read()
 528.             self.file.seek(0)
 529.         elif self.list is not None:
 530.             value = self.list
 531.         else:
 532.             value = None
 533.         return value
 534. 
 535.     def __getitem__(self, key):
 536.         """Dictionary style indexing."""
 537.         if self.list is None:
 538.             raise TypeError, "not indexable"
 539.         found = []
 540.         for item in self.list:
 541.             if item.name == key: found.append(item)
 542.         if not found:
 543.             raise KeyError, key
 544.         if len(found) == 1:
 545.             return found[0]
 546.         else:
 547.             return found
 548. 
 549.     def getvalue(self, key, default=None):
 550.         """Dictionary style get() method, including 'value' lookup."""
 551.         if key in self:
 552.             value = self[key]
 553.             if type(value) is type([]):
 554.                 return map(lambda v: v.value, value)
 555.             else:
 556.                 return value.value
 557.         else:
 558.             return default
 559. 
 560.     def getfirst(self, key, default=None):
 561.         """ Return the first value received."""
 562.         if key in self:
 563.             value = self[key]
 564.             if type(value) is type([]):
 565.                 return value[0].value
 566.             else:
 567.                 return value.value
 568.         else:
 569.             return default
 570. 
 571.     def getlist(self, key):
 572.         """ Return list of received values."""
 573.         if key in self:
 574.             value = self[key]
 575.             if type(value) is type([]):
 576.                 return map(lambda v: v.value, value)
 577.             else:
 578.                 return [value.value]
 579.         else:
 580.             return []
 581. 
 582.     def keys(self):
 583.         """Dictionary style keys() method."""
 584.         if self.list is None:
 585.             raise TypeError, "not indexable"
 586.         keys = []
 587.         for item in self.list:
 588.             if item.name not in keys: keys.append(item.name)
 589.         return keys
 590. 
 591.     def has_key(self, key):
 592.         """Dictionary style has_key() method."""
 593.         if self.list is None:
 594.             raise TypeError, "not indexable"
 595.         for item in self.list:
 596.             if item.name == key: return True
 597.         return False
 598. 
 599.     def __contains__(self, key):
 600.         """Dictionary style __contains__ method."""
 601.         if self.list is None:
 602.             raise TypeError, "not indexable"
 603.         for item in self.list:
 604.             if item.name == key: return True
 605.         return False
 606. 
 607.     def __len__(self):
 608.         """Dictionary style len(x) support."""
 609.         return len(self.keys())
 610. 
 611.     def read_urlencoded(self):
 612.         """Internal: read data in query string format."""
 613.         qs = self.fp.read(self.length)
 614.         self.list = list = []
 615.         for key, value in parse_qsl(qs, self.keep_blank_values,
 616.                                     self.strict_parsing):
 617.             list.append(MiniFieldStorage(key, value))
 618.         self.skip_lines()
 619. 
 620.     FieldStorageClass = None
 621. 
 622.     def read_multi(self, environ, keep_blank_values, strict_parsing):
 623.         """Internal: read a part that is itself multipart."""
 624.         ib = self.innerboundary
 625.         if not valid_boundary(ib):
 626.             raise ValueError, ('Invalid boundary in multipart form: %s'
 627.                                % `ib`)
 628.         self.list = []
 629.         klass = self.FieldStorageClass or self.__class__
 630.         part = klass(self.fp, {}, ib,
 631.                      environ, keep_blank_values, strict_parsing)
 632.         # Throw first part away
 633.         while not part.done:
 634.             headers = rfc822.Message(self.fp)
 635.             part = klass(self.fp, headers, ib,
 636.                          environ, keep_blank_values, strict_parsing)
 637.             self.list.append(part)
 638.         self.skip_lines()
 639. 
 640.     def read_single(self):
 641.         """Internal: read an atomic part."""
 642.         if self.length >= 0:
 643.             self.read_binary()
 644.             self.skip_lines()
 645.         else:
 646.             self.read_lines()
 647.         self.file.seek(0)
 648. 
 649.     bufsize = 8*1024            # I/O buffering size for copy to file
 650. 
 651.     def read_binary(self):
 652.         """Internal: read binary data."""
 653.         self.file = self.make_file('b')
 654.         todo = self.length
 655.         if todo >= 0:
 656.             while todo > 0:
 657.                 data = self.fp.read(min(todo, self.bufsize))
 658.                 if not data:
 659.                     self.done = -1
 660.                     break
 661.                 self.file.write(data)
 662.                 todo = todo - len(data)
 663. 
 664.     def read_lines(self):
 665.         """Internal: read lines until EOF or outerboundary."""
 666.         self.file = self.__file = StringIO()
 667.         if self.outerboundary:
 668.             self.read_lines_to_outerboundary()
 669.         else:
 670.             self.read_lines_to_eof()
 671. 
 672.     def __write(self, line):
 673.         if self.__file is not None:
 674.             if self.__file.tell() + len(line) > 1000:
 675.                 self.file = self.make_file('')
 676.                 self.file.write(self.__file.getvalue())
 677.                 self.__file = None
 678.         self.file.write(line)
 679. 
 680.     def read_lines_to_eof(self):
 681.         """Internal: read lines until EOF."""
 682.         while 1:
 683.             line = self.fp.readline()
 684.             if not line:
 685.                 self.done = -1
 686.                 break
 687.             self.__write(line)
 688. 
 689.     def read_lines_to_outerboundary(self):
 690.         """Internal: read lines until outerboundary."""
 691.         next = "--" + self.outerboundary
 692.         last = next + "--"
 693.         delim = ""
 694.         while 1:
 695.             line = self.fp.readline()
 696.             if not line:
 697.                 self.done = -1
 698.                 break
 699.             if line[:2] == "--":
 700.                 strippedline = line.strip()
 701.                 if strippedline == next:
 702.                     break
 703.                 if strippedline == last:
 704.                     self.done = 1
 705.                     break
 706.             odelim = delim
 707.             if line[-2:] == "\r\n":
 708.                 delim = "\r\n"
 709.                 line = line[:-2]
 710.             elif line[-1] == "\n":
 711.                 delim = "\n"
 712.                 line = line[:-1]
 713.             else:
 714.                 delim = ""
 715.             self.__write(odelim + line)
 716. 
 717.     def skip_lines(self):
 718.         """Internal: skip lines until outer boundary if defined."""
 719.         if not self.outerboundary or self.done:
 720.             return
 721.         next = "--" + self.outerboundary
 722.         last = next + "--"
 723.         while 1:
 724.             line = self.fp.readline()
 725.             if not line:
 726.                 self.done = -1
 727.                 break
 728.             if line[:2] == "--":
 729.                 strippedline = line.strip()
 730.                 if strippedline == next:
 731.                     break
 732.                 if strippedline == last:
 733.                     self.done = 1
 734.                     break
 735. 
 736.     def make_file(self, binary=None):
 737.         """Overridable: return a readable & writable file.
 738. 
 739.         The file will be used as follows:
 740.         - data is written to it
 741.         - seek(0)
 742.         - data is read from it
 743. 
 744.         The 'binary' argument is unused -- the file is always opened
 745.         in binary mode.
 746. 
 747.         This version opens a temporary file for reading and writing,
 748.         and immediately deletes (unlinks) it.  The trick (on Unix!) is
 749.         that the file can still be used, but it can't be opened by
 750.         another process, and it will automatically be deleted when it
 751.         is closed or when the current process terminates.
 752. 
 753.         If you want a more permanent file, you derive a class which
 754.         overrides this method.  If you want a visible temporary file
 755.         that is nevertheless automatically deleted when the script
 756.         terminates, try defining a __del__ method in a derived class
 757.         which unlinks the temporary files you have created.
 758. 
 759.         """
 760.         import tempfile
 761.         return tempfile.TemporaryFile("w+b")
 762. 
 763. 
 764. 
 765. # Backwards Compatibility Classes
 766. # ===============================
 767. 
 768. class FormContentDict(UserDict.UserDict):
 769.     """Form content as dictionary with a list of values per field.
 770. 
 771.     form = FormContentDict()
 772. 
 773.     form[key] -> [value, value, ...]
 774.     key in form -> Boolean
 775.     form.keys() -> [key, key, ...]
 776.     form.values() -> [[val, val, ...], [val, val, ...], ...]
 777.     form.items() ->  [(key, [val, val, ...]), (key, [val, val, ...]), ...]
 778.     form.dict == {key: [val, val, ...], ...}
 779. 
 780.     """
 781.     def __init__(self, environ=os.environ):
 782.         self.dict = self.data = parse(environ=environ)
 783.         self.query_string = environ['QUERY_STRING']
 784. 
 785. 
 786. class SvFormContentDict(FormContentDict):
 787.     """Form content as dictionary expecting a single value per field.
 788. 
 789.     If you only expect a single value for each field, then form[key]
 790.     will return that single value.  It will raise an IndexError if
 791.     that expectation is not true.  If you expect a field to have
 792.     possible multiple values, than you can use form.getlist(key) to
 793.     get all of the values.  values() and items() are a compromise:
 794.     they return single strings where there is a single value, and
 795.     lists of strings otherwise.
 796. 
 797.     """
 798.     def __getitem__(self, key):
 799.         if len(self.dict[key]) > 1:
 800.             raise IndexError, 'expecting a single value'
 801.         return self.dict[key][0]
 802.     def getlist(self, key):
 803.         return self.dict[key]
 804.     def values(self):
 805.         result = []
 806.         for value in self.dict.values():
 807.             if len(value) == 1:
 808.                 result.append(value[0])
 809.             else: result.append(value)
 810.         return result
 811.     def items(self):
 812.         result = []
 813.         for key, value in self.dict.items():
 814.             if len(value) == 1:
 815.                 result.append((key, value[0]))
 816.             else: result.append((key, value))
 817.         return result
 818. 
 819. 
 820. class InterpFormContentDict(SvFormContentDict):
 821.     """This class is present for backwards compatibility only."""
 822.     def __getitem__(self, key):
 823.         v = SvFormContentDict.__getitem__(self, key)
 824.         if v[0] in '0123456789+-.':
 825.             try: return int(v)
 826.             except ValueError:
 827.                 try: return float(v)
 828.                 except ValueError: pass
 829.         return v.strip()
 830.     def values(self):
 831.         result = []
 832.         for key in self.keys():
 833.             try:
 834.                 result.append(self[key])
 835.             except IndexError:
 836.                 result.append(self.dict[key])
 837.         return result
 838.     def items(self):
 839.         result = []
 840.         for key in self.keys():
 841.             try:
 842.                 result.append((key, self[key]))
 843.             except IndexError:
 844.                 result.append((key, self.dict[key]))
 845.         return result
 846. 
 847. 
 848. class FormContent(FormContentDict):
 849.     """This class is present for backwards compatibility only."""
 850.     def values(self, key):
 851.         if key in self.dict :return self.dict[key]
 852.         else: return None
 853.     def indexed_value(self, key, location):
 854.         if key in self.dict:
 855.             if len(self.dict[key]) > location:
 856.                 return self.dict[key][location]
 857.             else: return None
 858.         else: return None
 859.     def value(self, key):
 860.         if key in self.dict: return self.dict[key][0]
 861.         else: return None
 862.     def length(self, key):
 863.         return len(self.dict[key])
 864.     def stripped(self, key):
 865.         if key in self.dict: return self.dict[key][0].strip()
 866.         else: return None
 867.     def pars(self):
 868.         return self.dict
 869. 
 870. 
 871. # Test/debug code
 872. # ===============
 873. 
 874. def test(environ=os.environ):
 875.     """Robust test CGI script, usable as main program.
 876. 
 877.     Write minimal HTTP headers and dump all information provided to
 878.     the script in HTML form.
 879. 
 880.     """
 881.     print "Content-type: text/html"
 882.     print
 883.     sys.stderr = sys.stdout
 884.     try:
 885.         form = FieldStorage()   # Replace with other classes to test those
 886.         print_directory()
 887.         print_arguments()
 888.         print_form(form)
 889.         print_environ(environ)
 890.         print_environ_usage()
 891.         def f():
 892.             exec "testing print_exception() -- <I>italics?</I>"
 893.         def g(f=f):
 894.             f()
 895.         print "<H3>What follows is a test, not an actual exception:</H3>"
 896.         g()
 897.     except:
 898.         print_exception()
 899. 
 900.     print "<H1>Second try with a small maxlen...</H1>"
 901. 
 902.     global maxlen
 903.     maxlen = 50
 904.     try:
 905.         form = FieldStorage()   # Replace with other classes to test those
 906.         print_directory()
 907.         print_arguments()
 908.         print_form(form)
 909.         print_environ(environ)
 910.     except:
 911.         print_exception()
 912. 
 913. def print_exception(type=None, value=None, tb=None, limit=None):
 914.     if type is None:
 915.         type, value, tb = sys.exc_info()
 916.     import traceback
 917.     print
 918.     print "<H3>Traceback (most recent call last):</H3>"
 919.     list = traceback.format_tb(tb, limit) + \
 920.            traceback.format_exception_only(type, value)
 921.     print "<PRE>%s<B>%s</B></PRE>" % (
 922.         escape("".join(list[:-1])),
 923.         escape(list[-1]),
 924.         )
 925.     del tb
 926. 
 927. def print_environ(environ=os.environ):
 928.     """Dump the shell environment as HTML."""
 929.     keys = environ.keys()
 930.     keys.sort()
 931.     print
 932.     print "<H3>Shell Environment:</H3>"
 933.     print "<DL>"
 934.     for key in keys:
 935.         print "<DT>", escape(key), "<DD>", escape(environ[key])
 936.     print "</DL>"
 937.     print
 938. 
 939. def print_form(form):
 940.     """Dump the contents of a form as HTML."""
 941.     keys = form.keys()
 942.     keys.sort()
 943.     print
 944.     print "<H3>Form Contents:</H3>"
 945.     if not keys:
 946.         print "<P>No form fields."
 947.     print "<DL>"
 948.     for key in keys:
 949.         print "<DT>" + escape(key) + ":",
 950.         value = form[key]
 951.         print "<i>" + escape(`type(value)`) + "</i>"
 952.         print "<DD>" + escape(`value`)
 953.     print "</DL>"
 954.     print
 955. 
 956. def print_directory():
 957.     """Dump the current directory as HTML."""
 958.     print
 959.     print "<H3>Current Working Directory:</H3>"
 960.     try:
 961.         pwd = os.getcwd()
 962.     except os.error, msg:
 963.         print "os.error:", escape(str(msg))
 964.     else:
 965.         print escape(pwd)
 966.     print
 967. 
 968. def print_arguments():
 969.     print
 970.     print "<H3>Command Line Arguments:</H3>"
 971.     print
 972.     print sys.argv
 973.     print
 974. 
 975. def print_environ_usage():
 976.     """Dump a list of environment variables used by CGI as HTML."""
 977.     print """
 978. <H3>These environment variables could have been set:</H3>
 979. <UL>
 980. <LI>AUTH_TYPE
 981. <LI>CONTENT_LENGTH
 982. <LI>CONTENT_TYPE
 983. <LI>DATE_GMT
 984. <LI>DATE_LOCAL
 985. <LI>DOCUMENT_NAME
 986. <LI>DOCUMENT_ROOT
 987. <LI>DOCUMENT_URI
 988. <LI>GATEWAY_INTERFACE
 989. <LI>LAST_MODIFIED
 990. <LI>PATH
 991. <LI>PATH_INFO
 992. <LI>PATH_TRANSLATED
 993. <LI>QUERY_STRING
 994. <LI>REMOTE_ADDR
 995. <LI>REMOTE_HOST
 996. <LI>REMOTE_IDENT
 997. <LI>REMOTE_USER
 998. <LI>REQUEST_METHOD
 999. <LI>SCRIPT_NAME
1000. <LI>SERVER_NAME
1001. <LI>SERVER_PORT
1002. <LI>SERVER_PROTOCOL
1003. <LI>SERVER_ROOT
1004. <LI>SERVER_SOFTWARE
1005. </UL>
1006. In addition, HTTP headers sent by the server may be passed in the
1007. environment as well.  Here are some common variable names:
1008. <UL>
1009. <LI>HTTP_ACCEPT
1010. <LI>HTTP_CONNECTION
1011. <LI>HTTP_HOST
1012. <LI>HTTP_PRAGMA
1013. <LI>HTTP_REFERER
1014. <LI>HTTP_USER_AGENT
1015. </UL>
1016. """
1017. 
1018. 
1019. # Utilities
1020. # =========
1021. 
1022. def escape(s, quote=None):
1023.     """Replace special characters '&', '<' and '>' by SGML entities."""
1024.     s = s.replace("&", "&amp;") # Must be done first!
1025.     s = s.replace("<", "&lt;")
1026.     s = s.replace(">", "&gt;")
1027.     if quote:
1028.         s = s.replace('"', "&quot;")
1029.     return s
1030. 
1031. def valid_boundary(s, _vb_pattern="^[ -~]{0,200}[!-~]$"):
1032.     import re
1033.     return re.match(_vb_pattern, s)
1034. 
1035. # Invoke mainline
1036. # ===============
1037. 
1038. # Call test() when this file is run as a script (not imported as a module)
1039. if __name__ == '__main__':
1040.     test()