writeonce/docs/examples/writeonce-framework/internal/parse.wo
shoney.arickathil 771180fd28 feat: iterations 19 + 17 — Float/Bytes scalars (.wob v5), library kind + internal/
- Float full stack: literals (fraction/exponent; `0..10` still a range), f64
  opcodes 34-41, @table column, WAL bit-exact replay, json fractions in and
  shortest-round-trip out. IEEE-quiet — FDIV never traps where DIV does.
- Bytes: a wo_str with its own class id, so alloc/free/copy are shared but no
  Text builtin accepts one; len/at/slice/eq/concat, base64 both ways, json
  boundary as base64; TEXT_COPY preserves the kind.
- No implicit Int/Float mixing (WO-E201 in the typechecker, not the emitter,
  which picks the opcode from one side and would misread the other).
- One IEEE deviation: float_cmp total order (NaN last, -0.0 == +0.0) for
  indexes and order-by, keys canonicalized to match. `?Float` nil is a
  reserved quiet NaN — the zero word is +0.0, WO_NIL_SCALAR's bits are -2.0.
- Renderer prefers fixed over exponential in 1e-6..1e21: pure shortest makes
  a price of 900.0 read `9e+02`. One renderer for interp/json/float_to_text.
- Fixed en route: lexer double-counted the leading digit; is_scalar_shaped
  took Float/Bytes as Int-shaped; Bytes ownership needed a shared heap-scalar
  predicate or temps never dropped; order-by bit-compared negatives backwards.
- Iteration 17: `kind = "library"` (absent = program; bad value = WO-E109),
  entry-less check mode retiring the `--emit` workaround, Go's `internal/` as
  WO-E108 at the consumer's `use`. Driver-only; VM/.wob/GC untouched.
- Framework reorg: internal/{parse,serve}.wo; http/form.wo split out to keep
  media_type/form_values public (parse.wo had grown public surface).
- Docs: link audit (97 -> 88 broken, conflict markers resolved, 2 duplicate
  stories removed), 00-code-review verified 26/27, iterations re-sequenced.
- Also carries the pre-staged pub(read)/using/#if work from the index.
- Gates: corpus 103/0, test_wal 156/0, web-app 26/0, oop-accept ALL MET.

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

155 lines
4.9 KiB
Text

-- internal/parse.wo — HTTP/1.1 request parsing over a net connection.
--
-- INTERNAL (iteration 17): a consumer cannot `use` this module — the
-- `internal/` segment makes that WO-E108. The connection-level parser, the
-- carry-state record, and the %XX/query decoders are the framework's own
-- business; `media_type`/`form_values` are the public half and live in
-- `http/form.wo`.
--
-- Bounded reads only (`net.read`), so requests are buffered to the header
-- terminator, then the body to exactly Content-Length. Keep-alive means
-- bytes past this request belong to the NEXT one: the caller passes the
-- carry-over in and gets the new remainder back in Parsed.rest.
--
-- Parsed is a three-state answer (no tuples in the language):
-- closed=true peer ended the connection cleanly between requests
-- ok=false malformed request — answer 400 and close
-- ok=true, req non-nil one complete request
use net
use http -- Req lives in the public module now
const BODY_MAX = 1048576
pub typedef Parsed = {
closed: Bool,
ok: Bool,
?req: Req,
rest: Text
}
-- %XX decoding, '+' as space when plus_space (query strings only).
-- Malformed escapes pass through verbatim — parsing stays total.
fn hex_val(b: Int) -> Int {
if b >= 48 and b <= 57 { return b - 48; } -- 0-9
if b >= 97 and b <= 102 { return b - 87; } -- a-f
if b >= 65 and b <= 70 { return b - 55; } -- A-F
return -1;
}
pub fn url_decode(t: Text, plus_space: Bool) -> Text {
let out = "";
let i = 0;
let n = len(t);
while i < n {
let b = byte_at(t, i);
if b == 37 and i + 2 < n { -- '%'
let hi = hex_val(byte_at(t, i + 1));
let lo = hex_val(byte_at(t, i + 2));
if hi >= 0 and lo >= 0 {
out = out .. char_of(hi * 16 + lo);
i = i + 3;
continue;
}
}
if plus_space and b == 43 { -- '+'
out = out .. " ";
i = i + 1;
continue;
}
out = out .. substr(t, i, 1);
i = i + 1;
}
return out;
}
-- "a=1&b=hello+world" -> decoded pairs; a bare key maps to ""
pub fn parse_query(qs: Text) -> map<Text, Text> {
let q: map<Text, Text> = {};
if qs == "" { return q; }
for pair in split(qs, "&") {
if pair == "" { continue; }
let eq = index_of(pair, "=");
if eq < 0 {
q[url_decode(pair, true)] = "";
} else {
q[url_decode(substr(pair, 0, eq), true)] = url_decode(substr(pair, eq + 1, len(pair) - eq - 1), true);
}
}
return q;
}
fn malformed(rest: Text) -> Parsed {
return Parsed { closed: false, ok: false, req: nil, rest: rest };
}
-- One request off the connection. `carry` = leftover bytes from the same
-- connection's previous request (keep-alive).
pub fn parse_request(c: net.Conn, carry: Text) -> Parsed {
let buf = carry;
let header_end = index_of(buf, "\r\n\r\n");
while header_end == -1 {
let got = net.read(c, 8192);
if len(got) == 0 {
-- peer closed: clean between requests (empty buffer), torn otherwise
if trim(buf) == "" { return Parsed { closed: true, ok: true, req: nil, rest: "" }; }
return malformed("");
}
buf = buf .. got;
header_end = index_of(buf, "\r\n\r\n");
if header_end == -1 and len(buf) > BODY_MAX { return malformed(""); }
}
let lines = split(substr(buf, 0, header_end), "\r\n");
let req_line = split_ws(trim(lines[0]));
if len(req_line) < 3 { return malformed(""); }
let method = req_line[0];
let target = req_line[1];
-- path / query split, both %-decoded ('+' is a space only in the query)
let path = target;
let query: map<Text, Text> = {};
let qm = index_of(target, "?");
if qm >= 0 {
path = substr(target, 0, qm);
query = parse_query(substr(target, qm + 1, len(target) - qm - 1));
}
path = url_decode(path, false);
let headers: map<Text, Text> = {};
let i = 1;
while i < len(lines) {
let line = trim(lines[i]);
i = i + 1;
if line == "" { continue; }
let colon = index_of(line, ":");
if colon > 0 {
headers[to_lower(substr(line, 0, colon))] = trim(substr(line, colon + 1, len(line) - colon - 1));
}
}
let want = 0;
let cl = headers["content-length"];
if cl != nil {
let n = parse_int(cl);
if n == nil { return malformed(""); }
if n < 0 or n > BODY_MAX { return malformed(""); }
want = n;
}
let body = substr(buf, header_end + 4, len(buf) - header_end - 4);
while len(body) < want {
let got = net.read(c, 8192);
if len(got) == 0 { return malformed(""); } -- peer died mid-body
body = body .. got;
}
-- bytes past the declared body belong to the next request on this conn
let rest = "";
if len(body) > want {
rest = substr(body, want, len(body) - want);
body = substr(body, 0, want);
}
let req = Req { method: method, path: path, params: {}, query: query,
headers: headers, body: body, principal: "" };
return Parsed { closed: false, ok: true, req: req, rest: rest };
}