ยปCore Development>Code coverage>Lib/wsgiref/handlers.py

Python code coverage for Lib/wsgiref/handlers.py

#countcontent
1n/a"""Base classes for server/gateway implementations"""
2n/a
3n/afrom .util import FileWrapper, guess_scheme, is_hop_by_hop
4n/afrom .headers import Headers
5n/a
6n/aimport sys, os, time
7n/a
8n/a__all__ = [
9n/a 'BaseHandler', 'SimpleHandler', 'BaseCGIHandler', 'CGIHandler',
10n/a 'IISCGIHandler', 'read_environ'
11n/a]
12n/a
13n/a# Weekday and month names for HTTP date/time formatting; always English!
14n/a_weekdayname = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
15n/a_monthname = [None, # Dummy so we can use 1-based month numbers
16n/a "Jan", "Feb", "Mar", "Apr", "May", "Jun",
17n/a "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
18n/a
19n/adef format_date_time(timestamp):
20n/a year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp)
21n/a return "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
22n/a _weekdayname[wd], day, _monthname[month], year, hh, mm, ss
23n/a )
24n/a
25n/a_is_request = {
26n/a 'SCRIPT_NAME', 'PATH_INFO', 'QUERY_STRING', 'REQUEST_METHOD', 'AUTH_TYPE',
27n/a 'CONTENT_TYPE', 'CONTENT_LENGTH', 'HTTPS', 'REMOTE_USER', 'REMOTE_IDENT',
28n/a}.__contains__
29n/a
30n/adef _needs_transcode(k):
31n/a return _is_request(k) or k.startswith('HTTP_') or k.startswith('SSL_') \
32n/a or (k.startswith('REDIRECT_') and _needs_transcode(k[9:]))
33n/a
34n/adef read_environ():
35n/a """Read environment, fixing HTTP variables"""
36n/a enc = sys.getfilesystemencoding()
37n/a esc = 'surrogateescape'
38n/a try:
39n/a ''.encode('utf-8', esc)
40n/a except LookupError:
41n/a esc = 'replace'
42n/a environ = {}
43n/a
44n/a # Take the basic environment from native-unicode os.environ. Attempt to
45n/a # fix up the variables that come from the HTTP request to compensate for
46n/a # the bytes->unicode decoding step that will already have taken place.
47n/a for k, v in os.environ.items():
48n/a if _needs_transcode(k):
49n/a
50n/a # On win32, the os.environ is natively Unicode. Different servers
51n/a # decode the request bytes using different encodings.
52n/a if sys.platform == 'win32':
53n/a software = os.environ.get('SERVER_SOFTWARE', '').lower()
54n/a
55n/a # On IIS, the HTTP request will be decoded as UTF-8 as long
56n/a # as the input is a valid UTF-8 sequence. Otherwise it is
57n/a # decoded using the system code page (mbcs), with no way to
58n/a # detect this has happened. Because UTF-8 is the more likely
59n/a # encoding, and mbcs is inherently unreliable (an mbcs string
60n/a # that happens to be valid UTF-8 will not be decoded as mbcs)
61n/a # always recreate the original bytes as UTF-8.
62n/a if software.startswith('microsoft-iis/'):
63n/a v = v.encode('utf-8').decode('iso-8859-1')
64n/a
65n/a # Apache mod_cgi writes bytes-as-unicode (as if ISO-8859-1) direct
66n/a # to the Unicode environ. No modification needed.
67n/a elif software.startswith('apache/'):
68n/a pass
69n/a
70n/a # Python 3's http.server.CGIHTTPRequestHandler decodes
71n/a # using the urllib.unquote default of UTF-8, amongst other
72n/a # issues.
73n/a elif (
74n/a software.startswith('simplehttp/')
75n/a and 'python/3' in software
76n/a ):
77n/a v = v.encode('utf-8').decode('iso-8859-1')
78n/a
79n/a # For other servers, guess that they have written bytes to
80n/a # the environ using stdio byte-oriented interfaces, ending up
81n/a # with the system code page.
82n/a else:
83n/a v = v.encode(enc, 'replace').decode('iso-8859-1')
84n/a
85n/a # Recover bytes from unicode environ, using surrogate escapes
86n/a # where available (Python 3.1+).
87n/a else:
88n/a v = v.encode(enc, esc).decode('iso-8859-1')
89n/a
90n/a environ[k] = v
91n/a return environ
92n/a
93n/a
94n/aclass BaseHandler:
95n/a """Manage the invocation of a WSGI application"""
96n/a
97n/a # Configuration parameters; can override per-subclass or per-instance
98n/a wsgi_version = (1,0)
99n/a wsgi_multithread = True
100n/a wsgi_multiprocess = True
101n/a wsgi_run_once = False
102n/a
103n/a origin_server = True # We are transmitting direct to client
104n/a http_version = "1.0" # Version that should be used for response
105n/a server_software = None # String name of server software, if any
106n/a
107n/a # os_environ is used to supply configuration from the OS environment:
108n/a # by default it's a copy of 'os.environ' as of import time, but you can
109n/a # override this in e.g. your __init__ method.
110n/a os_environ= read_environ()
111n/a
112n/a # Collaborator classes
113n/a wsgi_file_wrapper = FileWrapper # set to None to disable
114n/a headers_class = Headers # must be a Headers-like class
115n/a
116n/a # Error handling (also per-subclass or per-instance)
117n/a traceback_limit = None # Print entire traceback to self.get_stderr()
118n/a error_status = "500 Internal Server Error"
119n/a error_headers = [('Content-Type','text/plain')]
120n/a error_body = b"A server error occurred. Please contact the administrator."
121n/a
122n/a # State variables (don't mess with these)
123n/a status = result = None
124n/a headers_sent = False
125n/a headers = None
126n/a bytes_sent = 0
127n/a
128n/a def run(self, application):
129n/a """Invoke the application"""
130n/a # Note to self: don't move the close()! Asynchronous servers shouldn't
131n/a # call close() from finish_response(), so if you close() anywhere but
132n/a # the double-error branch here, you'll break asynchronous servers by
133n/a # prematurely closing. Async servers must return from 'run()' without
134n/a # closing if there might still be output to iterate over.
135n/a try:
136n/a self.setup_environ()
137n/a self.result = application(self.environ, self.start_response)
138n/a self.finish_response()
139n/a except:
140n/a try:
141n/a self.handle_error()
142n/a except:
143n/a # If we get an error handling an error, just give up already!
144n/a self.close()
145n/a raise # ...and let the actual server figure it out.
146n/a
147n/a
148n/a def setup_environ(self):
149n/a """Set up the environment for one request"""
150n/a
151n/a env = self.environ = self.os_environ.copy()
152n/a self.add_cgi_vars()
153n/a
154n/a env['wsgi.input'] = self.get_stdin()
155n/a env['wsgi.errors'] = self.get_stderr()
156n/a env['wsgi.version'] = self.wsgi_version
157n/a env['wsgi.run_once'] = self.wsgi_run_once
158n/a env['wsgi.url_scheme'] = self.get_scheme()
159n/a env['wsgi.multithread'] = self.wsgi_multithread
160n/a env['wsgi.multiprocess'] = self.wsgi_multiprocess
161n/a
162n/a if self.wsgi_file_wrapper is not None:
163n/a env['wsgi.file_wrapper'] = self.wsgi_file_wrapper
164n/a
165n/a if self.origin_server and self.server_software:
166n/a env.setdefault('SERVER_SOFTWARE',self.server_software)
167n/a
168n/a
169n/a def finish_response(self):
170n/a """Send any iterable data, then close self and the iterable
171n/a
172n/a Subclasses intended for use in asynchronous servers will
173n/a want to redefine this method, such that it sets up callbacks
174n/a in the event loop to iterate over the data, and to call
175n/a 'self.close()' once the response is finished.
176n/a """
177n/a try:
178n/a if not self.result_is_file() or not self.sendfile():
179n/a for data in self.result:
180n/a self.write(data)
181n/a self.finish_content()
182n/a finally:
183n/a self.close()
184n/a
185n/a
186n/a def get_scheme(self):
187n/a """Return the URL scheme being used"""
188n/a return guess_scheme(self.environ)
189n/a
190n/a
191n/a def set_content_length(self):
192n/a """Compute Content-Length or switch to chunked encoding if possible"""
193n/a try:
194n/a blocks = len(self.result)
195n/a except (TypeError,AttributeError,NotImplementedError):
196n/a pass
197n/a else:
198n/a if blocks==1:
199n/a self.headers['Content-Length'] = str(self.bytes_sent)
200n/a return
201n/a # XXX Try for chunked encoding if origin server and client is 1.1
202n/a
203n/a
204n/a def cleanup_headers(self):
205n/a """Make any necessary header changes or defaults
206n/a
207n/a Subclasses can extend this to add other defaults.
208n/a """
209n/a if 'Content-Length' not in self.headers:
210n/a self.set_content_length()
211n/a
212n/a def start_response(self, status, headers,exc_info=None):
213n/a """'start_response()' callable as specified by PEP 3333"""
214n/a
215n/a if exc_info:
216n/a try:
217n/a if self.headers_sent:
218n/a # Re-raise original exception if headers sent
219n/a raise exc_info[0](exc_info[1]).with_traceback(exc_info[2])
220n/a finally:
221n/a exc_info = None # avoid dangling circular ref
222n/a elif self.headers is not None:
223n/a raise AssertionError("Headers already set!")
224n/a
225n/a self.status = status
226n/a self.headers = self.headers_class(headers)
227n/a status = self._convert_string_type(status, "Status")
228n/a assert len(status)>=4,"Status must be at least 4 characters"
229n/a assert status[:3].isdigit(), "Status message must begin w/3-digit code"
230n/a assert status[3]==" ", "Status message must have a space after code"
231n/a
232n/a if __debug__:
233n/a for name, val in headers:
234n/a name = self._convert_string_type(name, "Header name")
235n/a val = self._convert_string_type(val, "Header value")
236n/a assert not is_hop_by_hop(name),"Hop-by-hop headers not allowed"
237n/a
238n/a return self.write
239n/a
240n/a def _convert_string_type(self, value, title):
241n/a """Convert/check value type."""
242n/a if type(value) is str:
243n/a return value
244n/a raise AssertionError(
245n/a "{0} must be of type str (got {1})".format(title, repr(value))
246n/a )
247n/a
248n/a def send_preamble(self):
249n/a """Transmit version/status/date/server, via self._write()"""
250n/a if self.origin_server:
251n/a if self.client_is_modern():
252n/a self._write(('HTTP/%s %s\r\n' % (self.http_version,self.status)).encode('iso-8859-1'))
253n/a if 'Date' not in self.headers:
254n/a self._write(
255n/a ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
256n/a )
257n/a if self.server_software and 'Server' not in self.headers:
258n/a self._write(('Server: %s\r\n' % self.server_software).encode('iso-8859-1'))
259n/a else:
260n/a self._write(('Status: %s\r\n' % self.status).encode('iso-8859-1'))
261n/a
262n/a def write(self, data):
263n/a """'write()' callable as specified by PEP 3333"""
264n/a
265n/a assert type(data) is bytes, \
266n/a "write() argument must be a bytes instance"
267n/a
268n/a if not self.status:
269n/a raise AssertionError("write() before start_response()")
270n/a
271n/a elif not self.headers_sent:
272n/a # Before the first output, send the stored headers
273n/a self.bytes_sent = len(data) # make sure we know content-length
274n/a self.send_headers()
275n/a else:
276n/a self.bytes_sent += len(data)
277n/a
278n/a # XXX check Content-Length and truncate if too many bytes written?
279n/a self._write(data)
280n/a self._flush()
281n/a
282n/a
283n/a def sendfile(self):
284n/a """Platform-specific file transmission
285n/a
286n/a Override this method in subclasses to support platform-specific
287n/a file transmission. It is only called if the application's
288n/a return iterable ('self.result') is an instance of
289n/a 'self.wsgi_file_wrapper'.
290n/a
291n/a This method should return a true value if it was able to actually
292n/a transmit the wrapped file-like object using a platform-specific
293n/a approach. It should return a false value if normal iteration
294n/a should be used instead. An exception can be raised to indicate
295n/a that transmission was attempted, but failed.
296n/a
297n/a NOTE: this method should call 'self.send_headers()' if
298n/a 'self.headers_sent' is false and it is going to attempt direct
299n/a transmission of the file.
300n/a """
301n/a return False # No platform-specific transmission by default
302n/a
303n/a
304n/a def finish_content(self):
305n/a """Ensure headers and content have both been sent"""
306n/a if not self.headers_sent:
307n/a # Only zero Content-Length if not set by the application (so
308n/a # that HEAD requests can be satisfied properly, see #3839)
309n/a self.headers.setdefault('Content-Length', "0")
310n/a self.send_headers()
311n/a else:
312n/a pass # XXX check if content-length was too short?
313n/a
314n/a def close(self):
315n/a """Close the iterable (if needed) and reset all instance vars
316n/a
317n/a Subclasses may want to also drop the client connection.
318n/a """
319n/a try:
320n/a if hasattr(self.result,'close'):
321n/a self.result.close()
322n/a finally:
323n/a self.result = self.headers = self.status = self.environ = None
324n/a self.bytes_sent = 0; self.headers_sent = False
325n/a
326n/a
327n/a def send_headers(self):
328n/a """Transmit headers to the client, via self._write()"""
329n/a self.cleanup_headers()
330n/a self.headers_sent = True
331n/a if not self.origin_server or self.client_is_modern():
332n/a self.send_preamble()
333n/a self._write(bytes(self.headers))
334n/a
335n/a
336n/a def result_is_file(self):
337n/a """True if 'self.result' is an instance of 'self.wsgi_file_wrapper'"""
338n/a wrapper = self.wsgi_file_wrapper
339n/a return wrapper is not None and isinstance(self.result,wrapper)
340n/a
341n/a
342n/a def client_is_modern(self):
343n/a """True if client can accept status and headers"""
344n/a return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
345n/a
346n/a
347n/a def log_exception(self,exc_info):
348n/a """Log the 'exc_info' tuple in the server log
349n/a
350n/a Subclasses may override to retarget the output or change its format.
351n/a """
352n/a try:
353n/a from traceback import print_exception
354n/a stderr = self.get_stderr()
355n/a print_exception(
356n/a exc_info[0], exc_info[1], exc_info[2],
357n/a self.traceback_limit, stderr
358n/a )
359n/a stderr.flush()
360n/a finally:
361n/a exc_info = None
362n/a
363n/a def handle_error(self):
364n/a """Log current error, and send error output to client if possible"""
365n/a self.log_exception(sys.exc_info())
366n/a if not self.headers_sent:
367n/a self.result = self.error_output(self.environ, self.start_response)
368n/a self.finish_response()
369n/a # XXX else: attempt advanced recovery techniques for HTML or text?
370n/a
371n/a def error_output(self, environ, start_response):
372n/a """WSGI mini-app to create error output
373n/a
374n/a By default, this just uses the 'error_status', 'error_headers',
375n/a and 'error_body' attributes to generate an output page. It can
376n/a be overridden in a subclass to dynamically generate diagnostics,
377n/a choose an appropriate message for the user's preferred language, etc.
378n/a
379n/a Note, however, that it's not recommended from a security perspective to
380n/a spit out diagnostics to any old user; ideally, you should have to do
381n/a something special to enable diagnostic output, which is why we don't
382n/a include any here!
383n/a """
384n/a start_response(self.error_status,self.error_headers[:],sys.exc_info())
385n/a return [self.error_body]
386n/a
387n/a
388n/a # Pure abstract methods; *must* be overridden in subclasses
389n/a
390n/a def _write(self,data):
391n/a """Override in subclass to buffer data for send to client
392n/a
393n/a It's okay if this method actually transmits the data; BaseHandler
394n/a just separates write and flush operations for greater efficiency
395n/a when the underlying system actually has such a distinction.
396n/a """
397n/a raise NotImplementedError
398n/a
399n/a def _flush(self):
400n/a """Override in subclass to force sending of recent '_write()' calls
401n/a
402n/a It's okay if this method is a no-op (i.e., if '_write()' actually
403n/a sends the data.
404n/a """
405n/a raise NotImplementedError
406n/a
407n/a def get_stdin(self):
408n/a """Override in subclass to return suitable 'wsgi.input'"""
409n/a raise NotImplementedError
410n/a
411n/a def get_stderr(self):
412n/a """Override in subclass to return suitable 'wsgi.errors'"""
413n/a raise NotImplementedError
414n/a
415n/a def add_cgi_vars(self):
416n/a """Override in subclass to insert CGI variables in 'self.environ'"""
417n/a raise NotImplementedError
418n/a
419n/a
420n/aclass SimpleHandler(BaseHandler):
421n/a """Handler that's just initialized with streams, environment, etc.
422n/a
423n/a This handler subclass is intended for synchronous HTTP/1.0 origin servers,
424n/a and handles sending the entire response output, given the correct inputs.
425n/a
426n/a Usage::
427n/a
428n/a handler = SimpleHandler(
429n/a inp,out,err,env, multithread=False, multiprocess=True
430n/a )
431n/a handler.run(app)"""
432n/a
433n/a def __init__(self,stdin,stdout,stderr,environ,
434n/a multithread=True, multiprocess=False
435n/a ):
436n/a self.stdin = stdin
437n/a self.stdout = stdout
438n/a self.stderr = stderr
439n/a self.base_env = environ
440n/a self.wsgi_multithread = multithread
441n/a self.wsgi_multiprocess = multiprocess
442n/a
443n/a def get_stdin(self):
444n/a return self.stdin
445n/a
446n/a def get_stderr(self):
447n/a return self.stderr
448n/a
449n/a def add_cgi_vars(self):
450n/a self.environ.update(self.base_env)
451n/a
452n/a def _write(self,data):
453n/a result = self.stdout.write(data)
454n/a if result is None or result == len(data):
455n/a return
456n/a from warnings import warn
457n/a warn("SimpleHandler.stdout.write() should not do partial writes",
458n/a DeprecationWarning)
459n/a while True:
460n/a data = data[result:]
461n/a if not data:
462n/a break
463n/a result = self.stdout.write(data)
464n/a
465n/a def _flush(self):
466n/a self.stdout.flush()
467n/a self._flush = self.stdout.flush
468n/a
469n/a
470n/aclass BaseCGIHandler(SimpleHandler):
471n/a
472n/a """CGI-like systems using input/output/error streams and environ mapping
473n/a
474n/a Usage::
475n/a
476n/a handler = BaseCGIHandler(inp,out,err,env)
477n/a handler.run(app)
478n/a
479n/a This handler class is useful for gateway protocols like ReadyExec and
480n/a FastCGI, that have usable input/output/error streams and an environment
481n/a mapping. It's also the base class for CGIHandler, which just uses
482n/a sys.stdin, os.environ, and so on.
483n/a
484n/a The constructor also takes keyword arguments 'multithread' and
485n/a 'multiprocess' (defaulting to 'True' and 'False' respectively) to control
486n/a the configuration sent to the application. It sets 'origin_server' to
487n/a False (to enable CGI-like output), and assumes that 'wsgi.run_once' is
488n/a False.
489n/a """
490n/a
491n/a origin_server = False
492n/a
493n/a
494n/aclass CGIHandler(BaseCGIHandler):
495n/a
496n/a """CGI-based invocation via sys.stdin/stdout/stderr and os.environ
497n/a
498n/a Usage::
499n/a
500n/a CGIHandler().run(app)
501n/a
502n/a The difference between this class and BaseCGIHandler is that it always
503n/a uses 'wsgi.run_once' of 'True', 'wsgi.multithread' of 'False', and
504n/a 'wsgi.multiprocess' of 'True'. It does not take any initialization
505n/a parameters, but always uses 'sys.stdin', 'os.environ', and friends.
506n/a
507n/a If you need to override any of these parameters, use BaseCGIHandler
508n/a instead.
509n/a """
510n/a
511n/a wsgi_run_once = True
512n/a # Do not allow os.environ to leak between requests in Google App Engine
513n/a # and other multi-run CGI use cases. This is not easily testable.
514n/a # See http://bugs.python.org/issue7250
515n/a os_environ = {}
516n/a
517n/a def __init__(self):
518n/a BaseCGIHandler.__init__(
519n/a self, sys.stdin.buffer, sys.stdout.buffer, sys.stderr,
520n/a read_environ(), multithread=False, multiprocess=True
521n/a )
522n/a
523n/a
524n/aclass IISCGIHandler(BaseCGIHandler):
525n/a """CGI-based invocation with workaround for IIS path bug
526n/a
527n/a This handler should be used in preference to CGIHandler when deploying on
528n/a Microsoft IIS without having set the config allowPathInfo option (IIS>=7)
529n/a or metabase allowPathInfoForScriptMappings (IIS<7).
530n/a """
531n/a wsgi_run_once = True
532n/a os_environ = {}
533n/a
534n/a # By default, IIS gives a PATH_INFO that duplicates the SCRIPT_NAME at
535n/a # the front, causing problems for WSGI applications that wish to implement
536n/a # routing. This handler strips any such duplicated path.
537n/a
538n/a # IIS can be configured to pass the correct PATH_INFO, but this causes
539n/a # another bug where PATH_TRANSLATED is wrong. Luckily this variable is
540n/a # rarely used and is not guaranteed by WSGI. On IIS<7, though, the
541n/a # setting can only be made on a vhost level, affecting all other script
542n/a # mappings, many of which break when exposed to the PATH_TRANSLATED bug.
543n/a # For this reason IIS<7 is almost never deployed with the fix. (Even IIS7
544n/a # rarely uses it because there is still no UI for it.)
545n/a
546n/a # There is no way for CGI code to tell whether the option was set, so a
547n/a # separate handler class is provided.
548n/a def __init__(self):
549n/a environ= read_environ()
550n/a path = environ.get('PATH_INFO', '')
551n/a script = environ.get('SCRIPT_NAME', '')
552n/a if (path+'/').startswith(script+'/'):
553n/a environ['PATH_INFO'] = path[len(script):]
554n/a BaseCGIHandler.__init__(
555n/a self, sys.stdin.buffer, sys.stdout.buffer, sys.stderr,
556n/a environ, multithread=False, multiprocess=True
557n/a )