MCPcopy Create free account
hub / github.com/vercel/vercel / _read_chunked_body

Function _read_chunked_body

python/vercel-runtime/src/vercel_runtime/utils.py:124–162  ·  view source on GitHub ↗
(rfile: _Readable, max_bytes: int)

Source from the content-addressed store, hash-verified

122
123
124def _read_chunked_body(rfile: _Readable, max_bytes: int) -> bytes:
125 chunks: list[bytes] = []
126 total = 0
127 while True:
128 size_line = rfile.readline(_MAX_CHUNK_SIZE_LINE)
129 if not size_line:
130 raise ValueError(
131 "could not read chunk size: unexpected end of stream"
132 )
133 size_token = size_line.split(b";", 1)[0].strip()
134 try:
135 chunk_size = int(size_token, 16)
136 except ValueError as exc:
137 raise ValueError(f"invalid chunk size: {size_token!r}") from exc
138 if chunk_size == 0:
139 # Drain trailer headers up to the terminating blank line. Bounded
140 # so a hostile peer cannot pin the connection with endless lines.
141 trailer_total = 0
142 while True:
143 trailer = rfile.readline(_MAX_CHUNK_SIZE_LINE)
144 if trailer in (b"\r\n", b"\n", b""):
145 break
146 trailer_total += len(trailer)
147 if trailer_total > _MAX_TRAILER_BYTES:
148 raise ValueError(
149 "chunked request trailer exceeds maximum size"
150 )
151 break
152 total += chunk_size
153 if total > max_bytes:
154 raise ValueError("chunked request body exceeds maximum size")
155 data = rfile.read(chunk_size)
156 if len(data) != chunk_size:
157 raise ValueError(
158 "could not read full chunk: unexpected end of stream"
159 )
160 chunks.append(data)
161 rfile.readline(_MAX_CHUNK_SIZE_LINE)
162 return b"".join(chunks)

Callers 1

read_wsgi_request_bodyFunction · 0.85

Calls 6

stripMethod · 0.80
splitMethod · 0.80
appendMethod · 0.80
readlineMethod · 0.45
readMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected