Part 4 · The web and everyday protocols

How HTTP works: HTTP/1.1, HTTP/2, HTTP/3 and QUIC

lesson 09 / 1918 min1 labpublished updated
Course contents
On this page
  1. Anatomy of a request and a response
  2. Proxies, reverse proxies and CDNs in the HTTP path
  3. Methods
  4. Status codes
  5. Important headers
  6. CORS
  7. HTTP/1.1: text, one request at a time
  8. HTTP/2: one connection, parallel streams
  9. QUIC and HTTP/3
  10. WebSocket, SSE and gRPC

In lesson 1 you saw the shortest possible HTTP conversation: the browser says GET / and the server answers 200 OK with a page. HTTP is exactly that, a request/response protocol: the client sends one request, the server sends back one response. It is also stateless: each request stands on its own, and the server doesn’t remember the previous one. Anything that needs memory, like being logged in, is added on top, usually with cookies.

This lesson reads requests and responses in detail, explains methods, status codes and the headers that matter, then shows how HTTP/1.1, HTTP/2 and HTTP/3 carry the same requests in different ways over TCP, TLS and QUIC.

Since 2022, the HTTP standard has been split into two parts: the semantics (methods, status codes, headers; RFC 9110), which are the same in every version, and the wire format, which differs between HTTP/1.1 (RFC 9112), HTTP/2 (RFC 9113) and HTTP/3 (RFC 9114). Think of the semantics as the interface and each version as a different implementation of it. The semantics come first here, then the three versions.

Anatomy of a request and a response

GET /api/orders?limit=10 HTTP/1.1
Host: shop.example.com
User-Agent: curl/8.9.1
Accept: application/json
Authorization: Bearer eyJhbGciOi...

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 27
Cache-Control: private, max-age=60
ETag: "a1b2c3"

{"orders":[],"next":null}
  • The first line of a request holds the method, the request target and the version. Then come the headers, an empty line (\r\n\r\n) and an optional body.
  • The Host header is mandatory in HTTP/1.1. It is what lets hundreds of sites share one IP (virtual hosting).
  • Where the body ends is set either by Content-Length or by Transfer-Encoding: chunked (length-prefixed chunks, for streaming).
# HTTP/1.1 is just text. Type a request by hand and read the raw response.
# (local server here, so the bytes are exactly what the server sends)
python3 -m http.server 8001 &
printf 'GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n' | nc 127.0.0.1 8001
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.13.16
Date: Fri, 16 Oct 2026 10:29:49 GMT
Content-type: text/html; charset=utf-8
Content-Length: 776

<!DOCTYPE HTML>
...

That is the whole protocol: a request line, a few headers, an empty line, then the body. You just sent an HTTP request by hand.

Proxies, reverse proxies and CDNs in the HTTP path

A request often doesn’t go straight from the browser to the program that answers it. On the way there can be a proxy: a server that receives HTTP requests and passes them on to another server, the backend or upstream, then passes the response back. Several of the status codes and headers below only make sense with that picture in mind:

  • Forward proxy: acts on behalf of the client (a corporate proxy). For HTTPS, the client asks the proxy for a raw TCP tunnel with the CONNECT host:443 method.
  • Reverse proxy: acts on behalf of the server (NGINX, Envoy, HAProxy, Traefik, a CDN): TLS termination, routing, caching, compression, rate limiting.
  • The real path of a production request often looks like Client ⇢ CDN ⇢ Load Balancer ⇢ Reverse Proxy or Ingress ⇢ Service ⇢ App, and every layer has its own timeouts, headers and logs.

Methods

MethodPurposeSafeIdempotent
GETReadYesYes
HEADLike GET, without the bodyYesYes
OPTIONSCapabilities; the CORS preflightYesYes
POSTCreate, or any custom actionNoNo
PUTReplace the whole resourceNoYes
PATCHChange part of the resourceNoNo (by default)
DELETEDeleteNoYes

Idempotent means that repeating a request has the same result as sending it once. On a network this is vital: when a request times out, you don’t know whether it ran or not, and idempotent requests can be retried safely. For POST, use an idempotency key (a header carrying a unique ID) so the server can recognize repeats.

Status codes

ClassMeaningImportant codes
1xxInformational101 Switching Protocols (WebSocket), 103 Early Hints
2xxSuccess200 OK, 201 Created, 204 No Content, 206 Partial Content
3xxRedirection301, 308 permanent; 302, 307 temporary (307 and 308 keep the method); 304 Not Modified
4xxClient error400, 401 (authentication required), 403 (not allowed), 404, 405, 409 Conflict, 413, 422, 429 Too Many Requests
5xxServer error500, 502 Bad Gateway (proxy got a bad reply from the backend), 503 Service Unavailable, 504 Gateway Timeout (proxy got no reply from the backend)

Important headers

  • Content negotiation: Accept, Accept-Encoding (gzip, br, zstd) and Accept-Language; in the response, Content-Type, Content-Encoding and Vary.
  • Caching:
    • Cache-Control: max-age=3600, public (any cache, for up to an hour); private (only the browser); no-cache (check with the server before using it); no-store (don’t store it at all).
    • Validation: the server sends an ETag (or Last-Modified). Later, the client sends If-None-Match, and if nothing has changed, the server returns 304 Not Modified with no body.
    • Vary: Accept-Encoding tells caches to keep separate copies based on that header.
    • For assets: put a hash of the content in the file name (app.3f9a1c.js) and send max-age=31536000, immutable.
  • Cookies: Set-Cookie: sid=...; Secure; HttpOnly; SameSite=Lax; Path=/; Max-Age=3600. HttpOnly hides the cookie from JavaScript, so a script an attacker manages to inject into your page (XSS, cross-site scripting) can’t steal it; Secure sends the cookie only over HTTPS; and SameSite stops other websites from making your browser send requests with your cookie attached (CSRF, cross-site request forgery).
  • Proxy headers: X-Forwarded-For (the client’s original IP, as the proxies saw it), X-Forwarded-Proto, and the newer standard Forwarded. Trust only the values your own proxies added; a client can write anything there.
  • Security: Strict-Transport-Security (HSTS: always use HTTPS), Content-Security-Policy, X-Content-Type-Options: nosniff.

CORS

By default, a browser doesn’t let JavaScript on app.example.com read the response to a request it made to api.other.com. This is the same-origin policy, where an origin is scheme + host + port. CORS is how a server grants permission anyway:

  • For “simple” requests, the server puts Access-Control-Allow-Origin: https://app.example.com in the response.
  • For other requests (for example, ones with Authorization or Content-Type: application/json), the browser first sends a preflight request with OPTIONS, and the server must answer with Access-Control-Allow-Methods, Access-Control-Allow-Headers and possibly Access-Control-Max-Age.
  • With cookies or other credentials, the value * is not allowed, and Access-Control-Allow-Credentials: true is required.
  • CORS is a browser mechanism; curl and servers ignore it. CORS is not your API’s security. Authentication is.

HTTP/1.1: text, one request at a time

  • Persistent connections (keep-alive) are the default: several requests in a row over one TCP connection.
  • But each connection has only one request in flight at a time (pipelining was never used in practice). One slow response makes all the others wait: head-of-line blocking at the HTTP level.
  • To compensate, browsers open up to 6 parallel connections to each host, and each one goes through its own handshake and slow start.
  • Headers are text and repetitive (large cookies on every request).

HTTP/2: one connection, parallel streams

HTTP/1.1HTTP/1.1 (text)TLSTCPIPHTTP/2HTTP/2 (frames, streams)TLSTCPIPHTTP/3HTTP/3 (QPACK)QUIC: streams + TLS 1.3UDPIPHTTP/3 moves streams and encryption into QUIC, over UDP
The protocol stacks of HTTP/1.1, HTTP/2 and HTTP/3.
  • Binary framing: messages are split into binary frames (HEADERS, DATA, SETTINGS, WINDOW_UPDATE, RST_STREAM, GOAWAY and others).
  • Multiplexing: dozens or hundreds of concurrent streams over one TCP connection. Responses arrive interleaved, without waiting for each other.
  • HPACK: header compression with static and dynamic tables. A repeated header shrinks to a small index.
  • Per-stream flow control, and prioritization.
  • Server push effectively failed, and browsers removed support for it. Its replacement is 103 Early Hints.
  • In practice it always runs over TLS, and the version is agreed during the TLS handshake, through an extension called ALPN (the next lesson shows it).
  • The remaining problem: head-of-line blocking at the TCP level. A single lost packet stalls every stream, because TCP knows only one ordered stream.
  • gRPC is built on HTTP/2.

QUIC and HTTP/3

QUIC (RFC 9000) is a new transport protocol built on top of UDP and implemented in user space (inside the application, not the kernel):

  • Independent streams at the transport level: a lost packet stalls only its own stream. Head-of-line blocking is gone.
  • TLS 1.3 built into the protocol: the transport handshake and the encryption handshake are one and the same. A new connection takes one RTT (versus 2 to 3 RTTs for TCP + TLS), and a repeat connection can use 0-RTT, sending the request in the very first packet. The catch: an attacker could replay that packet, so 0-RTT is only safe for idempotent requests (ones that are safe to repeat, like GET; see the methods table).
  • Almost everything is encrypted, even a large part of QUIC’s own header. Middleboxes (devices in the middle of the path, such as firewalls and NAT routers) can’t read or tamper with it, so the protocol is free to evolve without breaking them.
  • Connection migration: a connection is identified by a connection ID, not by the 5-tuple, so moving from Wi-Fi to 4G doesn’t break it.
  • Congestion control and retransmission live inside QUIC and can be swapped out.
  • HTTP/3 is the same HTTP semantics over QUIC, with QPACK instead of HPACK.
  • Discovery: the server first announces over TCP that it supports HTTP/3, either with an Alt-Svc: h3=":443" header or with a DNS record of type HTTPS. Next time, the browser tries QUIC, and if UDP is blocked, it falls back to HTTP/2.
curl -sI --http2 https://cloudflare.com | head -1
curl -sI --http3 https://cloudflare.com | head -1      # needs a curl built with HTTP/3 support
curl -sI https://cloudflare.com | grep -i alt-svc      # the server advertising HTTP/3
HTTP/2 200
HTTP/3 200
alt-svc: h3=":443"; ma=86400

The line alt-svc: h3=":443" is the server’s announcement: “I also speak HTTP/3, on port 443 over UDP.” The browser notes it and tries QUIC on the next request; if UDP is blocked, it falls back to HTTP/2.

WebSocket, SSE and gRPC

  • WebSocket (RFC 6455) starts as an HTTP/1.1 request with Upgrade: websocket (answered with 101 Switching Protocols) and turns into a long-lived, two-way channel of frames. Load balancers must support the upgrade and long idle timeouts. To get through NATs and proxies, send periodic ping/pong frames.
  • Server-Sent Events: one long response with text/event-stream. It only goes from server to client, but it is simple and runs over plain HTTP.
  • gRPC: RPC with Protocol Buffers over HTTP/2, with bidirectional streaming, deadlines and its own status codes. Because it is HTTP/2, the load balancer must spread traffic at L7 (per request), not L4 (per connection); otherwise every request lands on one backend.