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

Wire chunked into the response

The response parser has to choose how to read the body based on the headers. Today you make it pick chunked decoding when Transfer-Encoding says so, and length framing otherwise.

The goal

Dispatch body reading to chunked decoding when Transfer-Encoding is chunked, else use Content-Length.

Start here - the target
TO DO
Scenario: Choosing the body framing from the headers
Givena response with header Transfer-Encoding "chunked" and body bytes "3\r\nabc\r\n0\r\n\r\n"
Whenits body is read
Thenthe body is "abc" (decoded as chunked)
Anda response with Content-Length "3" and body "abc" is still read by length - the header decides which framing applies
Background

You now have three ways to read a body - by length, to end of stream, and chunked - and the response’s headers decide which one applies. The rule the parser follows, in order: if Transfer-Encoding: chunked is present, decode the body as chunks; otherwise if Content-Length is present, read that many bytes; otherwise read to the end of the stream. Chunked takes precedence when both somehow appear.

This is the dispatch that makes Do handle any real response. It reuses the chunked decoder from the last two lessons and the length and read-to-end readers from chapter three - the parser just picks the right one by inspecting the headers. With framing fully general, the client can read whatever a server sends, which is the precondition for reading several responses off one connection.

Make it work
// when reading the body, check the headers IN ORDER:
// 1. Transfer-Encoding: chunked -> decodeChunked
// 2. Content-Length present -> readN(length)
// 3. otherwise -> readAll (to EOF)
// chunked takes priority over Content-Length when both appear.
CheckpointDONE
The response parser selects chunked or length framing from the headers. Commit and stop here.