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

Require a Host header

HTTP/1.1 makes the Host header mandatory - it is how one server tells apart the many sites it may host. Today you reject a 1.1 request that omits it, the first real protocol rule your parser enforces.

The goal

Report an error when an HTTP/1.1 request has no Host header, and accept it when Host is present.

Start here - the target
TO DO
Scenario: Enforcing the Host requirement
Givenan HTTP/1.1 request with headers {"Accept": "*/*"} and no Host
Whenit is validated
Thenit is reported as an error
Andthe same request with "Host: example.com" added validates successfully
Background

HTTP/1.1 introduced virtual hosting: one IP address and port can serve many different websites, and the Host header is how the client says which one it wants. Because of that the spec makes Host mandatory for 1.1 — a request without it is malformed, no matter how well-formed everything else is.

This is your parser’s first semantic rule, as opposed to pure syntax. Checking it now means the server can answer a hostless request with a clean 400 later instead of guessing. Lean on the case-insensitive Get you just built — clients capitalize Host every possible way.

Make it work
if version == "HTTP/1.1" && headers.Get("Host") == "" {
return errors.New("missing Host header")
}
CheckpointDONE
An HTTP/1.1 request without Host is rejected. Commit.