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.

History
286 lines·8.4 KB·NASM Raw Blame
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, 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.
7;
8; build/tiny-http [port] default 8080
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
19%define SYS_SENDTO 44
20%define SYS_BIND 49
21%define SYS_LISTEN 50
22%define SYS_SETSOCKOPT 54
23%define SYS_EXIT 60
25%define AF_INET 2
26%define SOCK_STREAM 1
27%define SOL_SOCKET 1
28%define SO_REUSEADDR 2
29%define MSG_NOSIGNAL 0x4000 ; a client that hangs up must not kill us
31%define REQUEST_MAX 4096
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
44; text name, "bytes"... defines name and name.len
45%macro text 2+
46%1: db %2
47%{1}.len equ $ - %1
48%endmacro
50 section .text
51 global _start
53_start:
54 mov edi, 8080
55 cmp qword [rsp], 2 ; argc: was a port given?
57 mov rsi, [rsp + 16] ; argv[1]
59 mov edi, eax
60.socket:
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
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
72 mov r12d, eax ; r12 = the listening socket, for good
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
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
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
102 js .accept ; EINTR, ECONNABORTED: take the next one
103 mov r13d, eax ; r13 = this client
104 call serve
105 mov eax, SYS_CLOSE
106 mov edi, r13d
107 syscall
110; Reads one request from r13, finds its route and sends the response.
111serve:
112 mov eax, SYS_READ
113 mov edi, r13d
114 lea rsi, [request]
115 mov edx, REQUEST_MAX
116 syscall
117 test rax, rax
118 jle .gone ; nothing arrived: nothing to answer
119 lea rbx, [garbled_route]
120 cmp rax, 5
122 cmp dword [request], 'GET '
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
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
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
147 mov rsi, r8
148 mov rdi, [rbx + PATH]
149 repe cmpsb
151.next: add rbx, ROUTE_SIZE
154.send: lea rdi, [response] ; status line, then the headers
155 mov rsi, [rbx + STATUS]
156 mov rcx, [rbx + STATUS_LEN]
157 rep movsb
158 mov rsi, [rbx + TYPE]
159 mov rcx, [rbx + TYPE_LEN]
160 rep movsb
161 lea rsi, [length_header]
162 mov ecx, length_header.len
163 rep movsb
164 mov rax, [rbx + BODY_LEN]
166 lea rsi, [header_end]
167 mov ecx, header_end.len
168 rep movsb
170 lea rsi, [response]
171 mov rdx, rdi
172 sub rdx, rsi
174 mov rsi, [rbx + BODY]
175 mov rdx, [rbx + BODY_LEN]
177.gone: ret
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
185 mov edi, r13d
186 mov r10d, MSG_NOSIGNAL
187 xor r8d, r8d
188 xor r9d, r9d
189 syscall
190 test rax, rax
191 jle .done ; the client went away
192 add rsi, rax
193 sub rdx, rax
195.done: ret
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
208 lea rcx, [digits + 20]
209 sub rcx, rsi
210 rep movsb
211 ret
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
223 imul eax, eax, 10
224 add eax, ecx
225 cmp eax, 65535
228.end: test eax, eax
230 ret
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
244 mov edi, 2 ; stderr
245 syscall
246 mov eax, SYS_EXIT
247 mov edi, 1
248 syscall
250 section .rodata
251 align 8
252routes: ; path status content type body
253 dq root, root.len, ok, ok.len, html, html.len, index, index.len
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
259one: dd 1
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
264text html, "Content-Type: text/html; charset=utf-8", 13, 10
265text plain, "Content-Type: text/plain; charset=utf-8", 13, 10
266text length_header, "Content-Length: "
267text header_end, 13, 10, "Connection: close", 13, 10, 13, 10
268text root, "/"
269text health, "/health"
270text alive, "ok", 10
271text nothing, "not found", 10
272text garbled, "tiny-http answers GET and nothing else", 10
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
277index: incbin "index.html"
278index.len equ $ - index
280 section .bss
281addr: resb 16 ; sockaddr_in
282request:
283 resb REQUEST_MAX
284response:
285 resb 256
286digits: resb 20