Benjamin Peterson | 8c703a0 | 2010-03-11 22:05:58 +0000 | [diff] [blame] | 1 | #! /usr/local/bin/python |
Guido van Rossum | 1c9daa8 | 1995-09-18 21:52:37 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 467d723 | 2001-02-13 13:13:33 +0000 | [diff] [blame] | 3 | # NOTE: the above "/usr/local/bin/python" is NOT a mistake. It is |
| 4 | # intentionally NOT "/usr/bin/env python". On many systems |
| 5 | # (e.g. Solaris), /usr/local/bin is not in $PATH as passed to CGI |
| 6 | # scripts, and /usr/local/bin is the default directory where Python is |
| 7 | # installed, so /usr/bin/env would be unable to find python. Granted, |
| 8 | # binary installations by Linux vendors often install Python in |
| 9 | # /usr/bin. So let those vendors patch cgi.py to match their choice |
| 10 | # of installation. |
| 11 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 12 | """Support module for CGI (Common Gateway Interface) scripts. |
Guido van Rossum | 1c9daa8 | 1995-09-18 21:52:37 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 14 | This module defines a number of utilities for use by CGI scripts |
| 15 | written in Python. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 16 | """ |
| 17 | |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 18 | # History |
| 19 | # ------- |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 20 | # |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 21 | # Michael McLay started this module. Steve Majewski changed the |
| 22 | # interface to SvFormContentDict and FormContentDict. The multipart |
| 23 | # parsing was inspired by code submitted by Andreas Paepcke. Guido van |
| 24 | # Rossum rewrote, reformatted and documented the module and is currently |
| 25 | # responsible for its maintenance. |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 26 | # |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 52b8c29 | 2001-06-29 13:06:06 +0000 | [diff] [blame] | 28 | __version__ = "2.6" |
Guido van Rossum | 0147db0 | 1996-03-09 03:16:04 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 30 | |
| 31 | # Imports |
| 32 | # ======= |
| 33 | |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 34 | from io import StringIO, BytesIO, TextIOWrapper |
Serhiy Storchaka | 2e576f5 | 2017-04-24 09:05:00 +0300 | [diff] [blame] | 35 | from collections.abc import Mapping |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 36 | import sys |
| 37 | import os |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 38 | import urllib.parse |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 39 | from email.parser import FeedParser |
Senthil Kumaran | b4cbb92 | 2014-01-11 22:20:16 -0800 | [diff] [blame] | 40 | from email.message import Message |
Georg Brandl | 1f7fffb | 2010-10-15 15:57:45 +0000 | [diff] [blame] | 41 | import html |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 42 | import locale |
| 43 | import tempfile |
Inada Naoki | e52ab42 | 2021-04-29 11:36:04 +0900 | [diff] [blame^] | 44 | import warnings |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 45 | |
INADA Naoki | 698865d | 2018-06-19 17:28:50 +0900 | [diff] [blame] | 46 | __all__ = ["MiniFieldStorage", "FieldStorage", "parse", "parse_multipart", |
Martin Panter | 1cd2772 | 2016-06-06 01:53:28 +0000 | [diff] [blame] | 47 | "parse_header", "test", "print_exception", "print_environ", |
Guido van Rossum | a8423a9 | 2001-03-19 13:40:44 +0000 | [diff] [blame] | 48 | "print_form", "print_directory", "print_arguments", |
INADA Naoki | 698865d | 2018-06-19 17:28:50 +0900 | [diff] [blame] | 49 | "print_environ_usage"] |
Guido van Rossum | c204c70 | 1996-09-05 19:07:11 +0000 | [diff] [blame] | 50 | |
| 51 | # Logging support |
| 52 | # =============== |
| 53 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 54 | logfile = "" # Filename to log to, if not empty |
| 55 | logfp = None # File object to log to, if not None |
Guido van Rossum | c204c70 | 1996-09-05 19:07:11 +0000 | [diff] [blame] | 56 | |
| 57 | def initlog(*allargs): |
| 58 | """Write a log message, if there is a log file. |
| 59 | |
| 60 | Even though this function is called initlog(), you should always |
| 61 | use log(); log is a variable that is set either to initlog |
| 62 | (initially), to dolog (once the log file has been opened), or to |
| 63 | nolog (when logging is disabled). |
| 64 | |
| 65 | The first argument is a format string; the remaining arguments (if |
| 66 | any) are arguments to the % operator, so e.g. |
| 67 | log("%s: %s", "a", "b") |
| 68 | will write "a: b" to the log file, followed by a newline. |
| 69 | |
| 70 | If the global logfp is not None, it should be a file object to |
| 71 | which log data is written. |
| 72 | |
| 73 | If the global logfp is None, the global logfile may be a string |
| 74 | giving a filename to open, in append mode. This file should be |
| 75 | world writable!!! If the file can't be opened, logging is |
| 76 | silently disabled (since there is no safe place where we could |
| 77 | send an error message). |
| 78 | |
| 79 | """ |
Victor Stinner | d33344a | 2011-07-14 22:28:36 +0200 | [diff] [blame] | 80 | global log, logfile, logfp |
Inada Naoki | e52ab42 | 2021-04-29 11:36:04 +0900 | [diff] [blame^] | 81 | warnings.warn("cgi.log() is deprecated as of 3.10. Use logging instead", |
| 82 | DeprecationWarning, stacklevel=2) |
Guido van Rossum | c204c70 | 1996-09-05 19:07:11 +0000 | [diff] [blame] | 83 | if logfile and not logfp: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 84 | try: |
Inada Naoki | e52ab42 | 2021-04-29 11:36:04 +0900 | [diff] [blame^] | 85 | logfp = open(logfile, "a", encoding="locale") |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 86 | except OSError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 87 | pass |
Guido van Rossum | c204c70 | 1996-09-05 19:07:11 +0000 | [diff] [blame] | 88 | if not logfp: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 89 | log = nolog |
Guido van Rossum | c204c70 | 1996-09-05 19:07:11 +0000 | [diff] [blame] | 90 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 91 | log = dolog |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 92 | log(*allargs) |
Guido van Rossum | c204c70 | 1996-09-05 19:07:11 +0000 | [diff] [blame] | 93 | |
| 94 | def dolog(fmt, *args): |
| 95 | """Write a log message to the log file. See initlog() for docs.""" |
| 96 | logfp.write(fmt%args + "\n") |
| 97 | |
| 98 | def nolog(*allargs): |
| 99 | """Dummy function, assigned to log when logging is disabled.""" |
| 100 | pass |
| 101 | |
Victor Stinner | d33344a | 2011-07-14 22:28:36 +0200 | [diff] [blame] | 102 | def closelog(): |
| 103 | """Close the log file.""" |
| 104 | global log, logfile, logfp |
| 105 | logfile = '' |
| 106 | if logfp: |
| 107 | logfp.close() |
| 108 | logfp = None |
| 109 | log = initlog |
| 110 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 111 | log = initlog # The current logging function |
Guido van Rossum | c204c70 | 1996-09-05 19:07:11 +0000 | [diff] [blame] | 112 | |
| 113 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 114 | # Parsing functions |
| 115 | # ================= |
| 116 | |
Guido van Rossum | ad16471 | 1997-05-13 19:03:23 +0000 | [diff] [blame] | 117 | # Maximum input we will accept when REQUEST_METHOD is POST |
| 118 | # 0 ==> unlimited input |
| 119 | maxlen = 0 |
| 120 | |
Adam Goldschmidt | fcbe0cb | 2021-02-15 00:41:57 +0200 | [diff] [blame] | 121 | def parse(fp=None, environ=os.environ, keep_blank_values=0, |
| 122 | strict_parsing=0, separator='&'): |
Guido van Rossum | 773ab27 | 1996-07-23 03:46:24 +0000 | [diff] [blame] | 123 | """Parse a query in the environment or from a file (default stdin) |
| 124 | |
| 125 | Arguments, all optional: |
| 126 | |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 127 | fp : file pointer; default: sys.stdin.buffer |
Guido van Rossum | 773ab27 | 1996-07-23 03:46:24 +0000 | [diff] [blame] | 128 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 129 | environ : environment dictionary; default: os.environ |
Guido van Rossum | 773ab27 | 1996-07-23 03:46:24 +0000 | [diff] [blame] | 130 | |
| 131 | keep_blank_values: flag indicating whether blank values in |
Senthil Kumaran | 30e86a4 | 2010-08-09 20:01:35 +0000 | [diff] [blame] | 132 | percent-encoded forms should be treated as blank strings. |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 133 | A true value indicates that blanks should be retained as |
Guido van Rossum | 773ab27 | 1996-07-23 03:46:24 +0000 | [diff] [blame] | 134 | blank strings. The default false value indicates that |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 135 | blank values are to be ignored and treated as if they were |
| 136 | not included. |
Guido van Rossum | e08c04c | 1996-11-11 19:29:11 +0000 | [diff] [blame] | 137 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 138 | strict_parsing: flag indicating what to do with parsing errors. |
| 139 | If false (the default), errors are silently ignored. |
| 140 | If true, errors raise a ValueError exception. |
Adam Goldschmidt | fcbe0cb | 2021-02-15 00:41:57 +0200 | [diff] [blame] | 141 | |
| 142 | separator: str. The symbol to use for separating the query arguments. |
| 143 | Defaults to &. |
Guido van Rossum | 773ab27 | 1996-07-23 03:46:24 +0000 | [diff] [blame] | 144 | """ |
Raymond Hettinger | a144900 | 2002-05-31 23:54:44 +0000 | [diff] [blame] | 145 | if fp is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 146 | fp = sys.stdin |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 147 | |
| 148 | # field keys and values (except for files) are returned as strings |
| 149 | # an encoding is required to decode the bytes read from self.fp |
| 150 | if hasattr(fp,'encoding'): |
| 151 | encoding = fp.encoding |
| 152 | else: |
| 153 | encoding = 'latin-1' |
| 154 | |
| 155 | # fp.read() must return bytes |
| 156 | if isinstance(fp, TextIOWrapper): |
| 157 | fp = fp.buffer |
| 158 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 159 | if not 'REQUEST_METHOD' in environ: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 160 | environ['REQUEST_METHOD'] = 'GET' # For testing stand-alone |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 161 | if environ['REQUEST_METHOD'] == 'POST': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 162 | ctype, pdict = parse_header(environ['CONTENT_TYPE']) |
| 163 | if ctype == 'multipart/form-data': |
Adam Goldschmidt | fcbe0cb | 2021-02-15 00:41:57 +0200 | [diff] [blame] | 164 | return parse_multipart(fp, pdict, separator=separator) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 165 | elif ctype == 'application/x-www-form-urlencoded': |
Eric S. Raymond | 7e9b4f5 | 2001-02-09 09:59:10 +0000 | [diff] [blame] | 166 | clength = int(environ['CONTENT_LENGTH']) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 167 | if maxlen and clength > maxlen: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 168 | raise ValueError('Maximum content length exceeded') |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 169 | qs = fp.read(clength).decode(encoding) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 170 | else: |
| 171 | qs = '' # Unknown content-type |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 172 | if 'QUERY_STRING' in environ: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 173 | if qs: qs = qs + '&' |
| 174 | qs = qs + environ['QUERY_STRING'] |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 175 | elif sys.argv[1:]: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 176 | if qs: qs = qs + '&' |
| 177 | qs = qs + sys.argv[1] |
| 178 | environ['QUERY_STRING'] = qs # XXX Shouldn't, really |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 179 | elif 'QUERY_STRING' in environ: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 180 | qs = environ['QUERY_STRING'] |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 181 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 182 | if sys.argv[1:]: |
| 183 | qs = sys.argv[1] |
| 184 | else: |
| 185 | qs = "" |
| 186 | environ['QUERY_STRING'] = qs # XXX Shouldn't, really |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 187 | return urllib.parse.parse_qs(qs, keep_blank_values, strict_parsing, |
Adam Goldschmidt | fcbe0cb | 2021-02-15 00:41:57 +0200 | [diff] [blame] | 188 | encoding=encoding, separator=separator) |
Guido van Rossum | e780877 | 1995-08-07 20:12:09 +0000 | [diff] [blame] | 189 | |
| 190 | |
Adam Goldschmidt | fcbe0cb | 2021-02-15 00:41:57 +0200 | [diff] [blame] | 191 | def parse_multipart(fp, pdict, encoding="utf-8", errors="replace", separator='&'): |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 192 | """Parse multipart input. |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 194 | Arguments: |
| 195 | fp : input file |
Johannes Gijsbers | c7fc10a | 2005-01-08 13:56:36 +0000 | [diff] [blame] | 196 | pdict: dictionary containing other parameters of content-type header |
Amber Brown | 545c955 | 2018-05-14 18:11:55 -0400 | [diff] [blame] | 197 | encoding, errors: request encoding and error handler, passed to |
| 198 | FieldStorage |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 199 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 200 | Returns a dictionary just like parse_qs(): keys are the field names, each |
Pierre Quentel | cc3fa20 | 2017-05-08 14:08:34 +0200 | [diff] [blame] | 201 | value is a list of values for that field. For non-file fields, the value |
| 202 | is a list of strings. |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 203 | """ |
Mike Lei | c143cc3 | 2020-12-17 01:34:19 +0000 | [diff] [blame] | 204 | # RFC 2046, Section 5.1 : The "multipart" boundary delimiters are always |
Pierre Quentel | cc3fa20 | 2017-05-08 14:08:34 +0200 | [diff] [blame] | 205 | # represented as 7bit US-ASCII. |
| 206 | boundary = pdict['boundary'].decode('ascii') |
| 207 | ctype = "multipart/form-data; boundary={}".format(boundary) |
| 208 | headers = Message() |
| 209 | headers.set_type(ctype) |
roger | d8cf351 | 2020-06-15 16:58:54 +0200 | [diff] [blame] | 210 | try: |
| 211 | headers['Content-Length'] = pdict['CONTENT-LENGTH'] |
| 212 | except KeyError: |
| 213 | pass |
Amber Brown | 545c955 | 2018-05-14 18:11:55 -0400 | [diff] [blame] | 214 | fs = FieldStorage(fp, headers=headers, encoding=encoding, errors=errors, |
Adam Goldschmidt | fcbe0cb | 2021-02-15 00:41:57 +0200 | [diff] [blame] | 215 | environ={'REQUEST_METHOD': 'POST'}, separator=separator) |
Pierre Quentel | cc3fa20 | 2017-05-08 14:08:34 +0200 | [diff] [blame] | 216 | return {k: fs.getlist(k) for k in fs} |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 217 | |
Fred Drake | 9a0a65b | 2008-12-04 19:24:50 +0000 | [diff] [blame] | 218 | def _parseparam(s): |
| 219 | while s[:1] == ';': |
| 220 | s = s[1:] |
| 221 | end = s.find(';') |
Senthil Kumaran | 1ef0c03 | 2011-10-20 01:05:44 +0800 | [diff] [blame] | 222 | while end > 0 and (s.count('"', 0, end) - s.count('\\"', 0, end)) % 2: |
Fred Drake | 9a0a65b | 2008-12-04 19:24:50 +0000 | [diff] [blame] | 223 | end = s.find(';', end + 1) |
| 224 | if end < 0: |
| 225 | end = len(s) |
| 226 | f = s[:end] |
| 227 | yield f.strip() |
| 228 | s = s[end:] |
| 229 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 230 | def parse_header(line): |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 231 | """Parse a Content-type like header. |
| 232 | |
| 233 | Return the main content-type and a dictionary of options. |
| 234 | |
| 235 | """ |
Fred Drake | 9a0a65b | 2008-12-04 19:24:50 +0000 | [diff] [blame] | 236 | parts = _parseparam(';' + line) |
| 237 | key = parts.__next__() |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 238 | pdict = {} |
Fred Drake | 9a0a65b | 2008-12-04 19:24:50 +0000 | [diff] [blame] | 239 | for p in parts: |
Eric S. Raymond | 7e9b4f5 | 2001-02-09 09:59:10 +0000 | [diff] [blame] | 240 | i = p.find('=') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 241 | if i >= 0: |
Eric S. Raymond | 7e9b4f5 | 2001-02-09 09:59:10 +0000 | [diff] [blame] | 242 | name = p[:i].strip().lower() |
| 243 | value = p[i+1:].strip() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 244 | if len(value) >= 2 and value[0] == value[-1] == '"': |
| 245 | value = value[1:-1] |
Johannes Gijsbers | 9e15dd6 | 2004-08-14 15:39:34 +0000 | [diff] [blame] | 246 | value = value.replace('\\\\', '\\').replace('\\"', '"') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 247 | pdict[name] = value |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 248 | return key, pdict |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 249 | |
| 250 | |
Guido van Rossum | 243ddcd | 1996-03-07 06:33:07 +0000 | [diff] [blame] | 251 | # Classes for field storage |
| 252 | # ========================= |
| 253 | |
| 254 | class MiniFieldStorage: |
| 255 | |
Guido van Rossum | 0147db0 | 1996-03-09 03:16:04 +0000 | [diff] [blame] | 256 | """Like FieldStorage, for use when no file uploads are possible.""" |
Guido van Rossum | 243ddcd | 1996-03-07 06:33:07 +0000 | [diff] [blame] | 257 | |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 258 | # Dummy attributes |
| 259 | filename = None |
| 260 | list = None |
| 261 | type = None |
Guido van Rossum | 773ab27 | 1996-07-23 03:46:24 +0000 | [diff] [blame] | 262 | file = None |
Guido van Rossum | 4032c2c | 1996-03-09 04:04:35 +0000 | [diff] [blame] | 263 | type_options = {} |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 264 | disposition = None |
| 265 | disposition_options = {} |
| 266 | headers = {} |
Guido van Rossum | 243ddcd | 1996-03-07 06:33:07 +0000 | [diff] [blame] | 267 | |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 268 | def __init__(self, name, value): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 269 | """Constructor from field name and value.""" |
| 270 | self.name = name |
| 271 | self.value = value |
Guido van Rossum | 773ab27 | 1996-07-23 03:46:24 +0000 | [diff] [blame] | 272 | # self.file = StringIO(value) |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 273 | |
| 274 | def __repr__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 275 | """Return printable representation.""" |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 276 | return "MiniFieldStorage(%r, %r)" % (self.name, self.value) |
Guido van Rossum | 243ddcd | 1996-03-07 06:33:07 +0000 | [diff] [blame] | 277 | |
| 278 | |
| 279 | class FieldStorage: |
| 280 | |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 281 | """Store a sequence of fields, reading multipart/form-data. |
Guido van Rossum | 243ddcd | 1996-03-07 06:33:07 +0000 | [diff] [blame] | 282 | |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 283 | This class provides naming, typing, files stored on disk, and |
| 284 | more. At the top level, it is accessible like a dictionary, whose |
| 285 | keys are the field names. (Note: None can occur as a field name.) |
| 286 | The items are either a Python list (if there's multiple values) or |
| 287 | another FieldStorage or MiniFieldStorage object. If it's a single |
| 288 | object, it has the following attributes: |
Guido van Rossum | 243ddcd | 1996-03-07 06:33:07 +0000 | [diff] [blame] | 289 | |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 290 | name: the field name, if specified; otherwise None |
Guido van Rossum | 243ddcd | 1996-03-07 06:33:07 +0000 | [diff] [blame] | 291 | |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 292 | filename: the filename, if specified; otherwise None; this is the |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 293 | client side filename, *not* the file name on which it is |
| 294 | stored (that's a temporary file you don't deal with) |
Guido van Rossum | 243ddcd | 1996-03-07 06:33:07 +0000 | [diff] [blame] | 295 | |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 296 | value: the value as a *string*; for file uploads, this |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 297 | transparently reads the file every time you request the value |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 298 | and returns *bytes* |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 299 | |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 300 | file: the file(-like) object from which you can read the data *as |
| 301 | bytes* ; None if the data is stored a simple string |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 302 | |
| 303 | type: the content-type, or None if not specified |
| 304 | |
| 305 | type_options: dictionary of options specified on the content-type |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 306 | line |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 307 | |
| 308 | disposition: content-disposition, or None if not specified |
| 309 | |
| 310 | disposition_options: dictionary of corresponding options |
| 311 | |
Barry Warsaw | 596097e | 2008-06-12 02:38:51 +0000 | [diff] [blame] | 312 | headers: a dictionary(-like) object (sometimes email.message.Message or a |
Armin Rigo | 3a703b6 | 2005-09-19 09:11:04 +0000 | [diff] [blame] | 313 | subclass thereof) containing *all* headers |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 314 | |
| 315 | The class is subclassable, mostly for the purpose of overriding |
| 316 | the make_file() method, which is called internally to come up with |
| 317 | a file open for reading and writing. This makes it possible to |
| 318 | override the default choice of storing all files in a temporary |
| 319 | directory and unlinking them as soon as they have been opened. |
| 320 | |
| 321 | """ |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 322 | def __init__(self, fp=None, headers=None, outerboundary=b'', |
| 323 | environ=os.environ, keep_blank_values=0, strict_parsing=0, |
matthewbelisle-wf | 2091448 | 2018-10-19 05:52:59 -0500 | [diff] [blame] | 324 | limit=None, encoding='utf-8', errors='replace', |
Adam Goldschmidt | fcbe0cb | 2021-02-15 00:41:57 +0200 | [diff] [blame] | 325 | max_num_fields=None, separator='&'): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 326 | """Constructor. Read multipart/* until last part. |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 327 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 328 | Arguments, all optional: |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 329 | |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 330 | fp : file pointer; default: sys.stdin.buffer |
Guido van Rossum | b1b4f94 | 1998-05-08 19:55:51 +0000 | [diff] [blame] | 331 | (not used when the request method is GET) |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 332 | Can be : |
| 333 | 1. a TextIOWrapper object |
| 334 | 2. an object whose read() and readline() methods return bytes |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 335 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 336 | headers : header dictionary-like object; default: |
| 337 | taken from environ as per CGI spec |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 338 | |
Guido van Rossum | 773ab27 | 1996-07-23 03:46:24 +0000 | [diff] [blame] | 339 | outerboundary : terminating multipart boundary |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 340 | (for internal use only) |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 341 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 342 | environ : environment dictionary; default: os.environ |
Guido van Rossum | 773ab27 | 1996-07-23 03:46:24 +0000 | [diff] [blame] | 343 | |
| 344 | keep_blank_values: flag indicating whether blank values in |
Senthil Kumaran | 30e86a4 | 2010-08-09 20:01:35 +0000 | [diff] [blame] | 345 | percent-encoded forms should be treated as blank strings. |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 346 | A true value indicates that blanks should be retained as |
Guido van Rossum | 773ab27 | 1996-07-23 03:46:24 +0000 | [diff] [blame] | 347 | blank strings. The default false value indicates that |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 348 | blank values are to be ignored and treated as if they were |
| 349 | not included. |
Guido van Rossum | 773ab27 | 1996-07-23 03:46:24 +0000 | [diff] [blame] | 350 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 351 | strict_parsing: flag indicating what to do with parsing errors. |
| 352 | If false (the default), errors are silently ignored. |
| 353 | If true, errors raise a ValueError exception. |
Guido van Rossum | e08c04c | 1996-11-11 19:29:11 +0000 | [diff] [blame] | 354 | |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 355 | limit : used internally to read parts of multipart/form-data forms, |
| 356 | to exit from the reading loop when reached. It is the difference |
| 357 | between the form content-length and the number of bytes already |
| 358 | read |
| 359 | |
| 360 | encoding, errors : the encoding and error handler used to decode the |
| 361 | binary stream to strings. Must be the same as the charset defined |
| 362 | for the page sending the form (content-type : meta http-equiv or |
| 363 | header) |
| 364 | |
matthewbelisle-wf | 2091448 | 2018-10-19 05:52:59 -0500 | [diff] [blame] | 365 | max_num_fields: int. If set, then __init__ throws a ValueError |
| 366 | if there are more than n fields read by parse_qsl(). |
| 367 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 368 | """ |
| 369 | method = 'GET' |
| 370 | self.keep_blank_values = keep_blank_values |
| 371 | self.strict_parsing = strict_parsing |
matthewbelisle-wf | 2091448 | 2018-10-19 05:52:59 -0500 | [diff] [blame] | 372 | self.max_num_fields = max_num_fields |
Adam Goldschmidt | fcbe0cb | 2021-02-15 00:41:57 +0200 | [diff] [blame] | 373 | self.separator = separator |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 374 | if 'REQUEST_METHOD' in environ: |
Eric S. Raymond | 7e9b4f5 | 2001-02-09 09:59:10 +0000 | [diff] [blame] | 375 | method = environ['REQUEST_METHOD'].upper() |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 376 | self.qs_on_post = None |
Guido van Rossum | 0185283 | 1998-06-25 02:40:17 +0000 | [diff] [blame] | 377 | if method == 'GET' or method == 'HEAD': |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 378 | if 'QUERY_STRING' in environ: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 379 | qs = environ['QUERY_STRING'] |
| 380 | elif sys.argv[1:]: |
| 381 | qs = sys.argv[1] |
| 382 | else: |
| 383 | qs = "" |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 384 | qs = qs.encode(locale.getpreferredencoding(), 'surrogateescape') |
| 385 | fp = BytesIO(qs) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 386 | if headers is None: |
| 387 | headers = {'content-type': |
| 388 | "application/x-www-form-urlencoded"} |
| 389 | if headers is None: |
Guido van Rossum | cff311a | 1998-06-11 14:06:59 +0000 | [diff] [blame] | 390 | headers = {} |
| 391 | if method == 'POST': |
| 392 | # Set default content-type for POST to what's traditional |
| 393 | headers['content-type'] = "application/x-www-form-urlencoded" |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 394 | if 'CONTENT_TYPE' in environ: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 395 | headers['content-type'] = environ['CONTENT_TYPE'] |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 396 | if 'QUERY_STRING' in environ: |
| 397 | self.qs_on_post = environ['QUERY_STRING'] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 398 | if 'CONTENT_LENGTH' in environ: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 399 | headers['content-length'] = environ['CONTENT_LENGTH'] |
Senthil Kumaran | b4cbb92 | 2014-01-11 22:20:16 -0800 | [diff] [blame] | 400 | else: |
| 401 | if not (isinstance(headers, (Mapping, Message))): |
| 402 | raise TypeError("headers must be mapping or an instance of " |
| 403 | "email.message.Message") |
| 404 | self.headers = headers |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 405 | if fp is None: |
| 406 | self.fp = sys.stdin.buffer |
| 407 | # self.fp.read() must return bytes |
| 408 | elif isinstance(fp, TextIOWrapper): |
| 409 | self.fp = fp.buffer |
| 410 | else: |
Senthil Kumaran | b4cbb92 | 2014-01-11 22:20:16 -0800 | [diff] [blame] | 411 | if not (hasattr(fp, 'read') and hasattr(fp, 'readline')): |
| 412 | raise TypeError("fp must be file pointer") |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 413 | self.fp = fp |
| 414 | |
| 415 | self.encoding = encoding |
| 416 | self.errors = errors |
| 417 | |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 418 | if not isinstance(outerboundary, bytes): |
| 419 | raise TypeError('outerboundary must be bytes, not %s' |
| 420 | % type(outerboundary).__name__) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 421 | self.outerboundary = outerboundary |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 422 | |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 423 | self.bytes_read = 0 |
| 424 | self.limit = limit |
| 425 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 426 | # Process content-disposition header |
| 427 | cdisp, pdict = "", {} |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 428 | if 'content-disposition' in self.headers: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 429 | cdisp, pdict = parse_header(self.headers['content-disposition']) |
| 430 | self.disposition = cdisp |
| 431 | self.disposition_options = pdict |
| 432 | self.name = None |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 433 | if 'name' in pdict: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 434 | self.name = pdict['name'] |
| 435 | self.filename = None |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 436 | if 'filename' in pdict: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 437 | self.filename = pdict['filename'] |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 438 | self._binary_file = self.filename is not None |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 439 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 440 | # Process content-type header |
Barry Warsaw | 302331a | 1999-01-08 17:42:03 +0000 | [diff] [blame] | 441 | # |
| 442 | # Honor any existing content-type header. But if there is no |
| 443 | # content-type header, use some sensible defaults. Assume |
| 444 | # outerboundary is "" at the outer level, but something non-false |
| 445 | # inside a multi-part. The default for an inner part is text/plain, |
| 446 | # but for an outer part it should be urlencoded. This should catch |
| 447 | # bogus clients which erroneously forget to include a content-type |
| 448 | # header. |
| 449 | # |
| 450 | # See below for what we do if there does exist a content-type header, |
| 451 | # but it happens to be something we don't understand. |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 452 | if 'content-type' in self.headers: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 453 | ctype, pdict = parse_header(self.headers['content-type']) |
Guido van Rossum | ce900de | 1999-06-02 18:44:22 +0000 | [diff] [blame] | 454 | elif self.outerboundary or method != 'POST': |
Barry Warsaw | 302331a | 1999-01-08 17:42:03 +0000 | [diff] [blame] | 455 | ctype, pdict = "text/plain", {} |
| 456 | else: |
| 457 | ctype, pdict = 'application/x-www-form-urlencoded', {} |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 458 | self.type = ctype |
| 459 | self.type_options = pdict |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 460 | if 'boundary' in pdict: |
Amber Brown | 545c955 | 2018-05-14 18:11:55 -0400 | [diff] [blame] | 461 | self.innerboundary = pdict['boundary'].encode(self.encoding, |
| 462 | self.errors) |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 463 | else: |
| 464 | self.innerboundary = b"" |
| 465 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 466 | clen = -1 |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 467 | if 'content-length' in self.headers: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 468 | try: |
Eric S. Raymond | 7e9b4f5 | 2001-02-09 09:59:10 +0000 | [diff] [blame] | 469 | clen = int(self.headers['content-length']) |
Skip Montanaro | db5d144 | 2002-03-23 05:50:17 +0000 | [diff] [blame] | 470 | except ValueError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 471 | pass |
| 472 | if maxlen and clen > maxlen: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 473 | raise ValueError('Maximum content length exceeded') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 474 | self.length = clen |
Pierre Quentel | 2d7caca | 2019-09-11 13:05:53 +0200 | [diff] [blame] | 475 | if self.limit is None and clen >= 0: |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 476 | self.limit = clen |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 477 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 478 | self.list = self.file = None |
| 479 | self.done = 0 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 480 | if ctype == 'application/x-www-form-urlencoded': |
| 481 | self.read_urlencoded() |
| 482 | elif ctype[:10] == 'multipart/': |
Guido van Rossum | f574500 | 1998-10-20 14:43:02 +0000 | [diff] [blame] | 483 | self.read_multi(environ, keep_blank_values, strict_parsing) |
Barry Warsaw | 302331a | 1999-01-08 17:42:03 +0000 | [diff] [blame] | 484 | else: |
Guido van Rossum | 60a3bd8 | 1999-06-11 18:26:09 +0000 | [diff] [blame] | 485 | self.read_single() |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 486 | |
Brett Cannon | f79126f | 2013-08-23 15:15:48 -0400 | [diff] [blame] | 487 | def __del__(self): |
| 488 | try: |
| 489 | self.file.close() |
| 490 | except AttributeError: |
| 491 | pass |
| 492 | |
Berker Peksag | bf5e960 | 2015-02-06 10:21:37 +0200 | [diff] [blame] | 493 | def __enter__(self): |
| 494 | return self |
| 495 | |
| 496 | def __exit__(self, *args): |
| 497 | self.file.close() |
| 498 | |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 499 | def __repr__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 500 | """Return a printable representation.""" |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 501 | return "FieldStorage(%r, %r, %r)" % ( |
| 502 | self.name, self.filename, self.value) |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 503 | |
Guido van Rossum | 4061cbe | 2002-09-11 18:20:34 +0000 | [diff] [blame] | 504 | def __iter__(self): |
| 505 | return iter(self.keys()) |
| 506 | |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 507 | def __getattr__(self, name): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 508 | if name != 'value': |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 509 | raise AttributeError(name) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 510 | if self.file: |
| 511 | self.file.seek(0) |
| 512 | value = self.file.read() |
| 513 | self.file.seek(0) |
| 514 | elif self.list is not None: |
| 515 | value = self.list |
| 516 | else: |
| 517 | value = None |
| 518 | return value |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 519 | |
| 520 | def __getitem__(self, key): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 521 | """Dictionary style indexing.""" |
| 522 | if self.list is None: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 523 | raise TypeError("not indexable") |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 524 | found = [] |
| 525 | for item in self.list: |
| 526 | if item.name == key: found.append(item) |
| 527 | if not found: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 528 | raise KeyError(key) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 529 | if len(found) == 1: |
| 530 | return found[0] |
| 531 | else: |
| 532 | return found |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 533 | |
Moshe Zadka | a1a4b59 | 2000-08-25 21:47:56 +0000 | [diff] [blame] | 534 | def getvalue(self, key, default=None): |
| 535 | """Dictionary style get() method, including 'value' lookup.""" |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 536 | if key in self: |
Moshe Zadka | a1a4b59 | 2000-08-25 21:47:56 +0000 | [diff] [blame] | 537 | value = self[key] |
Victor Stinner | f1c7ca9 | 2011-01-14 13:08:27 +0000 | [diff] [blame] | 538 | if isinstance(value, list): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 539 | return [x.value for x in value] |
Moshe Zadka | a1a4b59 | 2000-08-25 21:47:56 +0000 | [diff] [blame] | 540 | else: |
| 541 | return value.value |
| 542 | else: |
| 543 | return default |
| 544 | |
Guido van Rossum | 1bfb388 | 2001-09-05 19:45:34 +0000 | [diff] [blame] | 545 | def getfirst(self, key, default=None): |
| 546 | """ Return the first value received.""" |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 547 | if key in self: |
Guido van Rossum | 1bfb388 | 2001-09-05 19:45:34 +0000 | [diff] [blame] | 548 | value = self[key] |
Victor Stinner | f1c7ca9 | 2011-01-14 13:08:27 +0000 | [diff] [blame] | 549 | if isinstance(value, list): |
Guido van Rossum | 1bfb388 | 2001-09-05 19:45:34 +0000 | [diff] [blame] | 550 | return value[0].value |
| 551 | else: |
| 552 | return value.value |
| 553 | else: |
| 554 | return default |
| 555 | |
| 556 | def getlist(self, key): |
| 557 | """ Return list of received values.""" |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 558 | if key in self: |
Guido van Rossum | 1bfb388 | 2001-09-05 19:45:34 +0000 | [diff] [blame] | 559 | value = self[key] |
Victor Stinner | f1c7ca9 | 2011-01-14 13:08:27 +0000 | [diff] [blame] | 560 | if isinstance(value, list): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 561 | return [x.value for x in value] |
Guido van Rossum | 1bfb388 | 2001-09-05 19:45:34 +0000 | [diff] [blame] | 562 | else: |
| 563 | return [value.value] |
| 564 | else: |
| 565 | return [] |
| 566 | |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 567 | def keys(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 568 | """Dictionary style keys() method.""" |
| 569 | if self.list is None: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 570 | raise TypeError("not indexable") |
Thomas Wouters | 8ce81f7 | 2007-09-20 18:22:40 +0000 | [diff] [blame] | 571 | return list(set(item.name for item in self.list)) |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 572 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 573 | def __contains__(self, key): |
| 574 | """Dictionary style __contains__ method.""" |
| 575 | if self.list is None: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 576 | raise TypeError("not indexable") |
Thomas Wouters | 8ce81f7 | 2007-09-20 18:22:40 +0000 | [diff] [blame] | 577 | return any(item.name == key for item in self.list) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 578 | |
Guido van Rossum | 88b85d4 | 1997-01-11 19:21:33 +0000 | [diff] [blame] | 579 | def __len__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 580 | """Dictionary style len(x) support.""" |
| 581 | return len(self.keys()) |
Guido van Rossum | 88b85d4 | 1997-01-11 19:21:33 +0000 | [diff] [blame] | 582 | |
Senthil Kumaran | b4cbb92 | 2014-01-11 22:20:16 -0800 | [diff] [blame] | 583 | def __bool__(self): |
| 584 | if self.list is None: |
| 585 | raise TypeError("Cannot be converted to bool.") |
Thomas Wouters | 8ce81f7 | 2007-09-20 18:22:40 +0000 | [diff] [blame] | 586 | return bool(self.list) |
| 587 | |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 588 | def read_urlencoded(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 589 | """Internal: read data in query string format.""" |
| 590 | qs = self.fp.read(self.length) |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 591 | if not isinstance(qs, bytes): |
| 592 | raise ValueError("%s should return bytes, got %s" \ |
| 593 | % (self.fp, type(qs).__name__)) |
| 594 | qs = qs.decode(self.encoding, self.errors) |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 595 | if self.qs_on_post: |
| 596 | qs += '&' + self.qs_on_post |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 597 | query = urllib.parse.parse_qsl( |
| 598 | qs, self.keep_blank_values, self.strict_parsing, |
matthewbelisle-wf | 2091448 | 2018-10-19 05:52:59 -0500 | [diff] [blame] | 599 | encoding=self.encoding, errors=self.errors, |
Adam Goldschmidt | fcbe0cb | 2021-02-15 00:41:57 +0200 | [diff] [blame] | 600 | max_num_fields=self.max_num_fields, separator=self.separator) |
matthewbelisle-wf | 2091448 | 2018-10-19 05:52:59 -0500 | [diff] [blame] | 601 | self.list = [MiniFieldStorage(key, value) for key, value in query] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 602 | self.skip_lines() |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 603 | |
Guido van Rossum | 030d2ec | 1998-12-09 22:16:46 +0000 | [diff] [blame] | 604 | FieldStorageClass = None |
| 605 | |
Guido van Rossum | f574500 | 1998-10-20 14:43:02 +0000 | [diff] [blame] | 606 | def read_multi(self, environ, keep_blank_values, strict_parsing): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 607 | """Internal: read a part that is itself multipart.""" |
Guido van Rossum | 2e441f7 | 2001-07-25 21:00:19 +0000 | [diff] [blame] | 608 | ib = self.innerboundary |
| 609 | if not valid_boundary(ib): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 610 | raise ValueError('Invalid boundary in multipart form: %r' % (ib,)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 611 | self.list = [] |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 612 | if self.qs_on_post: |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 613 | query = urllib.parse.parse_qsl( |
| 614 | self.qs_on_post, self.keep_blank_values, self.strict_parsing, |
matthewbelisle-wf | 2091448 | 2018-10-19 05:52:59 -0500 | [diff] [blame] | 615 | encoding=self.encoding, errors=self.errors, |
Adam Goldschmidt | fcbe0cb | 2021-02-15 00:41:57 +0200 | [diff] [blame] | 616 | max_num_fields=self.max_num_fields, separator=self.separator) |
matthewbelisle-wf | 2091448 | 2018-10-19 05:52:59 -0500 | [diff] [blame] | 617 | self.list.extend(MiniFieldStorage(key, value) for key, value in query) |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 618 | |
Guido van Rossum | 030d2ec | 1998-12-09 22:16:46 +0000 | [diff] [blame] | 619 | klass = self.FieldStorageClass or self.__class__ |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 620 | first_line = self.fp.readline() # bytes |
| 621 | if not isinstance(first_line, bytes): |
| 622 | raise ValueError("%s should return bytes, got %s" \ |
| 623 | % (self.fp, type(first_line).__name__)) |
| 624 | self.bytes_read += len(first_line) |
Donald Stufft | d90f8d1 | 2015-03-29 16:43:23 -0400 | [diff] [blame] | 625 | |
| 626 | # Ensure that we consume the file until we've hit our inner boundary |
| 627 | while (first_line.strip() != (b"--" + self.innerboundary) and |
| 628 | first_line): |
| 629 | first_line = self.fp.readline() |
| 630 | self.bytes_read += len(first_line) |
| 631 | |
matthewbelisle-wf | b79b5c0 | 2018-10-23 03:14:35 -0500 | [diff] [blame] | 632 | # Propagate max_num_fields into the sub class appropriately |
| 633 | max_num_fields = self.max_num_fields |
| 634 | if max_num_fields is not None: |
| 635 | max_num_fields -= len(self.list) |
| 636 | |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 637 | while True: |
| 638 | parser = FeedParser() |
| 639 | hdr_text = b"" |
| 640 | while True: |
| 641 | data = self.fp.readline() |
| 642 | hdr_text += data |
| 643 | if not data.strip(): |
| 644 | break |
| 645 | if not hdr_text: |
| 646 | break |
| 647 | # parser takes strings, not bytes |
| 648 | self.bytes_read += len(hdr_text) |
| 649 | parser.feed(hdr_text.decode(self.encoding, self.errors)) |
| 650 | headers = parser.close() |
Victor Stinner | 6579459 | 2015-08-18 10:21:10 -0700 | [diff] [blame] | 651 | |
| 652 | # Some clients add Content-Length for part headers, ignore them |
| 653 | if 'content-length' in headers: |
| 654 | del headers['content-length'] |
| 655 | |
Pierre Quentel | 2d7caca | 2019-09-11 13:05:53 +0200 | [diff] [blame] | 656 | limit = None if self.limit is None \ |
| 657 | else self.limit - self.bytes_read |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 658 | part = klass(self.fp, headers, ib, environ, keep_blank_values, |
Pierre Quentel | 2d7caca | 2019-09-11 13:05:53 +0200 | [diff] [blame] | 659 | strict_parsing, limit, |
Adam Goldschmidt | fcbe0cb | 2021-02-15 00:41:57 +0200 | [diff] [blame] | 660 | self.encoding, self.errors, max_num_fields, self.separator) |
matthewbelisle-wf | 2091448 | 2018-10-19 05:52:59 -0500 | [diff] [blame] | 661 | |
matthewbelisle-wf | b79b5c0 | 2018-10-23 03:14:35 -0500 | [diff] [blame] | 662 | if max_num_fields is not None: |
| 663 | max_num_fields -= 1 |
| 664 | if part.list: |
| 665 | max_num_fields -= len(part.list) |
| 666 | if max_num_fields < 0: |
| 667 | raise ValueError('Max number of fields exceeded') |
matthewbelisle-wf | 2091448 | 2018-10-19 05:52:59 -0500 | [diff] [blame] | 668 | |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 669 | self.bytes_read += part.bytes_read |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 670 | self.list.append(part) |
Florent Xicluna | 331c3fd | 2013-07-07 12:44:28 +0200 | [diff] [blame] | 671 | if part.done or self.bytes_read >= self.length > 0: |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 672 | break |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 673 | self.skip_lines() |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 674 | |
| 675 | def read_single(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 676 | """Internal: read an atomic part.""" |
| 677 | if self.length >= 0: |
| 678 | self.read_binary() |
| 679 | self.skip_lines() |
| 680 | else: |
| 681 | self.read_lines() |
| 682 | self.file.seek(0) |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 683 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 684 | bufsize = 8*1024 # I/O buffering size for copy to file |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 685 | |
| 686 | def read_binary(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 687 | """Internal: read binary data.""" |
Guido van Rossum | a1a6852 | 2007-08-28 03:11:34 +0000 | [diff] [blame] | 688 | self.file = self.make_file() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 689 | todo = self.length |
| 690 | if todo >= 0: |
| 691 | while todo > 0: |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 692 | data = self.fp.read(min(todo, self.bufsize)) # bytes |
| 693 | if not isinstance(data, bytes): |
| 694 | raise ValueError("%s should return bytes, got %s" |
| 695 | % (self.fp, type(data).__name__)) |
| 696 | self.bytes_read += len(data) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 697 | if not data: |
| 698 | self.done = -1 |
| 699 | break |
| 700 | self.file.write(data) |
| 701 | todo = todo - len(data) |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 702 | |
| 703 | def read_lines(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 704 | """Internal: read lines until EOF or outerboundary.""" |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 705 | if self._binary_file: |
| 706 | self.file = self.__file = BytesIO() # store data as bytes for files |
| 707 | else: |
| 708 | self.file = self.__file = StringIO() # as strings for other fields |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 709 | if self.outerboundary: |
| 710 | self.read_lines_to_outerboundary() |
| 711 | else: |
| 712 | self.read_lines_to_eof() |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 713 | |
Guido van Rossum | 52b8c29 | 2001-06-29 13:06:06 +0000 | [diff] [blame] | 714 | def __write(self, line): |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 715 | """line is always bytes, not string""" |
Guido van Rossum | 52b8c29 | 2001-06-29 13:06:06 +0000 | [diff] [blame] | 716 | if self.__file is not None: |
| 717 | if self.__file.tell() + len(line) > 1000: |
Guido van Rossum | a1a6852 | 2007-08-28 03:11:34 +0000 | [diff] [blame] | 718 | self.file = self.make_file() |
| 719 | data = self.__file.getvalue() |
| 720 | self.file.write(data) |
Guido van Rossum | 52b8c29 | 2001-06-29 13:06:06 +0000 | [diff] [blame] | 721 | self.__file = None |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 722 | if self._binary_file: |
| 723 | # keep bytes |
| 724 | self.file.write(line) |
| 725 | else: |
| 726 | # decode to string |
| 727 | self.file.write(line.decode(self.encoding, self.errors)) |
Guido van Rossum | 52b8c29 | 2001-06-29 13:06:06 +0000 | [diff] [blame] | 728 | |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 729 | def read_lines_to_eof(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 730 | """Internal: read lines until EOF.""" |
| 731 | while 1: |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 732 | line = self.fp.readline(1<<16) # bytes |
| 733 | self.bytes_read += len(line) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 734 | if not line: |
| 735 | self.done = -1 |
| 736 | break |
Guido van Rossum | 52b8c29 | 2001-06-29 13:06:06 +0000 | [diff] [blame] | 737 | self.__write(line) |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 738 | |
| 739 | def read_lines_to_outerboundary(self): |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 740 | """Internal: read lines until outerboundary. |
| 741 | Data is read as bytes: boundaries and line ends must be converted |
| 742 | to bytes for comparisons. |
| 743 | """ |
| 744 | next_boundary = b"--" + self.outerboundary |
| 745 | last_boundary = next_boundary + b"--" |
| 746 | delim = b"" |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 747 | last_line_lfend = True |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 748 | _read = 0 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 749 | while 1: |
roger | d8cf351 | 2020-06-15 16:58:54 +0200 | [diff] [blame] | 750 | |
| 751 | if self.limit is not None and 0 <= self.limit <= _read: |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 752 | break |
| 753 | line = self.fp.readline(1<<16) # bytes |
| 754 | self.bytes_read += len(line) |
| 755 | _read += len(line) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 756 | if not line: |
| 757 | self.done = -1 |
| 758 | break |
Serhiy Storchaka | c7bfe0e | 2013-06-17 16:34:41 +0300 | [diff] [blame] | 759 | if delim == b"\r": |
| 760 | line = delim + line |
| 761 | delim = b"" |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 762 | if line.startswith(b"--") and last_line_lfend: |
| 763 | strippedline = line.rstrip() |
| 764 | if strippedline == next_boundary: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 765 | break |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 766 | if strippedline == last_boundary: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 767 | self.done = 1 |
| 768 | break |
| 769 | odelim = delim |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 770 | if line.endswith(b"\r\n"): |
| 771 | delim = b"\r\n" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 772 | line = line[:-2] |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 773 | last_line_lfend = True |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 774 | elif line.endswith(b"\n"): |
| 775 | delim = b"\n" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 776 | line = line[:-1] |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 777 | last_line_lfend = True |
Serhiy Storchaka | c7bfe0e | 2013-06-17 16:34:41 +0300 | [diff] [blame] | 778 | elif line.endswith(b"\r"): |
| 779 | # We may interrupt \r\n sequences if they span the 2**16 |
| 780 | # byte boundary |
| 781 | delim = b"\r" |
| 782 | line = line[:-1] |
| 783 | last_line_lfend = False |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 784 | else: |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 785 | delim = b"" |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 786 | last_line_lfend = False |
Guido van Rossum | 52b8c29 | 2001-06-29 13:06:06 +0000 | [diff] [blame] | 787 | self.__write(odelim + line) |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 788 | |
| 789 | def skip_lines(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 790 | """Internal: skip lines until outer boundary if defined.""" |
| 791 | if not self.outerboundary or self.done: |
| 792 | return |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 793 | next_boundary = b"--" + self.outerboundary |
| 794 | last_boundary = next_boundary + b"--" |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 795 | last_line_lfend = True |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 796 | while True: |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 797 | line = self.fp.readline(1<<16) |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 798 | self.bytes_read += len(line) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 799 | if not line: |
| 800 | self.done = -1 |
| 801 | break |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 802 | if line.endswith(b"--") and last_line_lfend: |
Eric S. Raymond | 7e9b4f5 | 2001-02-09 09:59:10 +0000 | [diff] [blame] | 803 | strippedline = line.strip() |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 804 | if strippedline == next_boundary: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 805 | break |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 806 | if strippedline == last_boundary: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 807 | self.done = 1 |
| 808 | break |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 809 | last_line_lfend = line.endswith(b'\n') |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 810 | |
Guido van Rossum | a1a6852 | 2007-08-28 03:11:34 +0000 | [diff] [blame] | 811 | def make_file(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 812 | """Overridable: return a readable & writable file. |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 813 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 814 | The file will be used as follows: |
| 815 | - data is written to it |
| 816 | - seek(0) |
| 817 | - data is read from it |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 818 | |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 819 | The file is opened in binary mode for files, in text mode |
| 820 | for other fields |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 821 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 822 | This version opens a temporary file for reading and writing, |
| 823 | and immediately deletes (unlinks) it. The trick (on Unix!) is |
| 824 | that the file can still be used, but it can't be opened by |
| 825 | another process, and it will automatically be deleted when it |
| 826 | is closed or when the current process terminates. |
Guido van Rossum | 4032c2c | 1996-03-09 04:04:35 +0000 | [diff] [blame] | 827 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 828 | If you want a more permanent file, you derive a class which |
| 829 | overrides this method. If you want a visible temporary file |
| 830 | that is nevertheless automatically deleted when the script |
| 831 | terminates, try defining a __del__ method in a derived class |
| 832 | which unlinks the temporary files you have created. |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 833 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 834 | """ |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 835 | if self._binary_file: |
| 836 | return tempfile.TemporaryFile("wb+") |
| 837 | else: |
| 838 | return tempfile.TemporaryFile("w+", |
| 839 | encoding=self.encoding, newline = '\n') |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 840 | |
Guido van Rossum | 243ddcd | 1996-03-07 06:33:07 +0000 | [diff] [blame] | 841 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 842 | # Test/debug code |
| 843 | # =============== |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 844 | |
Guido van Rossum | 773ab27 | 1996-07-23 03:46:24 +0000 | [diff] [blame] | 845 | def test(environ=os.environ): |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 846 | """Robust test CGI script, usable as main program. |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 847 | |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 848 | Write minimal HTTP headers and dump all information provided to |
| 849 | the script in HTML form. |
| 850 | |
| 851 | """ |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 852 | print("Content-type: text/html") |
| 853 | print() |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 854 | sys.stderr = sys.stdout |
| 855 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 856 | form = FieldStorage() # Replace with other classes to test those |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 857 | print_directory() |
| 858 | print_arguments() |
Guido van Rossum | a3c6a8a | 2000-09-19 04:11:46 +0000 | [diff] [blame] | 859 | print_form(form) |
| 860 | print_environ(environ) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 861 | print_environ_usage() |
| 862 | def f(): |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 863 | exec("testing print_exception() -- <I>italics?</I>") |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 864 | def g(f=f): |
| 865 | f() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 866 | print("<H3>What follows is a test, not an actual exception:</H3>") |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 867 | g() |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 868 | except: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 869 | print_exception() |
Guido van Rossum | f85de8a | 1996-08-20 20:22:39 +0000 | [diff] [blame] | 870 | |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 871 | print("<H1>Second try with a small maxlen...</H1>") |
Guido van Rossum | 57d51f2 | 2000-09-16 21:16:01 +0000 | [diff] [blame] | 872 | |
Guido van Rossum | ad16471 | 1997-05-13 19:03:23 +0000 | [diff] [blame] | 873 | global maxlen |
| 874 | maxlen = 50 |
| 875 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 876 | form = FieldStorage() # Replace with other classes to test those |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 877 | print_directory() |
| 878 | print_arguments() |
Guido van Rossum | a3c6a8a | 2000-09-19 04:11:46 +0000 | [diff] [blame] | 879 | print_form(form) |
| 880 | print_environ(environ) |
Guido van Rossum | ad16471 | 1997-05-13 19:03:23 +0000 | [diff] [blame] | 881 | except: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 882 | print_exception() |
Guido van Rossum | ad16471 | 1997-05-13 19:03:23 +0000 | [diff] [blame] | 883 | |
Guido van Rossum | f85de8a | 1996-08-20 20:22:39 +0000 | [diff] [blame] | 884 | def print_exception(type=None, value=None, tb=None, limit=None): |
| 885 | if type is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 886 | type, value, tb = sys.exc_info() |
Guido van Rossum | f85de8a | 1996-08-20 20:22:39 +0000 | [diff] [blame] | 887 | import traceback |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 888 | print() |
| 889 | print("<H3>Traceback (most recent call last):</H3>") |
Guido van Rossum | f85de8a | 1996-08-20 20:22:39 +0000 | [diff] [blame] | 890 | list = traceback.format_tb(tb, limit) + \ |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 891 | traceback.format_exception_only(type, value) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 892 | print("<PRE>%s<B>%s</B></PRE>" % ( |
Georg Brandl | 1f7fffb | 2010-10-15 15:57:45 +0000 | [diff] [blame] | 893 | html.escape("".join(list[:-1])), |
| 894 | html.escape(list[-1]), |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 895 | )) |
Guido van Rossum | f15d159 | 1997-09-29 23:22:12 +0000 | [diff] [blame] | 896 | del tb |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 897 | |
Guido van Rossum | 773ab27 | 1996-07-23 03:46:24 +0000 | [diff] [blame] | 898 | def print_environ(environ=os.environ): |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 899 | """Dump the shell environment as HTML.""" |
Guido van Rossum | a1a6852 | 2007-08-28 03:11:34 +0000 | [diff] [blame] | 900 | keys = sorted(environ.keys()) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 901 | print() |
| 902 | print("<H3>Shell Environment:</H3>") |
| 903 | print("<DL>") |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 904 | for key in keys: |
Georg Brandl | 1f7fffb | 2010-10-15 15:57:45 +0000 | [diff] [blame] | 905 | print("<DT>", html.escape(key), "<DD>", html.escape(environ[key])) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 906 | print("</DL>") |
| 907 | print() |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 908 | |
| 909 | def print_form(form): |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 910 | """Dump the contents of a form as HTML.""" |
Guido van Rossum | a1a6852 | 2007-08-28 03:11:34 +0000 | [diff] [blame] | 911 | keys = sorted(form.keys()) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 912 | print() |
| 913 | print("<H3>Form Contents:</H3>") |
Guido van Rossum | 57d51f2 | 2000-09-16 21:16:01 +0000 | [diff] [blame] | 914 | if not keys: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 915 | print("<P>No form fields.") |
| 916 | print("<DL>") |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 917 | for key in keys: |
Georg Brandl | 1f7fffb | 2010-10-15 15:57:45 +0000 | [diff] [blame] | 918 | print("<DT>" + html.escape(key) + ":", end=' ') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 919 | value = form[key] |
Georg Brandl | 1f7fffb | 2010-10-15 15:57:45 +0000 | [diff] [blame] | 920 | print("<i>" + html.escape(repr(type(value))) + "</i>") |
| 921 | print("<DD>" + html.escape(repr(value))) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 922 | print("</DL>") |
| 923 | print() |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 924 | |
| 925 | def print_directory(): |
| 926 | """Dump the current directory as HTML.""" |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 927 | print() |
| 928 | print("<H3>Current Working Directory:</H3>") |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 929 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 930 | pwd = os.getcwd() |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 931 | except OSError as msg: |
Andrew Svetlov | 8b33dd8 | 2012-12-24 19:58:48 +0200 | [diff] [blame] | 932 | print("OSError:", html.escape(str(msg))) |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 933 | else: |
Georg Brandl | 1f7fffb | 2010-10-15 15:57:45 +0000 | [diff] [blame] | 934 | print(html.escape(pwd)) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 935 | print() |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 936 | |
Guido van Rossum | a8738a5 | 1996-03-14 21:30:28 +0000 | [diff] [blame] | 937 | def print_arguments(): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 938 | print() |
| 939 | print("<H3>Command Line Arguments:</H3>") |
| 940 | print() |
| 941 | print(sys.argv) |
| 942 | print() |
Guido van Rossum | a8738a5 | 1996-03-14 21:30:28 +0000 | [diff] [blame] | 943 | |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 944 | def print_environ_usage(): |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 945 | """Dump a list of environment variables used by CGI as HTML.""" |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 946 | print(""" |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 947 | <H3>These environment variables could have been set:</H3> |
| 948 | <UL> |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 949 | <LI>AUTH_TYPE |
| 950 | <LI>CONTENT_LENGTH |
| 951 | <LI>CONTENT_TYPE |
| 952 | <LI>DATE_GMT |
| 953 | <LI>DATE_LOCAL |
| 954 | <LI>DOCUMENT_NAME |
| 955 | <LI>DOCUMENT_ROOT |
| 956 | <LI>DOCUMENT_URI |
| 957 | <LI>GATEWAY_INTERFACE |
| 958 | <LI>LAST_MODIFIED |
| 959 | <LI>PATH |
| 960 | <LI>PATH_INFO |
| 961 | <LI>PATH_TRANSLATED |
| 962 | <LI>QUERY_STRING |
| 963 | <LI>REMOTE_ADDR |
| 964 | <LI>REMOTE_HOST |
| 965 | <LI>REMOTE_IDENT |
| 966 | <LI>REMOTE_USER |
| 967 | <LI>REQUEST_METHOD |
| 968 | <LI>SCRIPT_NAME |
| 969 | <LI>SERVER_NAME |
| 970 | <LI>SERVER_PORT |
| 971 | <LI>SERVER_PROTOCOL |
| 972 | <LI>SERVER_ROOT |
| 973 | <LI>SERVER_SOFTWARE |
| 974 | </UL> |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 975 | In addition, HTTP headers sent by the server may be passed in the |
| 976 | environment as well. Here are some common variable names: |
| 977 | <UL> |
| 978 | <LI>HTTP_ACCEPT |
| 979 | <LI>HTTP_CONNECTION |
| 980 | <LI>HTTP_HOST |
| 981 | <LI>HTTP_PRAGMA |
| 982 | <LI>HTTP_REFERER |
| 983 | <LI>HTTP_USER_AGENT |
| 984 | </UL> |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 985 | """) |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 986 | |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 987 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 988 | # Utilities |
| 989 | # ========= |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 990 | |
Benjamin Peterson | 4d59a78 | 2014-04-03 10:22:10 -0400 | [diff] [blame] | 991 | def valid_boundary(s): |
Guido van Rossum | 2e441f7 | 2001-07-25 21:00:19 +0000 | [diff] [blame] | 992 | import re |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 993 | if isinstance(s, bytes): |
| 994 | _vb_pattern = b"^[ -~]{0,200}[!-~]$" |
| 995 | else: |
| 996 | _vb_pattern = "^[ -~]{0,200}[!-~]$" |
Guido van Rossum | 2e441f7 | 2001-07-25 21:00:19 +0000 | [diff] [blame] | 997 | return re.match(_vb_pattern, s) |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 998 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 999 | # Invoke mainline |
| 1000 | # =============== |
| 1001 | |
| 1002 | # Call test() when this file is run as a script (not imported as a module) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 1003 | if __name__ == '__main__': |
Guido van Rossum | 7aee384 | 1996-03-07 18:00:44 +0000 | [diff] [blame] | 1004 | test() |