Connections
Keep-alive, head-of-line blocking, and the socket underneath.
A TCP connection is expensive to open. HTTP/1.1 keeps it.
One connection, many requests
Chapter 2 sent Connection: close every time. Leave it out:
GET / HTTP/1.1
Host: localhostContent-Length makes this possible — the client counts 615 bytes and knows the response
ended without a close.
Two requests on one socket
Write both, read both. The responses come back in order:
{ printf '%s\r\n' 'GET / HTTP/1.1' 'Host: localhost' ''
printf '%s\r\n' 'GET /50x.html HTTP/1.1' 'Host: localhost' \
'Connection: close' ''
} | nc -w 2 localhost 8080 | grep -E '^HTTP|^Content-Length'Two files, 615 bytes and 497, one connection. The second request was sent before the first response arrived.
Three works the same, and the statuses need not match:
{ printf '%s\r\n' 'GET / HTTP/1.1' 'Host: localhost' ''
printf '%s\r\n' 'GET /50x.html HTTP/1.1' 'Host: localhost' ''
printf '%s\r\n' 'GET /nope HTTP/1.1' 'Host: localhost' 'Connection: close' ''
} | nc -w 2 localhost 8080 | grep '^HTTP'Close on the first request and the second is never answered:
{ printf '%s\r\n' 'GET / HTTP/1.1' 'Host: localhost' \
'Connection: close' ''
printf '%s\r\n' 'GET /50x.html HTTP/1.1' 'Host: localhost' \
'Connection: close' ''
} | nc -w 2 localhost 8080 | grep -c '^HTTP'HTTP/1.0 and keep-alive
The default flipped between versions. 1.0 closes unless asked:
printf '%s\r\n' 'GET / HTTP/1.0' '' \
| nc localhost 8080 | grep -iE '^(HTTP|connection)'Asking changes the answer:
printf '%s\r\n' 'GET / HTTP/1.0' 'Connection: keep-alive' '' \
| nc -w 2 localhost 8080 | grep -iE '^(HTTP|connection)'Who closes it
Either side, any time. nginx waits 65 seconds by default:
keepalive_timeout 65;curl reuses connections
Give it two URLs and it opens one connection:
curl -sv localhost:8080/ localhost:8080/50x.html \
-o /dev/null -o /dev/null 2>&1 | grep -E 'Connected to|Re-using'One Connected to, one Re-using. curl will also count them:
curl -s -o /dev/null -o /dev/null -w 'connects=%{num_connects}\n' \
localhost:8080/ localhost:8080/50x.htmlThe second URL reports 0 new connections. A client object is meant to be shared, not
created per request.
Head-of-line blocking
Responses come back in request order. Swap the requests and the lengths swap:
{ printf '%s\r\n' 'GET /50x.html HTTP/1.1' 'Host: localhost' ''
printf '%s\r\n' 'GET / HTTP/1.1' 'Host: localhost' 'Connection: close' ''
} | nc -w 2 localhost 8080 | grep '^Content-Length'That is head-of-line blocking — the problem HTTP/2 solves, in chapter 12.
Pipelining
Sending request two before response one is what the snippets above did. HTTP/1.1 allows it; nobody deployed it:
| Problem | Why it killed pipelining |
|---|---|
| Ordering is still fixed | one slow response blocks the rest |
| Proxies mishandled it | responses returned out of order, or dropped |
| No way to cancel one | the whole connection has to go |
Browsers shipped it disabled, then removed it. It works above because nothing sits between the two ends.
Parallel connections
The workaround was more sockets — six per host, so six handshakes and six TLS negotiations.
That is why bundling mattered before HTTP/2: the seventh request waits.
Ports
The port decides where to connect, nothing else:
GET / HTTP/1.1
Host: localhost:8080
Connection: close