An HTTP/1.0 server for Linux in x86-64 assembly. No libc; every build starts it and curls it.
Route table: /, /health, and a 404 for everything elseAug 23
2# Starts the server on a spare port, asks it for three things, and checks
3# every answer. Run from the repository root after a build.
5port=$((20000 + $$ % 20000))
6./build/tiny-http "$port" &
7pid=$!
8trap 'kill "$pid" 2>/dev/null' EXIT INT TERM
9url="http://127.0.0.1:$port"
11for i in 1 2 3 4 5 6 7 8 9 10; do
Route table: /, /health, and a 404 for everything elseAug 23
12 curl -s -o /dev/null "$url/health" && break
14done
16fail() { echo "FAIL: $*"; exit 1; }
Serve index.html with a Content-Length counted at run timeSep 21
18page=$(curl -s -i "$url/") || fail "GET / got no answer"
19echo "$page" | head -n 1 | grep -q '200 OK' || fail "GET / did not answer 200"
20length=$(echo "$page" | tr -d '\r' | sed -n 's/^Content-Length: //p')
21body=$(curl -s "$url/" | wc -c)
22[ "$length" = "$body" ] || fail "Content-Length says $length, body has $body bytes"
23echo "GET / 200, $body bytes, Content-Length matches"
Route table: /, /health, and a 404 for everything elseAug 23
25[ "$(curl -s "$url/health")" = "ok" ] || fail "GET /health did not say ok"
26echo "GET /health 200, ok"
28code=$(curl -s -o /dev/null -w '%{http_code}' "$url/no/such/page")
29[ "$code" = 404 ] || fail "GET /no/such/page answered $code, not 404"
30echo "GET /no/... 404"
Send with MSG_NOSIGNAL so a client that hangs up can't kill the serverAug 29
32# Clients that hang up before reading must not take the server down.
33python3 - "$port" <<'PY'
34import socket, struct, sys
35for _ in range(20):
36 s = socket.create_connection(("127.0.0.1", int(sys.argv[1])))
37 s.sendall(b"GET / HTTP/1.0\r\n\r\n")
38 s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0))
39 s.close()
40PY
41[ "$(curl -s "$url/health")" = "ok" ] || fail "the server died after clients hung up"
42echo "20 clients hung up early; still serving"