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

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:

  1. Parse the Content-Length header from the request.
  2. After you find the \r\n\r\n boundary separating headers and body, read exactly Content-Length bytes from the socket.
  3. Convert those bytes to uppercase.
  4. Send them back to the client as the response body (don’t forget your own Content-Length header 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-Length bytes 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