build-an-http-server / lesson-17.md
Lesson 17 · Parsing requests

Serve the parsed request

Your server still replies with hard-coded bytes - today you feed it the real parser so the handler sees a Request and its response reflects what was actually asked for.

The goal

Wire the request parser into the connection loop so the response echoes the request's method and path.

Start here - the target
TO DO
Scenario: The server responds based on the parsed request
Giventhe server receives "GET /hello HTTP/1.1\r\nHost: example.com\r\n\r\n"
Whenit parses the request and responds
Thenthe response is "HTTP/1.1 200 OK\r\n\r\nGET /hello"
Background

The canned response from chapter one gives way to the real parser. Instead of ignoring the request, handle now turns it into a Request and builds its reply from actual fields — here, echoing the method and decoded path. The server has gone from “answers everything identically” to “answers based on what was asked.”

That is the whole point of the parsing chapter proved end to end: curl a path and watch it come back. The response is still assembled by hand, which is exactly what the next chapter replaces — a proper status line, headers, and body built from a Response.

Make it work
func handle(conn net.Conn) {
defer conn.Close()
req, _ := parseRequest(bufio.NewReader(conn))
body := req.Method + " " + req.Path
conn.Write([]byte("HTTP/1.1 200 OK\r\n\r\n" + body))
}
CheckpointDONE
curl http://localhost:PORT/hello now returns "GET /hello" - the server sees a real parsed request. Commit.