knaxuslearn · build · ship

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 no HEAD, no request bodies and no Range requests.
  • It speaks no TLS, so it cannot serve https. Real servers use a library such as OpenSSL for that.
  • It does not check the Host header, 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, ab and 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 epoll loop and all listening on the same port with SO_REUSEPORT. Linux then spreads new connections across them. nginx offers this as its reuseport option.
  • Better timers. Replace the once-a-second scan with a min-heap of deadlines.
  • HEAD and Range. Add them and test with curl -I and curl -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 epoll design, 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 strstr and sscanf over 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.

  1. Why does the Phase 1 server stop answering when one client connects and sends nothing, and what two changes fix that?
    read blocks until data arrives, and the server handles one client from start to finish before it calls accept again. Using non-blocking sockets together with epoll lets one thread move on to other clients whenever one has nothing to do.
  2. Why do we switch a connection to EPOLLOUT only 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, so epoll would report it again and again and the loop would spin with no work. We ask for it only when we have bytes to send.
  3. What does memmove in finish_response protect 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.
  4. 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.
  5. 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