; tiny-http: an HTTP/1.0 server for Linux in x86-64 assembly. ; ; No libc and no allocator: a handful of syscalls, three buffers and one ; connection at a time. Routes live in a table at the bottom of this file; ; every response is a status line, two headers and a body whose length is ; counted at run time. ; ; build/tiny-http [port] default 8080 ; curl -i http://127.0.0.1:8080/health bits 64 default rel %define SYS_READ 0 %define SYS_WRITE 1 %define SYS_CLOSE 3 %define SYS_SOCKET 41 %define SYS_ACCEPT 43 %define SYS_SENDTO 44 %define SYS_BIND 49 %define SYS_LISTEN 50 %define SYS_SETSOCKOPT 54 %define SYS_EXIT 60 %define AF_INET 2 %define SOCK_STREAM 1 %define SOL_SOCKET 1 %define SO_REUSEADDR 2 %define MSG_NOSIGNAL 0x4000 ; a client that hangs up must not kill us %define REQUEST_MAX 4096 ; One route: where its strings live and how long each one is. %define PATH 0 %define PATH_LEN 8 %define STATUS 16 %define STATUS_LEN 24 %define TYPE 32 %define TYPE_LEN 40 %define BODY 48 %define BODY_LEN 56 %define ROUTE_SIZE 64 ; text name, "bytes"... defines name and name.len %macro text 2+ %1: db %2 %{1}.len equ $ - %1 %endmacro section .text global _start _start: mov edi, 8080 cmp qword [rsp], 2 ; argc: was a port given? jb .socket mov rsi, [rsp + 16] ; argv[1] call parse_port mov edi, eax .socket: rol di, 8 ; the port in network byte order mov word [addr], AF_INET mov [addr + 2], di ; sin_addr stays 0: every interface mov eax, SYS_SOCKET mov edi, AF_INET mov esi, SOCK_STREAM xor edx, edx syscall test eax, eax js fail_socket mov r12d, eax ; r12 = the listening socket, for good mov eax, SYS_SETSOCKOPT ; restart without waiting out TIME_WAIT mov edi, r12d mov esi, SOL_SOCKET mov edx, SO_REUSEADDR lea r10, [one] mov r8d, 4 syscall mov eax, SYS_BIND mov edi, r12d lea rsi, [addr] mov edx, 16 syscall test eax, eax js fail_bind mov eax, SYS_LISTEN mov edi, r12d mov esi, 64 ; backlog syscall .accept: mov eax, SYS_ACCEPT mov edi, r12d xor esi, esi ; we never ask who it is xor edx, edx syscall test eax, eax js .accept ; EINTR, ECONNABORTED: take the next one mov r13d, eax ; r13 = this client call serve mov eax, SYS_CLOSE mov edi, r13d syscall jmp .accept ; Reads one request from r13, finds its route and sends the response. serve: mov eax, SYS_READ mov edi, r13d lea rsi, [request] mov edx, REQUEST_MAX syscall test rax, rax jle .gone ; nothing arrived: nothing to answer lea rbx, [garbled_route] cmp rax, 5 jb .send cmp dword [request], 'GET ' jne .send lea rsi, [request + 4] ; the path ends at a space or a '?' lea rdx, [request + rax] mov rdi, rsi .scan: cmp rdi, rdx jae .ended mov al, [rdi] cmp al, ' ' je .ended cmp al, '?' je .ended inc rdi jmp .scan .ended: mov r8, rsi ; r8 = path mov r9, rdi sub r9, rsi ; r9 = its length lea rbx, [routes] .route: mov rcx, [rbx + PATH_LEN] test rcx, rcx jz .send ; the last entry answers everything else cmp rcx, r9 jne .next mov rsi, r8 mov rdi, [rbx + PATH] repe cmpsb je .send .next: add rbx, ROUTE_SIZE jmp .route .send: lea rdi, [response] ; status line, then the headers mov rsi, [rbx + STATUS] mov rcx, [rbx + STATUS_LEN] rep movsb mov rsi, [rbx + TYPE] mov rcx, [rbx + TYPE_LEN] rep movsb lea rsi, [length_header] mov ecx, length_header.len rep movsb mov rax, [rbx + BODY_LEN] call decimal lea rsi, [header_end] mov ecx, header_end.len rep movsb lea rsi, [response] mov rdx, rdi sub rdx, rsi call send_all mov rsi, [rbx + BODY] mov rdx, [rbx + BODY_LEN] call send_all .gone: ret ; Sends rdx bytes from rsi to r13, however many calls that takes. ; syscall keeps rsi and rdx, so the loop needs no saves. send_all: test rdx, rdx jz .done mov eax, SYS_SENDTO mov edi, r13d mov r10d, MSG_NOSIGNAL xor r8d, r8d xor r9d, r9d syscall test rax, rax jle .done ; the client went away add rsi, rax sub rdx, rax jmp send_all .done: ret ; Writes rax in decimal at rdi and leaves rdi just past the last digit. decimal: lea rsi, [digits + 20] ; build it backwards, copy it forwards mov r10d, 10 .split: xor edx, edx div r10 add dl, '0' dec rsi mov [rsi], dl test rax, rax jnz .split lea rcx, [digits + 20] sub rcx, rsi rep movsb ret ; rsi = a NUL-terminated port number. Returns it in eax, or exits with usage. parse_port: xor eax, eax .digit: movzx ecx, byte [rsi] inc rsi test ecx, ecx jz .end sub ecx, '0' cmp ecx, 9 ja fail_usage imul eax, eax, 10 add eax, ecx cmp eax, 65535 ja fail_usage jmp .digit .end: test eax, eax jz fail_usage ret fail_usage: lea rsi, [usage] mov edx, usage.len jmp die fail_socket: lea rsi, [no_socket] mov edx, no_socket.len jmp die fail_bind: lea rsi, [no_bind] mov edx, no_bind.len die: mov eax, SYS_WRITE mov edi, 2 ; stderr syscall mov eax, SYS_EXIT mov edi, 1 syscall section .rodata align 8 routes: ; path status content type body dq root, root.len, ok, ok.len, html, html.len, index, index.len dq health, health.len, ok, ok.len, plain, plain.len, alive, alive.len dq 0, 0, missing, missing.len, plain, plain.len, nothing, nothing.len garbled_route: dq 0, 0, bad, bad.len, plain, plain.len, garbled, garbled.len one: dd 1 text ok, "HTTP/1.0 200 OK", 13, 10 text missing, "HTTP/1.0 404 Not Found", 13, 10 text bad, "HTTP/1.0 400 Bad Request", 13, 10 text html, "Content-Type: text/html; charset=utf-8", 13, 10 text plain, "Content-Type: text/plain; charset=utf-8", 13, 10 text length_header, "Content-Length: " text header_end, 13, 10, "Connection: close", 13, 10, 13, 10 text root, "/" text health, "/health" text alive, "ok", 10 text nothing, "not found", 10 text garbled, "tiny-http answers GET and nothing else", 10 text usage, "usage: tiny-http [port 1-65535]", 10 text no_socket, "tiny-http: socket() failed", 10 text no_bind, "tiny-http: cannot bind that port", 10 index: incbin "index.html" index.len equ $ - index section .bss addr: resb 16 ; sockaddr_in request: resb REQUEST_MAX response: resb 256 digits: resb 20