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 OKAug 14
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.
5 bits 64
6 default rel
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
17%define AF_INET 2
18%define SOCK_STREAM 1
20%define REQUEST_MAX 4096
22; text name, "bytes"... defines name and name.len
23%macro text 2+
24%1: db %2
25%{1}.len equ $ - %1
26%endmacro
28 section .text
31_start:
32 mov edi, 8080
33 rol di, 8 ; the port in network byte order
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
44 mov r12d, eax ; r12 = the listening socket, for good
46 mov eax, SYS_BIND
47 mov edi, r12d
49 mov edx, 16
50 syscall
51 test eax, eax
54 mov eax, SYS_LISTEN
55 mov edi, r12d
56 mov esi, 64 ; backlog
57 syscall
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
67 mov r13d, eax ; r13 = this client
69 mov eax, SYS_READ ; take the request off the socket,
70 mov edi, r13d ; or closing it would send a reset
72 mov edx, REQUEST_MAX
73 syscall
75 mov eax, SYS_WRITE
76 mov edi, r13d
77 lea rsi, [reply]
78 mov edx, reply.len
79 syscall
81 mov eax, SYS_CLOSE
82 mov edi, r13d
83 syscall
86fail: 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
95 section .rodata
96text reply, "HTTP/1.0 200 OK", 13, 10, "Content-Type: text/plain", 13, 10, 13, 10, "hello from tiny-http", 10
97text failed, "tiny-http: cannot listen on port 8080", 10
99 section .bss
100addr: resb 16 ; sockaddr_in
101request:
102 resb REQUEST_MAX