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
17%define SYS_BIND 49
18%define SYS_LISTEN 50
19%define SYS_EXIT 60
21%define AF_INET 2
22%define SOCK_STREAM 1
24%define REQUEST_MAX 4096
Route table: /, /health, and a 404 for everything elseAug 23
26; One route: where its strings live and how long each one is.
27%define PATH 0
28%define PATH_LEN 8
29%define STATUS 16
30%define STATUS_LEN 24
31%define TYPE 32
32%define TYPE_LEN 40
33%define BODY 48
34%define BODY_LEN 56
35%define ROUTE_SIZE 64
38%macro text 2+
39%1: db %2
40%{1}.len equ $ - %1
41%endmacro
43 section .text
46_start:
47 mov edi, 8080
50 mov rsi, [rsp + 16] ; argv[1]
51 call parse_port
52 mov edi, eax
53.socket:
58 mov eax, SYS_SOCKET
59 mov edi, AF_INET
60 mov esi, SOCK_STREAM
61 xor edx, edx
62 syscall
63 test eax, eax
67 mov eax, SYS_BIND
68 mov edi, r12d
70 mov edx, 16
71 syscall
72 test eax, eax
75 mov eax, SYS_LISTEN
76 mov edi, r12d
77 mov esi, 64 ; backlog
78 syscall
80.accept:
81 mov eax, SYS_ACCEPT
82 mov edi, r12d
83 xor esi, esi ; we never ask who it is
84 xor edx, edx
85 syscall
86 test eax, eax
Route table: /, /health, and a 404 for everything elseAug 23
87 js .accept ; EINTR, ECONNABORTED: take the next one
90 mov eax, SYS_CLOSE
91 mov edi, r13d
Route table: /, /health, and a 404 for everything elseAug 23
95; Reads one request from r13, finds its route and sends the response.
96serve:
97 mov eax, SYS_READ
100 mov edx, REQUEST_MAX
104 lea rbx, [garbled_route]
105 cmp rax, 5
112 mov rdi, rsi
113.scan: cmp rdi, rdx
115 mov al, [rdi]
116 cmp al, ' '
118 cmp al, '?'
120 inc rdi
122.ended: mov r8, rsi ; r8 = path
123 mov r9, rdi
124 sub r9, rsi ; r9 = its length
127.route: mov rcx, [rbx + PATH_LEN]
128 test rcx, rcx
130 cmp rcx, r9
132 mov rsi, r8
133 mov rdi, [rbx + PATH]
134 repe cmpsb
136.next: add rbx, ROUTE_SIZE
139.send: mov rsi, [rbx + STATUS]
140 mov rdx, [rbx + STATUS_LEN]
142 mov rsi, [rbx + TYPE]
143 mov rdx, [rbx + TYPE_LEN]
145 lea rsi, [header_end]
146 mov edx, header_end.len
148 mov rsi, [rbx + BODY]
149 mov rdx, [rbx + BODY_LEN]
151.gone: ret
153; Writes rdx bytes from rsi to r13.
154put: mov eax, SYS_WRITE
156 syscall
Take the port from argv[1]Aug 16
159; rsi = a NUL-terminated port number. Returns it in eax, or exits with usage.
160parse_port:
161 xor eax, eax
162.digit: movzx ecx, byte [rsi]
163 inc rsi
164 test ecx, ecx
166 sub ecx, '0'
167 cmp ecx, 9
168 ja fail_usage
169 imul eax, eax, 10
170 add eax, ecx
171 cmp eax, 65535
172 ja fail_usage
174.end: test eax, eax
175 jz fail_usage
176 ret
178fail_usage:
179 lea rsi, [usage]
180 mov edx, usage.len
182fail_socket:
183 lea rsi, [no_socket]
184 mov edx, no_socket.len
186fail_bind:
187 lea rsi, [no_bind]
188 mov edx, no_bind.len
189die: mov eax, SYS_WRITE
191 syscall
192 mov eax, SYS_EXIT
193 mov edi, 1
194 syscall
196 section .rodata
198routes: ; path status content type body
199 dq root, root.len, ok, ok.len, plain, plain.len, hello, hello.len
200 dq health, health.len, ok, ok.len, plain, plain.len, alive, alive.len
201 dq 0, 0, missing, missing.len, plain, plain.len, nothing, nothing.len
202garbled_route:
203 dq 0, 0, bad, bad.len, plain, plain.len, garbled, garbled.len
205text ok, "HTTP/1.0 200 OK", 13, 10
206text missing, "HTTP/1.0 404 Not Found", 13, 10
207text bad, "HTTP/1.0 400 Bad Request", 13, 10
208text plain, "Content-Type: text/plain; charset=utf-8", 13, 10
209text header_end, "Connection: close", 13, 10, 13, 10
210text root, "/"
211text health, "/health"
212text hello, "hello from tiny-http", 10
213text alive, "ok", 10
214text nothing, "not found", 10
215text garbled, "tiny-http answers GET and nothing else", 10
217text no_socket, "tiny-http: socket() failed", 10
218text no_bind, "tiny-http: cannot bind that port", 10
220 section .bss
221addr: resb 16 ; sockaddr_in
222request:
223 resb REQUEST_MAX