Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chunked Encoding (Write)

Servers also need to send chunked responses when they generate data dynamically, or stream large files, and don’t know the final Content-Length in advance.

Imagine you are streaming a video, or returning rows from a slow database query. Instead of waiting 10 seconds to calculate the total length and buffering it all in memory, you can send the data in chunks as soon as it’s ready!

Your Task

Create a new route: GET /stream.

When a client requests this endpoint, respond with Transfer-Encoding: chunked (do NOT include a Content-Length header).

You must send the body in at least two separate chunks, followed by the zero-length terminating chunk (0\r\n\r\n).

The combined payload of your chunks must EXACTLY match this quote by Tim Berners-Lee: The Web is more a social creation than a technical one.

Tip

Make sure you format the chunk sizes as Hexadecimal strings! 10 bytes should be a\r\n, not 10\r\n.

Reference: RFC 9112 Section 7.1


Important

Run the below docker command to test your solution.

docker run \
  --rm \
  --add-host host.docker.internal:host-gateway \
  codeberg.org/level0/buildit/http-server:latest \
  --addr host.docker.internal:8080 \
  --until chunked_encoding_write