tiny-http
An HTTP/1.0 server for Linux, written in x86-64 assembly. No libc, no allocator, no threads: it reads a request, looks the path up in a table, sends the answer and closes the connection.
nasm -f elf64 -I src/ -o build/server.o src/server.asm
ld.lld -s -o build/tiny-http build/server.o
./build/tiny-http 8080
curl -i http://127.0.0.1:8080/healthRoutes
| Path | Answer |
|---|---|
/ | src/index.html, pulled in with incbin |
/health | ok |
| anything else | 404 |
A request that is not a GET gets a 400. Add a route by adding a row to routes at the bottom of src/server.asm: a path, a status line, a content type and a body, each with its length.
How it fits together
_startparses the port, then opens the listening socket:socket,setsockopt(SO_REUSEADDR),bind,listen.- The accept loop hands each client to
serveand closes it afterwards. One connection at a time. servereads up to 4 KiB, cuts the path at the first space or?, and walks the route table withrepe cmpsb.- Headers are assembled in a 256-byte buffer.
decimalwritesContent-Lengthat run time. - Every byte goes out through
send_all, which usessendtowithMSG_NOSIGNAL, so a client that hangs up mid-answer can't raiseSIGPIPEand kill the server.
Tested on every push
The verified build assembles and links on the build host, then starts the server under Linux and talks to it with curl (see test/smoke.sh):
GET /must answer 200, and itsContent-Lengthmust match the body.GET /healthmust sayok.- An unknown path must get a 404.
- 20 clients that reset the connection before reading must leave the server running.