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.

Code
Blame·2 commits across 37 linesoldernewer
2
Link with -s: no symbol table in the shipped binarySep 21 3An 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.
5```sh
Link with -s: no symbol table in the shipped binarySep 21 6nasm -f elf64 -I src/ -o build/server.o src/server.asm
7ld.lld -s -o build/tiny-http build/server.o
8./build/tiny-http 8080
9curl -i http://127.0.0.1:8080/health
12## Routes
13
14| Path | Answer |
15|------------|-----------------------------------------|
16| `/` | `src/index.html`, pulled in with `incbin` |
17| `/health` | `ok` |
18| anything else | 404 |
19
20A 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.
21
22## How it fits together
23
24- `_start` parses the port, then opens the listening socket: `socket`, `setsockopt(SO_REUSEADDR)`, `bind`, `listen`.
25- The accept loop hands each client to `serve` and closes it afterwards. One connection at a time.
26- `serve` reads up to 4 KiB, cuts the path at the first space or `?`, and walks the route table with `repe cmpsb`.
27- Headers are assembled in a 256-byte buffer. `decimal` writes `Content-Length` at run time.
28- 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.
29
30## Tested on every push
31
32The 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`):
33
34- `GET /` must answer 200, and its `Content-Length` must match the body.
35- `GET /health` must say `ok`.
36- An unknown path must get a 404.
37- 20 clients that reset the connection before reading must leave the server running.