Skip to content

Chapter 4: HTTP & Web Servers


Metadata Card

FieldValue
Difficulty(Intermediate)
PrerequisitesVol 4 Chapter 3 (TCP), basic socket programming
KeywordsHTTP/1.1, request/response format, methods, status codes, Cache-Control, Cookie, CORS, persistent connection, pipelining, mini web server
Core SkillsRead any HTTP message; fully implement an HTTP/1.1 server in Python; configure caching strategies and CORS headers

Your Progress

"TCP has paved the way for reliable transmission; now you can run spell messages on the post road. HTTP is the universal letter format of the post road world — the mage's telescope uses HTTP to request spell scrolls, the beacon tower uses HTTP to return content. Your first beacon tower server starts here."

HTTP is the "application layer spell king" built on TCP's shoulders.

When you write a socket interface by hand, construct an HTTP message from scratch, and see the beacon tower respond with raw mana bytes which you parse line by line — that's when you truly understand HTTP.

Core Skills: Read and write raw HTTP request/response messages; understand the semantics of methods (GET/POST/PUT/DELETE), status code classes, and core headers.

HTTP Request Format

GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 ...
Accept: text/html,application/xhtml+xml,...
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
Connection: keep-alive

HTTP is plain text.

HTTP Methods & Semantics

MethodSafeIdempotentCachableBodyTypical Use
GETShouldn't haveRetrieve resource
HEADShouldn't haveCheck metadata only
OPTIONSShouldn't haveCheck CORS/server capabilities
POSTDepends on headersYesCreate resource (form submit)
PUTYesReplace resource (full update)
PATCHYesPartially update resource
DELETEVariesDelete resource

Status Code Families

CategoryRangeMeaningClassic Examples
1xx100-199Informational100 Continue
2xx200-299Success200 OK, 201 Created, 204 No Content
3xx300-399Redirection301 Moved Permanently, 302 Found, 304 Not Modified
4xx400-499Client Error400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 429 Too Many Requests
5xx500-599Server Error500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable

Key Headers

Cache-Control: HTTP/1.1 caching directives:

DirectiveMeaningExample
no-cacheMust validate before using cache. Every request asks "has this expired?"Cache-Control: no-cache
no-storeDon't cache at all (sensitive data)Cache-Control: no-store
max-age=<seconds>How long cache is valid from response generationCache-Control: max-age=3600
publicAny cache (including CDN/proxy) can storeCache-Control: public, max-age=86400
privateOnly end-user cache can store (CDN can't)Cache-Control: private, max-age=3600
must-revalidateMust validate after expiry, can't use stale cache directlyCache-Control: must-revalidate

Cookie & Set-Cookie: HTTP is stateless. Cookies attach "state" on HTTP headers.

Cookie AttributePurpose
Expires / Max-AgeCookie lifetime
DomainWhich domains receive this cookie
PathWhich paths the cookie attaches to
SecureOnly send over HTTPS
HttpOnlyJavaScript can't read (XSS protection)
SameSite=Lax|Strict|NoneCSRF protection, controls cross-site cookie sending

CORS: Cross-Origin Resource Sharing. Not a backend security mechanism — it's the browser's default restriction on non-same-origin requests.

Project Mini HTTP Server

The chapter includes a complete mini_httpd.py implementation supporting GET/POST/PUT/DELETE with JSON responses, CORS, persistent connections, and error handling. See the full source at the end of the chapter.


Traveler's Notes

I truly understood HTTP while writing this mini server. Before, using Flask/FastAPI/Django, I thought HTTP was just rest.get() → 200. The first time I received a raw request line GET / HTTP/1.1 using socket.bind + recv, I understood what frameworks actually hide.

Run your mini server and access it with curl. When you see the raw message match up, your understanding of HTTP will never be the same.


Next Stop Preview

Chapter 5: HTTPS & TLS: There are bandits on the post road. Your spell messages are plain text — any wild mage can peek. HTTPS and TLS add a sealing lock.

Built with VitePress | Software Systems Atlas