Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use SpooledTemporaryFile in wsgi body #572

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions uvicorn/middleware/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import concurrent.futures
import io
import sys
from tempfile import SpooledTemporaryFile


def build_environ(scope, message, body):
def build_environ(scope, message, body: SpooledTemporaryFile):
"""
Builds a scope and request message into a WSGI environ object.
"""
Expand All @@ -16,7 +17,7 @@ def build_environ(scope, message, body):
"SERVER_PROTOCOL": "HTTP/%s" % scope["http_version"],
"wsgi.version": (1, 0),
"wsgi.url_scheme": scope.get("scheme", "http"),
"wsgi.input": io.BytesIO(body),
"wsgi.input": body,
"wsgi.errors": sys.stdout,
"wsgi.multithread": True,
"wsgi.multiprocess": True,
Expand Down Expand Up @@ -78,13 +79,15 @@ def __init__(self, app, executor, scope):

async def __call__(self, receive, send):
message = await receive()
body = message.get("body", b"")
body_file = SpooledTemporaryFile(1024 * 1024)
body_file.write(message.get("body", b""))
more_body = message.get("more_body", False)
while more_body:
body_message = await receive()
body += body_message.get("body", b"")
body_file.write(body_message.get("body", b""))
more_body = body_message.get("more_body", False)
environ = build_environ(self.scope, message, body)
body_file.seek(0, 0)
environ = build_environ(self.scope, message, body_file)
self.loop = asyncio.get_event_loop()
wsgi = self.loop.run_in_executor(
self.executor, self.wsgi, environ, self.start_response
Expand Down Expand Up @@ -117,7 +120,7 @@ def start_response(self, status, response_headers, exc_info=None):
status_code, _ = status.split(" ", 1)
status_code = int(status_code)
headers = [
(name.encode("ascii"), value.encode("ascii"))
(name.strip().encode("ascii"), value.strip().encode("ascii"))
for name, value in response_headers
]
self.send_queue.append(
Expand Down