A tiny HTTP server
The accept loop
A server is a loop: accept a connection, serve it, close it, repeat. accept (43) blocks until a client arrives, so one process serves one client at a time:
server_loop: mov rax, 43 ; sys_accept mov rdi, listen_fd xor rsi, rsi xor rdx, rdx syscall mov r12, rax ; connection fd ; serve, then close and loop
Reading the request
Read the request bytes into a buffer. The request line is the first line: METHOD, path, and version separated by spaces. Parse until the first CRLF, then answer - a real server reads headers until a blank line.
Writing the response
A minimal response is a status line, one header, a blank line, and the body. Write it with sys_write on the connection descriptor, close the descriptor, and jump back to accept:
mov rax, 1 mov rdi, r12 mov rsi, response mov rdx, resplen syscall
The code on this page was not run: it needs a network service.
Article author: Arthur Isaev