An HTTP/1.0 server for Linux in x86-64 assembly. No libc; every build starts it and curls it.
Answer every connection with 200 OKAug 14
1; tiny-http: an HTTP/1.0 server for Linux in x86-64 assembly.
2;
Route table: /, /health, and a 404 for everything elseAug 23
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.
6; build/tiny-http [port] default 8080
Route table: /, /health, and a 404 for everything elseAug 23
7; curl -i http://127.0.0.1:8080/health
9 bits 64
10 default rel
12%define SYS_READ 0
13%define SYS_WRITE 1
14%define SYS_CLOSE 3
15%define SYS_SOCKET 41
16%define SYS_ACCEPT 43
Send with MSG_NOSIGNAL so a client that hangs up can't kill the serverAug 29
17%define SYS_SENDTO 44
19%define SYS_LISTEN 50
20%define SYS_EXIT 60
22%define AF_INET 2
23%define SOCK_STREAM 1
Send with MSG_NOSIGNAL so a client that hangs up can't kill the serverAug 29
24%define MSG_NOSIGNAL 0x4000 ; a client that hangs up must not kill us
26%define REQUEST_MAX 4096
Route table: /, /health, and a 404 for everything elseAug 23
28; One route: where its strings live and how long each one is.
29%define PATH 0
30%define PATH_LEN 8
31%define STATUS 16
32%define STATUS_LEN 24
33%define TYPE 32
34%define TYPE_LEN 40
35%define BODY 48
36%define BODY_LEN 56
37%define ROUTE_SIZE 64
40%macro text 2+
41%1: db %2
42%{1}.len equ $ - %1
43%endmacro
45 section .text
48_start:
49 mov edi, 8080
52 mov rsi, [rsp + 16] ; argv[1]
53 call parse_port
54 mov edi, eax
55.socket:
60 mov eax, SYS_SOCKET
61 mov edi, AF_INET
62 mov esi, SOCK_STREAM
63 xor edx, edx
64 syscall
65 test eax, eax
69 mov eax, SYS_BIND
70 mov edi, r12d
72 mov edx, 16
73 syscall
74 test eax, eax
77 mov eax, SYS_LISTEN
78 mov edi, r12d
79 mov esi, 64 ; backlog
80 syscall
82.accept:
83 mov eax, SYS_ACCEPT
84 mov edi, r12d
85 xor esi, esi ; we never ask who it is
86 xor edx, edx
87 syscall
88 test eax, eax
Route table: /, /health, and a 404 for everything elseAug 23
89 js .accept ; EINTR, ECONNABORTED: take the next one
92 mov eax, SYS_CLOSE
93 mov edi, r13d
Route table: /, /health, and a 404 for everything elseAug 23
97; Reads one request from r13, finds its route and sends the response.
98serve:
99 mov eax, SYS_READ
102 mov edx, REQUEST_MAX
106 lea rbx, [garbled_route]
107 cmp rax, 5
114 mov rdi, rsi
115.scan: cmp rdi, rdx
117 mov al, [rdi]
118 cmp al, ' '
120 cmp al, '?'
122 inc rdi
124.ended: mov r8, rsi ; r8 = path
125 mov r9, rdi
126 sub r9, rsi ; r9 = its length
129.route: mov rcx, [rbx + PATH_LEN]
130 test rcx, rcx
132 cmp rcx, r9
134 mov rsi, r8
135 mov rdi, [rbx + PATH]
136 repe cmpsb
138.next: add rbx, ROUTE_SIZE
141.send: mov rsi, [rbx + STATUS]
142 mov rdx, [rbx + STATUS_LEN]
145 mov rdx, [rbx + TYPE_LEN]
148 mov edx, header_end.len
151 mov rdx, [rbx + BODY_LEN]
Send with MSG_NOSIGNAL so a client that hangs up can't kill the serverAug 29
155; Sends rdx bytes from rsi to r13, however many calls that takes.
156; syscall keeps rsi and rdx, so the loop needs no saves.
157send_all:
158 test rdx, rdx
160 mov eax, SYS_SENDTO
Send with MSG_NOSIGNAL so a client that hangs up can't kill the serverAug 29
162 mov r10d, MSG_NOSIGNAL
163 xor r8d, r8d
164 xor r9d, r9d
168 add rsi, rax
169 sub rdx, rax
171.done: ret
Take the port from argv[1]Aug 16
173; rsi = a NUL-terminated port number. Returns it in eax, or exits with usage.
174parse_port:
175 xor eax, eax
176.digit: movzx ecx, byte [rsi]
177 inc rsi
178 test ecx, ecx
180 sub ecx, '0'
181 cmp ecx, 9
182 ja fail_usage
183 imul eax, eax, 10
184 add eax, ecx
185 cmp eax, 65535
186 ja fail_usage
188.end: test eax, eax
189 jz fail_usage
190 ret
192fail_usage:
193 lea rsi, [usage]
194 mov edx, usage.len
196fail_socket:
197 lea rsi, [no_socket]
198 mov edx, no_socket.len
200fail_bind:
201 lea rsi, [no_bind]
202 mov edx, no_bind.len
203die: mov eax, SYS_WRITE
205 syscall
206 mov eax, SYS_EXIT
207 mov edi, 1
208 syscall
210 section .rodata
212routes: ; path status content type body
213 dq root, root.len, ok, ok.len, plain, plain.len, hello, hello.len
214 dq health, health.len, ok, ok.len, plain, plain.len, alive, alive.len
215 dq 0, 0, missing, missing.len, plain, plain.len, nothing, nothing.len
216garbled_route:
217 dq 0, 0, bad, bad.len, plain, plain.len, garbled, garbled.len
219text ok, "HTTP/1.0 200 OK", 13, 10
220text missing, "HTTP/1.0 404 Not Found", 13, 10
221text bad, "HTTP/1.0 400 Bad Request", 13, 10
222text plain, "Content-Type: text/plain; charset=utf-8", 13, 10
223text header_end, "Connection: close", 13, 10, 13, 10
224text root, "/"
225text health, "/health"
226text hello, "hello from tiny-http", 10
227text alive, "ok", 10
228text nothing, "not found", 10
229text garbled, "tiny-http answers GET and nothing else", 10
231text no_socket, "tiny-http: socket() failed", 10
232text no_bind, "tiny-http: cannot bind that port", 10
234 section .bss
235addr: resb 16 ; sockaddr_in
236request:
237 resb REQUEST_MAX