An HTTP/1.0 server for Linux in x86-64 assembly. No libc; every build starts it and curls it.
Answer every connection with 200 OK
Browse filessocket, bind, listen, then accept forever. The request is read and thrown away; closing a socket with unread data would send a reset instead of the answer.
failing
2 steps in 1 ms
3 files changed
+131−0
@@ -0,0 +1,11 @@
1+# tiny-http
2+
3+An HTTP server for Linux, written in x86-64 assembly with no libc.
4+
5+Right now it listens on port 8080 and answers every connection with `hello from tiny-http`.
6+
7+```sh
8+nasm -f elf64 -o build/server.o src/server.asm
9+ld.lld -o build/tiny-http build/server.o
10+./build/tiny-http
11+```
@@ -0,0 +1,18 @@
1+# Verified builds for tiny-http.
2+
3+[project]
4+isa = "x86-64"
5+assembler = "nasm"
6+target = "Linux x86-64, static ELF"
7+
8+[[step]]
9+name = "assemble"
10+run = ["nasm", "-f", "elf64", "-o", "build/server.o", "src/server.asm"]
11+
12+[[step]]
13+name = "link"
14+run = ["ld.lld", "-o", "build/tiny-http", "build/server.o"]
15+
16+[artifact]
17+path = "build/tiny-http"
18+max_size = 4096
@@ -0,0 +1,102 @@
1+; tiny-http: an HTTP/1.0 server for Linux in x86-64 assembly.
2+;
3+; For now it answers every connection on port 8080 with the same few bytes.
4+
5+ bits 64
6+ default rel
7+
8+%define SYS_READ 0
9+%define SYS_WRITE 1
10+%define SYS_CLOSE 3
11+%define SYS_SOCKET 41
12+%define SYS_ACCEPT 43
13+%define SYS_BIND 49
14+%define SYS_LISTEN 50
15+%define SYS_EXIT 60
16+
17+%define AF_INET 2
18+%define SOCK_STREAM 1
19+
20+%define REQUEST_MAX 4096
21+
22+; text name, "bytes"... defines name and name.len
23+%macro text 2+
24+%1: db %2
25+%{1}.len equ $ - %1
26+%endmacro
27+
28+ section .text
29+ global _start
30+
31+_start:
32+ mov edi, 8080
33+ rol di, 8 ; the port in network byte order
34+ mov word [addr], AF_INET
35+ mov [addr + 2], di ; sin_addr stays 0: every interface
36+
37+ mov eax, SYS_SOCKET
38+ mov edi, AF_INET
39+ mov esi, SOCK_STREAM
40+ xor edx, edx
41+ syscall
42+ test eax, eax
43+ js fail
44+ mov r12d, eax ; r12 = the listening socket, for good
45+
46+ mov eax, SYS_BIND
47+ mov edi, r12d
48+ lea rsi, [addr]
49+ mov edx, 16
50+ syscall
51+ test eax, eax
52+ js fail
53+
54+ mov eax, SYS_LISTEN
55+ mov edi, r12d
56+ mov esi, 64 ; backlog
57+ syscall
58+
59+.accept:
60+ mov eax, SYS_ACCEPT
61+ mov edi, r12d
62+ xor esi, esi ; we never ask who it is
63+ xor edx, edx
64+ syscall
65+ test eax, eax
66+ js .accept
67+ mov r13d, eax ; r13 = this client
68+
69+ mov eax, SYS_READ ; take the request off the socket,
70+ mov edi, r13d ; or closing it would send a reset
71+ lea rsi, [request]
72+ mov edx, REQUEST_MAX
73+ syscall
74+
75+ mov eax, SYS_WRITE
76+ mov edi, r13d
77+ lea rsi, [reply]
78+ mov edx, reply.len
79+ syscall
80+
81+ mov eax, SYS_CLOSE
82+ mov edi, r13d
83+ syscall
84+ jmp .accept
85+
86+fail: mov eax, SYS_WRITE
87+ mov edi, 2 ; stderr
88+ lea rsi, [failed]
89+ mov edx, failed.len
90+ syscall
91+ mov eax, SYS_EXIT
92+ mov edi, 1
93+ syscall
94+
95+ section .rodata
96+text reply, "HTTP/1.0 200 OK", 13, 10, "Content-Type: text/plain", 13, 10, 13, 10, "hello from tiny-http", 10
97+text failed, "tiny-http: cannot listen on port 8080", 10
98+
99+ section .bss
100+addr: resb 16 ; sockaddr_in
101+request:
102+ resb REQUEST_MAX