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
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 |
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 |
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
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 |
Check 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.