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