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.

Send with MSG_NOSIGNAL so a client that hangs up can't kill the server

Browse files
write() to a socket the peer has reset raises SIGPIPE, and the default action ends the process. sendto(..., MSG_NOSIGNAL) returns EPIPE instead. send_all also loops until every byte is out.

The smoke test now resets 20 connections before reading, then checks the server still answers.
relayagentcommitted 1 parent 00ecec4 commit 39e4cf8834964166732a9be6216cf2445844f651
failing 3 steps in 3.14 s
2 files changed +33−7
modified src/server.asm +21−7
@@ -14,12 +14,14 @@
1414 %define SYS_CLOSE 3
1515 %define SYS_SOCKET 41
1616 %define SYS_ACCEPT 43
17+%define SYS_SENDTO 44
1718 %define SYS_BIND 49
1819 %define SYS_LISTEN 50
1920 %define SYS_EXIT 60
2021
2122 %define AF_INET 2
2223 %define SOCK_STREAM 1
24+%define MSG_NOSIGNAL 0x4000 ; a client that hangs up must not kill us
2325
2426 %define REQUEST_MAX 4096
2527
@@ -138,23 +140,35 @@ serve:
138140
139141 .send: mov rsi, [rbx + STATUS]
140142 mov rdx, [rbx + STATUS_LEN]
141− call put
143+ call send_all
142144 mov rsi, [rbx + TYPE]
143145 mov rdx, [rbx + TYPE_LEN]
144− call put
146+ call send_all
145147 lea rsi, [header_end]
146148 mov edx, header_end.len
147− call put
149+ call send_all
148150 mov rsi, [rbx + BODY]
149151 mov rdx, [rbx + BODY_LEN]
150− call put
152+ call send_all
151153 .gone: ret
152154
153−; Writes rdx bytes from rsi to r13.
154−put: mov eax, SYS_WRITE
155+; Sends rdx bytes from rsi to r13, however many calls that takes.
156+; syscall keeps rsi and rdx, so the loop needs no saves.
157+send_all:
158+ test rdx, rdx
159+ jz .done
160+ mov eax, SYS_SENDTO
155161 mov edi, r13d
162+ mov r10d, MSG_NOSIGNAL
163+ xor r8d, r8d
164+ xor r9d, r9d
156165 syscall
157− ret
166+ test rax, rax
167+ jle .done ; the client went away
168+ add rsi, rax
169+ sub rdx, rax
170+ jmp send_all
171+.done: ret
158172
159173 ; rsi = a NUL-terminated port number. Returns it in eax, or exits with usage.
160174 parse_port:
modified test/smoke.sh +12−0
@@ -25,4 +25,16 @@ code=$(curl -s -o /dev/null -w '%{http_code}' "$url/no/such/page")
2525 [ "$code" = 404 ] || fail "GET /no/such/page answered $code, not 404"
2626 echo "GET /no/... 404"
2727
28+# Clients that hang up before reading must not take the server down.
29+python3 - "$port" <<'PY'
30+import socket, struct, sys
31+for _ in range(20):
32+ s = socket.create_connection(("127.0.0.1", int(sys.argv[1])))
33+ s.sendall(b"GET / HTTP/1.0\r\n\r\n")
34+ s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0))
35+ s.close()
36+PY
37+[ "$(curl -s "$url/health")" = "ok" ] || fail "the server died after clients hung up"
38+echo "20 clients hung up early; still serving"
39+
2840 echo "smoke: 3 requests answered"