2014-03-28 22:22:00 +00:00
|
|
|
import sys
|
|
|
|
import traceback
|
2015-01-28 12:35:27 +00:00
|
|
|
from io import BytesIO
|
2013-07-01 12:22:27 +00:00
|
|
|
from unittest import TestCase
|
2014-03-28 22:22:00 +00:00
|
|
|
from wsgiref import simple_server
|
2012-08-16 11:01:16 +00:00
|
|
|
|
2014-03-28 22:22:00 +00:00
|
|
|
# If data is too large, socket will choke, so write chunks no larger than 32MB
|
2016-10-27 07:53:39 +00:00
|
|
|
# at a time. The rationale behind the 32MB can be found in #5596#comment:4.
|
2014-03-28 22:22:00 +00:00
|
|
|
MAX_SOCKET_CHUNK_SIZE = 32 * 1024 * 1024 # 32 MB
|
|
|
|
|
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class ServerHandler(simple_server.ServerHandler):
|
2017-01-20 09:20:53 +00:00
|
|
|
error_status = "500 INTERNAL SERVER ERROR"
|
2014-03-28 22:22:00 +00:00
|
|
|
|
|
|
|
def write(self, data):
|
|
|
|
"""'write()' callable as specified by PEP 3333"""
|
|
|
|
|
|
|
|
assert isinstance(data, bytes), "write() argument must be bytestring"
|
|
|
|
|
|
|
|
if not self.status:
|
|
|
|
raise AssertionError("write() before start_response()")
|
|
|
|
|
|
|
|
elif not self.headers_sent:
|
|
|
|
# Before the first output, send the stored headers
|
|
|
|
self.bytes_sent = len(data) # make sure we know content-length
|
|
|
|
self.send_headers()
|
|
|
|
else:
|
|
|
|
self.bytes_sent += len(data)
|
|
|
|
|
|
|
|
# XXX check Content-Length and truncate if too many bytes written?
|
|
|
|
data = BytesIO(data)
|
|
|
|
for chunk in iter(lambda: data.read(MAX_SOCKET_CHUNK_SIZE), b''):
|
|
|
|
self._write(chunk)
|
|
|
|
self._flush()
|
|
|
|
|
|
|
|
def error_output(self, environ, start_response):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().error_output(environ, start_response)
|
2014-03-28 22:22:00 +00:00
|
|
|
return ['\n'.join(traceback.format_exception(*sys.exc_info()))]
|
|
|
|
|
2009-05-07 15:39:06 +00:00
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class DummyHandler:
|
2013-03-18 22:06:24 +00:00
|
|
|
def log_request(self, *args, **kwargs):
|
2009-05-07 15:39:06 +00:00
|
|
|
pass
|
|
|
|
|
2013-03-18 22:06:24 +00:00
|
|
|
|
2009-05-07 15:39:06 +00:00
|
|
|
class FileWrapperHandler(ServerHandler):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2009-05-07 15:39:06 +00:00
|
|
|
self.request_handler = DummyHandler()
|
|
|
|
self._used_sendfile = False
|
|
|
|
|
|
|
|
def sendfile(self):
|
|
|
|
self._used_sendfile = True
|
|
|
|
return True
|
|
|
|
|
2013-03-18 22:06:24 +00:00
|
|
|
|
2009-05-07 15:39:06 +00:00
|
|
|
def wsgi_app(environ, start_response):
|
2017-01-20 09:20:53 +00:00
|
|
|
start_response('200 OK', [('Content-Type', 'text/plain')])
|
2012-08-16 11:01:16 +00:00
|
|
|
return [b'Hello World!']
|
2009-05-07 15:39:06 +00:00
|
|
|
|
2013-03-18 22:06:24 +00:00
|
|
|
|
2009-05-07 15:39:06 +00:00
|
|
|
def wsgi_app_file_wrapper(environ, start_response):
|
2017-01-20 09:20:53 +00:00
|
|
|
start_response('200 OK', [('Content-Type', 'text/plain')])
|
2012-08-16 11:01:16 +00:00
|
|
|
return environ['wsgi.file_wrapper'](BytesIO(b'foo'))
|
2009-05-07 15:39:06 +00:00
|
|
|
|
2013-03-18 22:06:24 +00:00
|
|
|
|
2009-05-07 15:39:06 +00:00
|
|
|
class WSGIFileWrapperTests(TestCase):
|
|
|
|
"""
|
2019-08-23 08:46:43 +00:00
|
|
|
The wsgi.file_wrapper works for the builtin server.
|
2013-03-18 22:06:24 +00:00
|
|
|
|
|
|
|
Tests for #9659: wsgi.file_wrapper in the builtin server.
|
|
|
|
We need to mock a couple of handlers and keep track of what
|
|
|
|
gets called when using a couple kinds of WSGI apps.
|
2009-05-07 15:39:06 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def test_file_wrapper_uses_sendfile(self):
|
|
|
|
env = {'SERVER_PROTOCOL': 'HTTP/1.0'}
|
2012-08-16 11:01:16 +00:00
|
|
|
handler = FileWrapperHandler(None, BytesIO(), BytesIO(), env)
|
2009-05-07 15:39:06 +00:00
|
|
|
handler.run(wsgi_app_file_wrapper)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertTrue(handler._used_sendfile)
|
2012-08-16 11:01:16 +00:00
|
|
|
self.assertEqual(handler.stdout.getvalue(), b'')
|
|
|
|
self.assertEqual(handler.stderr.getvalue(), b'')
|
2009-05-07 15:39:06 +00:00
|
|
|
|
|
|
|
def test_file_wrapper_no_sendfile(self):
|
|
|
|
env = {'SERVER_PROTOCOL': 'HTTP/1.0'}
|
2012-08-16 11:01:16 +00:00
|
|
|
handler = FileWrapperHandler(None, BytesIO(), BytesIO(), env)
|
2009-05-07 15:39:06 +00:00
|
|
|
handler.run(wsgi_app)
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertFalse(handler._used_sendfile)
|
2012-08-16 11:01:16 +00:00
|
|
|
self.assertEqual(handler.stdout.getvalue().splitlines()[-1], b'Hello World!')
|
|
|
|
self.assertEqual(handler.stderr.getvalue(), b'')
|
2013-03-18 22:06:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WriteChunkCounterHandler(ServerHandler):
|
|
|
|
"""
|
|
|
|
Server handler that counts the number of chunks written after headers were
|
|
|
|
sent. Used to make sure large response body chunking works properly.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2013-03-18 22:06:24 +00:00
|
|
|
self.request_handler = DummyHandler()
|
|
|
|
self.headers_written = False
|
|
|
|
self.write_chunk_counter = 0
|
|
|
|
|
|
|
|
def send_headers(self):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().send_headers()
|
2013-03-18 22:06:24 +00:00
|
|
|
self.headers_written = True
|
|
|
|
|
|
|
|
def _write(self, data):
|
|
|
|
if self.headers_written:
|
|
|
|
self.write_chunk_counter += 1
|
|
|
|
self.stdout.write(data)
|
|
|
|
|
|
|
|
|
|
|
|
def send_big_data_app(environ, start_response):
|
2017-01-20 09:20:53 +00:00
|
|
|
start_response('200 OK', [('Content-Type', 'text/plain')])
|
2013-03-18 22:06:24 +00:00
|
|
|
# Return a blob of data that is 1.5 times the maximum chunk size.
|
|
|
|
return [b'x' * (MAX_SOCKET_CHUNK_SIZE + MAX_SOCKET_CHUNK_SIZE // 2)]
|
|
|
|
|
|
|
|
|
|
|
|
class ServerHandlerChunksProperly(TestCase):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
The ServerHandler chunks data properly.
|
2013-03-18 22:06:24 +00:00
|
|
|
|
|
|
|
Tests for #18972: The logic that performs the math to break data into
|
|
|
|
32MB (MAX_SOCKET_CHUNK_SIZE) chunks was flawed, BUT it didn't actually
|
|
|
|
cause any problems.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_chunked_data(self):
|
|
|
|
env = {'SERVER_PROTOCOL': 'HTTP/1.0'}
|
|
|
|
handler = WriteChunkCounterHandler(None, BytesIO(), BytesIO(), env)
|
|
|
|
handler.run(send_big_data_app)
|
|
|
|
self.assertEqual(handler.write_chunk_counter, 2)
|