An HTTP/1.0 server for Linux in x86-64 assembly. No libc; every build starts it and curls it.
1; tiny-http: an HTTP/1.0 server for Linux in x86-64 assembly.
2;
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.
5;
6; build/tiny-http [port] default 8080
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
17%define SYS_SENDTO 44
18%define SYS_BIND 49
19%define SYS_LISTEN 50
20%define SYS_SETSOCKOPT 54
21%define SYS_EXIT 60
23%define AF_INET 2
24%define SOCK_STREAM 1
25%define SOL_SOCKET 1
26%define SO_REUSEADDR 2
27%define MSG_NOSIGNAL 0x4000 ; a client that hangs up must not kill us
29%define REQUEST_MAX 4096
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
42; text name, "bytes"... defines name and name.len
43%macro text 2+
44%1: db %2
45%{1}.len equ $ - %1
46%endmacro
48 section .text
51_start:
52 mov edi, 8080
53 cmp qword [rsp], 2 ; argc: was a port given?
55 mov rsi, [rsp + 16] ; argv[1]
56 call parse_port
57 mov edi, eax
58.socket:
59 rol di, 8 ; the port in network byte order
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
69 js fail_socket
70 mov r12d, eax ; r12 = the listening socket, for good
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
80 mov eax, SYS_BIND
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
101 mov r13d, eax ; r13 = this client
103 mov eax, SYS_CLOSE
104 mov edi, r13d
105 syscall
108; Reads one request from r13, finds its route and sends the response.
109serve:
110 mov eax, SYS_READ
111 mov edi, r13d
113 mov edx, REQUEST_MAX
114 syscall
115 test rax, rax
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]
155 mov rsi, [rbx + TYPE]
156 mov rdx, [rbx + TYPE_LEN]
158 lea rsi, [header_end]
159 mov edx, header_end.len
161 mov rsi, [rbx + BODY]
162 mov rdx, [rbx + BODY_LEN]
164.gone: ret
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
172 mov edi, r13d
173 mov r10d, MSG_NOSIGNAL
174 xor r8d, r8d
175 xor r9d, r9d
176 syscall
177 test rax, rax
179 add rsi, rax
180 sub rdx, rax
182.done: ret
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
215 mov edi, 2 ; stderr
216 syscall
217 mov eax, SYS_EXIT
218 mov edi, 1
219 syscall
221 section .rodata
222 align 8
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
230one: dd 1
232text ok, "HTTP/1.0 200 OK", 13, 10
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
243text usage, "usage: tiny-http [port 1-65535]", 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