# HTTP: read a request like a log

Read real requests and responses with curl -v, know what each status code family blames, see where a request spends its time, and how HTTP/1.1, HTTP/2 and HTTP/3 differ.

Course: [Networking for DevOps](https://devops.rajeev.pro/courses/networking), lesson 5. About 25 minutes, beginner. Source: https://devops.rajeev.pro/courses/networking/05-http

## What you will learn

- Read a request and response in curl -v, including the headers that matter
- Tell which side to blame from a status code, including 502 and 504
- Break a slow request into DNS, connect, TLS, wait and download time
- Explain what HTTP/2 and HTTP/3 change

> **Lab status.** **Verified lab:** `curl` against example.com, github.com, httpbin.org and cloudflare.com from a Linux host. The output shown is real. <br />**Cloud lab:** none needed.

Almost everything you deploy speaks HTTP, and every proxy, load balancer and app in between logs it. Being able to
read a request and response quickly is most of debugging a web service.

## Request line, headers, body

> **Figure.** Left, the request: GET / HTTP/1.1, Host example.com, User-Agent curl/8.18.0, Accept, no body. Right, the response: HTTP/1.1 200 OK, Content-Type text/html, Connection keep-alive, Server cloudflare, Age 5275, cf-cache-status HIT, alt-svc h3, then the HTML body.

```text
$ curl -v http://example.com -o /dev/null
> GET / HTTP/1.1
> Host: example.com
> User-Agent: curl/8.18.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/html; charset=utf-8
< Server: cloudflare
< Age: 5275
< cf-cache-status: HIT
```

(Trimmed.) HTTP is plain text over the TCP connection from lesson 3: a request line, headers, a blank line, then an
optional body. `Age: 5275` and `cf-cache-status: HIT` say this page came from Cloudflare's cache, 88 minutes old;
the real server was never asked.

## Methods and status codes

`GET` reads, `POST` creates, `PUT` replaces, `PATCH` changes part, `DELETE` removes. `GET`, `PUT` and `DELETE` are
meant to be safe to retry; `POST` isn't, which is why a load balancer won't retry it for you. A `POST` with a JSON
body, echoed back by httpbin:

```text
$ curl -s -X POST https://httpbin.org/post -H 'Content-Type: application/json' -d '{"order":42}'
{
  "headers": { "Content-Length": "12", "Content-Type": "application/json", ... },
  "method": "POST",
  "json": { "order": 42 },
  ...
}
```

The status code's first digit tells you whose problem it is:

> **Figure.** Four families. 2xx success: 200, 201, 204. 3xx redirect: 301, 302, 307, 304. 4xx client error: 400, 401, 403, 404, 429. 5xx server error: 500, 503, 502 bad gateway, 504 gateway timeout. A note: 502 and 504 come from the proxy; 502 means it got garbage or a reset from your app, 504 means your app never answered in time.

```text
$ curl -sIL http://github.com | grep -iE '^HTTP|^location'
HTTP/1.1 301 Moved Permanently
Location: https://github.com/
HTTP/2 200
```

`curl -L` follows redirects. This one sends plain HTTP to HTTPS, the most common redirect there is.

## Headers that matter in operations

| Header                      | Why you care                                       |
| --------------------------- | -------------------------------------------------- |
| `Host`                      | Picks the site. Wrong Host, wrong site or 404      |
| `Location`                  | Where a 3xx redirect sends you; loops show up here |
| `Cache-Control`, `Age`      | Whether a CDN or browser served a cached copy      |
| `X-Forwarded-For`           | The real client IP, added by proxies (lesson 9)    |
| `Strict-Transport-Security` | Tells browsers to use HTTPS only                   |
| `Retry-After`               | How long to wait after a 429 or 503                |

## Where the time goes

When "the site is slow", break the request into phases. `curl -w` prints each one:

> **Figure.** A timeline for https://github.com: DNS done at 130 ms, TCP connect at 223 ms, TLS at 327 ms, first byte at 431 ms, download complete at 1185 ms. 327 ms are spent before the request is even sent; the server's own work took 104 ms.

```text
$ curl -s -o /dev/null -w 'http %{http_version}  dns %{time_namelookup}s  connect %{time_connect}s  tls %{time_appconnect}s  first byte %{time_starttransfer}s  total %{time_total}s\n' https://github.com
http 2  dns 0.129716s  connect 0.223123s  tls 0.326634s  first byte 0.430652s  total 1.185359s
```

Each number is measured from the start. Slow DNS points at the resolver, slow connect at distance or packet loss,
slow TLS at certificate or handshake issues, and a long gap before the first byte at your app or database.

## HTTP/2 and HTTP/3

> **Figure.** Three stacks. HTTP/1.1: text over optional TLS over TCP, one request at a time. HTTP/2: binary frames multiplexed on one connection over TLS with ALPN h2 over TCP. HTTP/3: the same over QUIC with TLS 1.3 built in, over UDP. The lab saw http 2 for github.com, http 1.1 when forced, and alt-svc h3 from cloudflare.com.

```text
$ curl -s --http1.1 -o /dev/null -w 'http %{http_version}\n' https://github.com
http 1.1
$ curl -sI https://www.cloudflare.com | grep -iE '^HTTP|alt-svc'
HTTP/2 200
alt-svc: h3=":443"; ma=86400
```

Client and server agree on HTTP/2 during the TLS handshake (ALPN). A server offers HTTP/3 with the `alt-svc` header,
and browsers switch to it on later requests. Methods, status codes and headers don't change between versions, so
everything else in this lesson applies to all three.

## Mission: Profile three sites

For three sites you use, record the HTTP version, status code and the five timing phases. Find one that redirects and one served from a CDN cache, and explain which phase dominates for each.

- You need: curl
- You'll have: ~/http.txt

```sh
$ for u in https://github.com https://example.com https://www.cloudflare.com; do
    curl -s -o /dev/null -w "$u  %{http_code}  http %{http_version}  dns %{time_namelookup}  connect %{time_connect}  tls %{time_appconnect}  ttfb %{time_starttransfer}  total %{time_total}\n" $u
  done > ~/http.txt
$ curl -sIL http://github.com | grep -iE '^HTTP|^location' >> ~/http.txt
$ curl -sI https://example.com | grep -iE '^age|cache' >> ~/http.txt
```

Run it twice. The second run is often faster because DNS is cached.

## Try it

- Run `curl -v http://example.com -o /dev/null` and find the request line and the status line.
- Follow a redirect with `curl -sIL http://github.com`.
- Send a JSON POST to httpbin.org and read what the server received.
- Time a request with `curl -w` and work out its time to first byte.
- Do the mini mission and keep `~/http.txt`.

## Quiz

1. A load balancer returns 502 Bad Gateway. Where do you look first?
   - The user's browser
   - **The app behind the load balancer, which crashed, reset the connection or listens on the wrong port** (answer)
   - DNS
   - The TLS certificate
   Why: The proxy reached your app and got a bad or no response. 504 means it waited and gave up.

2. Which header lets one IP address serve many websites?
   - User-Agent
   - **Host** (answer)
   - Accept
   - Server
   Why: The server picks the site from the Host header (and, for HTTPS, from SNI during the TLS handshake).

3. curl shows connect at 223 ms, TLS done at 327 ms and first byte at 431 ms. How long did the server take to answer?
   - **About 104 ms** (answer)
   - 431 ms
   - 223 ms
   - 1185 ms
   Why: Time to first byte minus the end of the TLS handshake is the wait for the server's own work.

## Resources

- [HTTP overview](https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview) (MDN)
- [HTTP response status codes](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) (MDN)
- [Everything curl](https://everything.curl.dev/) (curl)
- [httpbin, an HTTP request and response service](https://httpbin.org/) (httpbin.org)
