the middleware now also triggers when Content-Length is '0' but X-Amz-Decoded-Content-Length or aws-chunked headers indicate a body should be present

This commit is contained in:
2026-03-22 00:24:04 +08:00
parent 7612cb054a
commit 366f8ce60d

View File

@@ -51,13 +51,17 @@ class _ChunkedTransferMiddleware:
self.app = app self.app = app
def __call__(self, environ, start_response): def __call__(self, environ, start_response):
has_content_length = environ.get("CONTENT_LENGTH") if environ.get("REQUEST_METHOD") not in ("PUT", "POST"):
if not has_content_length and environ.get("REQUEST_METHOD") in ("PUT", "POST"): return self.app(environ, start_response)
transfer_chunked = "chunked" in environ.get("HTTP_TRANSFER_ENCODING", "").lower()
has_body_hint = bool(environ.get("HTTP_X_AMZ_DECODED_CONTENT_LENGTH")) content_length = environ.get("CONTENT_LENGTH")
content_encoding = environ.get("HTTP_CONTENT_ENCODING", "") body_expected = (
has_aws_chunked = "aws-chunked" in content_encoding.lower() environ.get("HTTP_X_AMZ_DECODED_CONTENT_LENGTH")
if transfer_chunked or has_body_hint or has_aws_chunked: or "chunked" in environ.get("HTTP_TRANSFER_ENCODING", "").lower()
or "aws-chunked" in environ.get("HTTP_CONTENT_ENCODING", "").lower()
)
if body_expected and (not content_length or content_length == "0"):
try: try:
raw = environ["wsgi.input"] raw = environ["wsgi.input"]
body = raw.read() body = raw.read()
@@ -66,6 +70,7 @@ class _ChunkedTransferMiddleware:
if body: if body:
environ["wsgi.input"] = io.BytesIO(body) environ["wsgi.input"] = io.BytesIO(body)
environ["CONTENT_LENGTH"] = str(len(body)) environ["CONTENT_LENGTH"] = str(len(body))
return self.app(environ, start_response) return self.app(environ, start_response)