Open the context
Choose the project folder and name the files or behavior involved.
Decode a restricted chunked body from at most 65536 bytes with a 4096-byte output limit. Test embedded CRLF and the zero chunk, rejecting truncation, extensions, trailers and bytes after the terminator.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
def chunk_body(wire):
if type(wire) is not bytes or len(wire) > 65536:
raise ValueError("bounded bytes")
pos, body = 0, bytearray()
while True:
end = wire.find(b"\r\n", pos)
token = wire[pos:end] if end >= 0 else b""
if not 1 <= len(token) <= 8 or any(
c not in b"0123456789abcdefABCDEF" for c in token):
raise ValueError("plain hexadecimal size")
size, pos = int(token, 16), end + 2
if size == 0:
if wire[pos:] != b"\r\n":
raise ValueError("no trailers or extra bytes")
return bytes(body)
if len(body) + size > 4096:
raise ValueError("body limit")
if pos + size + 2 > len(wire):
raise ValueError("truncated chunk")
if wire[pos + size:pos + size + 2] != b"\r\n":
raise ValueError("chunk delimiter")
body.extend(wire[pos:pos + size])
pos += size + 2The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.