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·5 commits across 250 linesoldernewer
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
11
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
Answer every connection with 200 OKAug 14 18%define SYS_BIND 49
19%define SYS_LISTEN 50
Answer every connection with 200 OKAug 14 21%define SYS_EXIT 60
22
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
30
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
41
Answer every connection with 200 OKAug 14 42; text name, "bytes"... defines name and name.len
43%macro text 2+
44%1: db %2
45%{1}.len equ $ - %1
46%endmacro
47
48 section .text
49 global _start
50
51_start:
52 mov edi, 8080
Take the port from argv[1]Aug 16 53 cmp qword [rsp], 2 ; argc: was a port given?
54 jb .socket
55 mov rsi, [rsp + 16] ; argv[1]
56 call parse_port
57 mov edi, eax
58.socket:
Answer every connection with 200 OKAug 14 59 rol di, 8 ; the port in network byte order
60 mov word [addr], AF_INET
61 mov [addr + 2], di ; sin_addr stays 0: every interface
62
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
Answer every connection with 200 OKAug 14 70 mov r12d, eax ; r12 = the listening socket, for good
71
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
76 lea r10, [one]
77 mov r8d, 4
78 syscall
79
Answer every connection with 200 OKAug 14 80 mov eax, SYS_BIND
81 mov edi, r12d
82 lea rsi, [addr]
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
92
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
Answer every connection with 200 OKAug 14 101 mov r13d, eax ; r13 = this client
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
116 jle .gone ; nothing arrived: nothing to answer
117 lea rbx, [garbled_route]
118 cmp rax, 5
119 jb .send
120 cmp dword [request], 'GET '
121 jne .send
122
123 lea rsi, [request + 4] ; the path ends at a space or a '?'
124 lea rdx, [request + rax]
125 mov rdi, rsi
126.scan: cmp rdi, rdx
127 jae .ended
128 mov al, [rdi]
129 cmp al, ' '
130 je .ended
131 cmp al, '?'
132 je .ended
133 inc rdi
134 jmp .scan
135.ended: mov r8, rsi ; r8 = path
136 mov r9, rdi
137 sub r9, rsi ; r9 = its length
138
139 lea rbx, [routes]
140.route: mov rcx, [rbx + PATH_LEN]
141 test rcx, rcx
142 jz .send ; the last entry answers everything else
143 cmp rcx, r9
144 jne .next
145 mov rsi, r8
146 mov rdi, [rbx + PATH]
147 repe cmpsb
148 je .send
149.next: add rbx, ROUTE_SIZE
150 jmp .route
151
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]
165
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
170 jz .done
171 mov eax, SYS_SENDTO
174 xor r8d, r8d
175 xor r9d, r9d
178 jle .done ; the client went away
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
190 jz .end
191 sub ecx, '0'
192 cmp ecx, 9
194 imul eax, eax, 10
195 add eax, ecx
196 cmp eax, 65535
198 jmp .digit
199.end: test eax, eax
201 ret
202
203fail_usage:
204 lea rsi, [usage]
205 mov edx, usage.len
206 jmp die
207fail_socket:
208 lea rsi, [no_socket]
209 mov edx, no_socket.len
210 jmp die
211fail_bind:
212 lea rsi, [no_bind]
213 mov edx, no_bind.len
214die: mov eax, SYS_WRITE
Answer every connection with 200 OKAug 14 215 mov edi, 2 ; stderr
216 syscall
217 mov eax, SYS_EXIT
218 mov edi, 1
219 syscall
220
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
229
231
Route table: /, /health, and a 404 for everything elseAug 23 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
Take the port from argv[1]Aug 16 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