Handbooks / Computer Networks / Chapter 5
HTTP and TLS
7 pages · ~5 min✓ Reviewed
Builds on DNS.
HTTP Mental Model
1. Stateless request/response
- Text-based: client sends one request, server returns one response
- Server keeps no memory between requests
- Stateless is why cookies and tokens exist
2. Message shape
| Request | Response | |
|---|---|---|
| Start line | GET /path HTTP/1.1 | HTTP/1.1 200 OK |
| Next | headers | headers |
| Then | blank line | blank line |
| End | optional body | optional body |
http
GET /users?id=7 HTTP/1.1 Host: example.com Accept: application/json
a request with no body
3. URL and ports
- https://scheme
- example.comhost
- :443port
- /userspath
- ?id=7query
- #topfragment
- Default ports: HTTP 80, HTTPS 443
Fragment
The #fragment is never sent to the server.
Methods and Status Codes
1. Methods
| Method | Use | Idempotent |
|---|---|---|
GET | reads | yes |
POST | creates or triggers | no |
PUT | replaces whole resource | yes |
PATCH | changes part | no |
DELETE | removes | yes |
- Safe (no state change):
GET,HEAD,OPTIONS - Idempotent: repeating gives the same result
POSTis neither safe nor idempotent
2. Status Codes
- 1xxinfo
- 2xxsuccess
- 3xxredirect
- 4xxclient
- 5xxserver
| Code | Meaning |
|---|---|
200 | OK |
201 | Created |
204 | No Content |
301 / 302 | permanent / temporary redirect |
304 | Not Modified |
3. Errors
400bad request401unauthenticated403forbidden404not found429rate limited500server bug502bad gateway503unavailable504gateway timeout
401 vs 403
401 = who are you? 403 = you are known, but not allowed.
Headers and Cookies
1. Headers
| Request | Job |
|---|---|
| Host | required in HTTP/1.1 |
| User-Agent | client software |
| Accept | formats wanted |
Authorization: Bearer <token> | credentials |
| Content-Type | body format, application/json |
| Response | Job |
|---|---|
| Content-Type | body format |
| Content-Length | body size |
| Location | redirect target (3xx) |
Cache-Control: max-age=3600 | cache for 1 hour |
| ETag | validator for revalidation |
- ETagserver sends
- If-None-Matchclient revalidates
- 304reuse cached copy
2. Cookies
- Server sends Set-Cookie; browser returns Cookie on every matching request
http
Set-Cookie: id=abc; HttpOnly; Secure; SameSite=Lax; Max-Age=3600
Cookie: id=abc- HttpOnly: hidden from JavaScript
- Secure: HTTPS only
- SameSite=Lax/Strict: limits cross-site sending, defends against CSRF
Max-Age/Expires: lifetime;Path/Domain: scope
Common mistakes
Session token in a cookie without HttpOnly + Secure + SameSite, or credentials in a URL query string.
HTTP/1.1 vs HTTP/2 vs HTTP/3
1. HTTP/1.1
- Text protocol over TCP
- One request at a time per connection: head-of-line blocking
- Browsers open ~6 parallel connections per host
- Keep-alive reuses a connection
2. HTTP/2 and HTTP/3
| 1.1 | 2 | 3 | |
|---|---|---|---|
| Runs on | TCP | TCP | QUIC on UDP |
| Format | text | binary frames | binary frames |
| Requests | serial | multiplexed | multiplexed |
| HOL blocking | per connection | TCP level | own stream only |
- HTTP/2: many streams on one connection, HPACK header compression
- HTTP/3: TLS 1.3 built in, faster handshake, connection migration
3. Negotiation and Trade-off
- TLS handshakeALPN picks h2
- ResponseAlt-Svc: h3
- QUIC connectionHTTP/3 next
- Semantics unchanged: methods, status codes, headers
- 1.1 needs bundling and domain sharding for speed
- HTTP/2 and HTTP/3 make both unnecessary
Remember
HTTP/2 stays stuck on TCP-level blocking; only HTTP/3 on QUIC isolates packet loss per stream.
HTTPS, TLS Handshake and Certificates
1. What HTTPS Gives
- HTTPS = HTTP inside TLS, on port 443
- Encryption: nobody on the path can read the data
- Integrity: tampering is detected
- Server authentication: you reach the real domain
2. TLS 1.3 Handshake
- ClientHelloversions, ciphers, key share
- ServerHello+ certificate + key share
- Derive keysboth sides
- Finisheddata flows
- Takes 1 round trip
- ECDHE key exchange sets up the session
- AES-GCM or ChaCha20 then carries the data fast
- Ephemeral keys give forward secrecy
3. Certificates
- A certificate binds a domain name to a public key
- A certificate authority (CA) signs it
Browser validates up to its root store
- 1Leafyour domain
- 2Intermediatesigns leaf
- 3Roottrusted store
| Check | Verifies |
|---|---|
| Signature chain | each cert signed by the next, ends at a trusted root |
| Hostname (SAN) | domain is listed in the SAN |
| Validity dates | today is inside the dates |
| Revocation | OCSP or CRL says not revoked |
Let's Encrypt
Issues free certificates valid 90 days, so automate renewal.
HTTP Cheat Sheet
1. curl essentials
-ishows response headers,-vshows the TLS handshake and headers-Lfollows redirects,--http2/--http3pick the protocol
bash
curl -i URL curl -X POST -H 'Content-Type: application/json' -d '{"a":1}' URL curl -v URL curl -L URL curl --http2 URL # or --http3
2. Methods and status
| Method | Meaning | Idempotent |
|---|---|---|
| GET | read | yes |
| POST | create | no |
| PUT | replace | yes |
| PATCH | partial update | no |
| DELETE | remove | yes |
- 2xx ok, 3xx go elsewhere, 4xx your fault, 5xx server's fault
- 401 = who are you? 403 = not allowed
3. Protocols, cookies, TLS
| Version | Transport | Style |
|---|---|---|
| 1.1 | TCP | serial |
| 2 | TCP | multiplexed |
| 3 | QUIC / UDP | multiplexed |
- Cookie flags to always set: HttpOnly; Secure; SameSite=Lax
s_clientshows the cert chain; failures: expired cert, hostname mismatch, missing intermediate, untrusted CA
bash
openssl s_client -connect host:443 -servername hostCheck yourself
A user sends a valid token, but GET /admin fails. Is it 401 or 403?
- 403: the server knows who you are, but you aren't allowed.
- 401 means missing or invalid credentials.
Set-Cookie: sid=abc; Secure; SameSite=Lax. Can an injected script read it with document.cookie?
- Yes:
HttpOnlyis missing. SecureandSameSitedon't hide a cookie from JavaScript.
One packet is lost on an HTTP/2 connection carrying 10 streams. What stalls?
- All 10 streams: TCP delivers bytes in order, so everything waits for the retransmit.
- HTTP/3 over QUIC stalls only the stream that lost the packet.
The same PUT is sent three times, then the same POST three times. What differs?
- PUT is idempotent: the state after three calls equals the state after one.
- POST may create three resources, so it is not safe to retry blindly.