writeonce/docs/examples/porch/http/nego.wo
shoney.arickathil ffd791d05b refactor(porch): name the web framework porch, fix the wo.toml identifier claim
- docs/examples/writeonce-serve -> docs/examples/porch (git mv, history kept);
  `[deps]` key and import are now `porch` / `porch/http` / `porch/router`
- name history preserved on the library README, not rewritten into dated
  records: writeonce-framework -> writeonce-serve (08-25) -> porch (08-26).
  Stories, specs, plans and the audit reports keep the older name by the
  repo's own convention; only live docs and every path link were rewritten
- left alone deliberately: `internal/serve.wo`, `pub fn serve`, `serve_conn`,
  `app.serve(...)` — those are functions, not the module name
- web-app/wo.toml comment corrected: it claimed hyphens are not identifier
  characters and named a key this file never used. lexer.ml's `is_ident_cont`
  DOES accept `-` (an internal dash is part of the identifier, which is why
  binary minus needs spaces), so a hyphenated key would be legal too
- site now teaches the name: package card, the two-deps chapter and the
  handlers-are-classes chapter say `porch`; site-accept asserted the old
  /packages/serve route and caught the rename, as a gate should
- gates: web-app 46/0, site 21/0, deps-accept 8/0, oop-e2e 116/0,
  linkcheck 0 broken / 0 anchors; porch typechecks entry-less as kind=library

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

49 lines
No EOL
1.8 KiB
Text

-- http/nego.wo — framework v1 slice 2: response-side content negotiation
-- and ETag / conditional requests (the crypto slice's first ledger
-- consumer beyond the WS handshake).
-- Does the request accept this media type? Absent Accept = yes (RFC 9110
-- §12.5.1: no header means anything goes). Matching is exact, type/*,
-- or */*; q-values are stripped, not ranked — v1 answers CAN I send
-- this, not WHICH ONE is best (a ranking negotiation waits for an app
-- that serves alternates).
pub fn accepts(req: Req, mtype: Text) -> Bool {
let acc = req.headers["accept"];
if acc == nil { return true; }
let slash = index_of(mtype, "/");
let major = mtype;
if slash >= 0 { major = substr(mtype, 0, slash); }
for part in split(to_lower("${acc}"), ",") {
let item = trim(part);
let semi = index_of(item, ";");
if semi >= 0 { item = trim(substr(item, 0, semi)); }
if item == mtype { return true; }
if item == "*/*" { return true; }
if item == "${major}/*" { return true; }
}
return false;
}
-- A strong ETag for a body: quoted base64 of its SHA-256. Deterministic,
-- content-addressed — two identical bodies share one tag across
-- restarts and shards.
pub fn etag_for(body: Text) -> Text {
return "\"${base64_encode(sha256(bytes_of_text(body)))}\"";
}
-- Stamp the response's ETag and collapse it to 304 when the request's
-- If-None-Match already has it. The 304 keeps the etag header and
-- drops the body (RFC 9110 §15.4.5). Call it last in a handler:
-- return with_etag(req, ok_json(body));
pub fn with_etag(req: Req, take r: Resp) -> Resp {
let tag = etag_for(r.body);
r.headers["etag"] = tag;
let inm = req.headers["if-none-match"];
if inm != nil {
if trim(inm) == tag {
r.status = 304;
r.body = "";
}
}
return r;
}