A Request by Hand
Typing the protocol into a socket, byte for byte.
Everything curl did in chapter 1, typed by hand.
The request line
Method, path, version — then headers, then a blank line:
GET / HTTP/1.1
Host: localhost
Connection: closeTo send it, printf one argument per line and pipe it into nc:
printf '%s\r\n' \
'GET / HTTP/1.1' \
'Host: localhost' \
'Connection: close' \
'' | nc localhost 8080 | head -1The path is what follows the host, starting with /. It is never the full URL:
GET /nope HTTP/1.1
Host: localhost
Connection: closeAn unknown method reaches the server and is rejected by it, not by the syntax:
BREW / HTTP/1.1
Host: localhost
Connection: closeA version the server does not speak gets its own code:
GET / HTTP/9.9
Host: localhost
Connection: closeCRLF, not LF
Every line ends with \r\n. The line endings are the one thing the protocol block cannot
show, and nginx accepts bare \n anyway:
printf '%s\n' \
'GET / HTTP/1.1' \
'Host: localhost' \
'Connection: close' \
'' | nc localhost 8080 | head -1The blank line
Headers end with an empty line. Without it the server is still waiting:
GET / HTTP/1.1
Host: localhost
Connection: closeNothing came back — not an error, just silence until the timeout. The blank line is how the server knows you have finished asking.
Host is mandatory
HTTP/1.1 requires it. Omitting it is a client error:
GET / HTTP/1.1
Connection: closeHTTP/1.0 predates virtual hosting and does not need one:
GET / HTTP/1.0Header names are case-insensitive, so this is the same request:
GET / HTTP/1.1
hOsT: localhost
connection: closeThe status line
Version, code, reason phrase. The path decides which:
GET /50x.html HTTP/1.1
Host: localhost
Connection: closeResponse headers
nginx sends eight without being asked:
GET / HTTP/1.1
Host: localhost
Connection: closeContent-Length frames the body, ETag and Last-Modified drive caching in chapter 6,
and Accept-Ranges offers partial downloads.
Connection: close
Without it, the server keeps the socket open for another request:
GET / HTTP/1.1
Host: localhostAsk it to hang up and it says so:
GET / HTTP/1.1
Host: localhost
Connection: closeChapter 7 reuses that open socket for a second request.
Sending a body
Content-Length says how many bytes follow the blank line:
POST / HTTP/1.1
Host: localhost
Content-Length: 11
Connection: close
hello=worldIn the shell the body is a second printf, after the one that writes the headers:
{ printf '%s\r\n' \
'POST / HTTP/1.1' \
'Host: localhost' \
'Content-Length: 11' \
'Connection: close' \
''
printf 'hello=world'
} | nc localhost 8080 | head -1405 is the right answer: nginx serves static files and has nothing to do with a POST.
The body was still read — the path exists, the method is refused:
POST /nope HTTP/1.1
Host: localhost
Content-Length: 0
Connection: closeGetting it wrong
A request line that is not three parts:
GARBAGE400 means the bytes were unparseable. Everything from 405 upward means the server
understood you and declined.