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
237 lines·7.0 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, 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
8
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_EXIT 60
22%define AF_INET 2
23%define SOCK_STREAM 1
24%define MSG_NOSIGNAL 0x4000 ; a client that hangs up must not kill us
26%define REQUEST_MAX 4096
28; One route: where its strings live and how long each one is.
29%define PATH 0
30%define PATH_LEN 8
31%define STATUS 16
32%define STATUS_LEN 24
33%define TYPE 32
34%define TYPE_LEN 40
35%define BODY 48
36%define BODY_LEN 56
37%define ROUTE_SIZE 64
39; text name, "bytes"... defines name and name.len
40%macro text 2+
41%1: db %2
42%{1}.len equ $ - %1
43%endmacro
45 section .text
46 global _start
48_start:
49 mov edi, 8080
50 cmp qword [rsp], 2 ; argc: was a port given?
52 mov rsi, [rsp + 16] ; argv[1]
54 mov edi, eax
55.socket:
56 rol di, 8 ; the port in network byte order
57 mov word [addr], AF_INET
58 mov [addr + 2], di ; sin_addr stays 0: every interface
60 mov eax, SYS_SOCKET
61 mov edi, AF_INET
62 mov esi, SOCK_STREAM
63 xor edx, edx
64 syscall
65 test eax, eax
67 mov r12d, eax ; r12 = the listening socket, for good
69 mov eax, SYS_BIND
70 mov edi, r12d
71 lea rsi, [addr]
72 mov edx, 16
73 syscall
74 test eax, eax
77 mov eax, SYS_LISTEN
78 mov edi, r12d
79 mov esi, 64 ; backlog
80 syscall
82.accept:
83 mov eax, SYS_ACCEPT
84 mov edi, r12d
85 xor esi, esi ; we never ask who it is
86 xor edx, edx
87 syscall
88 test eax, eax
89 js .accept ; EINTR, ECONNABORTED: take the next one
90 mov r13d, eax ; r13 = this client
91 call serve
92 mov eax, SYS_CLOSE
93 mov edi, r13d
94 syscall
97; Reads one request from r13, finds its route and sends the response.
98serve:
99 mov eax, SYS_READ
100 mov edi, r13d
101 lea rsi, [request]
102 mov edx, REQUEST_MAX
103 syscall
104 test rax, rax
105 jle .gone ; nothing arrived: nothing to answer
106 lea rbx, [garbled_route]
107 cmp rax, 5
109 cmp dword [request], 'GET '
112 lea rsi, [request + 4] ; the path ends at a space or a '?'
113 lea rdx, [request + rax]
114 mov rdi, rsi
115.scan: cmp rdi, rdx
117 mov al, [rdi]
118 cmp al, ' '
120 cmp al, '?'
122 inc rdi
124.ended: mov r8, rsi ; r8 = path
125 mov r9, rdi
126 sub r9, rsi ; r9 = its length
128 lea rbx, [routes]
129.route: mov rcx, [rbx + PATH_LEN]
130 test rcx, rcx
131 jz .send ; the last entry answers everything else
132 cmp rcx, r9
134 mov rsi, r8
135 mov rdi, [rbx + PATH]
136 repe cmpsb
138.next: add rbx, ROUTE_SIZE
141.send: mov rsi, [rbx + STATUS]
142 mov rdx, [rbx + STATUS_LEN]
144 mov rsi, [rbx + TYPE]
145 mov rdx, [rbx + TYPE_LEN]
147 lea rsi, [header_end]
148 mov edx, header_end.len
150 mov rsi, [rbx + BODY]
151 mov rdx, [rbx + BODY_LEN]
153.gone: ret
155; Sends rdx bytes from rsi to r13, however many calls that takes.
156; syscall keeps rsi and rdx, so the loop needs no saves.
157send_all:
158 test rdx, rdx
160 mov eax, SYS_SENDTO
161 mov edi, r13d
162 mov r10d, MSG_NOSIGNAL
163 xor r8d, r8d
164 xor r9d, r9d
165 syscall
166 test rax, rax
167 jle .done ; the client went away
168 add rsi, rax
169 sub rdx, rax
171.done: ret
173; rsi = a NUL-terminated port number. Returns it in eax, or exits with usage.
174parse_port:
175 xor eax, eax
176.digit: movzx ecx, byte [rsi]
177 inc rsi
178 test ecx, ecx
180 sub ecx, '0'
181 cmp ecx, 9
183 imul eax, eax, 10
184 add eax, ecx
185 cmp eax, 65535
188.end: test eax, eax
190 ret
192fail_usage:
193 lea rsi, [usage]
194 mov edx, usage.len
195 jmp die
196fail_socket:
197 lea rsi, [no_socket]
198 mov edx, no_socket.len
199 jmp die
200fail_bind:
201 lea rsi, [no_bind]
202 mov edx, no_bind.len
203die: mov eax, SYS_WRITE
204 mov edi, 2 ; stderr
205 syscall
206 mov eax, SYS_EXIT
207 mov edi, 1
208 syscall
210 section .rodata
211 align 8
212routes: ; path status content type body
213 dq root, root.len, ok, ok.len, plain, plain.len, hello, hello.len
214 dq health, health.len, ok, ok.len, plain, plain.len, alive, alive.len
215 dq 0, 0, missing, missing.len, plain, plain.len, nothing, nothing.len
216garbled_route:
217 dq 0, 0, bad, bad.len, plain, plain.len, garbled, garbled.len
219text ok, "HTTP/1.0 200 OK", 13, 10
220text missing, "HTTP/1.0 404 Not Found", 13, 10
221text bad, "HTTP/1.0 400 Bad Request", 13, 10
222text plain, "Content-Type: text/plain; charset=utf-8", 13, 10
223text header_end, "Connection: close", 13, 10, 13, 10
224text root, "/"
225text health, "/health"
226text hello, "hello from tiny-http", 10
227text alive, "ok", 10
228text nothing, "not found", 10
229text garbled, "tiny-http answers GET and nothing else", 10
230text usage, "usage: tiny-http [port 1-65535]", 10
231text no_socket, "tiny-http: socket() failed", 10
232text no_bind, "tiny-http: cannot bind that port", 10
234 section .bss
235addr: resb 16 ; sockaddr_in
236request:
237 resb REQUEST_MAX