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·3 commits across 223 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
17%define SYS_BIND 49
18%define SYS_LISTEN 50
19%define SYS_EXIT 60
20
21%define AF_INET 2
22%define SOCK_STREAM 1
23
24%define REQUEST_MAX 4096
25
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
36
Answer every connection with 200 OKAug 14 37; text name, "bytes"... defines name and name.len
38%macro text 2+
39%1: db %2
40%{1}.len equ $ - %1
41%endmacro
42
43 section .text
44 global _start
45
46_start:
47 mov edi, 8080
Take the port from argv[1]Aug 16 48 cmp qword [rsp], 2 ; argc: was a port given?
49 jb .socket
50 mov rsi, [rsp + 16] ; argv[1]
51 call parse_port
52 mov edi, eax
53.socket:
Answer every connection with 200 OKAug 14 54 rol di, 8 ; the port in network byte order
55 mov word [addr], AF_INET
56 mov [addr + 2], di ; sin_addr stays 0: every interface
57
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
Answer every connection with 200 OKAug 14 65 mov r12d, eax ; r12 = the listening socket, for good
66
67 mov eax, SYS_BIND
68 mov edi, r12d
69 lea rsi, [addr]
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
79
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
Answer every connection with 200 OKAug 14 88 mov r13d, eax ; r13 = this client
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
Answer every connection with 200 OKAug 14 98 mov edi, r13d
100 mov edx, REQUEST_MAX
103 jle .gone ; nothing arrived: nothing to answer
104 lea rbx, [garbled_route]
105 cmp rax, 5
106 jb .send
107 cmp dword [request], 'GET '
108 jne .send
109
110 lea rsi, [request + 4] ; the path ends at a space or a '?'
111 lea rdx, [request + rax]
112 mov rdi, rsi
113.scan: cmp rdi, rdx
114 jae .ended
115 mov al, [rdi]
116 cmp al, ' '
117 je .ended
118 cmp al, '?'
119 je .ended
120 inc rdi
121 jmp .scan
122.ended: mov r8, rsi ; r8 = path
123 mov r9, rdi
124 sub r9, rsi ; r9 = its length
125
126 lea rbx, [routes]
127.route: mov rcx, [rbx + PATH_LEN]
128 test rcx, rcx
129 jz .send ; the last entry answers everything else
130 cmp rcx, r9
131 jne .next
132 mov rsi, r8
133 mov rdi, [rbx + PATH]
134 repe cmpsb
135 je .send
136.next: add rbx, ROUTE_SIZE
137 jmp .route
138
139.send: mov rsi, [rbx + STATUS]
140 mov rdx, [rbx + STATUS_LEN]
141 call put
142 mov rsi, [rbx + TYPE]
143 mov rdx, [rbx + TYPE_LEN]
144 call put
145 lea rsi, [header_end]
146 mov edx, header_end.len
147 call put
148 mov rsi, [rbx + BODY]
149 mov rdx, [rbx + BODY_LEN]
150 call put
151.gone: ret
152
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
165 jz .end
166 sub ecx, '0'
167 cmp ecx, 9
169 imul eax, eax, 10
170 add eax, ecx
171 cmp eax, 65535
173 jmp .digit
174.end: test eax, eax
176 ret
177
178fail_usage:
179 lea rsi, [usage]
180 mov edx, usage.len
181 jmp die
182fail_socket:
183 lea rsi, [no_socket]
184 mov edx, no_socket.len
185 jmp die
186fail_bind:
187 lea rsi, [no_bind]
188 mov edx, no_bind.len
189die: mov eax, SYS_WRITE
Answer every connection with 200 OKAug 14 190 mov edi, 2 ; stderr
191 syscall
192 mov eax, SYS_EXIT
193 mov edi, 1
194 syscall
195
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
204
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
Take the port from argv[1]Aug 16 216text usage, "usage: tiny-http [port 1-65535]", 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