An HTTP/1.0 server for Linux in x86-64 assembly. No libc; every build starts it and curls it.
Serve index.html with a Content-Length counted at run time
Browse filesThe page comes in with incbin. Headers are built in a 256-byte buffer and decimal() writes the length, so curl knows the body is complete without waiting for the close. The smoke test checks the header against the bytes it received.
failing
3 steps in 3.15 s
4 files changed
+63−14
@@ -7,7 +7,7 @@ target = "Linux x86-64, static ELF"
77
88 [[step]]
99 name = "assemble"
10−run = ["nasm", "-f", "elf64", "-o", "build/server.o", "src/server.asm"]
10+run = ["nasm", "-f", "elf64", "-I", "src/", "-o", "build/server.o", "src/server.asm"]
1111
1212 [[step]]
1313 name = "link"
@@ -0,0 +1,9 @@
1+<!doctype html>
2+<html lang="en">
3+<meta charset="utf-8">
4+<title>tiny-http</title>
5+<style>body{font:16px/1.5 system-ui;margin:4rem auto;max-width:34rem;padding:0 1rem;color:#1b2430}code{background:#eef2f6;padding:.1em .3em;border-radius:4px}</style>
6+<h1>tiny-http</h1>
7+<p>This page was sent by an HTTP server written in x86-64 assembly: no libc, no allocator, one connection at a time.</p>
8+<p>Try <code>/health</code>, or any other path for a 404.</p>
9+</html>
@@ -1,7 +1,9 @@
11 ; tiny-http: an HTTP/1.0 server for Linux in x86-64 assembly.
22 ;
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.
3+; No libc and no allocator: a handful of syscalls, three buffers and one
4+; connection at a time. Routes live in a table at the bottom of this file;
5+; every response is a status line, two headers and a body whose length is
6+; counted at run time.
57 ;
68 ; build/tiny-http [port] default 8080
79 ; curl -i http://127.0.0.1:8080/health
@@ -149,14 +151,25 @@ serve:
149151 .next: add rbx, ROUTE_SIZE
150152 jmp .route
151153
152−.send: mov rsi, [rbx + STATUS]
153− mov rdx, [rbx + STATUS_LEN]
154− call send_all
154+.send: lea rdi, [response] ; status line, then the headers
155+ mov rsi, [rbx + STATUS]
156+ mov rcx, [rbx + STATUS_LEN]
157+ rep movsb
155158 mov rsi, [rbx + TYPE]
156− mov rdx, [rbx + TYPE_LEN]
157− call send_all
159+ mov rcx, [rbx + TYPE_LEN]
160+ rep movsb
161+ lea rsi, [length_header]
162+ mov ecx, length_header.len
163+ rep movsb
164+ mov rax, [rbx + BODY_LEN]
165+ call decimal
158166 lea rsi, [header_end]
159− mov edx, header_end.len
167+ mov ecx, header_end.len
168+ rep movsb
169+
170+ lea rsi, [response]
171+ mov rdx, rdi
172+ sub rdx, rsi
160173 call send_all
161174 mov rsi, [rbx + BODY]
162175 mov rdx, [rbx + BODY_LEN]
@@ -181,6 +194,22 @@ send_all:
181194 jmp send_all
182195 .done: ret
183196
197+; Writes rax in decimal at rdi and leaves rdi just past the last digit.
198+decimal:
199+ lea rsi, [digits + 20] ; build it backwards, copy it forwards
200+ mov r10d, 10
201+.split: xor edx, edx
202+ div r10
203+ add dl, '0'
204+ dec rsi
205+ mov [rsi], dl
206+ test rax, rax
207+ jnz .split
208+ lea rcx, [digits + 20]
209+ sub rcx, rsi
210+ rep movsb
211+ ret
212+
184213 ; rsi = a NUL-terminated port number. Returns it in eax, or exits with usage.
185214 parse_port:
186215 xor eax, eax
@@ -221,7 +250,7 @@ die: mov eax, SYS_WRITE
221250 section .rodata
222251 align 8
223252 routes: ; path status content type body
224− dq root, root.len, ok, ok.len, plain, plain.len, hello, hello.len
253+ dq root, root.len, ok, ok.len, html, html.len, index, index.len
225254 dq health, health.len, ok, ok.len, plain, plain.len, alive, alive.len
226255 dq 0, 0, missing, missing.len, plain, plain.len, nothing, nothing.len
227256 garbled_route:
@@ -232,11 +261,12 @@ one: dd 1
232261 text ok, "HTTP/1.0 200 OK", 13, 10
233262 text missing, "HTTP/1.0 404 Not Found", 13, 10
234263 text bad, "HTTP/1.0 400 Bad Request", 13, 10
264+text html, "Content-Type: text/html; charset=utf-8", 13, 10
235265 text plain, "Content-Type: text/plain; charset=utf-8", 13, 10
236−text header_end, "Connection: close", 13, 10, 13, 10
266+text length_header, "Content-Length: "
267+text header_end, 13, 10, "Connection: close", 13, 10, 13, 10
237268 text root, "/"
238269 text health, "/health"
239−text hello, "hello from tiny-http", 10
240270 text alive, "ok", 10
241271 text nothing, "not found", 10
242272 text garbled, "tiny-http answers GET and nothing else", 10
@@ -244,7 +274,13 @@ text usage, "usage: tiny-http [port 1-65535]", 10
244274 text no_socket, "tiny-http: socket() failed", 10
245275 text no_bind, "tiny-http: cannot bind that port", 10
246276
277+index: incbin "index.html"
278+index.len equ $ - index
279+
247280 section .bss
248281 addr: resb 16 ; sockaddr_in
249282 request:
250283 resb REQUEST_MAX
284+response:
285+ resb 256
286+digits: resb 20
@@ -15,8 +15,12 @@ done
1515
1616 fail() { echo "FAIL: $*"; exit 1; }
1717
18−curl -s -i "$url/" | head -n 1 | grep -q '200 OK' || fail "GET / did not answer 200"
19−echo "GET / 200"
18+page=$(curl -s -i "$url/") || fail "GET / got no answer"
19+echo "$page" | head -n 1 | grep -q '200 OK' || fail "GET / did not answer 200"
20+length=$(echo "$page" | tr -d '\r' | sed -n 's/^Content-Length: //p')
21+body=$(curl -s "$url/" | wc -c)
22+[ "$length" = "$body" ] || fail "Content-Length says $length, body has $body bytes"
23+echo "GET / 200, $body bytes, Content-Length matches"
2024
2125 [ "$(curl -s "$url/health")" = "ok" ] || fail "GET /health did not say ok"
2226 echo "GET /health 200, ok"