acompiled

The catalog contains seeded project examples. Your workspace stores your own projects and conversations.

zeroeight/tiny-http

Public
Star9

An HTTP/1.0 server for Linux in x86-64 assembly. No libc; every build starts it and curls it.

Code
Blame·6 commits across 286 linesoldernewer
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
13
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
Answer every connection with 200 OKAug 14 20%define SYS_BIND 49
21%define SYS_LISTEN 50
Answer every connection with 200 OKAug 14 23%define SYS_EXIT 60
24
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
32
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
43
Answer every connection with 200 OKAug 14 44; text name, "bytes"... defines name and name.len
45%macro text 2+
46%1: db %2
47%{1}.len equ $ - %1
48%endmacro
49
50 section .text
51 global _start
52
53_start:
54 mov edi, 8080
Take the port from argv[1]Aug 16 55 cmp qword [rsp], 2 ; argc: was a port given?
56 jb .socket
57 mov rsi, [rsp + 16] ; argv[1]
58 call parse_port
59 mov edi, eax
60.socket:
Answer every connection with 200 OKAug 14 61 rol di, 8 ; the port in network byte order
62 mov word [addr], AF_INET
63 mov [addr + 2], di ; sin_addr stays 0: every interface
64
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
Answer every connection with 200 OKAug 14 72 mov r12d, eax ; r12 = the listening socket, for good
73
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
78 lea r10, [one]
79 mov r8d, 4
80 syscall
81
Answer every connection with 200 OKAug 14 82 mov eax, SYS_BIND
83 mov edi, r12d
84 lea rsi, [addr]
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
94
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
Answer every connection with 200 OKAug 14 103 mov r13d, eax ; r13 = this client
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
118 jle .gone ; nothing arrived: nothing to answer
119 lea rbx, [garbled_route]
120 cmp rax, 5
121 jb .send
122 cmp dword [request], 'GET '
123 jne .send
124
125 lea rsi, [request + 4] ; the path ends at a space or a '?'
126 lea rdx, [request + rax]
127 mov rdi, rsi
128.scan: cmp rdi, rdx
129 jae .ended
130 mov al, [rdi]
131 cmp al, ' '
132 je .ended
133 cmp al, '?'
134 je .ended
135 inc rdi
136 jmp .scan
137.ended: mov r8, rsi ; r8 = path
138 mov r9, rdi
139 sub r9, rsi ; r9 = its length
140
141 lea rbx, [routes]
142.route: mov rcx, [rbx + PATH_LEN]
143 test rcx, rcx
144 jz .send ; the last entry answers everything else
145 cmp rcx, r9
146 jne .next
147 mov rsi, r8
148 mov rdi, [rbx + PATH]
149 repe cmpsb
150 je .send
151.next: add rbx, ROUTE_SIZE
152 jmp .route
153
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]
165 call decimal
168 rep movsb
169
170 lea rsi, [response]
171 mov rdx, rdi
172 sub rdx, rsi
175 mov rdx, [rbx + BODY_LEN]
178
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
183 jz .done
184 mov eax, SYS_SENDTO
187 xor r8d, r8d
188 xor r9d, r9d
191 jle .done ; the client went away
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:
199 lea rsi, [digits + 20] ; build it backwards, copy it forwards
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
207 jnz .split
208 lea rcx, [digits + 20]
209 sub rcx, rsi
210 rep movsb
211 ret
212
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
219 jz .end
220 sub ecx, '0'
221 cmp ecx, 9
223 imul eax, eax, 10
224 add eax, ecx
225 cmp eax, 65535
227 jmp .digit
228.end: test eax, eax
230 ret
231
232fail_usage:
233 lea rsi, [usage]
234 mov edx, usage.len
235 jmp die
236fail_socket:
237 lea rsi, [no_socket]
238 mov edx, no_socket.len
239 jmp die
240fail_bind:
241 lea rsi, [no_bind]
242 mov edx, no_bind.len
243die: mov eax, SYS_WRITE
Answer every connection with 200 OKAug 14 244 mov edi, 2 ; stderr
245 syscall
246 mov eax, SYS_EXIT
247 mov edi, 1
248 syscall
249
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
258
260
Route table: /, /health, and a 404 for everything elseAug 23 261text ok, "HTTP/1.0 200 OK", 13, 10
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
Take the port from argv[1]Aug 16 273text usage, "usage: tiny-http [port 1-65535]", 10
274text no_socket, "tiny-http: socket() failed", 10
275text no_bind, "tiny-http: cannot bind that port", 10
278index.len equ $ - index
279
281addr: resb 16 ; sockaddr_in
282request:
283 resb REQUEST_MAX
285 resb 256
286digits: resb 20