Skip to content

Commit ea29372

Browse files
committed
Do multiple reads for WSGI even with content length
1 parent 386bddc commit ea29372

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/connectrpc/_server_sync.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ def prepare_response_headers(
115115
return headers
116116

117117

118+
def _read_body_with_content_length(
119+
environ: WSGIEnvironment, content_length: int
120+
) -> bytes:
121+
bytes_read = 0
122+
chunks = []
123+
input_stream: BytesIO = environ["wsgi.input"]
124+
while bytes_read < content_length:
125+
to_read = content_length - bytes_read
126+
chunk = input_stream.read(to_read)
127+
if not chunk:
128+
break
129+
chunks.append(chunk)
130+
bytes_read += len(chunk)
131+
return b"".join(chunks)
132+
133+
118134
def _read_body(environ: WSGIEnvironment) -> Iterator[bytes]:
119135
input_stream: BytesIO = environ["wsgi.input"]
120136
while True:
@@ -257,7 +273,7 @@ def _handle_post_request(
257273
content_length = environ.get("CONTENT_LENGTH")
258274
content_length = 0 if not content_length else int(content_length)
259275
if content_length > 0:
260-
req_body = environ["wsgi.input"].read(content_length)
276+
req_body = _read_body_with_content_length(environ, content_length)
261277
else:
262278
req_body = b"".join(_read_body(environ))
263279

0 commit comments

Comments
 (0)