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

Parse the Request Line

Right now, you are blindly returning a 200 OK response to every connection. A real web server routes requests based on what the client actually asked for!

To do this, you must parse the very first line of the incoming HTTP request, called the Request Line.

According to RFC 9112 Section 3, the Request Line has the following grammar:

request-line   = method SP request-target SP HTTP-version CRLF

Example: GET /index.html HTTP/1.1\r\n

  • method: The action to be performed (e.g., GET, POST). It tells the server what the client wants to do.
  • request-target: Often called the URI or path (e.g., /index.html), it tells the server which resource the action applies to.

Your Task

You must parse the Request Line of the incoming HTTP request and extract the request-target.

  • If the request target is exactly /, respond with HTTP/1.1 200 OK\r\n\r\n.
  • If the request target is anything else (e.g., /some/random/value), respond with HTTP/1.1 404 Not Found\r\n\r\n.

Tip

To parse the Request Line safely, read the incoming bytes until you find the first \r\n. That entire string is the Request Line. You can then split it by the space character ( ) to extract the method, target, and version.


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 parse_request_line