writeonce/docs/examples/writeonce-framework/http/serve.wo
shoney.arickathil 994d151a44 feat(framework): HTTP/1.1 parse + serialize + serve loop (iter 16 Task 2)
- http/parse.wo: bounded-read buffering to the header terminator, then
  exactly Content-Length body bytes; %XX decoding ('+' = space in query
  strings only, malformed escapes pass through — parsing stays total);
  path/query split with decoded pairs; header names lowercased; the
  three-state Parsed record (closed / malformed / request) with keep-alive
  carry-over — bytes past this request belong to the next one on the
  connection.
- http/serve.wo: Dispatcher interface (the router's seam), status/reason
  serialization with computed Content-Length, and the blocking loop:
  malformed -> 400 + close; a trapping handler -> 500 AND the loop lives;
  fds closed on every path; env.stopping() honored.
- Connection policy discovered by probing, not assumed: a parked keep-alive
  connection BLOCKS accept on a single-threaded server (probe: client 1
  idles open, client 2 starves). Policy: serve PIPELINED requests on one
  connection (carry non-empty), close when the client would idle; a proxy
  reconnects. README states it.

Verified against a throwaway echo app (not committed): %20 query decode;
two pipelined requests -> two responses on one connection; DIV0 handler ->
500 and the NEXT connection served; GARBAGE -> 400; SIGTERM stops clean.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-19 19:50:55 +02:00

80 lines
2.8 KiB
Text

-- http/serve.wo — response serialization + the blocking keep-alive serve
-- loop. The dispatch seam is the Dispatcher interface (the router's App
-- satisfies it, Task 3); it is wrapped in `try`, so a trapping handler
-- answers 500 and the loop lives — a bad request must never kill the
-- server. Every accept path closes its fd; `env.stopping()` is honored
-- between connections and between keep-alive requests (a SIGTERM landing
-- in a blocking call already unwinds cleanly — the runtime's stop story).
use net
use env
pub interface Dispatcher {
fn dispatch(req: Req) -> Resp
}
fn status_text(code: Int) -> Text {
switch code {
case 200: return "OK";
case 201: return "Created";
case 302: return "Found";
case 400: return "Bad Request";
case 401: return "Unauthorized";
case 404: return "Not Found";
case 409: return "Conflict";
case 500: return "Internal Server Error";
default: return "Status";
}
}
pub fn serialize(resp: Resp, keep: Bool) -> Text {
let head = "HTTP/1.1 ${resp.status} ${status_text(resp.status)}\r\n";
for k, v in resp.headers {
head = head .. "${k}: ${v}\r\n";
}
if keep {
head = head .. "Connection: keep-alive\r\n";
} else {
head = head .. "Connection: close\r\n";
}
head = head .. "Content-Length: ${len(resp.body)}\r\n\r\n";
return head .. resp.body;
}
pub fn serve(host: Text, port: Int, d: Dispatcher) -> Int {
let srv = net.listen(host, port);
print("listening on ${host}:${port}");
while true {
if env.stopping() { net.close(srv); return 0; }
let c = net.accept(srv);
let carry = "";
let alive = true;
while alive {
if env.stopping() { alive = false; continue; }
let p = try parse_request(c, carry) catch (e) nil; -- an IO trap = gone
if p == nil { alive = false; continue; }
if p.closed { alive = false; continue; }
if p.ok == false {
try net.write(c, serialize(bad_request("malformed request"), false)) catch (e) {}
alive = false;
continue;
}
let r = p.req;
if r == nil { alive = false; continue; }
carry = p.rest;
-- Connection policy for a single-threaded server: serve PIPELINED
-- requests on one connection (bytes already buffered), but close when
-- the client would idle — a parked keep-alive connection would block
-- `accept` and starve every other client. A proxy in front simply
-- reconnects; this is the honest shape until shards/fibers (8/11).
let keep = len(carry) > 0;
let conn = r.headers["connection"];
if conn != nil {
if to_lower(conn) == "close" { keep = false; }
}
let resp = try d.dispatch(r) catch (e) server_error();
try net.write(c, serialize(resp, keep)) catch (e) { alive = false; }
if keep == false { alive = false; }
}
net.close(c);
}
}