further fixes

This commit is contained in:
2026-03-22 00:16:30 +08:00
parent 966d524dca
commit 7612cb054a

View File

@@ -51,12 +51,21 @@ class _ChunkedTransferMiddleware:
self.app = app self.app = app
def __call__(self, environ, start_response): def __call__(self, environ, start_response):
transfer_encoding = environ.get("HTTP_TRANSFER_ENCODING", "") has_content_length = environ.get("CONTENT_LENGTH")
if "chunked" in transfer_encoding.lower() and "CONTENT_LENGTH" not in environ: if not has_content_length and environ.get("REQUEST_METHOD") in ("PUT", "POST"):
raw = environ["wsgi.input"] transfer_chunked = "chunked" in environ.get("HTTP_TRANSFER_ENCODING", "").lower()
body = raw.read() has_body_hint = bool(environ.get("HTTP_X_AMZ_DECODED_CONTENT_LENGTH"))
environ["wsgi.input"] = io.BytesIO(body) content_encoding = environ.get("HTTP_CONTENT_ENCODING", "")
environ["CONTENT_LENGTH"] = str(len(body)) has_aws_chunked = "aws-chunked" in content_encoding.lower()
if transfer_chunked or has_body_hint or has_aws_chunked:
try:
raw = environ["wsgi.input"]
body = raw.read()
except Exception:
body = b""
if body:
environ["wsgi.input"] = io.BytesIO(body)
environ["CONTENT_LENGTH"] = str(len(body))
return self.app(environ, start_response) return self.app(environ, start_response)