Chunked Encoding (Read)
You know how to read a request body using Content-Length. But what if a client wants
to stream data to you, and they don’t know the final size before they start sending?
In HTTP/1.1, this is solved using Chunked Transfer Encoding (RFC 9112 Section 7.1).
When a request contains the header Transfer-Encoding: chunked, the Content-Length header is omitted.
Instead, the body is sent in a series of chunks.
Each chunk follows this strict ABNF grammar:
chunk = chunk-size [ chunk-ext ] CRLF chunk-data CRLF
chunk-size = 1*HEXDIG
chunk-size: A hexadecimal number indicating the size of the following chunk data.[ chunk-ext ]: Optional extensions that you can safely ignore for this course.chunk-data: The actual payload bytes of the specified size.
Example of a chunked body:
4\r\n (Chunk size: 4 bytes in hex)
Wiki\r\n (Chunk data: 4 bytes)
5\r\n (Chunk size: 5 bytes in hex)
pedia\r\n (Chunk data: 5 bytes)
0\r\n (Final zero-length chunk terminating the stream)
\r\n (End of chunked body)
Your Task
Create a new /lowercase route to support Transfer-Encoding: chunked.
If the client sends a chunked request, you must parse the chunk sizes (hexadecimal!),
read exactly that many bytes of chunk data, skip the \r\n, and repeat until you encounter
a chunk of size 0.
Finally, combine all the chunk data, lowercase it, and return it in the response (you can
return it with a standard Content-Length response for now).
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_read