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;
Serve index.html with a Content-Length counted at run timeSep 21
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.
8; build/tiny-http [port] default 8080
Route table: /, /health, and a 404 for everything elseAug 23
9; curl -i http://127.0.0.1:8080/health
11 bits 64
12 default rel
14%define SYS_READ 0
15%define SYS_WRITE 1
16%define SYS_CLOSE 3
17%define SYS_SOCKET 41
18%define SYS_ACCEPT 43
Send with MSG_NOSIGNAL so a client that hangs up can't kill the serverAug 29
19%define SYS_SENDTO 44
21%define SYS_LISTEN 50
25%define AF_INET 2
26%define SOCK_STREAM 1
28%define SO_REUSEADDR 2
Send with MSG_NOSIGNAL so a client that hangs up can't kill the serverAug 29
29%define MSG_NOSIGNAL 0x4000 ; a client that hangs up must not kill us
31%define REQUEST_MAX 4096
Route table: /, /health, and a 404 for everything elseAug 23
33; One route: where its strings live and how long each one is.
34%define PATH 0
35%define PATH_LEN 8
36%define STATUS 16
37%define STATUS_LEN 24
38%define TYPE 32
39%define TYPE_LEN 40
40%define BODY 48
41%define BODY_LEN 56
42%define ROUTE_SIZE 64
45%macro text 2+
46%1: db %2
47%{1}.len equ $ - %1
48%endmacro
50 section .text
53_start:
54 mov edi, 8080
57 mov rsi, [rsp + 16] ; argv[1]
58 call parse_port
59 mov edi, eax
60.socket:
65 mov eax, SYS_SOCKET
66 mov edi, AF_INET
67 mov esi, SOCK_STREAM
68 xor edx, edx
69 syscall
70 test eax, eax
SO_REUSEADDR, so a restart doesn't wait out TIME_WAITSep 4
74 mov eax, SYS_SETSOCKOPT ; restart without waiting out TIME_WAIT
75 mov edi, r12d
76 mov esi, SOL_SOCKET
77 mov edx, SO_REUSEADDR
79 mov r8d, 4
80 syscall
83 mov edi, r12d
85 mov edx, 16
86 syscall
87 test eax, eax
90 mov eax, SYS_LISTEN
91 mov edi, r12d
92 mov esi, 64 ; backlog
93 syscall
95.accept:
96 mov eax, SYS_ACCEPT
97 mov edi, r12d
98 xor esi, esi ; we never ask who it is
99 xor edx, edx
100 syscall
101 test eax, eax
Route table: /, /health, and a 404 for everything elseAug 23
102 js .accept ; EINTR, ECONNABORTED: take the next one
105 mov eax, SYS_CLOSE
106 mov edi, r13d
Route table: /, /health, and a 404 for everything elseAug 23
110; Reads one request from r13, finds its route and sends the response.
111serve:
112 mov eax, SYS_READ
115 mov edx, REQUEST_MAX
119 lea rbx, [garbled_route]
120 cmp rax, 5
127 mov rdi, rsi
128.scan: cmp rdi, rdx
130 mov al, [rdi]
131 cmp al, ' '
133 cmp al, '?'
135 inc rdi
137.ended: mov r8, rsi ; r8 = path
138 mov r9, rdi
139 sub r9, rsi ; r9 = its length
142.route: mov rcx, [rbx + PATH_LEN]
143 test rcx, rcx
145 cmp rcx, r9
147 mov rsi, r8
148 mov rdi, [rbx + PATH]
149 repe cmpsb
151.next: add rbx, ROUTE_SIZE
Serve index.html with a Content-Length counted at run timeSep 21
154.send: lea rdi, [response] ; status line, then the headers
155 mov rsi, [rbx + STATUS]
156 mov rcx, [rbx + STATUS_LEN]
157 rep movsb
160 rep movsb
161 lea rsi, [length_header]
162 mov ecx, length_header.len
163 rep movsb
164 mov rax, [rbx + BODY_LEN]
168 rep movsb
171 mov rdx, rdi
172 sub rdx, rsi
175 mov rdx, [rbx + BODY_LEN]
Send with MSG_NOSIGNAL so a client that hangs up can't kill the serverAug 29
179; Sends rdx bytes from rsi to r13, however many calls that takes.
180; syscall keeps rsi and rdx, so the loop needs no saves.
181send_all:
182 test rdx, rdx
184 mov eax, SYS_SENDTO
Send with MSG_NOSIGNAL so a client that hangs up can't kill the serverAug 29
186 mov r10d, MSG_NOSIGNAL
187 xor r8d, r8d
188 xor r9d, r9d
192 add rsi, rax
193 sub rdx, rax
195.done: ret
Serve index.html with a Content-Length counted at run timeSep 21
197; Writes rax in decimal at rdi and leaves rdi just past the last digit.
198decimal:
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
209 sub rcx, rsi
210 rep movsb
211 ret
Take the port from argv[1]Aug 16
213; rsi = a NUL-terminated port number. Returns it in eax, or exits with usage.
214parse_port:
215 xor eax, eax
216.digit: movzx ecx, byte [rsi]
217 inc rsi
218 test ecx, ecx
220 sub ecx, '0'
221 cmp ecx, 9
222 ja fail_usage
223 imul eax, eax, 10
224 add eax, ecx
225 cmp eax, 65535
226 ja fail_usage
228.end: test eax, eax
229 jz fail_usage
230 ret
232fail_usage:
233 lea rsi, [usage]
234 mov edx, usage.len
236fail_socket:
237 lea rsi, [no_socket]
238 mov edx, no_socket.len
240fail_bind:
241 lea rsi, [no_bind]
242 mov edx, no_bind.len
243die: mov eax, SYS_WRITE
245 syscall
246 mov eax, SYS_EXIT
247 mov edi, 1
248 syscall
250 section .rodata
252routes: ; path status content type body
Serve index.html with a Content-Length counted at run timeSep 21
253 dq root, root.len, ok, ok.len, html, html.len, index, index.len
Route table: /, /health, and a 404 for everything elseAug 23
254 dq health, health.len, ok, ok.len, plain, plain.len, alive, alive.len
255 dq 0, 0, missing, missing.len, plain, plain.len, nothing, nothing.len
256garbled_route:
257 dq 0, 0, bad, bad.len, plain, plain.len, garbled, garbled.len
262text missing, "HTTP/1.0 404 Not Found", 13, 10
263text bad, "HTTP/1.0 400 Bad Request", 13, 10
Serve index.html with a Content-Length counted at run timeSep 21
264text html, "Content-Type: text/html; charset=utf-8", 13, 10
Route table: /, /health, and a 404 for everything elseAug 23
265text plain, "Content-Type: text/plain; charset=utf-8", 13, 10
Serve index.html with a Content-Length counted at run timeSep 21
266text length_header, "Content-Length: "
267text header_end, 13, 10, "Connection: close", 13, 10, 13, 10
269text health, "/health"
270text alive, "ok", 10
271text nothing, "not found", 10
272text garbled, "tiny-http answers GET and nothing else", 10
274text no_socket, "tiny-http: socket() failed", 10
275text no_bind, "tiny-http: cannot bind that port", 10
281addr: resb 16 ; sockaddr_in
282request:
283 resb REQUEST_MAX
285 resb 256
286digits: resb 20