Read Request Body
You know how to send a body, but what if the client sends one to you?
When a client sends data to the server (typically using a POST or PUT request),
they will include a Content-Length header in their request.
According to RFC 9112 Section 6, the length
of the message body is determined by the Content-Length header (unless Transfer-Encoding is present,
which we will cover later).
Your Task
Implement a new route: POST /uppercase.
When this route is called, you must:
- Parse the
Content-Lengthheader from the request. - After you find the
\r\n\r\nboundary separating headers and body, read exactlyContent-Lengthbytes from the socket. - Convert those bytes to uppercase.
- Send them back to the client as the response body (don’t forget your own
Content-Lengthheader in the response!).
Warning
A common mistake is reading the entire socket stream until EOF (End Of File). In HTTP/1.1, connections are kept alive! The client will not close the socket after sending the body. If you read until EOF, your server will hang forever. You MUST read exactly
Content-Lengthbytes and stop!
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 request_body