An HTTP/1.0 server for Linux in x86-64 assembly. No libc; every build starts it and curls it.
Route table: /, /health, and a 404 for everything else
Browse filesEach route is a path, a status line, a content type and a body, with lengths. serve() cuts the path at the first space or '?', then walks the table with repe cmpsb. Anything that is not a GET gets a 400.
failing
3 steps in 3.16 s
3 files changed
+109−19
@@ -17,7 +17,7 @@ run = ["ld.lld", "-o", "build/tiny-http", "build/server.o"]
1717 name = "serve real requests"
1818 on = "linux"
1919 run = ["sh", "test/smoke.sh"]
20−expect = "smoke: 1 request answered"
20+expect = "smoke: 3 requests answered"
2121
2222 [artifact]
2323 path = "build/tiny-http"
@@ -1,8 +1,10 @@
11 ; tiny-http: an HTTP/1.0 server for Linux in x86-64 assembly.
22 ;
3−; For now it answers every connection with the same few bytes.
3+; No libc and no allocator: a handful of syscalls, two buffers and one
4+; connection at a time. Routes live in a table at the bottom of this file.
45 ;
56 ; build/tiny-http [port] default 8080
7+; curl -i http://127.0.0.1:8080/health
68
79 bits 64
810 default rel
@@ -21,6 +23,17 @@
2123
2224 %define REQUEST_MAX 4096
2325
26+; One route: where its strings live and how long each one is.
27+%define PATH 0
28+%define PATH_LEN 8
29+%define STATUS 16
30+%define STATUS_LEN 24
31+%define TYPE 32
32+%define TYPE_LEN 40
33+%define BODY 48
34+%define BODY_LEN 56
35+%define ROUTE_SIZE 64
36+
2437 ; text name, "bytes"... defines name and name.len
2538 %macro text 2+
2639 %1: db %2
@@ -71,25 +84,77 @@ _start:
7184 xor edx, edx
7285 syscall
7386 test eax, eax
74− js .accept
87+ js .accept ; EINTR, ECONNABORTED: take the next one
7588 mov r13d, eax ; r13 = this client
76−
77− mov eax, SYS_READ ; take the request off the socket,
78− mov edi, r13d ; or closing it would send a reset
79− lea rsi, [request]
80− mov edx, REQUEST_MAX
89+ call serve
90+ mov eax, SYS_CLOSE
91+ mov edi, r13d
8192 syscall
93+ jmp .accept
8294
83− mov eax, SYS_WRITE
95+; Reads one request from r13, finds its route and sends the response.
96+serve:
97+ mov eax, SYS_READ
8498 mov edi, r13d
85− lea rsi, [reply]
86− mov edx, reply.len
99+ lea rsi, [request]
100+ mov edx, REQUEST_MAX
87101 syscall
88−
89− mov eax, SYS_CLOSE
102+ test rax, rax
103+ jle .gone ; nothing arrived: nothing to answer
104+ lea rbx, [garbled_route]
105+ cmp rax, 5
106+ jb .send
107+ cmp dword [request], 'GET '
108+ jne .send
109+
110+ lea rsi, [request + 4] ; the path ends at a space or a '?'
111+ lea rdx, [request + rax]
112+ mov rdi, rsi
113+.scan: cmp rdi, rdx
114+ jae .ended
115+ mov al, [rdi]
116+ cmp al, ' '
117+ je .ended
118+ cmp al, '?'
119+ je .ended
120+ inc rdi
121+ jmp .scan
122+.ended: mov r8, rsi ; r8 = path
123+ mov r9, rdi
124+ sub r9, rsi ; r9 = its length
125+
126+ lea rbx, [routes]
127+.route: mov rcx, [rbx + PATH_LEN]
128+ test rcx, rcx
129+ jz .send ; the last entry answers everything else
130+ cmp rcx, r9
131+ jne .next
132+ mov rsi, r8
133+ mov rdi, [rbx + PATH]
134+ repe cmpsb
135+ je .send
136+.next: add rbx, ROUTE_SIZE
137+ jmp .route
138+
139+.send: mov rsi, [rbx + STATUS]
140+ mov rdx, [rbx + STATUS_LEN]
141+ call put
142+ mov rsi, [rbx + TYPE]
143+ mov rdx, [rbx + TYPE_LEN]
144+ call put
145+ lea rsi, [header_end]
146+ mov edx, header_end.len
147+ call put
148+ mov rsi, [rbx + BODY]
149+ mov rdx, [rbx + BODY_LEN]
150+ call put
151+.gone: ret
152+
153+; Writes rdx bytes from rsi to r13.
154+put: mov eax, SYS_WRITE
90155 mov edi, r13d
91156 syscall
92− jmp .accept
157+ ret
93158
94159 ; rsi = a NUL-terminated port number. Returns it in eax, or exits with usage.
95160 parse_port:
@@ -129,7 +194,25 @@ die: mov eax, SYS_WRITE
129194 syscall
130195
131196 section .rodata
132−text reply, "HTTP/1.0 200 OK", 13, 10, "Content-Type: text/plain", 13, 10, 13, 10, "hello from tiny-http", 10
197+ align 8
198+routes: ; path status content type body
199+ dq root, root.len, ok, ok.len, plain, plain.len, hello, hello.len
200+ dq health, health.len, ok, ok.len, plain, plain.len, alive, alive.len
201+ dq 0, 0, missing, missing.len, plain, plain.len, nothing, nothing.len
202+garbled_route:
203+ dq 0, 0, bad, bad.len, plain, plain.len, garbled, garbled.len
204+
205+text ok, "HTTP/1.0 200 OK", 13, 10
206+text missing, "HTTP/1.0 404 Not Found", 13, 10
207+text bad, "HTTP/1.0 400 Bad Request", 13, 10
208+text plain, "Content-Type: text/plain; charset=utf-8", 13, 10
209+text header_end, "Connection: close", 13, 10, 13, 10
210+text root, "/"
211+text health, "/health"
212+text hello, "hello from tiny-http", 10
213+text alive, "ok", 10
214+text nothing, "not found", 10
215+text garbled, "tiny-http answers GET and nothing else", 10
133216 text usage, "usage: tiny-http [port 1-65535]", 10
134217 text no_socket, "tiny-http: socket() failed", 10
135218 text no_bind, "tiny-http: cannot bind that port", 10
@@ -1,6 +1,6 @@
11 #!/bin/sh
2−# Starts the server on a spare port, asks it for a page, and checks the
3−# answer. Run from the repository root after a build.
2+# Starts the server on a spare port, asks it for three things, and checks
3+# every answer. Run from the repository root after a build.
44 set -u
55 port=$((20000 + $$ % 20000))
66 ./build/tiny-http "$port" &
@@ -9,7 +9,7 @@ trap 'kill "$pid" 2>/dev/null' EXIT INT TERM
99 url="http://127.0.0.1:$port"
1010
1111 for i in 1 2 3 4 5 6 7 8 9 10; do
12− curl -s -o /dev/null "$url/" && break
12+ curl -s -o /dev/null "$url/health" && break
1313 sleep 0.1
1414 done
1515
@@ -18,4 +18,11 @@ fail() { echo "FAIL: $*"; exit 1; }
1818 curl -s -i "$url/" | head -n 1 | grep -q '200 OK' || fail "GET / did not answer 200"
1919 echo "GET / 200"
2020
21−echo "smoke: 1 request answered"
21+[ "$(curl -s "$url/health")" = "ok" ] || fail "GET /health did not say ok"
22+echo "GET /health 200, ok"
23+
24+code=$(curl -s -o /dev/null -w '%{http_code}' "$url/no/such/page")
25+[ "$code" = 404 ] || fail "GET /no/such/page answered $code, not 404"
26+echo "GET /no/... 404"
27+
28+echo "smoke: 3 requests answered"