build-an-http-client / lesson-28.md
Lesson 28 · Chunked bodies and persistent connections

A chunked then a plain response

The chapter's payoff is reading a chunked response and a length-framed response back to back off one connection, exercising every framing rule and the keep-alive alignment at once.

The goal

Read a chunked response then a length-framed response off a single stream in sequence.

Start here - the target
TO DO
Scenario: Mixed framings on one keep-alive stream
Givena stream holding "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n0\r\n\r\n" then "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nbye"
Whentwo requests are sent over one persistent connection to that stream
Thenthe first response body is "hello" (chunked) and the second is "bye" (length-framed)
Andthe chunked decoder consumed exactly through its "0\r\n\r\n" terminator, leaving the stream aligned for the second response
Background

This ties the chapter together. The first response is chunked - its body arrives as 5\r\nhello\r\n0\r\n\r\n and decodes to hello, with the decoder consuming exactly through the 0\r\n\r\n terminator. That precise stop is what leaves the stream sitting at the start of the second response, which is length-framed and reads bye by its Content-Length. Two different framings, one connection, both clean.

No new code is needed today - if the earlier lessons framed each body exactly, this just works, which is the satisfying proof that the framing rules compose. This is the walking skeleton grown into a real HTTP/1.1 transport: it can send requests and read any response the protocol allows, one after another, over a single stream. From here the project turns to the behaviors layered on top - encodings, redirects, and cookies.

Make it work
// no new code - this is the integration test. one persistent
// connection, two round-trips:
// conn := NewConn(stream)
// r1, _ := conn.Do(req1) // chunked body -> "hello"
// r2, _ := conn.Do(req2) // length-framed -> "bye"
// both parse because each body reader stops at its frame boundary.
CheckpointDONE
You can read mixed-framing responses off one persistent stream. The connection layer works - commit and stop here.