What HTTP Is
A text protocol over a socket, and the three tools to read it.
HTTP is text you can type. Everything in this course is bytes on a socket.
Request and response
One side asks, the other answers:
curl -sI localhost:8080 | head -5No handshake you write, no session, no “connect”. One request, one response.
Where HTTP sits
TCP carries the bytes. HTTP decides what they mean:
GET / HTTP/1.1
Host: localhost
Connection: closeChapter 2 sends that by hand. The shell for it is one printf piped into nc:
printf '%s\r\n' \
'GET / HTTP/1.1' \
'Host: localhost' \
'Connection: close' \
'' | nc localhost 8080 | head -2A server to practise against
One command gives you a server that answers honestly and never rate-limits:
docker run -d --name web \
-p 8080:80 nginx:1.27-alpineEvery chapter until TLS talks to this. 8080 on your machine maps to 80 in the
container. It serves two files:
curl -sI localhost:8080/index.html | grep -E 'HTTP|Content-Length'
curl -sI localhost:8080/50x.html | grep -E 'HTTP|Content-Length'The tools
| Tool | For |
|---|---|
curl |
Sending real requests, with -v to see them |
nc |
Typing the protocol by hand |
openssl s_client |
The same, but inside TLS |
curl -v
-v prints the conversation. > is sent, < is received:
curl -sv localhost:8080 -o /dev/null 2>&1 \
| grep -E '^[<>]' | head -10You typed a URL. curl added three headers, and the bare > is the blank line ending
them.
Reading only what you need
-I sends HEAD and prints headers only:
curl -sI localhost:8080 | grep -E 'HTTP|Content-Type'-i keeps the body as well:
curl -si localhost:8080 | grep -E 'HTTP/1.1|DOCTYPE'-w prints chosen fields instead of the response:
curl -s -o /dev/null localhost:8080 \
-w '%{http_code} %{size_download} %{content_type}'Stateless
The server keeps nothing between requests:
curl -s -o /dev/null -w '%{http_code} ' localhost:8080
curl -s -o /dev/null -w '%{http_code}' localhost:8080Logins, carts and sessions are built on top with cookies and tokens — chapter 9. Nothing in the protocol remembers you.
URLs
https://api.example.com:443/v1/users?page=2#top has six parts, and they do not all
travel:
| Part | Value | Sent |
|---|---|---|
| scheme | https |
no — picks port and TLS |
| host | api.example.com |
yes, as Host |
| port | 443 |
no — picks where to connect |
| path | /v1/users |
yes, in the request line |
| query | ?page=2 |
yes, in the request line |
| fragment | #top |
no |
The query is sent:
curl -sv 'localhost:8080/?page=2&sort=new' -o /dev/null 2>&1 \
| grep '^> GET'The fragment is not:
curl -sv 'localhost:8080/#top' -o /dev/null 2>&1 | grep '^> GET'The host and port become one header, and the scheme vanishes:
curl -sv http://localhost:8080/ -o /dev/null 2>&1 \
| grep -E '^> (GET|Host)'