HTTP, HTTPS and the TLS handshake
HTTP is the request/response protocol of the web; HTTPS is the same HTTP running inside a TLS encryption layer. Knowing methods, status codes, the TLS 1.3 handshake, SNI/ALPN and HTTP/1.1 vs 2 vs 3 is core interview material for any web or network role.
132.1 Why is it used?
- HTTP carries every web page and API call: the browser sends a request, the server returns a status code, headers and a body.
- Plain HTTP is plain text: on a cafe Wi-Fi the router (or any device on the path) can read even a login like user=asha&password=lab132.
- HTTPS = HTTP + TLS: encrypts the traffic and proves the server's identity with a certificate.
- TLS 1.3 finishes the handshake in 1 RTT, so security costs very little latency.
132.2 HTTP request and response
- Step 1
1.Browser 192.168.1.10 builds GET / HTTP/1.1, Host: shop.lab
- Step 2
2.Request goes to the cafe router 192.168.1.1, then across the internet
- Step 3
3.shop.lab web server (:80) replies HTTP/1.1 200 OK + headers + body
- Step 4
4.POST /login carries user=asha&password=lab132 in plain text
- Step 5
5.The router can read it: password visible!
This is the problem HTTPS solves
| Method / code | Meaning |
|---|---|
| GET / HEAD | Get data / get only the headers |
| POST | Send new data: order, login, form |
| PUT, PATCH, DELETE | Replace, partially update, delete |
| 2xx | Success: 200 OK, 201 Created |
| 3xx | Redirect: 301 Moved Permanently (with Location header) |
| 4xx | Client error: 404 Not Found |
| 5xx | Server error: 500, 501 Not Implemented, 503 |
132.3 curl -v: seeing request and response
- Lab: a private Docker client and a shop.lab server (192.168.152.10) with a self-signed cert made by openssl; HTTP on :80, HTTPS on :443.
- --resolve shop.lab:80:192.168.152.10 tells curl the IP, so no DNS is needed.
- Lines starting with > are the request, < are the response; the body comes after the blank line.
curl -v --resolve shop.lab:80:192.168.152.10 \ http://shop.lab/* Connected to shop.lab (192.168.152.10) port 80> GET / HTTP/1.1> Host: shop.lab> User-Agent: curl/8.7.1> Accept: */*>< HTTP/1.1 200 OK< Server: n132-shop< Content-Type: text/plain< Content-Length: 20<Welcome to shop.labcurl -I http://shop.lab/curl -v http://shop.lab/oldcurl -v http://shop.lab/missingcurl -v -d 'item=chai&qty=2' \ http://shop.lab/ordercurl -s -i -X DELETE http://shop.lab/order$ curl -I http://shop.lab/HTTP/1.1 200 OKServer: n132-shop $ curl -v http://shop.lab/old< HTTP/1.1 301 Moved Permanently< Location: /new $ curl -v http://shop.lab/missing< HTTP/1.1 404 Not Found $ curl -v -d 'item=chai&qty=2' http://shop.lab/order> POST /order HTTP/1.1> Content-Type: application/x-www-form-urlencoded< HTTP/1.1 201 CreatedOrder created: item=chai&qty=2 $ curl -s -i -X DELETE http://shop.lab/orderHTTP/1.1 501 Unsupported method ('DELETE')132.4 How the TLS 1.3 handshake works
- Step 1
1.TCP connects to shop.lab:443
- Step 2
2.ClientHello: TLS 1.3, SNI shop.lab, ALPN h2,http/1.1, key_share x25519
- Step 3
3.ServerHello: server key_share; then encrypted EncryptedExtensions (ALPN h2), Certificate CN=shop.lab, CertificateVerify, Finished
- Step 4
4.Both sides compute the same session key from the key shares (the key never travels)
- Step 5
5.Browser verifies the certificate and sends Finished: handshake done in 1 RTT
- Step 6
6.GET travels as TLS Application Data: the router sees only a7 3f 9c 12 ...
TLS 1.2 needed 2 round trips
- SNI (Server Name Indication): many sites share one IP, so the server must know which certificate to present before encryption starts.
- ALPN picks the HTTP version (h2 or http/1.1) inside the handshake, so no extra round trip.
- SNI is still plain text: a firewall can see which site you visit.
curl -v https://shop.lab/curl -v -k https://shop.lab/$ curl -v https://shop.lab/* TLSv1.3 (OUT), TLS handshake, Client hello (1):* TLSv1.3 (IN), TLS handshake, Server hello (2):* TLSv1.3 (IN), TLS handshake, Certificate (11):* TLSv1.3 (OUT), TLS alert, unknown CA (560):curl: (60) SSL certificate problem: self-signed certificate $ curl -v -k https://shop.lab/* TLSv1.3 (OUT), TLS handshake, Client hello (1):* TLSv1.3 (IN), TLS handshake, Server hello (2):* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):* TLSv1.3 (IN), TLS handshake, Certificate (11):* TLSv1.3 (IN), TLS handshake, CERT verify (15):* TLSv1.3 (IN), TLS handshake, Finished (20):* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):* TLSv1.3 (OUT), TLS handshake, Finished (20):* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 / x25519 / id-ecPublicKey* subject: CN=shop.lab* SSL certificate verify result: self-signed certificate (18), continuing anyway.- -k skips the certificate check: use it only in a lab.
132.5 Comparison: HTTP/1.1 vs HTTP/2 vs HTTP/3
| Point | HTTP/1.1 | HTTP/2 | HTTP/3 |
|---|---|---|---|
| Transport | TCP | TCP | QUIC over UDP |
| Requests per connection | One at a time (browser opens ~6 connections per site) | Many streams on one connection | Many independent streams |
| Packet loss | Blocks that connection | Head-of-line blocking: every stream waits | Only the affected stream waits |
| TLS | Optional (HTTPS) | TLS in practice | TLS 1.3 built in, 1 RTT setup |
| Network switch | Connection breaks | Connection breaks | Connection survives |
curl -v --http2 https://shop.lab/curl -v --http1.1 https://shop.lab/curl -v --http3 https://shop.lab/* ALPN: curl offers h2,http/1.1* ALPN: server accepted h2* using HTTP/2> GET / HTTP/2< HTTP/2 200Welcome to shop.lab (served over HTTP/2) * ALPN: curl offers http/1.1* ALPN: server accepted http/1.1< HTTP/1.1 200 OK $ curl -v -k --http3 https://shop.lab/curl: option --http3: the installed libcurl version doesn't support this132.6 What the network can see
tcpdump -i eth0 -U -w /tmp/web.pcap \ 'tcp port 80 or tcp port 443' &tshark -r /tmp/web.pcap -Y http.request \ -T fields -e ip.dst -e http.request.method \ -e http.host -e http.request.uritshark -r /tmp/web.pcap -Y tls.handshake \ -T fields -e ip.src -e tls.handshake.type \ -e tls.handshake.extensions_server_name \ -e tls.handshake.extensions_alpn_strtshark -r /tmp/web.pcap -Y tls -T fields \ -e frame.number -e ip.src -e _ws.col.Info192.168.152.10 GET shop.lab / 192.168.152.20 1 shop.lab h2,http/1.1192.168.152.10 2 16 192.168.152.20 Client Hello (SNI=shop.lab)18 192.168.152.10 Server Hello, Change Cipher Spec, Application Data, ...20 192.168.152.20 Change Cipher Spec, Application Data21 192.168.152.10 Application Data- Plain HTTP: method, host and path are fully visible.
- ClientHello (type 1): SNI and the ALPN list are plain text; the ServerHello's ALPN answer is encrypted in TLS 1.3.
- From frame 18 on, everything is Application Data: certificate and request are encrypted.
- On cafe Wi-Fi over HTTPS the router sees the server IP and SNI, not the path, cookies or password.
132.7 Common mistakes
- Thinking the lock icon means a safe site: it only means an encrypted connection and a cert for that domain; phishing sites get free certs too. Check the domain.
- Thinking HTTPS hides which site you visit: SNI and the IP are visible.
- Using curl -k outside a lab: it disables certificate verification.
- Confusing 4xx (client's fault) with 5xx (server's fault).
132.8 Interview tips
- TLS 1.3: ClientHello (ciphers, key_share, SNI, ALPN) -> ServerHello (key_share, cipher) + encrypted Certificate, CertificateVerify, Finished -> client Finished. 1 RTT vs 2 in TLS 1.2.
- The session key is derived on both sides (ECDHE); it is never sent over the network.
- SNI = which site (needed for many sites on one IP), ALPN = which HTTP version.
- HTTP/2 multiplexes streams on TCP but suffers head-of-line blocking; HTTP/3 uses QUIC on UDP to avoid it.