The response says HELLO. Your header reader finds the blank line, returns successfully, and the body reader gets nothing. Congratulations: the parser ate lunch before handing over the plate.
The mistake is treating one received chunk as one protocol unit. A chunk can contain the end of the headers and the beginning of the body. The delimiter tells the parser where to stop consuming; it does not authorize discarding the rest.
Truss’s newly published HTTP-client contract makes that ownership rule explicit: retain the consumed prefix and preserve the suffix for the next step. [1] I used that rule for a small Python exercise. This is a delimiter tutorial inspired by the source, not a Truss installation or proof-verifier run.
Keep the unread suffix in the return value
HTTP/1.1 places an empty line between the header section and the optional message body. In the simple byte form used here, the end marker is CRLF CRLF, written b'\r\n\r\n'. [3]
For my fixture, the header is 38 bytes and the body is five bytes. The splitter returns the complete header, including its final delimiter, plus any suffix from the current chunk. Future chunks remain the caller’s responsibility.
Original byte-ownership diagram for the synthetic response below. Image by Shivam Dwivedi.
Save this as split_head.py and run python3 split_head.py. It uses only the standard library. The byte-by-byte loop favors a visible ownership rule over an optimized search routine.
class HeadSplitter:
def __init__(self, limit=128):
self.limit, self.buffer, self.done = limit, bytearray(), False
def feed(self, chunk):
if self.done:
raise ValueError('head already returned')
for index, byte in enumerate(chunk):
if len(self.buffer) >= self.limit:
raise ValueError('head too large')
self.buffer.append(byte)
if self.buffer.endswith(b'\r\n\r\n'):
self.done = True
return bytes(self.buffer), chunk[index+1:]
return None
head = b'HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n'
body = b'HELLO'
wire = head + body
checked = 0
for cut in range(len(wire)+1):
parser, result, unread = HeadSplitter(len(head)), None, b''
for chunk in [wire[:cut], b'', wire[cut:]]:
if result is not None:
unread += chunk
else:
parsed = parser.feed(chunk)
if parsed is not None:
result, unread = parsed
assert result == head and unread == body
assert result + unread == wire
checked += 1
parser = HeadSplitter(len(head))
for byte in head:
last = parser.feed(bytes([byte]))
assert last == (head, b'')
try:
HeadSplitter(len(head)-1).feed(wire)
except ValueError:
pass
else:
raise AssertionError('oversized head accepted')
partial = HeadSplitter()
assert partial.feed(b'HTTP/1.1 200 OK\r\n') is None
print('head bytes:', len(head), 'body bytes:', len(body))
print('two-part schedules:', checked)
print('bytewise delimiter: matched')
print('short limit: rejected; incomplete head: waiting')
head bytes: 38 body bytes: 5
two-part schedules: 44
bytewise delimiter: matched
short limit: rejected; incomplete head: waiting
The key line is chunk[index+1:]. The delimiter has been consumed, so the next byte belongs to the next parsing stage. Returning it makes that handover explicit.
The limit applies to the accumulated head, including the delimiter. A head that ends exactly at the limit is accepted. A body arriving in the same chunk does not count against that head limit, because the method returns before appending it.
Test the receive schedule, not just the response
The wire fixture has 43 bytes. There are 44 ways to divide it into a prefix and suffix, including an empty first or last part. The loop checks every one, with an additional empty offer between the two parts. Each schedule must produce the same header and the same unread body.
The separate bytewise check forces the delimiter across successive calls. The short-limit case checks rejection, and an unfinished head remains waiting for more bytes. In a real connection, EOF while still waiting would need its own truncation verdict.
A common broken implementation searches only inside the latest chunk. It misses a delimiter split across receives. Another joins the chunks, finds the delimiter, then clears the whole buffer. It finds the boundary correctly and still loses the body. The conservation assertion catches that second mistake:
returned_head + unread_bytes == original_wire
This finite test covers every two-part split of one response and one bytewise schedule. It does not cover every partition, every malformed input or every receive history.
Stop at the boundary this exercise actually owns
Finding the header delimiter is only the first stage of HTTP framing. A real client must interpret the response status, request method and framing fields, then handle fixed-length, chunked or close-delimited bodies as appropriate. Informational responses can introduce another head before the final response. [3]
Truss’s own client source covers those wider cases and exposes separate budgets. Its documented proof fixtures establish selected profiles while leaving agreement for all live receive histories open. [1] Its checked-in metadata tests include mutations that reset carried state or corrupt a reported boundary; those are useful negative controls to inspect. [2] I inspected them without running the compiler or those tests.
The Python object is deliberately narrower. It does not validate HTTP field syntax, resolve conflicting length metadata, handle a body or perform network I/O. Keep it as a local teaching exercise, rather than putting it in front of untrusted traffic.
What does transfer is the return contract: report exactly what this stage consumed and preserve everything it did not. A parser can recognize a boundary perfectly and still be wrong about who owns the next byte.
References
Bret Taylor and Truss contributors, HTTP-client framing contract.
Truss contributors, metadata-line checks and negative controls.
IETF, RFC 9112: HTTP/1.1 message format and message-body length.
Asset credits
Original diagram and teaching implementation, CC BY 4.0. Credit: Shivam Dwivedi. Upstream code was inspected and linked, not copied into the example.
AI disclosure
AI was used to draft the article.

Top comments (0)