Projects / HTTP server / Wrap up
Wrap up
What this server does not do
This is a teaching server, and it leaves out a great deal:
- It answers only
GET, with noHEAD, no request bodies and noRangerequests. - It speaks no TLS, so it cannot serve
https. Real servers use a library such as OpenSSL for that. - It does not check the
Hostheader, decode percent-escapes, set cache headers or compress anything. - It runs on one thread and one CPU core. A busy machine has many more cores.
- It handles one request at a time per connection, and its timers are a simple scan.
- Its answers are accurate enough for
curl,aband browsers, but it has not been checked against the full HTTP/1.1 specification.
Keep going
- Several cores. Start one process per core, each with its own
epollloop and all listening on the same port withSO_REUSEPORT. Linux then spreads new connections across them. nginx offers this as itsreuseportoption. - Better timers. Replace the once-a-second scan with a min-heap of deadlines.
HEADandRange. Add them and test withcurl -Iandcurl -r 0-99.- Percent-decoding. Add it, then work out which of your path checks must run after it.
io_uring. Rewrite the I/O part on Linux's newer asynchronous interface and compare.
What a real server does differently
- nginx and similar servers use the same
epolldesign, with a fixed number of worker processes, each running one event loop, and a master process that restarts them. - They keep per-connection memory small and grow buffers only when needed, where ours reserves about 9.8 KB for every connection from the start.
- They parse HTTP incrementally with a dedicated state machine, and not with
strstrandsscanfover a whole buffer. - They support TLS, HTTP/2, compression, caching, logging, rate limiting and many more safeguards.
Check yourself
Answer each question in your head first, then open it to check.
Why does the Phase 1 server stop answering when one client connects and sends nothing, and what two changes fix that?
readblocks until data arrives, and the server handles one client from start to finish before it callsacceptagain. Using non-blocking sockets together withepolllets one thread move on to other clients whenever one has nothing to do.Why do we switch a connection to
EPOLLOUTonly after a response is ready, and not watch for it from the start?A socket with free space in its send buffer is nearly always writable, soepollwould report it again and again and the loop would spin with no work. We ask for it only when we have bytes to send.What does
memmoveinfinish_responseprotect against?A pipelining client may send the next request before it reads the first answer. Those bytes arrive in the same buffer, behind the first request. Moving them to the front keeps them for the next round, instead of throwing them away.Why did the keep-alive benchmark give about 460 requests a second before
TCP_NODELAY, and why did no earlier test catch it?The header and the body were sent in two small writes, and Nagle's algorithm held the second back until the client's delayed acknowledgement of the first, about 40 milliseconds per request. Earlier tests checked that answers were correct and that slow clients were handled, and none measured throughput.Teach it back: a teammate says that checking for
..in the path makes a static file server safe from path traversal. What do you answer?It stops one kind of attack. Symbolic links inside the folder, a path decoded after the check, or a folder with a similar name can still lead outside. The safe approach is to resolve the real path and check that it is inside the folder.
Sources
- epoll(7), the Linux manual page for the event interface used in Phase 2.
- sendfile(2), copying from a file to a socket inside the kernel.
- tcp(7), including the
TCP_NODELAYoption from Step 4.3. - RFC 9112: HTTP/1.1, the message format, persistent connections and pipelining.
- AddressSanitizer and LeakSanitizer, the tools used in Step 4.1.
- Socket sharding in NGINX 1.9.1, where nginx explains its
reuseportoption.