1 #! /usr/local/bin/python
3 # 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
12 """Support module for CGI (Common Gateway Interface) scripts.
14 This module defines a number of utilities for use by CGI scripts
18 # XXX Perhaps there should be a slimmed version that doesn't contain
19 # all those backwards compatible and debugging classes and functions?
24 # Michael McLay started this module. Steve Majewski changed the
25 # interface to SvFormContentDict and FormContentDict. The multipart
26 # parsing was inspired by code submitted by Andreas Paepcke. Guido van
27 # Rossum rewrote, reformatted and documented the module and is currently
28 # responsible for its maintenance.
37 from operator
import attrgetter
45 from cStringIO
import StringIO
47 from StringIO
import StringIO
49 __all__
= ["MiniFieldStorage", "FieldStorage", "FormContentDict",
50 "SvFormContentDict", "InterpFormContentDict", "FormContent",
51 "parse", "parse_qs", "parse_qsl", "parse_multipart",
52 "parse_header", "print_exception", "print_environ",
53 "print_form", "print_directory", "print_arguments",
54 "print_environ_usage", "escape"]
59 logfile
= "" # Filename to log to, if not empty
60 logfp
= None # File object to log to, if not None
62 def initlog(*allargs
):
63 """Write a log message, if there is a log file.
65 Even though this function is called initlog(), you should always
66 use log(); log is a variable that is set either to initlog
67 (initially), to dolog (once the log file has been opened), or to
68 nolog (when logging is disabled).
70 The first argument is a format string; the remaining arguments (if
71 any) are arguments to the % operator, so e.g.
72 log("%s: %s", "a", "b")
73 will write "a: b" to the log file, followed by a newline.
75 If the global logfp is not None, it should be a file object to
76 which log data is written.
78 If the global logfp is None, the global logfile may be a string
79 giving a filename to open, in append mode. This file should be
80 world writable!!! If the file can't be opened, logging is
81 silently disabled (since there is no safe place where we could
82 send an error message).
86 if logfile
and not logfp
:
88 logfp
= open(logfile
, "a")
97 def dolog(fmt
, *args
):
98 """Write a log message to the log file. See initlog() for docs."""
99 logfp
.write(fmt
%args
+ "\n")
102 """Dummy function, assigned to log when logging is disabled."""
105 log
= initlog
# The current logging function
111 # Maximum input we will accept when REQUEST_METHOD is POST
112 # 0 ==> unlimited input
115 def parse(fp
=None, environ
=os
.environ
, keep_blank_values
=0, strict_parsing
=0):
116 """Parse a query in the environment or from a file (default stdin)
118 Arguments, all optional:
120 fp : file pointer; default: sys.stdin
122 environ : environment dictionary; default: os.environ
124 keep_blank_values: flag indicating whether blank values in
125 URL encoded forms should be treated as blank strings.
126 A true value indicates that blanks should be retained as
127 blank strings. The default false value indicates that
128 blank values are to be ignored and treated as if they were
131 strict_parsing: flag indicating what to do with parsing errors.
132 If false (the default), errors are silently ignored.
133 If true, errors raise a ValueError exception.
137 if not 'REQUEST_METHOD' in environ
:
138 environ
['REQUEST_METHOD'] = 'GET' # For testing stand-alone
139 if environ
['REQUEST_METHOD'] == 'POST':
140 ctype
, pdict
= parse_header(environ
['CONTENT_TYPE'])
141 if ctype
== 'multipart/form-data':
142 return parse_multipart(fp
, pdict
)
143 elif ctype
== 'application/x-www-form-urlencoded':
144 clength
= int(environ
['CONTENT_LENGTH'])
145 if maxlen
and clength
> maxlen
:
146 raise ValueError, 'Maximum content length exceeded'
147 qs
= fp
.read(clength
)
149 qs
= '' # Unknown content-type
150 if 'QUERY_STRING' in environ
:
152 qs
= qs
+ environ
['QUERY_STRING']
155 qs
= qs
+ sys
.argv
[1]
156 environ
['QUERY_STRING'] = qs
# XXX Shouldn't, really
157 elif 'QUERY_STRING' in environ
:
158 qs
= environ
['QUERY_STRING']
164 environ
['QUERY_STRING'] = qs
# XXX Shouldn't, really
165 return parse_qs(qs
, keep_blank_values
, strict_parsing
)
168 def parse_qs(qs
, keep_blank_values
=0, strict_parsing
=0):
169 """Parse a query given as a string argument.
173 qs: URL-encoded query string to be parsed
175 keep_blank_values: flag indicating whether blank values in
176 URL encoded queries should be treated as blank strings.
177 A true value indicates that blanks should be retained as
178 blank strings. The default false value indicates that
179 blank values are to be ignored and treated as if they were
182 strict_parsing: flag indicating what to do with parsing errors.
183 If false (the default), errors are silently ignored.
184 If true, errors raise a ValueError exception.
187 for name
, value
in parse_qsl(qs
, keep_blank_values
, strict_parsing
):
189 dict[name
].append(value
)
194 def parse_qsl(qs
, keep_blank_values
=0, strict_parsing
=0):
195 """Parse a query given as a string argument.
199 qs: URL-encoded query string to be parsed
201 keep_blank_values: flag indicating whether blank values in
202 URL encoded queries should be treated as blank strings. A
203 true value indicates that blanks should be retained as blank
204 strings. The default false value indicates that blank values
205 are to be ignored and treated as if they were not included.
207 strict_parsing: flag indicating what to do with parsing errors. If
208 false (the default), errors are silently ignored. If true,
209 errors raise a ValueError exception.
211 Returns a list, as G-d intended.
213 pairs
= [s2
for s1
in qs
.split('&') for s2
in s1
.split(';')]
215 for name_value
in pairs
:
216 if not name_value
and not strict_parsing
:
218 nv
= name_value
.split('=', 1)
221 raise ValueError, "bad query field: %r" % (name_value
,)
222 # Handle case of a control-name with no equal sign
223 if keep_blank_values
:
227 if len(nv
[1]) or keep_blank_values
:
228 name
= urllib
.unquote(nv
[0].replace('+', ' '))
229 value
= urllib
.unquote(nv
[1].replace('+', ' '))
230 r
.append((name
, value
))
235 def parse_multipart(fp
, pdict
):
236 """Parse multipart input.
240 pdict: dictionary containing other parameters of content-type header
242 Returns a dictionary just like parse_qs(): keys are the field names, each
243 value is a list of values for that field. This is easy to use but not
244 much good if you are expecting megabytes to be uploaded -- in that case,
245 use the FieldStorage class instead which is much more flexible. Note
246 that content-type is the raw, unparsed contents of the content-type
249 XXX This does not parse nested multipart parts -- use FieldStorage for
252 XXX This should really be subsumed by FieldStorage altogether -- no
253 point in having two implementations of the same parsing algorithm.
257 if 'boundary' in pdict
:
258 boundary
= pdict
['boundary']
259 if not valid_boundary(boundary
):
260 raise ValueError, ('Invalid boundary in multipart form: %r'
263 nextpart
= "--" + boundary
264 lastpart
= "--" + boundary
+ "--"
268 while terminator
!= lastpart
:
272 # At start of next part. Read headers first.
273 headers
= mimetools
.Message(fp
)
274 clength
= headers
.getheader('content-length')
281 if maxlen
and bytes
> maxlen
:
282 raise ValueError, 'Maximum content length exceeded'
283 data
= fp
.read(bytes
)
286 # Read lines until end of part.
291 terminator
= lastpart
# End outer loop
294 terminator
= line
.strip()
295 if terminator
in (nextpart
, lastpart
):
303 # Strip final line terminator
305 if line
[-2:] == "\r\n":
307 elif line
[-1:] == "\n":
310 data
= "".join(lines
)
311 line
= headers
['content-disposition']
314 key
, params
= parse_header(line
)
315 if key
!= 'form-data':
318 name
= params
['name']
322 partdict
[name
].append(data
)
324 partdict
[name
] = [data
]
329 def parse_header(line
):
330 """Parse a Content-type like header.
332 Return the main content-type and a dictionary of options.
335 plist
= [x
.strip() for x
in line
.split(';')]
336 key
= plist
.pop(0).lower()
341 name
= p
[:i
].strip().lower()
342 value
= p
[i
+1:].strip()
343 if len(value
) >= 2 and value
[0] == value
[-1] == '"':
345 value
= value
.replace('\\\\', '\\').replace('\\"', '"')
350 # Classes for field storage
351 # =========================
353 class MiniFieldStorage
:
355 """Like FieldStorage, for use when no file uploads are possible."""
364 disposition_options
= {}
367 def __init__(self
, name
, value
):
368 """Constructor from field name and value."""
371 # self.file = StringIO(value)
374 """Return printable representation."""
375 return "MiniFieldStorage(%r, %r)" % (self
.name
, self
.value
)
380 """Store a sequence of fields, reading multipart/form-data.
382 This class provides naming, typing, files stored on disk, and
383 more. At the top level, it is accessible like a dictionary, whose
384 keys are the field names. (Note: None can occur as a field name.)
385 The items are either a Python list (if there's multiple values) or
386 another FieldStorage or MiniFieldStorage object. If it's a single
387 object, it has the following attributes:
389 name: the field name, if specified; otherwise None
391 filename: the filename, if specified; otherwise None; this is the
392 client side filename, *not* the file name on which it is
393 stored (that's a temporary file you don't deal with)
395 value: the value as a *string*; for file uploads, this
396 transparently reads the file every time you request the value
398 file: the file(-like) object from which you can read the data;
399 None if the data is stored a simple string
401 type: the content-type, or None if not specified
403 type_options: dictionary of options specified on the content-type
406 disposition: content-disposition, or None if not specified
408 disposition_options: dictionary of corresponding options
410 headers: a dictionary(-like) object (sometimes rfc822.Message or a
411 subclass thereof) containing *all* headers
413 The class is subclassable, mostly for the purpose of overriding
414 the make_file() method, which is called internally to come up with
415 a file open for reading and writing. This makes it possible to
416 override the default choice of storing all files in a temporary
417 directory and unlinking them as soon as they have been opened.
421 def __init__(self
, fp
=None, headers
=None, outerboundary
="",
422 environ
=os
.environ
, keep_blank_values
=0, strict_parsing
=0):
423 """Constructor. Read multipart/* until last part.
425 Arguments, all optional:
427 fp : file pointer; default: sys.stdin
428 (not used when the request method is GET)
430 headers : header dictionary-like object; default:
431 taken from environ as per CGI spec
433 outerboundary : terminating multipart boundary
434 (for internal use only)
436 environ : environment dictionary; default: os.environ
438 keep_blank_values: flag indicating whether blank values in
439 URL encoded forms should be treated as blank strings.
440 A true value indicates that blanks should be retained as
441 blank strings. The default false value indicates that
442 blank values are to be ignored and treated as if they were
445 strict_parsing: flag indicating what to do with parsing errors.
446 If false (the default), errors are silently ignored.
447 If true, errors raise a ValueError exception.
451 self
.keep_blank_values
= keep_blank_values
452 self
.strict_parsing
= strict_parsing
453 if 'REQUEST_METHOD' in environ
:
454 method
= environ
['REQUEST_METHOD'].upper()
455 if method
== 'GET' or method
== 'HEAD':
456 if 'QUERY_STRING' in environ
:
457 qs
= environ
['QUERY_STRING']
464 headers
= {'content-type':
465 "application/x-www-form-urlencoded"}
469 # Set default content-type for POST to what's traditional
470 headers
['content-type'] = "application/x-www-form-urlencoded"
471 if 'CONTENT_TYPE' in environ
:
472 headers
['content-type'] = environ
['CONTENT_TYPE']
473 if 'CONTENT_LENGTH' in environ
:
474 headers
['content-length'] = environ
['CONTENT_LENGTH']
475 self
.fp
= fp
or sys
.stdin
476 self
.headers
= headers
477 self
.outerboundary
= outerboundary
479 # Process content-disposition header
480 cdisp
, pdict
= "", {}
481 if 'content-disposition' in self
.headers
:
482 cdisp
, pdict
= parse_header(self
.headers
['content-disposition'])
483 self
.disposition
= cdisp
484 self
.disposition_options
= pdict
487 self
.name
= pdict
['name']
489 if 'filename' in pdict
:
490 self
.filename
= pdict
['filename']
492 # Process content-type header
494 # Honor any existing content-type header. But if there is no
495 # content-type header, use some sensible defaults. Assume
496 # outerboundary is "" at the outer level, but something non-false
497 # inside a multi-part. The default for an inner part is text/plain,
498 # but for an outer part it should be urlencoded. This should catch
499 # bogus clients which erroneously forget to include a content-type
502 # See below for what we do if there does exist a content-type header,
503 # but it happens to be something we don't understand.
504 if 'content-type' in self
.headers
:
505 ctype
, pdict
= parse_header(self
.headers
['content-type'])
506 elif self
.outerboundary
or method
!= 'POST':
507 ctype
, pdict
= "text/plain", {}
509 ctype
, pdict
= 'application/x-www-form-urlencoded', {}
511 self
.type_options
= pdict
512 self
.innerboundary
= ""
513 if 'boundary' in pdict
:
514 self
.innerboundary
= pdict
['boundary']
516 if 'content-length' in self
.headers
:
518 clen
= int(self
.headers
['content-length'])
521 if maxlen
and clen
> maxlen
:
522 raise ValueError, 'Maximum content length exceeded'
525 self
.list = self
.file = None
527 if ctype
== 'application/x-www-form-urlencoded':
528 self
.read_urlencoded()
529 elif ctype
[:10] == 'multipart/':
530 self
.read_multi(environ
, keep_blank_values
, strict_parsing
)
535 """Return a printable representation."""
536 return "FieldStorage(%r, %r, %r)" % (
537 self
.name
, self
.filename
, self
.value
)
540 return iter(self
.keys())
542 def __getattr__(self
, name
):
544 raise AttributeError, name
547 value
= self
.file.read()
549 elif self
.list is not None:
555 def __getitem__(self
, key
):
556 """Dictionary style indexing."""
557 if self
.list is None:
558 raise TypeError, "not indexable"
560 for item
in self
.list:
561 if item
.name
== key
: found
.append(item
)
569 def getvalue(self
, key
, default
=None):
570 """Dictionary style get() method, including 'value' lookup."""
573 if type(value
) is type([]):
574 return map(attrgetter('value'), value
)
580 def getfirst(self
, key
, default
=None):
581 """ Return the first value received."""
584 if type(value
) is type([]):
585 return value
[0].value
591 def getlist(self
, key
):
592 """ Return list of received values."""
595 if type(value
) is type([]):
596 return map(attrgetter('value'), value
)
603 """Dictionary style keys() method."""
604 if self
.list is None:
605 raise TypeError, "not indexable"
607 for item
in self
.list:
608 if item
.name
not in keys
: keys
.append(item
.name
)
611 def has_key(self
, key
):
612 """Dictionary style has_key() method."""
613 if self
.list is None:
614 raise TypeError, "not indexable"
615 for item
in self
.list:
616 if item
.name
== key
: return True
619 def __contains__(self
, key
):
620 """Dictionary style __contains__ method."""
621 if self
.list is None:
622 raise TypeError, "not indexable"
623 for item
in self
.list:
624 if item
.name
== key
: return True
628 """Dictionary style len(x) support."""
629 return len(self
.keys())
631 def read_urlencoded(self
):
632 """Internal: read data in query string format."""
633 qs
= self
.fp
.read(self
.length
)
634 self
.list = list = []
635 for key
, value
in parse_qsl(qs
, self
.keep_blank_values
,
636 self
.strict_parsing
):
637 list.append(MiniFieldStorage(key
, value
))
640 FieldStorageClass
= None
642 def read_multi(self
, environ
, keep_blank_values
, strict_parsing
):
643 """Internal: read a part that is itself multipart."""
644 ib
= self
.innerboundary
645 if not valid_boundary(ib
):
646 raise ValueError, 'Invalid boundary in multipart form: %r' % (ib
,)
648 klass
= self
.FieldStorageClass
or self
.__class
__
649 part
= klass(self
.fp
, {}, ib
,
650 environ
, keep_blank_values
, strict_parsing
)
651 # Throw first part away
653 headers
= rfc822
.Message(self
.fp
)
654 part
= klass(self
.fp
, headers
, ib
,
655 environ
, keep_blank_values
, strict_parsing
)
656 self
.list.append(part
)
659 def read_single(self
):
660 """Internal: read an atomic part."""
668 bufsize
= 8*1024 # I/O buffering size for copy to file
670 def read_binary(self
):
671 """Internal: read binary data."""
672 self
.file = self
.make_file('b')
676 data
= self
.fp
.read(min(todo
, self
.bufsize
))
680 self
.file.write(data
)
681 todo
= todo
- len(data
)
683 def read_lines(self
):
684 """Internal: read lines until EOF or outerboundary."""
685 self
.file = self
.__file
= StringIO()
686 if self
.outerboundary
:
687 self
.read_lines_to_outerboundary()
689 self
.read_lines_to_eof()
691 def __write(self
, line
):
692 if self
.__file
is not None:
693 if self
.__file
.tell() + len(line
) > 1000:
694 self
.file = self
.make_file('')
695 self
.file.write(self
.__file
.getvalue())
697 self
.file.write(line
)
699 def read_lines_to_eof(self
):
700 """Internal: read lines until EOF."""
702 line
= self
.fp
.readline()
708 def read_lines_to_outerboundary(self
):
709 """Internal: read lines until outerboundary."""
710 next
= "--" + self
.outerboundary
714 line
= self
.fp
.readline()
719 strippedline
= line
.strip()
720 if strippedline
== next
:
722 if strippedline
== last
:
726 if line
[-2:] == "\r\n":
729 elif line
[-1] == "\n":
734 self
.__write
(odelim
+ line
)
736 def skip_lines(self
):
737 """Internal: skip lines until outer boundary if defined."""
738 if not self
.outerboundary
or self
.done
:
740 next
= "--" + self
.outerboundary
743 line
= self
.fp
.readline()
748 strippedline
= line
.strip()
749 if strippedline
== next
:
751 if strippedline
== last
:
755 def make_file(self
, binary
=None):
756 """Overridable: return a readable & writable file.
758 The file will be used as follows:
759 - data is written to it
761 - data is read from it
763 The 'binary' argument is unused -- the file is always opened
766 This version opens a temporary file for reading and writing,
767 and immediately deletes (unlinks) it. The trick (on Unix!) is
768 that the file can still be used, but it can't be opened by
769 another process, and it will automatically be deleted when it
770 is closed or when the current process terminates.
772 If you want a more permanent file, you derive a class which
773 overrides this method. If you want a visible temporary file
774 that is nevertheless automatically deleted when the script
775 terminates, try defining a __del__ method in a derived class
776 which unlinks the temporary files you have created.
780 return tempfile
.TemporaryFile("w+b")
784 # Backwards Compatibility Classes
785 # ===============================
787 class FormContentDict(UserDict
.UserDict
):
788 """Form content as dictionary with a list of values per field.
790 form = FormContentDict()
792 form[key] -> [value, value, ...]
793 key in form -> Boolean
794 form.keys() -> [key, key, ...]
795 form.values() -> [[val, val, ...], [val, val, ...], ...]
796 form.items() -> [(key, [val, val, ...]), (key, [val, val, ...]), ...]
797 form.dict == {key: [val, val, ...], ...}
800 def __init__(self
, environ
=os
.environ
):
801 self
.dict = self
.data
= parse(environ
=environ
)
802 self
.query_string
= environ
['QUERY_STRING']
805 class SvFormContentDict(FormContentDict
):
806 """Form content as dictionary expecting a single value per field.
808 If you only expect a single value for each field, then form[key]
809 will return that single value. It will raise an IndexError if
810 that expectation is not true. If you expect a field to have
811 possible multiple values, than you can use form.getlist(key) to
812 get all of the values. values() and items() are a compromise:
813 they return single strings where there is a single value, and
814 lists of strings otherwise.
817 def __getitem__(self
, key
):
818 if len(self
.dict[key
]) > 1:
819 raise IndexError, 'expecting a single value'
820 return self
.dict[key
][0]
821 def getlist(self
, key
):
822 return self
.dict[key
]
825 for value
in self
.dict.values():
827 result
.append(value
[0])
828 else: result
.append(value
)
832 for key
, value
in self
.dict.items():
834 result
.append((key
, value
[0]))
835 else: result
.append((key
, value
))
839 class InterpFormContentDict(SvFormContentDict
):
840 """This class is present for backwards compatibility only."""
841 def __getitem__(self
, key
):
842 v
= SvFormContentDict
.__getitem
__(self
, key
)
843 if v
[0] in '0123456789+-.':
847 except ValueError: pass
851 for key
in self
.keys():
853 result
.append(self
[key
])
855 result
.append(self
.dict[key
])
859 for key
in self
.keys():
861 result
.append((key
, self
[key
]))
863 result
.append((key
, self
.dict[key
]))
867 class FormContent(FormContentDict
):
868 """This class is present for backwards compatibility only."""
869 def values(self
, key
):
870 if key
in self
.dict :return self
.dict[key
]
872 def indexed_value(self
, key
, location
):
874 if len(self
.dict[key
]) > location
:
875 return self
.dict[key
][location
]
878 def value(self
, key
):
879 if key
in self
.dict: return self
.dict[key
][0]
881 def length(self
, key
):
882 return len(self
.dict[key
])
883 def stripped(self
, key
):
884 if key
in self
.dict: return self
.dict[key
][0].strip()
893 def test(environ
=os
.environ
):
894 """Robust test CGI script, usable as main program.
896 Write minimal HTTP headers and dump all information provided to
897 the script in HTML form.
900 print "Content-type: text/html"
902 sys
.stderr
= sys
.stdout
904 form
= FieldStorage() # Replace with other classes to test those
908 print_environ(environ
)
909 print_environ_usage()
911 exec "testing print_exception() -- <I>italics?</I>"
914 print "<H3>What follows is a test, not an actual exception:</H3>"
919 print "<H1>Second try with a small maxlen...</H1>"
924 form
= FieldStorage() # Replace with other classes to test those
928 print_environ(environ
)
932 def print_exception(type=None, value
=None, tb
=None, limit
=None):
934 type, value
, tb
= sys
.exc_info()
937 print "<H3>Traceback (most recent call last):</H3>"
938 list = traceback
.format_tb(tb
, limit
) + \
939 traceback
.format_exception_only(type, value
)
940 print "<PRE>%s<B>%s</B></PRE>" % (
941 escape("".join(list[:-1])),
946 def print_environ(environ
=os
.environ
):
947 """Dump the shell environment as HTML."""
948 keys
= environ
.keys()
951 print "<H3>Shell Environment:</H3>"
954 print "<DT>", escape(key
), "<DD>", escape(environ
[key
])
958 def print_form(form
):
959 """Dump the contents of a form as HTML."""
963 print "<H3>Form Contents:</H3>"
965 print "<P>No form fields."
968 print "<DT>" + escape(key
) + ":",
970 print "<i>" + escape(repr(type(value
))) + "</i>"
971 print "<DD>" + escape(repr(value
))
975 def print_directory():
976 """Dump the current directory as HTML."""
978 print "<H3>Current Working Directory:</H3>"
981 except os
.error
, msg
:
982 print "os.error:", escape(str(msg
))
987 def print_arguments():
989 print "<H3>Command Line Arguments:</H3>"
994 def print_environ_usage():
995 """Dump a list of environment variables used by CGI as HTML."""
997 <H3>These environment variables could have been set:</H3>
1007 <LI>GATEWAY_INTERFACE
1025 In addition, HTTP headers sent by the server may be passed in the
1026 environment as well. Here are some common variable names:
1041 def escape(s
, quote
=None):
1042 '''Replace special characters "&", "<" and ">" to HTML-safe sequences.
1043 If the optional flag quote is true, the quotation mark character (")
1044 is also translated.'''
1045 s
= s
.replace("&", "&") # Must be done first!
1046 s
= s
.replace("<", "<")
1047 s
= s
.replace(">", ">")
1049 s
= s
.replace('"', """)
1052 def valid_boundary(s
, _vb_pattern
="^[ -~]{0,200}[!-~]$"):
1054 return re
.match(_vb_pattern
, s
)
1059 # Call test() when this file is run as a script (not imported as a module)
1060 if __name__
== '__main__':