Mercurial > cpython-lru_cache-weakref
annotate Lib/cgi.py @ 74006:88198b93ff2f
Issue #13560: Add PyUnicode_EncodeLocale()
* Use PyUnicode_EncodeLocale() in time.strftime() if wcsftime() is not
available
* Document my last changes in Misc/NEWS
| author | Victor Stinner <victor.stinner@haypocalc.com> |
|---|---|
| date | Sat, 17 Dec 2011 04:13:41 +0100 |
| parents | f82ebf9b3a52 |
| children | 59ea872d8b6b 47f98a550d42 |
| 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 |
|
11870
d7eca5b0960a
Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents:
11849
diff
changeset
|
18 # History |
|
d7eca5b0960a
Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents:
11849
diff
changeset
|
19 # ------- |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15669
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 # 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
|
22 # 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
|
23 # 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
|
24 # 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
|
25 # responsible for its maintenance. |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15669
diff
changeset
|
26 # |
|
3432
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
27 |
|
18337
c2a60de91d2c
Solve SF bug #231249: cgi.py opens too many (temporary) files.
Guido van Rossum <guido@python.org>
parents:
17074
diff
changeset
|
28 __version__ = "2.6" |
|
3446
623ba9bdeb22
Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents:
3444
diff
changeset
|
29 |
|
3432
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
30 |
|
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
31 # Imports |
|
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
32 # ======= |
|
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
33 |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
34 from io import StringIO, BytesIO, TextIOWrapper |
|
3432
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
35 import sys |
|
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
36 import os |
|
48334
5a416a6417d3
Make a new urllib package .
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
47643
diff
changeset
|
37 import urllib.parse |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
38 from email.parser import FeedParser |
|
48998
65e23683c010
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48410
diff
changeset
|
39 from warnings import warn |
|
65362
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
63883
diff
changeset
|
40 import html |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
41 import locale |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
42 import tempfile |
|
3432
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
43 |
|
46915
e94574455d9d
Remove old backwards-compatibility classes from the cgi module.
Georg Brandl <georg@python.org>
parents:
45156
diff
changeset
|
44 __all__ = ["MiniFieldStorage", "FieldStorage", |
|
17074
8323b098c6ce
Add a whole lot of stuff to __all__.
Guido van Rossum <guido@python.org>
parents:
16821
diff
changeset
|
45 "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
|
46 "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
|
47 "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
|
48 "print_environ_usage", "escape"] |
|
4214
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
49 |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
50 # Logging support |
|
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 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
53 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
|
54 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
|
55 |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
56 def initlog(*allargs): |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
57 """Write a log message, if there is a log file. |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
58 |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
59 Even though this function is called initlog(), you should always |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
60 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
|
61 (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
|
62 nolog (when logging is disabled). |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
63 |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
64 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
|
65 any) are arguments to the % operator, so e.g. |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
66 log("%s: %s", "a", "b") |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
67 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
|
68 |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
69 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
|
70 which log data is written. |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
71 |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 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
|
76 send an error message). |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
77 |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
78 """ |
|
71337
66e519792e4c
Add cgi.closelog() function to close the log file
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
79 global log, logfile, logfp |
|
4214
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
80 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
|
81 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
82 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
|
83 except IOError: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
84 pass |
|
4214
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
85 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
|
86 log = nolog |
|
4214
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
87 else: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
88 log = dolog |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
25214
diff
changeset
|
89 log(*allargs) |
|
4214
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
90 |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
91 def dolog(fmt, *args): |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
92 """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
|
93 logfp.write(fmt%args + "\n") |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
94 |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
95 def nolog(*allargs): |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
96 """Dummy function, assigned to log when logging is disabled.""" |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
97 pass |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
98 |
|
71337
66e519792e4c
Add cgi.closelog() function to close the log file
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
99 def closelog(): |
|
66e519792e4c
Add cgi.closelog() function to close the log file
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
100 """Close the log file.""" |
|
66e519792e4c
Add cgi.closelog() function to close the log file
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
101 global log, logfile, logfp |
|
66e519792e4c
Add cgi.closelog() function to close the log file
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
102 logfile = '' |
|
66e519792e4c
Add cgi.closelog() function to close the log file
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
103 if logfp: |
|
66e519792e4c
Add cgi.closelog() function to close the log file
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
104 logfp.close() |
|
66e519792e4c
Add cgi.closelog() function to close the log file
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
105 logfp = None |
|
66e519792e4c
Add cgi.closelog() function to close the log file
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
106 log = initlog |
|
66e519792e4c
Add cgi.closelog() function to close the log file
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
107 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
108 log = initlog # The current logging function |
|
4214
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
109 |
|
688d566cb15b
Added logging support.
Guido van Rossum <guido@python.org>
parents:
4213
diff
changeset
|
110 |
|
3432
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
111 # Parsing functions |
|
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
112 # ================= |
|
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
113 |
|
5499
b905372edd4f
Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents:
5302
diff
changeset
|
114 # 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
|
115 # 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
|
116 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
|
117 |
|
4534
d90d7b541ada
Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents:
4486
diff
changeset
|
118 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
|
119 """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
|
120 |
|
d1c47f4f1541
Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents:
3630
diff
changeset
|
121 Arguments, all optional: |
|
d1c47f4f1541
Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents:
3630
diff
changeset
|
122 |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
123 fp : file pointer; default: sys.stdin.buffer |
|
3794
d1c47f4f1541
Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents:
3630
diff
changeset
|
124 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
125 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
|
126 |
|
d1c47f4f1541
Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents:
3630
diff
changeset
|
127 keep_blank_values: flag indicating whether blank values in |
|
63886
50f8f5247c81
Merged revisions 83901 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
53909
diff
changeset
|
128 percent-encoded forms should be treated as blank strings. |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15669
diff
changeset
|
129 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
|
130 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
|
131 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
|
132 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
|
133 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
134 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
|
135 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
|
136 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
|
137 """ |
|
23609
456d58451fee
Use is None rather than general boolean
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
138 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
|
139 fp = sys.stdin |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
140 |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
141 # field keys and values (except for files) are returned as strings |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
142 # an encoding is required to decode the bytes read from self.fp |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
143 if hasattr(fp,'encoding'): |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
144 encoding = fp.encoding |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
145 else: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
146 encoding = 'latin-1' |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
147 |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
148 # fp.read() must return bytes |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
149 if isinstance(fp, TextIOWrapper): |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
150 fp = fp.buffer |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
151 |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
152 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 elif ctype == 'application/x-www-form-urlencoded': |
|
16503
8d7655ac8bf8
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
159 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
|
160 if maxlen and clength > maxlen: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42932
diff
changeset
|
161 raise ValueError('Maximum content length exceeded') |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
162 qs = fp.read(clength).decode(encoding) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
163 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
164 qs = '' # Unknown content-type |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
165 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
|
166 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
|
167 qs = qs + environ['QUERY_STRING'] |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15669
diff
changeset
|
168 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
|
169 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
|
170 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
|
171 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
|
172 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
|
173 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
|
174 else: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
175 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
|
176 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
|
177 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
178 qs = "" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
179 environ['QUERY_STRING'] = qs # XXX Shouldn't, really |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
180 return urllib.parse.parse_qs(qs, keep_blank_values, strict_parsing, |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
181 encoding=encoding) |
|
48998
65e23683c010
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48410
diff
changeset
|
182 |
|
2918
cfa9b3a84872
added parse_qs(query_string)
Guido van Rossum <guido@python.org>
parents:
2538
diff
changeset
|
183 |
|
48998
65e23683c010
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48410
diff
changeset
|
184 # parse query string function called from urlparse, |
|
65e23683c010
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48410
diff
changeset
|
185 # this is done in order to maintain backward compatiblity. |
|
2918
cfa9b3a84872
added parse_qs(query_string)
Guido van Rossum <guido@python.org>
parents:
2538
diff
changeset
|
186 |
|
4534
d90d7b541ada
Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents:
4486
diff
changeset
|
187 def parse_qs(qs, keep_blank_values=0, strict_parsing=0): |
|
48998
65e23683c010
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48410
diff
changeset
|
188 """Parse a query given as a string argument.""" |
|
65e23683c010
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48410
diff
changeset
|
189 warn("cgi.parse_qs is deprecated, use urllib.parse.parse_qs instead", |
|
53909
238502460795
#4351: more appropriate DeprecationWarning stacklevels
Philip Jenvey <pjenvey@underboss.org>
parents:
49976
diff
changeset
|
190 DeprecationWarning, 2) |
|
48998
65e23683c010
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48410
diff
changeset
|
191 return urllib.parse.parse_qs(qs, keep_blank_values, strict_parsing) |
|
10920
3faa63430924
Patch by Jim Fulton: new function parse_qsl(), which is like
Guido van Rossum <guido@python.org>
parents:
10914
diff
changeset
|
192 |
|
3faa63430924
Patch by Jim Fulton: new function parse_qsl(), which is like
Guido van Rossum <guido@python.org>
parents:
10914
diff
changeset
|
193 def parse_qsl(qs, keep_blank_values=0, strict_parsing=0): |
|
48998
65e23683c010
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48410
diff
changeset
|
194 """Parse a query given as a string argument.""" |
|
49101
c46bcccf2af2
Added a warning filter to don't show the warning during
Facundo Batista <facundobatista@gmail.com>
parents:
48998
diff
changeset
|
195 warn("cgi.parse_qsl is deprecated, use urllib.parse.parse_qsl instead", |
|
53909
238502460795
#4351: more appropriate DeprecationWarning stacklevels
Philip Jenvey <pjenvey@underboss.org>
parents:
49976
diff
changeset
|
196 DeprecationWarning, 2) |
|
48998
65e23683c010
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48410
diff
changeset
|
197 return urllib.parse.parse_qsl(qs, keep_blank_values, strict_parsing) |
| 2135 | 198 |
|
3446
623ba9bdeb22
Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents:
3444
diff
changeset
|
199 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
|
200 """Parse multipart input. |
| 2135 | 201 |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
202 Arguments: |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
203 fp : input file |
|
34367
5cd4fffaf910
Patch #1079734: remove dependencies on (deprecated) rfc822 and mimetools
Johannes Gijsbers <jlg@dds.nl>
parents:
34294
diff
changeset
|
204 pdict: dictionary containing other parameters of content-type header |
|
3432
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
205 |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15669
diff
changeset
|
206 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
|
207 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
|
208 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
|
209 use the FieldStorage class instead which is much more flexible. Note |
|
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15669
diff
changeset
|
210 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
|
211 header. |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15669
diff
changeset
|
212 |
|
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15669
diff
changeset
|
213 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
|
214 that. |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15669
diff
changeset
|
215 |
|
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15669
diff
changeset
|
216 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
|
217 point in having two implementations of the same parsing algorithm. |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39575
diff
changeset
|
218 Also, FieldStorage protects itself better against certain DoS attacks |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39575
diff
changeset
|
219 by limiting the size of the data read in one chunk. The API here |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39575
diff
changeset
|
220 does not support that kind of protection. This also affects parse() |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39575
diff
changeset
|
221 since it can call parse_multipart(). |
|
3432
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
222 |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
223 """ |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47632
diff
changeset
|
224 import http.client |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47632
diff
changeset
|
225 |
|
18833
3cc66750687d
Fix a denial-of-service attack, SF bug #443120.
Guido van Rossum <guido@python.org>
parents:
18337
diff
changeset
|
226 boundary = "" |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
227 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
|
228 boundary = pdict['boundary'] |
|
18833
3cc66750687d
Fix a denial-of-service attack, SF bug #443120.
Guido van Rossum <guido@python.org>
parents:
18337
diff
changeset
|
229 if not valid_boundary(boundary): |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42932
diff
changeset
|
230 raise ValueError('Invalid boundary in multipart form: %r' |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28089
diff
changeset
|
231 % (boundary,)) |
|
19119
600a9a2b7a18
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
18833
diff
changeset
|
232 |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
233 nextpart = "--" + boundary |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
234 lastpart = "--" + boundary + "--" |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
235 partdict = {} |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
236 terminator = "" |
|
3432
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
237 |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
238 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
|
239 bytes = -1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
240 data = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
241 if terminator: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
242 # At start of next part. Read headers first. |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47632
diff
changeset
|
243 headers = http.client.parse_headers(fp) |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47632
diff
changeset
|
244 clength = headers.get('content-length') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
245 if clength: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
246 try: |
|
16503
8d7655ac8bf8
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
247 bytes = int(clength) |
|
8d7655ac8bf8
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
248 except ValueError: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
249 pass |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
250 if bytes > 0: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
251 if maxlen and bytes > maxlen: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42932
diff
changeset
|
252 raise ValueError('Maximum content length exceeded') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
253 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
|
254 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
255 data = "" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
256 # 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
|
257 lines = [] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
258 while 1: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
259 line = fp.readline() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
260 if not line: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
261 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
|
262 break |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
263 if line.startswith("--"): |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
264 terminator = line.rstrip() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
265 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
|
266 break |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
267 lines.append(line) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
268 # Done with part. |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
269 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
|
270 continue |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
271 if bytes < 0: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
272 if lines: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
273 # 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
|
274 line = lines[-1] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
275 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
|
276 line = line[:-2] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
277 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
|
278 line = line[:-1] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
279 lines[-1] = line |
|
16503
8d7655ac8bf8
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
280 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
|
281 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
|
282 if not line: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
283 continue |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
284 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
|
285 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
|
286 continue |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
287 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
|
288 name = params['name'] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
289 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
290 continue |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
291 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
|
292 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
|
293 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
294 partdict[name] = [data] |
|
3432
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
295 |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
296 return partdict |
| 2135 | 297 |
| 298 | |
|
49976
89e738c792c0
Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
49101
diff
changeset
|
299 def _parseparam(s): |
|
89e738c792c0
Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
49101
diff
changeset
|
300 while s[:1] == ';': |
|
89e738c792c0
Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
49101
diff
changeset
|
301 s = s[1:] |
|
89e738c792c0
Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
49101
diff
changeset
|
302 end = s.find(';') |
|
72989
f5bd78b11275
3.2 - Fix closes Issue12529 - cgi.parse_header failure on double quotes and
Senthil Kumaran <senthil@uthcode.com>
parents:
68257
diff
changeset
|
303 while end > 0 and (s.count('"', 0, end) - s.count('\\"', 0, end)) % 2: |
|
49976
89e738c792c0
Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
49101
diff
changeset
|
304 end = s.find(';', end + 1) |
|
89e738c792c0
Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
49101
diff
changeset
|
305 if end < 0: |
|
89e738c792c0
Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
49101
diff
changeset
|
306 end = len(s) |
|
89e738c792c0
Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
49101
diff
changeset
|
307 f = s[:end] |
|
89e738c792c0
Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
49101
diff
changeset
|
308 yield f.strip() |
|
89e738c792c0
Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
49101
diff
changeset
|
309 s = s[end:] |
|
89e738c792c0
Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
49101
diff
changeset
|
310 |
|
3432
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
311 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
|
312 """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
|
313 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
314 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
|
315 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
316 """ |
|
49976
89e738c792c0
Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
49101
diff
changeset
|
317 parts = _parseparam(';' + line) |
|
89e738c792c0
Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
49101
diff
changeset
|
318 key = parts.__next__() |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
319 pdict = {} |
|
49976
89e738c792c0
Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
49101
diff
changeset
|
320 for p in parts: |
|
16503
8d7655ac8bf8
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
321 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
|
322 if i >= 0: |
|
16503
8d7655ac8bf8
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
323 name = p[:i].strip().lower() |
|
8d7655ac8bf8
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
324 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
|
325 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
|
326 value = value[1:-1] |
|
33131
846ba2972d7c
Let cgi.parse_header() properly unquote headers (patch #1008597).
Johannes Gijsbers <jlg@dds.nl>
parents:
32758
diff
changeset
|
327 value = value.replace('\\\\', '\\').replace('\\"', '"') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
328 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
|
329 return key, pdict |
|
3432
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
330 |
|
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
331 |
|
3437
d70149202c93
Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents:
3435
diff
changeset
|
332 # Classes for field storage |
|
d70149202c93
Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents:
3435
diff
changeset
|
333 # ========================= |
|
d70149202c93
Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents:
3435
diff
changeset
|
334 |
|
d70149202c93
Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents:
3435
diff
changeset
|
335 class MiniFieldStorage: |
|
d70149202c93
Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents:
3435
diff
changeset
|
336 |
|
3446
623ba9bdeb22
Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents:
3444
diff
changeset
|
337 """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
|
338 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
339 # Dummy attributes |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
340 filename = None |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
341 list = None |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
342 type = None |
|
3794
d1c47f4f1541
Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents:
3630
diff
changeset
|
343 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
|
344 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
|
345 disposition = None |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
346 disposition_options = {} |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
347 headers = {} |
|
3437
d70149202c93
Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents:
3435
diff
changeset
|
348 |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
349 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
|
350 """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
|
351 self.name = name |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
352 self.value = value |
|
3794
d1c47f4f1541
Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents:
3630
diff
changeset
|
353 # self.file = StringIO(value) |
|
3437
d70149202c93
Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents:
3435
diff
changeset
|
354 |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
355 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
|
356 """Return printable representation.""" |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28089
diff
changeset
|
357 return "MiniFieldStorage(%r, %r)" % (self.name, self.value) |
|
3437
d70149202c93
Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents:
3435
diff
changeset
|
358 |
|
d70149202c93
Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents:
3435
diff
changeset
|
359 |
|
d70149202c93
Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents:
3435
diff
changeset
|
360 class FieldStorage: |
|
d70149202c93
Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents:
3435
diff
changeset
|
361 |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
362 """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
|
363 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
364 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
|
365 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
|
366 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
|
367 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
|
368 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
|
369 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
|
370 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
371 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
|
372 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
373 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
|
374 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
|
375 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
|
376 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
377 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
|
378 transparently reads the file every time you request the value |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
379 and returns *bytes* |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
380 |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
381 file: the file(-like) object from which you can read the data *as |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
382 bytes* ; 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
|
383 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
384 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
|
385 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
386 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
|
387 line |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
388 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
389 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
|
390 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
391 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
|
392 |
|
47632
18eaa1d5f394
Patch by Humberto Diogenes for issue 2849, removing rfc822 module from
Barry Warsaw <barry@python.org>
parents:
46915
diff
changeset
|
393 headers: a dictionary(-like) object (sometimes email.message.Message or a |
|
35598
40ddf366f5a1
Reverted revision 1.83, which introduced a bug and subtle incompatibility
Armin Rigo <arigo@tunes.org>
parents:
35299
diff
changeset
|
394 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
|
395 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
396 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
|
397 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
|
398 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
|
399 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
|
400 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
|
401 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
402 """ |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
403 def __init__(self, fp=None, headers=None, outerboundary=b'', |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
404 environ=os.environ, keep_blank_values=0, strict_parsing=0, |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
405 limit=None, encoding='utf-8', errors='replace'): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
406 """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
|
407 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
408 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
|
409 |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
410 fp : file pointer; default: sys.stdin.buffer |
|
8284
f834e197bd14
Make Tim O'Malley's requested change: in FieldStorage.__init__(), when
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
411 (not used when the request method is GET) |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
412 Can be : |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
413 1. a TextIOWrapper object |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
414 2. an object whose read() and readline() methods return bytes |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
415 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
416 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
|
417 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
|
418 |
|
3794
d1c47f4f1541
Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents:
3630
diff
changeset
|
419 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
|
420 (for internal use only) |
|
3437
d70149202c93
Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents:
3435
diff
changeset
|
421 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
422 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
|
423 |
|
d1c47f4f1541
Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents:
3630
diff
changeset
|
424 keep_blank_values: flag indicating whether blank values in |
|
63886
50f8f5247c81
Merged revisions 83901 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
53909
diff
changeset
|
425 percent-encoded forms should be treated as blank strings. |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15669
diff
changeset
|
426 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
|
427 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
|
428 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
|
429 not included. |
|
3794
d1c47f4f1541
Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents:
3630
diff
changeset
|
430 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
431 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
|
432 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
|
433 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
|
434 |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
435 limit : used internally to read parts of multipart/form-data forms, |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
436 to exit from the reading loop when reached. It is the difference |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
437 between the form content-length and the number of bytes already |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
438 read |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
439 |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
440 encoding, errors : the encoding and error handler used to decode the |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
441 binary stream to strings. Must be the same as the charset defined |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
442 for the page sending the form (content-type : meta http-equiv or |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
443 header) |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
444 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
445 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
446 method = 'GET' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
447 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
|
448 self.strict_parsing = strict_parsing |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
449 if 'REQUEST_METHOD' in environ: |
|
16503
8d7655ac8bf8
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
450 method = environ['REQUEST_METHOD'].upper() |
|
48410
81fd8a8baa9a
Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48334
diff
changeset
|
451 self.qs_on_post = None |
|
8548
fac33716a218
Treat "HEAD" same as "GET", so that CGI scripts won't fail.
Guido van Rossum <guido@python.org>
parents:
8485
diff
changeset
|
452 if method == 'GET' or method == 'HEAD': |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
453 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
|
454 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
|
455 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
|
456 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
|
457 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
458 qs = "" |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
459 qs = qs.encode(locale.getpreferredencoding(), 'surrogateescape') |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
460 fp = BytesIO(qs) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
461 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
|
462 headers = {'content-type': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
463 "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
|
464 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
|
465 headers = {} |
|
7f94b4958056
Be more careful than the previous patch. The default content-type
Guido van Rossum <guido@python.org>
parents:
8477
diff
changeset
|
466 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
|
467 # 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
|
468 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
|
469 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
|
470 headers['content-type'] = environ['CONTENT_TYPE'] |
|
48410
81fd8a8baa9a
Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48334
diff
changeset
|
471 if 'QUERY_STRING' in environ: |
|
81fd8a8baa9a
Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48334
diff
changeset
|
472 self.qs_on_post = environ['QUERY_STRING'] |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
473 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
|
474 headers['content-length'] = environ['CONTENT_LENGTH'] |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
475 if fp is None: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
476 self.fp = sys.stdin.buffer |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
477 # self.fp.read() must return bytes |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
478 elif isinstance(fp, TextIOWrapper): |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
479 self.fp = fp.buffer |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
480 else: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
481 self.fp = fp |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
482 |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
483 self.encoding = encoding |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
484 self.errors = errors |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
485 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
486 self.headers = headers |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
487 if not isinstance(outerboundary, bytes): |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
488 raise TypeError('outerboundary must be bytes, not %s' |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
489 % type(outerboundary).__name__) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
490 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
|
491 |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
492 self.bytes_read = 0 |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
493 self.limit = limit |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
494 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
495 # 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
|
496 cdisp, pdict = "", {} |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
497 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
|
498 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
|
499 self.disposition = cdisp |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
500 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
|
501 self.name = None |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
502 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
|
503 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
|
504 self.filename = None |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
505 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
|
506 self.filename = pdict['filename'] |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
507 self._binary_file = self.filename is not None |
|
3437
d70149202c93
Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents:
3435
diff
changeset
|
508 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
509 # 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
|
510 # |
|
28d3a9fcb577
FieldStorage.__init__(): if there is no content-type header, use
Barry Warsaw <barry@python.org>
parents:
9615
diff
changeset
|
511 # 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
|
512 # 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
|
513 # 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
|
514 # 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
|
515 # 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
|
516 # 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
|
517 # header. |
|
28d3a9fcb577
FieldStorage.__init__(): if there is no content-type header, use
Barry Warsaw <barry@python.org>
parents:
9615
diff
changeset
|
518 # |
|
28d3a9fcb577
FieldStorage.__init__(): if there is no content-type header, use
Barry Warsaw <barry@python.org>
parents:
9615
diff
changeset
|
519 # 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
|
520 # 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
|
521 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
|
522 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
|
523 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
|
524 ctype, pdict = "text/plain", {} |
|
28d3a9fcb577
FieldStorage.__init__(): if there is no content-type header, use
Barry Warsaw <barry@python.org>
parents:
9615
diff
changeset
|
525 else: |
|
28d3a9fcb577
FieldStorage.__init__(): if there is no content-type header, use
Barry Warsaw <barry@python.org>
parents:
9615
diff
changeset
|
526 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
|
527 self.type = ctype |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
528 self.type_options = pdict |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
529 if 'boundary' in pdict: |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
530 self.innerboundary = pdict['boundary'].encode(self.encoding) |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
531 else: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
532 self.innerboundary = b"" |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
533 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
534 clen = -1 |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
535 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
|
536 try: |
|
16503
8d7655ac8bf8
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
537 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
|
538 except ValueError: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
539 pass |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
540 if maxlen and clen > maxlen: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42932
diff
changeset
|
541 raise ValueError('Maximum content length exceeded') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
542 self.length = clen |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
543 if self.limit is None and clen: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
544 self.limit = clen |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
545 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
546 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
|
547 self.done = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
548 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
|
549 self.read_urlencoded() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
550 elif ctype[:10] == 'multipart/': |
|
9410
620e02250c80
Patch by Jim Fulton, who writes:
Guido van Rossum <guido@python.org>
parents:
8548
diff
changeset
|
551 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
|
552 else: |
|
9821
28d3a9fcb577
FieldStorage.__init__(): if there is no content-type header, use
Barry Warsaw <barry@python.org>
parents:
9615
diff
changeset
|
553 self.read_single() |
|
3437
d70149202c93
Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents:
3435
diff
changeset
|
554 |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
555 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
|
556 """Return a printable representation.""" |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28089
diff
changeset
|
557 return "FieldStorage(%r, %r, %r)" % ( |
|
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28089
diff
changeset
|
558 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
|
559 |
|
25214
5330c0620810
Patch suggested by Hamish Lawson: add an __iter__() that returns
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
560 def __iter__(self): |
|
5330c0620810
Patch suggested by Hamish Lawson: add an __iter__() that returns
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
561 return iter(self.keys()) |
|
5330c0620810
Patch suggested by Hamish Lawson: add an __iter__() that returns
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
562 |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
563 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
|
564 if name != 'value': |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42932
diff
changeset
|
565 raise AttributeError(name) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
566 if self.file: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
567 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
|
568 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
|
569 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
|
570 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
|
571 value = self.list |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
572 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
573 value = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
574 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
|
575 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
576 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
|
577 """Dictionary style indexing.""" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
578 if self.list is None: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42932
diff
changeset
|
579 raise TypeError("not indexable") |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
580 found = [] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
581 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
|
582 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
|
583 if not found: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42932
diff
changeset
|
584 raise KeyError(key) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
585 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
|
586 return found[0] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
587 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
588 return found |
|
3437
d70149202c93
Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents:
3435
diff
changeset
|
589 |
|
14196
41234ae95d2a
Closing patch #101120 -- After everyone agreed.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
13840
diff
changeset
|
590 def getvalue(self, key, default=None): |
|
41234ae95d2a
Closing patch #101120 -- After everyone agreed.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
13840
diff
changeset
|
591 """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
|
592 if key in self: |
|
14196
41234ae95d2a
Closing patch #101120 -- After everyone agreed.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
13840
diff
changeset
|
593 value = self[key] |
|
67591
96ffe4a311dd
cgi: use isinstance(x, list) instead of type(x) == type([])
Victor Stinner <victor.stinner@haypocalc.com>
parents:
67590
diff
changeset
|
594 if isinstance(value, list): |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41760
diff
changeset
|
595 return [x.value for x in value] |
|
14196
41234ae95d2a
Closing patch #101120 -- After everyone agreed.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
13840
diff
changeset
|
596 else: |
|
41234ae95d2a
Closing patch #101120 -- After everyone agreed.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
13840
diff
changeset
|
597 return value.value |
|
41234ae95d2a
Closing patch #101120 -- After everyone agreed.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
13840
diff
changeset
|
598 else: |
|
41234ae95d2a
Closing patch #101120 -- After everyone agreed.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
13840
diff
changeset
|
599 return default |
|
41234ae95d2a
Closing patch #101120 -- After everyone agreed.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
13840
diff
changeset
|
600 |
|
19744
f1430e3382bb
Class FieldStorage: add two new methods, getfirst() and getlist(),
Guido van Rossum <guido@python.org>
parents:
19119
diff
changeset
|
601 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
|
602 """ Return the first value received.""" |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
603 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
|
604 value = self[key] |
|
67591
96ffe4a311dd
cgi: use isinstance(x, list) instead of type(x) == type([])
Victor Stinner <victor.stinner@haypocalc.com>
parents:
67590
diff
changeset
|
605 if isinstance(value, list): |
|
19744
f1430e3382bb
Class FieldStorage: add two new methods, getfirst() and getlist(),
Guido van Rossum <guido@python.org>
parents:
19119
diff
changeset
|
606 return value[0].value |
|
f1430e3382bb
Class FieldStorage: add two new methods, getfirst() and getlist(),
Guido van Rossum <guido@python.org>
parents:
19119
diff
changeset
|
607 else: |
|
f1430e3382bb
Class FieldStorage: add two new methods, getfirst() and getlist(),
Guido van Rossum <guido@python.org>
parents:
19119
diff
changeset
|
608 return value.value |
|
f1430e3382bb
Class FieldStorage: add two new methods, getfirst() and getlist(),
Guido van Rossum <guido@python.org>
parents:
19119
diff
changeset
|
609 else: |
|
f1430e3382bb
Class FieldStorage: add two new methods, getfirst() and getlist(),
Guido van Rossum <guido@python.org>
parents:
19119
diff
changeset
|
610 return default |
|
f1430e3382bb
Class FieldStorage: add two new methods, getfirst() and getlist(),
Guido van Rossum <guido@python.org>
parents:
19119
diff
changeset
|
611 |
|
f1430e3382bb
Class FieldStorage: add two new methods, getfirst() and getlist(),
Guido van Rossum <guido@python.org>
parents:
19119
diff
changeset
|
612 def getlist(self, key): |
|
f1430e3382bb
Class FieldStorage: add two new methods, getfirst() and getlist(),
Guido van Rossum <guido@python.org>
parents:
19119
diff
changeset
|
613 """ Return list of received values.""" |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
614 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
|
615 value = self[key] |
|
67591
96ffe4a311dd
cgi: use isinstance(x, list) instead of type(x) == type([])
Victor Stinner <victor.stinner@haypocalc.com>
parents:
67590
diff
changeset
|
616 if isinstance(value, list): |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41760
diff
changeset
|
617 return [x.value for x in value] |
|
19744
f1430e3382bb
Class FieldStorage: add two new methods, getfirst() and getlist(),
Guido van Rossum <guido@python.org>
parents:
19119
diff
changeset
|
618 else: |
|
f1430e3382bb
Class FieldStorage: add two new methods, getfirst() and getlist(),
Guido van Rossum <guido@python.org>
parents:
19119
diff
changeset
|
619 return [value.value] |
|
f1430e3382bb
Class FieldStorage: add two new methods, getfirst() and getlist(),
Guido van Rossum <guido@python.org>
parents:
19119
diff
changeset
|
620 else: |
|
f1430e3382bb
Class FieldStorage: add two new methods, getfirst() and getlist(),
Guido van Rossum <guido@python.org>
parents:
19119
diff
changeset
|
621 return [] |
|
f1430e3382bb
Class FieldStorage: add two new methods, getfirst() and getlist(),
Guido van Rossum <guido@python.org>
parents:
19119
diff
changeset
|
622 |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
623 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
|
624 """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
|
625 if self.list is None: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42932
diff
changeset
|
626 raise TypeError("not indexable") |
|
43418
0c8fec76111c
Merged revisions 58211-58220 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
627 return list(set(item.name for item in self.list)) |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
628 |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
629 def __contains__(self, key): |
|
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
630 """Dictionary style __contains__ method.""" |
|
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
631 if self.list is None: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42932
diff
changeset
|
632 raise TypeError("not indexable") |
|
43418
0c8fec76111c
Merged revisions 58211-58220 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
633 return any(item.name == key for item in self.list) |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23609
diff
changeset
|
634 |
| 4931 | 635 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
|
636 """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
|
637 return len(self.keys()) |
| 4931 | 638 |
|
43418
0c8fec76111c
Merged revisions 58211-58220 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
639 def __nonzero__(self): |
|
0c8fec76111c
Merged revisions 58211-58220 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
640 return bool(self.list) |
|
0c8fec76111c
Merged revisions 58211-58220 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
641 |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
642 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
|
643 """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
|
644 qs = self.fp.read(self.length) |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
645 if not isinstance(qs, bytes): |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
646 raise ValueError("%s should return bytes, got %s" \ |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
647 % (self.fp, type(qs).__name__)) |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
648 qs = qs.decode(self.encoding, self.errors) |
|
48410
81fd8a8baa9a
Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48334
diff
changeset
|
649 if self.qs_on_post: |
|
81fd8a8baa9a
Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48334
diff
changeset
|
650 qs += '&' + self.qs_on_post |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
651 self.list = [] |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
652 query = urllib.parse.parse_qsl( |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
653 qs, self.keep_blank_values, self.strict_parsing, |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
654 encoding=self.encoding, errors=self.errors) |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
655 for key, value in query: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
656 self.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
|
657 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
|
658 |
|
9615
c03787ec9811
In read_multi, allow a subclass to override the class we instantiate
Guido van Rossum <guido@python.org>
parents:
9410
diff
changeset
|
659 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
|
660 |
|
9410
620e02250c80
Patch by Jim Fulton, who writes:
Guido van Rossum <guido@python.org>
parents:
8548
diff
changeset
|
661 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
|
662 """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
|
663 ib = self.innerboundary |
|
3cc66750687d
Fix a denial-of-service attack, SF bug #443120.
Guido van Rossum <guido@python.org>
parents:
18337
diff
changeset
|
664 if not valid_boundary(ib): |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42932
diff
changeset
|
665 raise ValueError('Invalid boundary in multipart form: %r' % (ib,)) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
666 self.list = [] |
|
48410
81fd8a8baa9a
Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48334
diff
changeset
|
667 if self.qs_on_post: |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
668 query = urllib.parse.parse_qsl( |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
669 self.qs_on_post, self.keep_blank_values, self.strict_parsing, |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
670 encoding=self.encoding, errors=self.errors) |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
671 for key, value in query: |
|
48410
81fd8a8baa9a
Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48334
diff
changeset
|
672 self.list.append(MiniFieldStorage(key, value)) |
|
81fd8a8baa9a
Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48334
diff
changeset
|
673 FieldStorageClass = None |
|
81fd8a8baa9a
Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48334
diff
changeset
|
674 |
|
9615
c03787ec9811
In read_multi, allow a subclass to override the class we instantiate
Guido van Rossum <guido@python.org>
parents:
9410
diff
changeset
|
675 klass = self.FieldStorageClass or self.__class__ |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
676 first_line = self.fp.readline() # bytes |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
677 if not isinstance(first_line, bytes): |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
678 raise ValueError("%s should return bytes, got %s" \ |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
679 % (self.fp, type(first_line).__name__)) |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
680 self.bytes_read += len(first_line) |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
681 # first line holds boundary ; ignore it, or check that |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
682 # b"--" + ib == first_line.strip() ? |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
683 while True: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
684 parser = FeedParser() |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
685 hdr_text = b"" |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
686 while True: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
687 data = self.fp.readline() |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
688 hdr_text += data |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
689 if not data.strip(): |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
690 break |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
691 if not hdr_text: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
692 break |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
693 # parser takes strings, not bytes |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
694 self.bytes_read += len(hdr_text) |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
695 parser.feed(hdr_text.decode(self.encoding, self.errors)) |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
696 headers = parser.close() |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
697 part = klass(self.fp, headers, ib, environ, keep_blank_values, |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
698 strict_parsing,self.limit-self.bytes_read, |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
699 self.encoding, self.errors) |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
700 self.bytes_read += part.bytes_read |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
701 self.list.append(part) |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
702 if self.bytes_read >= self.length: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
703 break |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
704 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
|
705 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
706 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
|
707 """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
|
708 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
|
709 self.read_binary() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
710 self.skip_lines() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
711 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
712 self.read_lines() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
713 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
|
714 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
715 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
|
716 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
717 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
|
718 """Internal: read binary data.""" |
|
42930
cbd50ece3b61
Make test_cgi pass. I changed the internal file created by make_file()
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
719 self.file = self.make_file() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
720 todo = self.length |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
721 if todo >= 0: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
722 while todo > 0: |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
723 data = self.fp.read(min(todo, self.bufsize)) # bytes |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
724 if not isinstance(data, bytes): |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
725 raise ValueError("%s should return bytes, got %s" |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
726 % (self.fp, type(data).__name__)) |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
727 self.bytes_read += len(data) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
728 if not data: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
729 self.done = -1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
730 break |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
731 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
|
732 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
|
733 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
734 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
|
735 """Internal: read lines until EOF or outerboundary.""" |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
736 if self._binary_file: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
737 self.file = self.__file = BytesIO() # store data as bytes for files |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
738 else: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
739 self.file = self.__file = StringIO() # as strings for other fields |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
740 if self.outerboundary: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
741 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
|
742 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
743 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
|
744 |
|
18337
c2a60de91d2c
Solve SF bug #231249: cgi.py opens too many (temporary) files.
Guido van Rossum <guido@python.org>
parents:
17074
diff
changeset
|
745 def __write(self, line): |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
746 """line is always bytes, not string""" |
|
18337
c2a60de91d2c
Solve SF bug #231249: cgi.py opens too many (temporary) files.
Guido van Rossum <guido@python.org>
parents:
17074
diff
changeset
|
747 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
|
748 if self.__file.tell() + len(line) > 1000: |
|
42930
cbd50ece3b61
Make test_cgi pass. I changed the internal file created by make_file()
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
749 self.file = self.make_file() |
|
cbd50ece3b61
Make test_cgi pass. I changed the internal file created by make_file()
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
750 data = self.__file.getvalue() |
|
cbd50ece3b61
Make test_cgi pass. I changed the internal file created by make_file()
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
751 self.file.write(data) |
|
18337
c2a60de91d2c
Solve SF bug #231249: cgi.py opens too many (temporary) files.
Guido van Rossum <guido@python.org>
parents:
17074
diff
changeset
|
752 self.__file = None |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
753 if self._binary_file: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
754 # keep bytes |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
755 self.file.write(line) |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
756 else: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
757 # decode to string |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
758 self.file.write(line.decode(self.encoding, self.errors)) |
|
18337
c2a60de91d2c
Solve SF bug #231249: cgi.py opens too many (temporary) files.
Guido van Rossum <guido@python.org>
parents:
17074
diff
changeset
|
759 |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
760 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
|
761 """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
|
762 while 1: |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
763 line = self.fp.readline(1<<16) # bytes |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
764 self.bytes_read += len(line) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
765 if not line: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
766 self.done = -1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
767 break |
|
18337
c2a60de91d2c
Solve SF bug #231249: cgi.py opens too many (temporary) files.
Guido van Rossum <guido@python.org>
parents:
17074
diff
changeset
|
768 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
|
769 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
770 def read_lines_to_outerboundary(self): |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
771 """Internal: read lines until outerboundary. |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
772 Data is read as bytes: boundaries and line ends must be converted |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
773 to bytes for comparisons. |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
774 """ |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
775 next_boundary = b"--" + self.outerboundary |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
776 last_boundary = next_boundary + b"--" |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
777 delim = b"" |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39575
diff
changeset
|
778 last_line_lfend = True |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
779 _read = 0 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
780 while 1: |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
781 if _read >= self.limit: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
782 break |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
783 line = self.fp.readline(1<<16) # bytes |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
784 self.bytes_read += len(line) |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
785 _read += len(line) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
786 if not line: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
787 self.done = -1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
788 break |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
789 if line.startswith(b"--") and last_line_lfend: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
790 strippedline = line.rstrip() |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
791 if strippedline == next_boundary: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
792 break |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
793 if strippedline == last_boundary: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
794 self.done = 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
795 break |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
796 odelim = delim |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
797 if line.endswith(b"\r\n"): |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
798 delim = b"\r\n" |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
799 line = line[:-2] |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39575
diff
changeset
|
800 last_line_lfend = True |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
801 elif line.endswith(b"\n"): |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
802 delim = b"\n" |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
803 line = line[:-1] |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39575
diff
changeset
|
804 last_line_lfend = True |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
805 else: |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
806 delim = b"" |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39575
diff
changeset
|
807 last_line_lfend = False |
|
18337
c2a60de91d2c
Solve SF bug #231249: cgi.py opens too many (temporary) files.
Guido van Rossum <guido@python.org>
parents:
17074
diff
changeset
|
808 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
|
809 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
810 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
|
811 """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
|
812 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
|
813 return |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
814 next_boundary = b"--" + self.outerboundary |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
815 last_boundary = next_boundary + b"--" |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39575
diff
changeset
|
816 last_line_lfend = True |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
817 while True: |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39575
diff
changeset
|
818 line = self.fp.readline(1<<16) |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
819 self.bytes_read += len(line) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
820 if not line: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
821 self.done = -1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
822 break |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
823 if line.endswith(b"--") and last_line_lfend: |
|
16503
8d7655ac8bf8
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
824 strippedline = line.strip() |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
825 if strippedline == next_boundary: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
826 break |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
827 if strippedline == last_boundary: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
828 self.done = 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
829 break |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
830 last_line_lfend = line.endswith(b'\n') |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
831 |
|
42930
cbd50ece3b61
Make test_cgi pass. I changed the internal file created by make_file()
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
832 def make_file(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
833 """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
|
834 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
835 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
|
836 - 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
|
837 - seek(0) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
838 - 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
|
839 |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
840 The file is opened in binary mode for files, in text mode |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
841 for other fields |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
842 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
843 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
|
844 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
|
845 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
|
846 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
|
847 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
|
848 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
849 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
|
850 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
|
851 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
|
852 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
|
853 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
|
854 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
855 """ |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
856 if self._binary_file: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
857 return tempfile.TemporaryFile("wb+") |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
858 else: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
859 return tempfile.TemporaryFile("w+", |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
860 encoding=self.encoding, newline = '\n') |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15669
diff
changeset
|
861 |
|
3437
d70149202c93
Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents:
3435
diff
changeset
|
862 |
|
3432
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
863 # Test/debug code |
|
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
864 # =============== |
| 2135 | 865 |
|
3794
d1c47f4f1541
Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents:
3630
diff
changeset
|
866 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
|
867 """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
|
868 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
869 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
|
870 the script in HTML form. |
| 2135 | 871 |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
872 """ |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
873 print("Content-type: text/html") |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
874 print() |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
875 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
|
876 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
877 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
|
878 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
|
879 print_arguments() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
880 print_form(form) |
|
3794
d1c47f4f1541
Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents:
3630
diff
changeset
|
881 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
|
882 print_environ_usage() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
883 def f(): |
|
39782
c94f37054f19
Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents:
39610
diff
changeset
|
884 exec("testing print_exception() -- <I>italics?</I>") |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
885 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
|
886 f() |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
887 print("<H3>What follows is a test, not an actual exception:</H3>") |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
888 g() |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
889 except: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
890 print_exception() |
|
4072
f66562536f11
Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents:
3958
diff
changeset
|
891 |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
892 print("<H1>Second try with a small maxlen...</H1>") |
|
14645
2d30c35feebe
Improve the test output a bit.
Guido van Rossum <guido@python.org>
parents:
14606
diff
changeset
|
893 |
|
5499
b905372edd4f
Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents:
5302
diff
changeset
|
894 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
|
895 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
|
896 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
897 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
|
898 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
|
899 print_arguments() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
900 print_form(form) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
901 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
|
902 except: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
903 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
|
904 |
|
4072
f66562536f11
Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents:
3958
diff
changeset
|
905 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
|
906 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
|
907 type, value, tb = sys.exc_info() |
|
4072
f66562536f11
Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents:
3958
diff
changeset
|
908 import traceback |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
909 print() |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
910 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
|
911 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
|
912 traceback.format_exception_only(type, value) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
913 print("<PRE>%s<B>%s</B></PRE>" % ( |
|
65362
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
63883
diff
changeset
|
914 html.escape("".join(list[:-1])), |
|
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
63883
diff
changeset
|
915 html.escape(list[-1]), |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
916 )) |
|
6347
2656d1eea792
Use sys.exc_info() where needed.
Guido van Rossum <guido@python.org>
parents:
5988
diff
changeset
|
917 del tb |
| 2135 | 918 |
|
3794
d1c47f4f1541
Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents:
3630
diff
changeset
|
919 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
|
920 """Dump the shell environment as HTML.""" |
|
42930
cbd50ece3b61
Make test_cgi pass. I changed the internal file created by make_file()
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
921 keys = sorted(environ.keys()) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
922 print() |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
923 print("<H3>Shell Environment:</H3>") |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
924 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
|
925 for key in keys: |
|
65362
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
63883
diff
changeset
|
926 print("<DT>", html.escape(key), "<DD>", html.escape(environ[key])) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
927 print("</DL>") |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
928 print() |
|
3432
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
929 |
|
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
930 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
|
931 """Dump the contents of a form as HTML.""" |
|
42930
cbd50ece3b61
Make test_cgi pass. I changed the internal file created by make_file()
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
932 keys = sorted(form.keys()) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
933 print() |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
934 print("<H3>Form Contents:</H3>") |
|
14645
2d30c35feebe
Improve the test output a bit.
Guido van Rossum <guido@python.org>
parents:
14606
diff
changeset
|
935 if not keys: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
936 print("<P>No form fields.") |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
937 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
|
938 for key in keys: |
|
65362
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
63883
diff
changeset
|
939 print("<DT>" + html.escape(key) + ":", end=' ') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
940 value = form[key] |
|
65362
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
63883
diff
changeset
|
941 print("<i>" + html.escape(repr(type(value))) + "</i>") |
|
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
63883
diff
changeset
|
942 print("<DD>" + html.escape(repr(value))) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
943 print("</DL>") |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
944 print() |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
945 |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
946 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
|
947 """Dump the current directory as HTML.""" |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
948 print() |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
949 print("<H3>Current Working Directory:</H3>") |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
950 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
951 pwd = os.getcwd() |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
952 except os.error as msg: |
|
65362
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
63883
diff
changeset
|
953 print("os.error:", html.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
|
954 else: |
|
65362
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
63883
diff
changeset
|
955 print(html.escape(pwd)) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
956 print() |
| 2135 | 957 |
|
3451
5abbc9b5fdf0
Added print_arguments() function.
Guido van Rossum <guido@python.org>
parents:
3447
diff
changeset
|
958 def print_arguments(): |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
959 print() |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
960 print("<H3>Command Line Arguments:</H3>") |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
961 print() |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
962 print(sys.argv) |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
963 print() |
|
3451
5abbc9b5fdf0
Added print_arguments() function.
Guido van Rossum <guido@python.org>
parents:
3447
diff
changeset
|
964 |
| 2135 | 965 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
|
966 """Dump a list of environment variables used by CGI as HTML.""" |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
967 print(""" |
|
3432
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
968 <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
|
969 <UL> |
| 2135 | 970 <LI>AUTH_TYPE |
| 971 <LI>CONTENT_LENGTH | |
| 972 <LI>CONTENT_TYPE | |
| 973 <LI>DATE_GMT | |
| 974 <LI>DATE_LOCAL | |
| 975 <LI>DOCUMENT_NAME | |
| 976 <LI>DOCUMENT_ROOT | |
| 977 <LI>DOCUMENT_URI | |
| 978 <LI>GATEWAY_INTERFACE | |
| 979 <LI>LAST_MODIFIED | |
| 980 <LI>PATH | |
| 981 <LI>PATH_INFO | |
| 982 <LI>PATH_TRANSLATED | |
| 983 <LI>QUERY_STRING | |
| 984 <LI>REMOTE_ADDR | |
| 985 <LI>REMOTE_HOST | |
| 986 <LI>REMOTE_IDENT | |
| 987 <LI>REMOTE_USER | |
| 988 <LI>REQUEST_METHOD | |
| 989 <LI>SCRIPT_NAME | |
| 990 <LI>SERVER_NAME | |
| 991 <LI>SERVER_PORT | |
| 992 <LI>SERVER_PROTOCOL | |
| 993 <LI>SERVER_ROOT | |
| 994 <LI>SERVER_SOFTWARE | |
| 995 </UL> | |
|
3444
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
996 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
|
997 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
|
998 <UL> |
|
e76ca34d48f3
Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents:
3437
diff
changeset
|
999 <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
|
1000 <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
|
1001 <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
|
1002 <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
|
1003 <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
|
1004 <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
|
1005 </UL> |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
1006 """) |
| 2135 | 1007 |
|
3432
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
1008 |
|
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
1009 # Utilities |
|
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
1010 # ========= |
| 2135 | 1011 |
|
5870
fe4f1eb14d53
Add optional 'quote' flag argument to escape(); if true, translate '"'
Guido van Rossum <guido@python.org>
parents:
5654
diff
changeset
|
1012 def escape(s, quote=None): |
|
65362
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
63883
diff
changeset
|
1013 """Deprecated API.""" |
|
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
63883
diff
changeset
|
1014 warn("cgi.escape is deprecated, use html.escape instead", |
|
73914
f82ebf9b3a52
Issue #13248: turn 3.2's PendingDeprecationWarning into 3.3's DeprecationWarning (cgi, importlib, nntplib, smtpd).
Florent Xicluna <florent.xicluna@gmail.com>
parents:
72990
diff
changeset
|
1015 DeprecationWarning, stacklevel=2) |
|
16503
8d7655ac8bf8
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
1016 s = s.replace("&", "&") # Must be done first! |
|
8d7655ac8bf8
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
1017 s = s.replace("<", "<") |
|
8d7655ac8bf8
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
1018 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
|
1019 if quote: |
|
16503
8d7655ac8bf8
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
1020 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
|
1021 return s |
| 2135 | 1022 |
|
65362
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
63883
diff
changeset
|
1023 |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
1024 def valid_boundary(s, _vb_pattern=None): |
|
18833
3cc66750687d
Fix a denial-of-service attack, SF bug #443120.
Guido van Rossum <guido@python.org>
parents:
18337
diff
changeset
|
1025 import re |
|
67590
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
1026 if isinstance(s, bytes): |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
1027 _vb_pattern = b"^[ -~]{0,200}[!-~]$" |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
1028 else: |
|
d864328a74e4
Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65362
diff
changeset
|
1029 _vb_pattern = "^[ -~]{0,200}[!-~]$" |
|
18833
3cc66750687d
Fix a denial-of-service attack, SF bug #443120.
Guido van Rossum <guido@python.org>
parents:
18337
diff
changeset
|
1030 return re.match(_vb_pattern, s) |
|
3432
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
1031 |
|
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
1032 # Invoke mainline |
|
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
1033 # =============== |
| 2135 | 1034 |
|
3432
6abc75ec6d29
File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents:
3106
diff
changeset
|
1035 # 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
|
1036 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
|
1037 test() |
