diff options
author | Martin Panter <vadmium+py@gmail.com> | 2016-06-05 06:28:55 (GMT) |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2016-06-05 06:28:55 (GMT) |
commit | ed0425c60abe1b746b1ca5b4039984d2ad6583c4 (patch) | |
tree | dba5a8819bf4f6cf6ef4561b3b03cdb777794ddb /Lib/wsgiref | |
parent | 889f914edb4253bdf4b475ed594199d67fcc08b7 (diff) | |
download | cpython-ed0425c60abe1b746b1ca5b4039984d2ad6583c4.zip cpython-ed0425c60abe1b746b1ca5b4039984d2ad6583c4.tar.gz cpython-ed0425c60abe1b746b1ca5b4039984d2ad6583c4.tar.bz2 |
Issue #24291: Avoid WSGIRequestHandler doing partial writes
If the underlying send() method indicates a partial write, such as when the
call is interrupted to handle a signal, the server would silently drop the
remaining data.
Also add deprecated support for SimpleHandler.stdout.write() doing partial
writes.
Diffstat (limited to 'Lib/wsgiref')
-rw-r--r-- | Lib/wsgiref/handlers.py | 12 | ||||
-rw-r--r-- | Lib/wsgiref/simple_server.py | 17 |
2 files changed, 23 insertions, 6 deletions
diff --git a/Lib/wsgiref/handlers.py b/Lib/wsgiref/handlers.py index acb3547..f4300b8 100644 --- a/Lib/wsgiref/handlers.py +++ b/Lib/wsgiref/handlers.py @@ -450,7 +450,17 @@ class SimpleHandler(BaseHandler): self.environ.update(self.base_env) def _write(self,data): - self.stdout.write(data) + result = self.stdout.write(data) + if result is None or result == len(data): + return + from warnings import warn + warn("SimpleHandler.stdout.write() should not do partial writes", + DeprecationWarning) + while True: + data = data[result:] + if not data: + break + result = self.stdout.write(data) def _flush(self): self.stdout.flush() diff --git a/Lib/wsgiref/simple_server.py b/Lib/wsgiref/simple_server.py index e396788..7fddbe8 100644 --- a/Lib/wsgiref/simple_server.py +++ b/Lib/wsgiref/simple_server.py @@ -11,6 +11,7 @@ module. See also the BaseHTTPServer module docs for other API information. """ from http.server import BaseHTTPRequestHandler, HTTPServer +from io import BufferedWriter import sys import urllib.parse from wsgiref.handlers import SimpleHandler @@ -126,11 +127,17 @@ class WSGIRequestHandler(BaseHTTPRequestHandler): if not self.parse_request(): # An error code has been sent, just exit return - handler = ServerHandler( - self.rfile, self.wfile, self.get_stderr(), self.get_environ() - ) - handler.request_handler = self # backpointer for logging - handler.run(self.server.get_app()) + # Avoid passing the raw file object wfile, which can do partial + # writes (Issue 24291) + stdout = BufferedWriter(self.wfile) + try: + handler = ServerHandler( + self.rfile, stdout, self.get_stderr(), self.get_environ() + ) + handler.request_handler = self # backpointer for logging + handler.run(self.server.get_app()) + finally: + stdout.detach() |