acompiled

The catalog contains seeded project examples. Your workspace stores your own projects and conversations.

zeroeight/tiny-http

Public
Star9

An HTTP/1.0 server for Linux in x86-64 assembly. No libc; every build starts it and curls it.

1 branch · 1 tag History
README.md

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/health

Routes

PathAnswer
/src/index.html, pulled in with incbin
/healthok
anything else404

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

  • _start parses the port, then opens the listening socket: socket, setsockopt(SO_REUSEADDR), bind, listen.
  • The accept loop hands each client to serve and closes it afterwards. One connection at a time.
  • serve reads up to 4 KiB, cuts the path at the first space or ?, and walks the route table with repe cmpsb.
  • Headers are assembled in a 256-byte buffer. decimal writes Content-Length at run time.
  • Every byte goes out through send_all, which uses sendto with MSG_NOSIGNAL, so a client that hangs up mid-answer can't raise SIGPIPE and 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 its Content-Length must match the body.
  • GET /health must say ok.
  • An unknown path must get a 404.
  • 20 clients that reset the connection before reading must leave the server running.