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
23%define AF_INET 2
24%define SOCK_STREAM 1
26%define SO_REUSEADDR 2
Send with MSG_NOSIGNAL so a client that hangs up can't kill the serverAug 29
27%define MSG_NOSIGNAL 0x4000 ; a client that hangs up must not kill us
29%define REQUEST_MAX 4096
Route table: /, /health, and a 404 for everything elseAug 23
31; One route: where its strings live and how long each one is.
32%define PATH 0
33%define PATH_LEN 8
34%define STATUS 16
35%define STATUS_LEN 24
36%define TYPE 32
37%define TYPE_LEN 40
38%define BODY 48
39%define BODY_LEN 56
40%define ROUTE_SIZE 64
43%macro text 2+
44%1: db %2
45%{1}.len equ $ - %1
46%endmacro
48 section .text
51_start:
52 mov edi, 8080
55 mov rsi, [rsp + 16] ; argv[1]
56 call parse_port
57 mov edi, eax
58.socket:
63 mov eax, SYS_SOCKET
64 mov edi, AF_INET
65 mov esi, SOCK_STREAM
66 xor edx, edx
67 syscall
68 test eax, eax
SO_REUSEADDR, so a restart doesn't wait out TIME_WAITSep 4
72 mov eax, SYS_SETSOCKOPT ; restart without waiting out TIME_WAIT
73 mov edi, r12d
74 mov esi, SOL_SOCKET
75 mov edx, SO_REUSEADDR
77 mov r8d, 4
78 syscall
81 mov edi, r12d
83 mov edx, 16
84 syscall
85 test eax, eax
88 mov eax, SYS_LISTEN
89 mov edi, r12d
90 mov esi, 64 ; backlog
91 syscall
93.accept:
94 mov eax, SYS_ACCEPT
95 mov edi, r12d
96 xor esi, esi ; we never ask who it is
97 xor edx, edx
98 syscall
99 test eax, eax
Route table: /, /health, and a 404 for everything elseAug 23
100 js .accept ; EINTR, ECONNABORTED: take the next one
103 mov eax, SYS_CLOSE
104 mov edi, r13d
Route table: /, /health, and a 404 for everything elseAug 23
108; Reads one request from r13, finds its route and sends the response.
109serve:
110 mov eax, SYS_READ
113 mov edx, REQUEST_MAX
117 lea rbx, [garbled_route]
118 cmp rax, 5
125 mov rdi, rsi
126.scan: cmp rdi, rdx
128 mov al, [rdi]
129 cmp al, ' '
131 cmp al, '?'
133 inc rdi
135.ended: mov r8, rsi ; r8 = path
136 mov r9, rdi
137 sub r9, rsi ; r9 = its length
140.route: mov rcx, [rbx + PATH_LEN]
141 test rcx, rcx
143 cmp rcx, r9
145 mov rsi, r8
146 mov rdi, [rbx + PATH]
147 repe cmpsb
149.next: add rbx, ROUTE_SIZE
152.send: mov rsi, [rbx + STATUS]
153 mov rdx, [rbx + STATUS_LEN]
156 mov rdx, [rbx + TYPE_LEN]
159 mov edx, header_end.len
162 mov rdx, [rbx + BODY_LEN]
Send with MSG_NOSIGNAL so a client that hangs up can't kill the serverAug 29
166; Sends rdx bytes from rsi to r13, however many calls that takes.
167; syscall keeps rsi and rdx, so the loop needs no saves.
168send_all:
169 test rdx, rdx
171 mov eax, SYS_SENDTO
Send with MSG_NOSIGNAL so a client that hangs up can't kill the serverAug 29
173 mov r10d, MSG_NOSIGNAL
174 xor r8d, r8d
175 xor r9d, r9d
179 add rsi, rax
180 sub rdx, rax
182.done: ret
Take the port from argv[1]Aug 16
184; rsi = a NUL-terminated port number. Returns it in eax, or exits with usage.
185parse_port:
186 xor eax, eax
187.digit: movzx ecx, byte [rsi]
188 inc rsi
189 test ecx, ecx
191 sub ecx, '0'
192 cmp ecx, 9
193 ja fail_usage
194 imul eax, eax, 10
195 add eax, ecx
196 cmp eax, 65535
197 ja fail_usage
199.end: test eax, eax
200 jz fail_usage
201 ret
203fail_usage:
204 lea rsi, [usage]
205 mov edx, usage.len
207fail_socket:
208 lea rsi, [no_socket]
209 mov edx, no_socket.len
211fail_bind:
212 lea rsi, [no_bind]
213 mov edx, no_bind.len
214die: mov eax, SYS_WRITE
216 syscall
217 mov eax, SYS_EXIT
218 mov edi, 1
219 syscall
221 section .rodata
223routes: ; path status content type body
224 dq root, root.len, ok, ok.len, plain, plain.len, hello, hello.len
225 dq health, health.len, ok, ok.len, plain, plain.len, alive, alive.len
226 dq 0, 0, missing, missing.len, plain, plain.len, nothing, nothing.len
227garbled_route:
228 dq 0, 0, bad, bad.len, plain, plain.len, garbled, garbled.len
233text missing, "HTTP/1.0 404 Not Found", 13, 10
234text bad, "HTTP/1.0 400 Bad Request", 13, 10
235text plain, "Content-Type: text/plain; charset=utf-8", 13, 10
236text header_end, "Connection: close", 13, 10, 13, 10
237text root, "/"
238text health, "/health"
239text hello, "hello from tiny-http", 10
240text alive, "ok", 10
241text nothing, "not found", 10
242text garbled, "tiny-http answers GET and nothing else", 10
244text no_socket, "tiny-http: socket() failed", 10
245text no_bind, "tiny-http: cannot bind that port", 10
247 section .bss
248addr: resb 16 ; sockaddr_in
249request:
250 resb REQUEST_MAX