The design language that makes your AI harness better at design.
JavaScript
Replace tiny_http with the shared HTTP core in serve-question and component review (#1072)
Paul Bakaus1e46f67ee822
diff --git a/crates/common/Cargo.toml b/crates/common/Cargo.tomlindex 3fb637e01..e967acd3f 100644--- a/crates/common/Cargo.toml+++ b/crates/common/Cargo.toml@@ -8,12 +8,12 @@ publish.workspace = true[features]# Seed keys and references (`seed` module): CSPRNG keys and a salted,# one-way reference. Off by default so the wasm build never pulls getrandom.-seed = ["dep:sha2", "dep:getrandom", "dep:serde_json"]+seed = ["dep:sha2", "dep:getrandom"][dependencies]sha2 = { version = "0.10", optional = true }getrandom = { version = "0.2", optional = true }-serde_json = { workspace = true, optional = true }+serde_json = { workspace = true }[target.'cfg(unix)'.dependencies]libc = "0.2"diff --git a/crates/common/src/http.rs b/crates/common/src/http.rsnew file mode 100644index 000000000..26b0a09a8--- /dev/null+++ b/crates/common/src/http.rs@@ -0,0 +1,1302 @@+//! A small HTTP/1.1 server core shared by the engine's local servers:+//! `TcpListener`, one thread per connection, request parsing (headers ++//! Content-Length / chunked body), plain responses, and long-lived streamed+//! responses (SSE, parked polls) with client-close detection. Every response+//! closes its connection. Stands in for `node:http`; nothing here is+//! contract-visible except the routes the servers build on it.+//!+//! The live helper (`impeccable live-server`) drives connections itself. The+//! decision page (`serve-question`) and component review handle one request+//! at a time on their own thread, which [`SerialServer`] provides.++use std::collections::HashMap;+use std::io::{BufRead, BufReader, Read, Write};+use std::net::{Shutdown, SocketAddr, TcpListener, TcpStream, ToSocketAddrs};+use std::sync::atomic::{AtomicBool, Ordering};+use std::sync::mpsc::{channel, Receiver, RecvTimeoutError};+use std::sync::Arc;+use std::time::Duration;++pub struct Request {+ pub method: String,+ /// Raw request target (path + query), as `req.url` in Node.+ pub target: String,+ pub path: String,+ pub query: Vec<(String, String)>,+ /// Lower-cased header names.+ pub headers: HashMap<String, String>,+ pub body: Vec<u8>,+ /// Set when the body exceeded `max_body` while reading (payload too large).+ pub body_truncated: bool,+}++impl Request {+ /// `url.searchParams.get(name)`: first value or None.+ pub fn query_get(&self, name: &str) -> Option<&str> {+ self.query+ .iter()+ .find(|(k, _)| k == name)+ .map(|(_, v)| v.as_str())+ }+ pub fn header(&self, name: &str) -> Option<&str> {+ self.headers+ .get(&name.to_ascii_lowercase())+ .map(String::as_str)+ }+}++/// Percent-decode with `+` as space (`URLSearchParams` semantics), lossy UTF-8.+pub fn decode_query_component(s: &str) -> String {+ let bytes = s.as_bytes();+ let mut out: Vec<u8> = Vec::with_capacity(bytes.len());+ let mut i = 0;+ while i < bytes.len() {+ match bytes[i] {+ b'+' => {+ out.push(b' ');+ i += 1;+ }+ b'%' if i + 2 < bytes.len() => {+ let h = (bytes[i + 1] as char).to_digit(16);+ let l = (bytes[i + 2] as char).to_digit(16);+ match (h, l) {+ (Some(h), Some(l)) => {+ out.push((h * 16 + l) as u8);+ i += 3;+ }+ _ => {+ out.push(b'%');+ i += 1;+ }+ }+ }+ b => {+ out.push(b);+ i += 1;+ }+ }+ }+ String::from_utf8_lossy(&out).into_owned()+}++/// Split a request target into (pathname, query pairs). Mirrors what+/// `new URL(req.url, base)` exposes: the pathname is kept as sent (minus a+/// fragment), search params are decoded.+pub fn parse_target(target: &str) -> (String, Vec<(String, String)>) {+ let no_frag = target.split('#').next().unwrap_or("");+ let (path, query) = match no_frag.find('?') {+ Some(i) => (&no_frag[..i], &no_frag[i + 1..]),+ None => (no_frag, ""),+ };+ let mut pairs = Vec::new();+ for part in query.split('&') {+ if part.is_empty() {+ continue;+ }+ let (k, v) = match part.find('=') {+ Some(i) => (&part[..i], &part[i + 1..]),+ None => (part, ""),+ };+ pairs.push((decode_query_component(k), decode_query_component(v)));+ }+ let path = if path.is_empty() {+ "/".to_string()+ } else {+ normalize_path(path)+ };+ (path, pairs)+}++/// WHATWG URL path normalization for the cases that matter here: collapse+/// `.` and `..` segments.+fn normalize_path(p: &str) -> String {+ let mut segs: Vec<&str> = Vec::new();+ for seg in p.split('/').skip(1) {+ match seg {+ "." => {}+ ".." => {+ segs.pop();+ }+ s => segs.push(s),+ }+ }+ let mut out = String::from("/");+ out.push_str(&segs.join("/"));+ if p.ends_with("/..") || p.ends_with("/.") {+ if !out.ends_with('/') {+ out.push('/');+ }+ }+ out+}++/// Ceiling on the request head (request line + headers). Node's http server+/// rejects heads over ~16 KB by default (`maxHeaderSize`); this hand-rolled+/// parser used to accumulate header lines unbounded, letting a hostile+/// pre-auth localhost connection grow memory without limit. 64 KB keeps 4x+/// headroom over Node's default for legitimate local tooling while staying+/// finite. A head that exhausts the budget reads as EOF below, so the request+/// is dropped the way any malformed input is: connection closed, no panic.+const MAX_HEAD_BYTES: u64 = 64 * 1024;++/// Read one HTTP request from the stream. `max_body` caps the body read; a+/// body over the cap sets `body_truncated` (the caller answers 413). A body+/// that ends early (EOF or a read timeout before Content-Length bytes, a+/// chunked body without its terminating chunk, a malformed chunk size) is+/// not a request: it reads as None and the connection is dropped, so a+/// handler never acts on part of an upload.+pub fn read_request(stream: &TcpStream, max_body: usize) -> Option<Request> {+ let mut reader = BufReader::new(stream.try_clone().ok()?).take(MAX_HEAD_BYTES);+ let mut line = String::new();+ // Request line (skip stray CRLFs between requests).+ loop {+ line.clear();+ let n = reader.read_line(&mut line).ok()?;+ if n == 0 {+ return None;+ }+ if line.trim_end_matches(['\r', '\n']).is_empty() {+ continue;+ }+ break;+ }+ let req_line = line.trim_end_matches(['\r', '\n']).to_string();+ let mut parts = req_line.split_whitespace();+ let method = parts.next()?.to_string();+ let target = parts.next().unwrap_or("/").to_string();+ let mut headers: HashMap<String, String> = HashMap::new();+ loop {+ line.clear();+ let n = reader.read_line(&mut line).ok()?;+ if n == 0 {+ return None;+ }+ let l = line.trim_end_matches(['\r', '\n']);+ if l.is_empty() {+ break;+ }+ if let Some(i) = l.find(':') {+ let name = l[..i].trim().to_ascii_lowercase();+ let value = l[i + 1..].trim().to_string();+ headers.entry(name).or_insert(value);+ }+ }+ // The head is done; the body has its own cap (`max_body`), so drop the+ // head budget and read the body from the underlying buffered stream.+ let mut reader = reader.into_inner();+ let mut body: Vec<u8> = Vec::new();+ let mut truncated = false;+ let content_length = headers+ .get("content-length")+ .and_then(|v| v.trim().parse::<usize>().ok());+ let chunked = content_length.is_none()+ && headers+ .get("transfer-encoding")+ .map(|v| v.to_ascii_lowercase().contains("chunked"))+ .unwrap_or(false);+ // A client that sent `Expect: 100-continue` holds its body until told to+ // go on. Say so whenever the body will be read; an over-cap+ // Content-Length is refused with the final response instead.+ let expects_continue = headers+ .get("expect")+ .is_some_and(|v| v.eq_ignore_ascii_case("100-continue"));+ let body_wanted = match content_length {+ Some(len) => len > 0 && len <= max_body,+ None => chunked,+ };+ if expects_continue && body_wanted {+ let mut out = stream;+ if out.write_all(b"HTTP/1.1 100 Continue\r\n\r\n").is_err() || out.flush().is_err() {+ return None;+ }+ }+ if let Some(len) = content_length {+ if len > max_body {+ truncated = true;+ // Drain what we can so the peer does not see a reset before the+ // response, then stop. A client waiting on 100-continue sends+ // nothing, so there is nothing to drain.+ let mut remaining = if expects_continue { 0 } else { len };+ let mut buf = [0u8; 8192];+ let mut drained = 0usize;+ while remaining > 0 && drained < max_body + 65536 {+ let want = remaining.min(buf.len());+ match reader.read(&mut buf[..want]) {+ Ok(0) | Err(_) => break,+ Ok(n) => {+ remaining -= n;+ drained += n;+ }+ }+ }+ } else {+ body.resize(len, 0);+ let mut read = 0;+ while read < len {+ match reader.read(&mut body[read..]) {+ Ok(0) => break,+ Ok(n) => read += n,+ Err(_) => break,+ }+ }+ if read < len {+ // The upload ended short: never hand on part of it.+ return None;+ }+ }+ } else if chunked {+ loop {+ line.clear();+ if reader.read_line(&mut line).ok()? == 0 {+ // EOF before the terminating chunk.+ return None;+ }+ let size_str = line.trim().split(';').next().unwrap_or("").trim();+ let size = usize::from_str_radix(size_str, 16).ok()?;+ if size == 0 {+ // trailer + final CRLF+ loop {+ line.clear();+ let n = reader.read_line(&mut line).unwrap_or(0);+ if n == 0 || line.trim_end_matches(['\r', '\n']).is_empty() {+ break;+ }+ }+ break;+ }+ // The declared size comes straight off the wire: never allocate+ // it up front (a huge chunk-size line would OOM-abort the process+ // under panic=abort). Stream the chunk through a small buffer and+ // stop as soon as the body would exceed the cap. The comparison+ // must not add first: `body.len() + size` wraps when `size` is+ // near usize::MAX, which would bypass the cap entirely.+ if size > max_body.saturating_sub(body.len()) {+ truncated = true;+ break;+ }+ let mut remaining = size;+ let mut buf = [0u8; 8192];+ let mut eof = false;+ while remaining > 0 {+ let want = remaining.min(buf.len());+ match reader.read(&mut buf[..want]) {+ Ok(0) | Err(_) => {+ eof = true;+ break;+ }+ Ok(n) => {+ remaining -= n;+ body.extend_from_slice(&buf[..n]);+ }+ }+ }+ if eof {+ return None;+ }+ line.clear();+ if reader.read_line(&mut line).ok()? == 0 {+ return None;+ }+ }+ }+ let (path, query) = parse_target(&target);+ Some(Request {+ method,+ target,+ path,+ query,+ headers,+ body,+ body_truncated: truncated,+ })+}++fn reason(status: u16) -> &'static str {+ match status {+ 200 => "OK",+ 202 => "Accepted",+ 204 => "No Content",+ 400 => "Bad Request",+ 401 => "Unauthorized",+ 403 => "Forbidden",+ 404 => "Not Found",+ 409 => "Conflict",+ 410 => "Gone",+ 413 => "Payload Too Large",+ 415 => "Unsupported Media Type",+ 422 => "Unprocessable Entity",+ 500 => "Internal Server Error",+ _ => "",+ }+}++/// A response under construction: status, headers (in insertion order), body.+pub struct Response {+ pub status: u16,+ pub headers: Vec<(String, String)>,+ pub body: Vec<u8>,+}++impl Response {+ pub fn new(status: u16) -> Response {+ Response {+ status,+ headers: Vec::new(),+ body: Vec::new(),+ }+ }+ pub fn header(mut self, name: &str, value: &str) -> Response {+ self.headers.push((name.to_string(), value.to_string()));+ self+ }+ pub fn text(mut self, body: &str) -> Response {+ self.body = body.as_bytes().to_vec();+ self+ }+ pub fn bytes(mut self, body: Vec<u8>) -> Response {+ self.body = body;+ self+ }+ pub fn json(self, v: &serde_json::Value) -> Response {+ let s = serde_json::to_string(v).unwrap_or_else(|_| "null".into());+ self.header("Content-Type", "application/json").text(&s)+ }+}++/// Write a complete response and close the connection.+pub fn send_response(stream: &mut TcpStream, cors: &[(String, String)], res: &Response) {+ let mut head = format!("HTTP/1.1 {} {}\r\n", res.status, reason(res.status));+ for (k, v) in cors {+ head.push_str(&format!("{}: {}\r\n", k, v));+ }+ for (k, v) in &res.headers {+ head.push_str(&format!("{}: {}\r\n", k, v));+ }+ head.push_str(&format!("Content-Length: {}\r\n", res.body.len()));+ head.push_str("Connection: close\r\n\r\n");+ let _ = stream.write_all(head.as_bytes());+ let _ = stream.write_all(&res.body);+ let _ = stream.flush();+ let _ = stream.shutdown(Shutdown::Write);+}++/// A long-lived response written incrementally (chunked transfer encoding).+pub struct StreamResponse {+ stream: TcpStream,+ open: bool,+}++impl StreamResponse {+ pub fn begin(+ mut stream: TcpStream,+ cors: &[(String, String)],+ status: u16,+ headers: &[(&str, &str)],+ ) -> StreamResponse {+ let mut head = format!("HTTP/1.1 {} {}\r\n", status, reason(status));+ for (k, v) in cors {+ head.push_str(&format!("{}: {}\r\n", k, v));+ }+ for (k, v) in headers {+ head.push_str(&format!("{}: {}\r\n", k, v));+ }+ head.push_str("Transfer-Encoding: chunked\r\n\r\n");+ let ok = stream.write_all(head.as_bytes()).is_ok() && stream.flush().is_ok();+ StreamResponse { stream, open: ok }+ }+ /// Write one chunk; false when the client is gone.+ pub fn write(&mut self, data: &[u8]) -> bool {+ if !self.open || data.is_empty() {+ return self.open;+ }+ let head = format!("{:x}\r\n", data.len());+ let ok = self.stream.write_all(head.as_bytes()).is_ok()+ && self.stream.write_all(data).is_ok()+ && self.stream.write_all(b"\r\n").is_ok()+ && self.stream.flush().is_ok();+ if !ok {+ self.open = false;+ }+ ok+ }+ pub fn end(&mut self) {+ if self.open {+ let _ = self.stream.write_all(b"0\r\n\r\n");+ let _ = self.stream.flush();+ self.open = false;+ }+ let _ = self.stream.shutdown(Shutdown::Both);+ }+ pub fn is_open(&self) -> bool {+ self.open+ }+}++/// Watch a connection for the peer closing it (Node's `req.on('close')`):+/// a reader thread blocks on the socket and calls `on_close` when the read+/// returns EOF or an error, unless `done` was set first (we closed it).+pub fn watch_close(+ stream: &TcpStream,+ done: Arc<AtomicBool>,+ on_close: impl FnOnce() + Send + 'static,+) {+ let Ok(mut clone) = stream.try_clone() else {+ return;+ };+ let _ = clone.set_read_timeout(None);+ std::thread::spawn(move || {+ let mut buf = [0u8; 64];+ loop {+ match clone.read(&mut buf) {+ Ok(0) | Err(_) => break,+ Ok(_) => continue,+ }+ }+ if !done.load(Ordering::SeqCst) {+ on_close();+ }+ });+}++/// Set a per-`read` timeout on the socket. This is `SO_RCVTIMEO`: it bounds a+/// single blocked `read`, not the whole connection, and any byte received+/// resets it. It is a coarse backstop only; the total lane-hold bound is+/// [`read_request_deadline`], not this.+pub fn set_header_timeout(stream: &TcpStream) {+ let _ = stream.set_read_timeout(Some(Duration::from_secs(60)));+}++/// Total wall-clock budget for reading one request (request line + headers ++/// body) from an accepted connection while it may still hold a place in the+/// turnstile lane.+///+/// [`set_header_timeout`] is per-`read` (`SO_RCVTIMEO`): any byte inside the+/// window resets it, so a slow-drip or silent peer keeps each `read` returning+/// `Ok(1)` (or blocking under the window) and `read_request` never returns,+/// pinning the connection's ticket and wedging every later mutation behind it.+/// This is one deadline across the whole read, enforced by a watchdog that+/// shuts the socket down when it elapses, turning that pre-auth lane-hold DoS+/// into a bounded, localhost-only stall (BLOCKER, triage D3). 10s is far more+/// than any legitimate local upload needs (the race test's ~4 KB over ~500 ms+/// finishes in a twentieth of it).+pub const READ_REQUEST_DEADLINE: Duration = Duration::from_secs(10);++/// Like [`read_request`] but bounded by a total wall-clock `deadline` across+/// the entire head+body read. A watchdog thread shuts the socket down if the+/// deadline elapses before the read finishes, so the blocked `read` returns and+/// `read_request` gives up (`None`, or a truncated body); the caller returns+/// and the ticket is released on `Drop`. A connection therefore cannot hold its+/// place in the lane past `deadline`, no matter how slowly (or never) it feeds+/// the socket.+pub fn read_request_deadline(+ stream: &TcpStream,+ max_body: usize,+ deadline: Duration,+) -> Option<Request> {+ // Bound the read at the socket as well as at the watchdog. Windows does+ // not unblock a `recv` already parked in the kernel when another thread+ // calls `shutdown` on the same socket, so there the watchdog alone cannot+ // enforce the deadline and a silent connection held its ticket for the+ // whole 60s header timeout. A read that makes no progress for the deadline+ // now returns on every platform and the handler drops its ticket; the+ // watchdog stays as the backstop for a connection that keeps trickling+ // bytes without ever completing a request.+ let prev_timeout = stream.read_timeout().ok().flatten();+ let _ = stream.set_read_timeout(Some(deadline));+ let done = Arc::new(AtomicBool::new(false));+ let watch_stream = stream.try_clone().ok()?;+ let watch_done = done.clone();+ std::thread::spawn(move || {+ // Poll the done flag so a fast, normal read frees the watchdog promptly+ // instead of pinning a thread for the whole deadline.+ let step = Duration::from_millis(50);+ let mut waited = Duration::ZERO;+ while waited < deadline {+ if watch_done.load(Ordering::SeqCst) {+ return;+ }+ std::thread::sleep(step);+ waited += step;+ }+ if !watch_done.load(Ordering::SeqCst) {+ // Unblock any in-progress read so `read_request` returns; the+ // handler then drops its ticket and the connection closes.+ let _ = watch_stream.shutdown(Shutdown::Both);+ }+ });+ let req = read_request(stream, max_body);+ done.store(true, Ordering::SeqCst);+ let _ = stream.set_read_timeout(prev_timeout);+ req+}++/// Serializes state-mutating request handlers in body-completion order.+///+/// Node's `http` server is single-threaded: a `/events` handler reads its+/// body, and its state mutation runs inside the `end` callback, atomically,+/// before any other request's `end` callback. So the server that creates a+/// session (`generate`) and the checkpoint that references it+/// (`generate_started`) never interleave; the checkpoint sees a store the+/// `generate` before it already wrote. This server is thread-per-connection,+/// so without help two concurrent POSTs race on who reaches the state lock+/// first, and a checkpoint whose small body finishes fast can overtake the+/// `generate` that creates its session, which turns into `404+/// unknown_session`.+///+/// This is the single FIFO those mutations pass through. The accept loop+/// hands every connection a ticket, so tickets are ordered by arrival. A+/// handler reads and parses its FULL request first (that stays parallel);+/// only then, before it touches server state, does it call+/// [`Ticket::wait_turn`], which blocks until every earlier ticket has been+/// released. There is **no timeout**: a checkpoint waits for the earlier+/// `generate` however long that `generate`'s body takes to arrive, so the+/// ordering is deterministic regardless of connection speed. A ticket is+/// released when its handler finishes, when the connection is about to park+/// (SSE, long polls) or run something long, or when the connection dies+/// (`Drop`) without producing a request. So a handler only ever blocks on+/// earlier connections that are alive and still working.+///+/// The wait is unbounded on purpose (ordering must not give up), so on its own+/// a slow-drip or silent connection accepted first would pin its ticket inside+/// [`read_request`] and wedge every later mutation forever. That is prevented+/// upstream, not here: a connection reads its request under a total wall-clock+/// deadline ([`read_request_deadline`]), so a peer that never finishes its+/// request is force-dropped and releases its ticket within that bound. A hung+/// or abandoned upload can delay a later mutation by at most the deadline, and+/// only from localhost.+///+/// Read-only routes (`/status`, `/health`, `/design-system*`, `/source`,+/// `/live.js`, `/detect.js`, `/`) and long-lived routes (SSE, parked polls)+/// do not sit in this lane: they release their ticket up front (read-only)+/// or as soon as they have registered and are about to park (long-lived),+/// so a mutation never waits on a stream and a stream never holds the lane.+pub struct Turnstile {+ inner: std::sync::Mutex<TurnstileState>,+ cv: std::sync::Condvar,+}++struct TurnstileState {+ next: u64,+ /// Tickets issued but not yet released.+ outstanding: std::collections::BTreeSet<u64>,+}++impl Default for Turnstile {+ fn default() -> Self {+ Turnstile {+ inner: std::sync::Mutex::new(TurnstileState {+ next: 1,+ outstanding: std::collections::BTreeSet::new(),+ }),+ cv: std::sync::Condvar::new(),+ }+ }+}++impl Turnstile {+ fn state(&self) -> std::sync::MutexGuard<'_, TurnstileState> {+ match self.inner.lock() {+ Ok(g) => g,+ Err(p) => p.into_inner(),+ }+ }++ /// Called by the accept loop, in accept order.+ pub fn issue(self: &Arc<Self>) -> Ticket {+ let mut st = self.state();+ let n = st.next;+ st.next += 1;+ st.outstanding.insert(n);+ Ticket {+ turnstile: self.clone(),+ n,+ released: false,+ }+ }++ /// Block until every earlier outstanding ticket is released. No timeout:+ /// ordering is by arrival and never gives up, so a later mutation cannot+ /// overtake an earlier one whose body is still arriving. An earlier+ /// connection that dies without producing a request releases its ticket+ /// on `Drop`, so this wait only ever tracks earlier connections that are+ /// alive and still working.+ fn wait_turn(&self, n: u64) {+ let mut st = self.state();+ loop {+ match st.outstanding.iter().next() {+ Some(&first) if first < n => {}+ _ => return,+ }+ st = match self.cv.wait(st) {+ Ok(g) => g,+ Err(p) => p.into_inner(),+ };+ }+ }++ fn release(&self, n: u64) {+ let mut st = self.state();+ if st.outstanding.remove(&n) {+ self.cv.notify_all();+ }+ }+}++/// One connection's place in the arrival order. Dropping it releases the+/// ticket, so a connection that never produces a request (EOF, timeout)+/// cannot hold later ones up: it leaves the lane the instant it dies.+pub struct Ticket {+ turnstile: Arc<Turnstile>,+ n: u64,+ released: bool,+}++impl Ticket {+ /// Wait for the connections accepted before this one to finish with+ /// server state. Call before the first state access of a handler; a+ /// no-op after `release`.+ pub fn wait_turn(&self) {+ if !self.released {+ self.turnstile.wait_turn(self.n);+ }+ }+ /// Let later connections proceed. Call before parking or before running+ /// anything long that does not need arrival ordering.+ pub fn release(&mut self) {+ if !self.released {+ self.released = true;+ self.turnstile.release(self.n);+ }+ }+}++impl Drop for Ticket {+ fn drop(&mut self) {+ self.release();+ }+}++/// One request read off its own connection, waiting for its answer.+/// Dropping it unanswered sends a bare 500, so a connection never hangs.+pub struct Exchange {+ pub request: Request,+ stream: Option<TcpStream>,+}++impl Exchange {+ /// Write the response and close the connection.+ pub fn respond(mut self, res: &Response) {+ if let Some(mut stream) = self.stream.take() {+ send_response(&mut stream, &[], res);+ }+ }+}++impl Drop for Exchange {+ fn drop(&mut self) {+ if let Some(mut stream) = self.stream.take() {+ send_response(&mut stream, &[], &Response::new(500));+ }+ }+}++/// A server whose requests are handled one at a time on the caller's thread,+/// in the order their requests finished arriving.+///+/// Each accepted connection gets its own thread that reads one request (under+/// [`READ_REQUEST_DEADLINE`]) and queues it; the caller takes requests with+/// [`SerialServer::recv`] or [`SerialServer::recv_timeout`] and answers each+/// with [`Exchange::respond`]. No connection waits on another to be read, so a+/// browser that opens a burst of sockets and holds some idle gets every+/// request answered. Handlers stay single-threaded, so they share state+/// without locking. Dropping the server stops accepting and frees the port.+pub struct SerialServer {+ rx: Receiver<Exchange>,+ addr: SocketAddr,+ stop: Arc<AtomicBool>,+}++impl SerialServer {+ /// Bind `addr` and start accepting. `max_body` caps each request body;+ /// a body over the cap arrives with `body_truncated` set.+ pub fn bind(addr: impl ToSocketAddrs, max_body: usize) -> std::io::Result<SerialServer> {+ SerialServer::from_listener(TcpListener::bind(addr)?, max_body)+ }++ pub fn from_listener(listener: TcpListener, max_body: usize) -> std::io::Result<SerialServer> {+ let addr = listener.local_addr()?;+ let stop = Arc::new(AtomicBool::new(false));+ let (tx, rx) = channel::<Exchange>();+ let accept_stop = stop.clone();+ std::thread::spawn(move || loop {+ let stream = match listener.accept() {+ Ok((stream, _)) => stream,+ Err(_) => {+ if accept_stop.load(Ordering::SeqCst) {+ return;+ }+ // EMFILE and friends: back off instead of spinning.+ std::thread::sleep(Duration::from_millis(50));+ continue;+ }+ };+ if accept_stop.load(Ordering::SeqCst) {+ return;+ }+ let tx = tx.clone();+ std::thread::spawn(move || {+ let _ = stream.set_nonblocking(false);+ set_header_timeout(&stream);+ let Some(request) = read_request_deadline(&stream, max_body, READ_REQUEST_DEADLINE)+ else {+ return;+ };+ let exchange = Exchange {+ request,+ stream: Some(stream),+ };+ // The server is gone: the exchange drops and closes.+ let _ = tx.send(exchange);+ });+ });+ Ok(SerialServer { rx, addr, stop })+ }++ pub fn local_addr(&self) -> SocketAddr {+ self.addr+ }++ pub fn port(&self) -> u16 {+ self.addr.port()+ }++ /// The next request, blocking. None only if the accept thread died.+ pub fn recv(&self) -> Option<Exchange> {+ self.rx.recv().ok()+ }++ /// The next request within `wait`, or `Ok(None)` when none arrived.+ pub fn recv_timeout(&self, wait: Duration) -> Result<Option<Exchange>, String> {+ match self.rx.recv_timeout(wait) {+ Ok(exchange) => Ok(Some(exchange)),+ Err(RecvTimeoutError::Timeout) => Ok(None),+ Err(RecvTimeoutError::Disconnected) => Err("http server stopped accepting".into()),+ }+ }+}++impl Drop for SerialServer {+ fn drop(&mut self) {+ self.stop.store(true, Ordering::SeqCst);+ // Wake the blocked accept so the thread exits and drops the listener.+ let mut wake = self.addr;+ if wake.ip().is_unspecified() {+ wake.set_ip(match wake {+ SocketAddr::V4(_) => std::net::Ipv4Addr::LOCALHOST.into(),+ SocketAddr::V6(_) => std::net::Ipv6Addr::LOCALHOST.into(),+ });+ }+ let _ = TcpStream::connect_timeout(&wake, Duration::from_secs(1));+ }+}++/// Test support for the servers built on this core: hold `idle` connections+/// open without a request (a browser's spare keep-alive sockets), open+/// `burst` more, send `request` on each, and count how many get an answer+/// within five seconds. tiny_http 0.12 stranded part of such a burst (#1071).+#[doc(hidden)]+pub fn burst_answered(addr: SocketAddr, request: &[u8], idle: usize, burst: usize) -> usize {+ let _idle: Vec<TcpStream> = (0..idle).filter_map(|_| TcpStream::connect(addr).ok()).collect();+ let mut socks: Vec<TcpStream> = (0..burst).filter_map(|_| TcpStream::connect(addr).ok()).collect();+ for s in socks.iter_mut() {+ let _ = s.write_all(request);+ let _ = s.set_read_timeout(Some(Duration::from_secs(5)));+ }+ socks+ .iter_mut()+ .map(|s| {+ let mut buf = [0u8; 64];+ matches!(s.read(&mut buf), Ok(n) if n > 0)+ })+ .filter(|answered| *answered)+ .count()+}++#[cfg(test)]+mod tests {+ use super::*;+ use std::net::TcpListener;++ #[test]+ fn serial_server_answers_a_burst_of_connections() {+ for _ in 0..6 {+ let listener = TcpListener::bind("127.0.0.1:0").expect("bind");+ let addr = listener.local_addr().expect("addr");+ // Connected before the server accepts, so the backlog drains as+ // one burst, with idle sockets holding their place in front.+ let idle: Vec<TcpStream> = (0..4).map(|_| TcpStream::connect(addr).unwrap()).collect();+ let server = SerialServer::from_listener(listener, 1024).expect("serve");+ let handler = std::thread::spawn(move || {+ for _ in 0..8 {+ if let Some(ex) = server.recv() {+ ex.respond(&Response::new(404));+ }+ }+ });+ let answered = burst_answered(addr, b"GET /img/0 HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n", 4, 8);+ assert_eq!(answered, 8, "connections stranded behind idle ones");+ drop(idle);+ handler.join().unwrap();+ }+ }++ #[test]+ fn serial_server_frees_its_port_when_dropped() {+ let server = SerialServer::bind("127.0.0.1:0", 1024).expect("bind");+ let addr = server.local_addr();+ drop(server);+ let deadline = std::time::Instant::now() + Duration::from_secs(5);+ while TcpListener::bind(addr).is_err() {+ assert!(std::time::Instant::now() < deadline, "port still held after drop");+ std::thread::sleep(Duration::from_millis(20));+ }+ }++ #[test]+ fn an_unanswered_exchange_gets_a_500() {+ let server = SerialServer::bind("127.0.0.1:0", 1024).expect("bind");+ let mut client = TcpStream::connect(server.local_addr()).expect("connect");+ client.write_all(b"GET / HTTP/1.1\r\nHost: h\r\n\r\n").unwrap();+ drop(server.recv().expect("request"));+ let mut out = String::new();+ let _ = client.read_to_string(&mut out);+ assert!(out.starts_with("HTTP/1.1 500 "), "{out}");+ }++ /// Run `read_request` on a raw request written by a client thread.+ fn read_raw(raw: Vec<u8>, max_body: usize) -> Option<Request> {+ let listener = TcpListener::bind("127.0.0.1:0").expect("bind");+ let addr = listener.local_addr().expect("addr");+ let client = std::thread::spawn(move || {+ let mut s = TcpStream::connect(addr).expect("connect");+ let _ = s.write_all(&raw);+ let _ = s.flush();+ let _ = s.shutdown(Shutdown::Write);+ // Hold the read half open until the server is done.+ let mut sink = Vec::new();+ let _ = s.read_to_end(&mut sink);+ });+ let (stream, _) = listener.accept().expect("accept");+ set_header_timeout(&stream);+ let req = read_request(&stream, max_body);+ drop(stream);+ let _ = client.join();+ req+ }++ #[test]+ fn chunked_body_is_decoded() {+ let req = read_raw(+ b"POST /x HTTP/1.1\r\nHost: h\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n3\r\nabc\r\n0\r\n\r\n".to_vec(),+ 1024 * 1024,+ )+ .expect("request");+ assert_eq!(req.body, b"helloabc");+ assert!(!req.body_truncated);+ }++ #[test]+ fn huge_declared_chunk_size_is_capped_not_allocated() {+ // A wire-supplied chunk size of ~256 TB must not be allocated up+ // front (under panic=abort that OOM would take the server down); the+ // request comes back truncated instead.+ let req = read_raw(+ b"POST /x HTTP/1.1\r\nHost: h\r\nTransfer-Encoding: chunked\r\n\r\nffffffffffff\r\nhi".to_vec(),+ 1024 * 1024,+ )+ .expect("request");+ assert!(req.body_truncated);+ }++ #[test]+ fn chunk_size_near_usize_max_does_not_wrap_past_cap() {+ // After a 1-byte chunk, a declared size of usize::MAX made the old+ // `body.len() + size > max_body` guard wrap to 0 and pass, bypassing+ // the cap entirely. It must read as over-cap (truncated), not bypass.+ let req = read_raw(+ b"POST /x HTTP/1.1\r\nHost: h\r\nTransfer-Encoding: chunked\r\n\r\n1\r\na\r\nffffffffffffffff\r\nrest".to_vec(),+ 1024 * 1024,+ )+ .expect("request");+ assert!(req.body_truncated);+ assert_eq!(req.body, b"a");+ }++ #[test]+ fn short_content_length_body_is_not_a_request() {+ // Valid JSON as far as it goes, then EOF before the declared length.+ let raw = b"POST /x HTTP/1.1\r\nHost: h\r\nContent-Length: 20\r\n\r\n{\"ok\":true}".to_vec();+ assert!(read_raw(raw, 1024 * 1024).is_none());+ }++ #[test]+ fn unterminated_chunked_body_is_not_a_request() {+ let raw = b"POST /x HTTP/1.1\r\nHost: h\r\nTransfer-Encoding: chunked\r\n\r\nb\r\n{\"ok\":true}\r\n".to_vec();+ assert!(read_raw(raw, 1024 * 1024).is_none());+ // Cut inside a chunk, too.+ let raw = b"POST /x HTTP/1.1\r\nHost: h\r\nTransfer-Encoding: chunked\r\n\r\n10\r\n{\"ok\":".to_vec();+ assert!(read_raw(raw, 1024 * 1024).is_none());+ }++ #[test]+ fn malformed_chunk_size_is_not_a_request() {+ let raw = b"POST /x HTTP/1.1\r\nHost: h\r\nTransfer-Encoding: chunked\r\n\r\nzz\r\nhello\r\n0\r\n\r\n".to_vec();+ assert!(read_raw(raw, 1024 * 1024).is_none());+ }++ #[test]+ fn a_short_upload_is_never_queued() {+ let server = SerialServer::bind("127.0.0.1:0", 1024).expect("bind");+ let mut client = TcpStream::connect(server.local_addr()).expect("connect");+ client+ .write_all(b"POST /answer HTTP/1.1\r\nHost: h\r\nContent-Length: 20\r\n\r\n{\"ok\":true}")+ .unwrap();+ client.shutdown(Shutdown::Write).unwrap();+ // The connection is dropped without an answer.+ let mut out = Vec::new();+ let _ = client.read_to_end(&mut out);+ assert!(out.is_empty(), "{}", String::from_utf8_lossy(&out));+ assert!(server.recv_timeout(Duration::from_millis(200)).unwrap().is_none());+ }++ #[test]+ fn expect_100_continue_is_answered_before_the_body_is_read() {+ let listener = TcpListener::bind("127.0.0.1:0").expect("bind");+ let addr = listener.local_addr().expect("addr");+ let client = std::thread::spawn(move || {+ let mut s = TcpStream::connect(addr).expect("connect");+ s.write_all(b"POST /x HTTP/1.1\r\nHost: h\r\nContent-Length: 5\r\nExpect: 100-continue\r\n\r\n")+ .unwrap();+ // Send the body only once told to continue, as curl does.+ s.set_read_timeout(Some(Duration::from_secs(5))).unwrap();+ let want = b"HTTP/1.1 100 Continue\r\n\r\n";+ let mut got = vec![0u8; want.len()];+ s.read_exact(&mut got).expect("100 Continue before the body");+ assert_eq!(&got, want);+ s.write_all(b"hello").unwrap();+ let mut sink = Vec::new();+ let _ = s.read_to_end(&mut sink);+ });+ let (stream, _) = listener.accept().expect("accept");+ set_header_timeout(&stream);+ let req = read_request_deadline(&stream, 1024, Duration::from_secs(5)).expect("request");+ assert_eq!(req.body, b"hello");+ drop(stream);+ client.join().unwrap();+ }++ #[test]+ fn expect_100_continue_over_the_cap_is_refused_without_waiting() {+ // No 100 is sent and nothing is drained: the request comes back+ // truncated at once for the caller's 413.+ let listener = TcpListener::bind("127.0.0.1:0").expect("bind");+ let addr = listener.local_addr().expect("addr");+ let mut s = TcpStream::connect(addr).expect("connect");+ s.write_all(b"POST /x HTTP/1.1\r\nHost: h\r\nContent-Length: 4096\r\nExpect: 100-continue\r\n\r\n")+ .unwrap();+ let (stream, _) = listener.accept().expect("accept");+ set_header_timeout(&stream);+ let started = std::time::Instant::now();+ let req = read_request(&stream, 1024).expect("request");+ assert!(req.body_truncated);+ assert!(started.elapsed() < Duration::from_secs(2));+ }++ #[test]+ fn chunk_overflowing_cap_marks_truncated() {+ let req = read_raw(+ b"POST /x HTTP/1.1\r\nHost: h\r\nTransfer-Encoding: chunked\r\n\r\n4\r\nabcd\r\n4\r\nefgh\r\n0\r\n\r\n".to_vec(),+ 6,+ )+ .expect("request");+ assert!(req.body_truncated);+ assert!(req.body.len() <= 6);+ }++ #[test]+ fn oversized_header_block_is_dropped() {+ // A head past MAX_HEAD_BYTES reads as EOF: the request is dropped+ // (connection closed) instead of accumulating headers unbounded.+ let mut raw = b"GET / HTTP/1.1\r\nHost: h\r\n".to_vec();+ raw.extend_from_slice(b"X-Big: ");+ raw.extend(std::iter::repeat(b'a').take(2 * MAX_HEAD_BYTES as usize));+ raw.extend_from_slice(b"\r\n\r\n");+ assert!(read_raw(raw, 1024 * 1024).is_none());+ }++ #[test]+ fn head_under_cap_with_body_still_parses() {+ // Sanity: the head budget must not eat into the body read.+ let req = read_raw(+ b"POST /x HTTP/1.1\r\nHost: h\r\nContent-Length: 5\r\n\r\nhello".to_vec(),+ 1024 * 1024,+ )+ .expect("request");+ assert_eq!(req.body, b"hello");+ assert!(!req.body_truncated);+ }++ /// The generate/generate_started race, reproduced end to end over real+ /// sockets: a `generate` (accepted first) whose large body arrives in+ /// slow, delayed chunks, and a small `generate_started` checkpoint+ /// (accepted second) that reaches the server fast and races ahead.+ ///+ /// The checkpoint is the `unknown_session` gate: it must never run its+ /// store lookup before the `generate` that creates the session has run+ /// its insert, no matter how slowly the `generate` body dribbles in.+ /// Under the old 250 ms accept-order grace, a body slower than the grace+ /// let the checkpoint skip the still-arriving `generate` and observe an+ /// empty store (`unknown_session`); with the grace gone the checkpoint+ /// waits for the whole slow body. Looped so a flaky reorder would show.+ #[test]+ fn slow_generate_body_never_lets_checkpoint_see_unknown_session() {+ use std::collections::HashSet;+ use std::sync::mpsc;+ use std::sync::Mutex;++ let iters: usize = std::env::var("IMPECCABLE_RACE_ITERS")+ .ok()+ .and_then(|v| v.parse().ok())+ .unwrap_or(10);++ for iter in 0..iters {+ let listener = TcpListener::bind("127.0.0.1:0").expect("bind");+ let addr = listener.local_addr().expect("addr");+ let turnstile = Arc::new(Turnstile::default());+ // Stand-in for the session store: the ids `generate` has created.+ let store: Arc<Mutex<HashSet<String>>> = Arc::new(Mutex::new(HashSet::new()));+ // Server -> test: fires once the first connection is accepted, so+ // the test only connects the checkpoint after the generate holds+ // the earlier ticket (deterministic accept order).+ let (accepted_tx, accepted_rx) = mpsc::channel::<()>();+ // Checkpoint handler -> test: whether it saw the session.+ let (result_tx, result_rx) = mpsc::channel::<bool>();++ let srv_turnstile = turnstile.clone();+ let srv_store = store.clone();+ let server = std::thread::spawn(move || {+ let mut handlers = Vec::new();+ for i in 0..2 {+ let (stream, _) = listener.accept().expect("accept");+ let _ = stream.set_nonblocking(false);+ // Ticket issued in accept order, exactly as the live+ // server's accept loop does.+ let ticket = srv_turnstile.issue();+ if i == 0 {+ let _ = accepted_tx.send(());+ }+ let store = srv_store.clone();+ let result_tx = result_tx.clone();+ handlers.push(std::thread::spawn(move || {+ // Ticket lives for the whole handler; dropped at the+ // end -> released, letting the next mutation proceed.+ let ticket = ticket;+ set_header_timeout(&stream);+ let Some(req) = read_request(&stream, 1024 * 1024) else {+ return;+ };+ // Full body is read; only now take the ordering turn.+ ticket.wait_turn();+ let msg: serde_json::Value =+ serde_json::from_slice(&req.body).unwrap_or(serde_json::Value::Null);+ let ty = msg.get("type").and_then(|v| v.as_str()).unwrap_or("");+ let id = msg+ .get("id")+ .and_then(|v| v.as_str())+ .unwrap_or("")+ .to_string();+ if ty == "generate" {+ store.lock().unwrap().insert(id);+ } else {+ let known = store.lock().unwrap().contains(&id);+ let _ = result_tx.send(known);+ }+ }));+ }+ for h in handlers {+ let _ = h.join();+ }+ });++ let sid = format!("sess-{iter}");+ // The generate body is large; dribble it in chunks with sleeps so+ // it takes ~300 ms to finish arriving (well past the old grace).+ let gen_body = serde_json::json!({+ "type": "generate",+ "id": sid,+ "pad": "x".repeat(4000),+ })+ .to_string();+ let gen_head = format!(+ "POST /events HTTP/1.1\r\nHost: h\r\nContent-Length: {}\r\n\r\n",+ gen_body.len()+ );++ let mut gen = TcpStream::connect(addr).expect("connect generate");+ gen.set_nodelay(true).ok();+ gen.write_all(gen_head.as_bytes()).expect("write gen head");+ gen.flush().ok();+ // Block until the server has accepted the generate connection, so+ // the checkpoint that follows is guaranteed the later ticket.+ accepted_rx.recv().expect("generate accepted");++ let gen_writer = std::thread::spawn(move || {+ let bytes = gen_body.into_bytes();+ // Six chunks with 100 ms gaps: the body finishes arriving+ // ~500 ms after it starts, well past the old 250 ms grace, so+ // a checkpoint that gave up on the grace would beat the insert.+ for chunk in bytes.chunks(bytes.len() / 6 + 1) {+ if gen.write_all(chunk).is_err() {+ break;+ }+ let _ = gen.flush();+ std::thread::sleep(Duration::from_millis(100));+ }+ // Hold the connection open until the server is done reading.+ let mut sink = Vec::new();+ let _ = gen.read_to_end(&mut sink);+ });++ // The checkpoint: small, sent complete and fast, right away.+ let cp_body = serde_json::json!({+ "type": "checkpoint",+ "id": sid,+ "reason": "generate_started",+ })+ .to_string();+ let cp_raw = format!(+ "POST /events HTTP/1.1\r\nHost: h\r\nContent-Length: {}\r\n\r\n{}",+ cp_body.len(),+ cp_body+ );+ let mut cp = TcpStream::connect(addr).expect("connect checkpoint");+ cp.set_nodelay(true).ok();+ cp.write_all(cp_raw.as_bytes()).expect("write checkpoint");+ cp.flush().ok();++ let known = result_rx+ .recv_timeout(Duration::from_secs(10))+ .expect("checkpoint handler ran");+ assert!(+ known,+ "iteration {iter}: checkpoint observed unknown_session; \+ the slow generate body was overtaken (ordering broke)",+ );++ let mut sink = Vec::new();+ let _ = cp.read_to_end(&mut sink);+ let _ = gen_writer.join();+ let _ = server.join();+ }+ }++ /// The Fable BLOCKER, reproduced at the lane level: a connection accepted+ /// first that takes a ticket and then goes silent must not wedge a later+ /// state-mutating request beyond the read deadline.+ ///+ /// The silent connection sends a partial head and never finishes, so its+ /// handler blocks inside `read_request`. Under the plain per-`read` timeout+ /// it held its ticket for the whole (up to 60s) window and every later+ /// mutation waited on it with `wait_turn`'s NO-timeout condvar. With+ /// `read_request_deadline` the silent connection is force-dropped at the+ /// total deadline and drops its ticket, so the later request takes its turn+ /// within the bound. Reverting to a deadline-less read makes this hang and+ /// the timed `recv` below fail, so it stays a distinguishing test.+ #[test]+ fn silent_connection_holding_ticket_does_not_wedge_later_events_beyond_deadline() {+ use std::sync::mpsc;++ // Short in-test deadline keeps the test fast; production uses+ // READ_REQUEST_DEADLINE (10s).+ let deadline = Duration::from_millis(400);++ let listener = TcpListener::bind("127.0.0.1:0").expect("bind");+ let addr = listener.local_addr().expect("addr");+ let turnstile = Arc::new(Turnstile::default());+ // Server -> test: the first connection has been accepted (and holds the+ // lower ticket), so the later request is guaranteed the higher ticket.+ let (accepted_tx, accepted_rx) = mpsc::channel::<()>();+ // Later handler -> test: it took its turn and ran.+ let (ran_tx, ran_rx) = mpsc::channel::<()>();++ let srv_turnstile = turnstile.clone();+ let server = std::thread::spawn(move || {+ let mut handlers = Vec::new();+ for i in 0..2 {+ let (stream, _) = listener.accept().expect("accept");+ let _ = stream.set_nonblocking(false);+ // Ticket issued in accept order, as the live accept loop does.+ let ticket = srv_turnstile.issue();+ if i == 0 {+ let _ = accepted_tx.send(());+ }+ let ran_tx = ran_tx.clone();+ handlers.push(std::thread::spawn(move || {+ let ticket = ticket;+ set_header_timeout(&stream);+ // The silent connection (i == 0) reads nothing complete and+ // returns None at the deadline, dropping its ticket. The+ // later one reads a full request, then takes its turn.+ if read_request_deadline(&stream, 1024 * 1024, deadline).is_some() {+ ticket.wait_turn();+ let _ = ran_tx.send(());+ }+ }));+ }+ for h in handlers {+ let _ = h.join();+ }+ });++ // Connection A (accepted first, lower ticket): a partial head, then+ // silence. Kept open until the assertion so its ticket can only be+ // released by the deadline, not by an early peer close.+ let mut silent = TcpStream::connect(addr).expect("connect silent");+ silent.set_nodelay(true).ok();+ silent+ .write_all(b"POST /events HTTP/1.1\r\nHost: h\r\n")+ .expect("write partial head");+ silent.flush().ok();+ accepted_rx.recv().expect("silent connection accepted");++ // Connection B (accepted second, higher ticket): a complete request+ // that must take its turn once the silent ticket is released.+ let body = "{\"type\":\"checkpoint\"}";+ let raw = format!(+ "POST /events HTTP/1.1\r\nHost: h\r\nContent-Length: {}\r\n\r\n{}",+ body.len(),+ body+ );+ let mut later = TcpStream::connect(addr).expect("connect later");+ later.set_nodelay(true).ok();+ later.write_all(raw.as_bytes()).expect("write later");+ later.flush().ok();++ // Must run within a small multiple of the deadline. Before the fix the+ // silent ticket was held for the whole read window and this never+ // arrived; generous slack absorbs CI scheduling. The Windows runner+ // schedules the watchdog's polling loop against a ~15.6ms system timer+ // while the whole crate's tests run in parallel, so it gets a wider+ // margin; the bound stays far under the 60s read timeout a+ // deadline-less read would hold the ticket for, so the test still+ // distinguishes the fix from the regression.+ let slack = if cfg!(windows) { deadline * 12 } else { deadline * 6 };+ ran_rx.recv_timeout(slack).expect(+ "later /events was wedged behind a silent connection past the read deadline",+ );++ drop(silent);+ drop(later);+ let _ = server.join();+ }+}diff --git a/crates/common/src/lib.rs b/crates/common/src/lib.rsindex 82519e2fc..598ed2ef5 100644--- a/crates/common/src/lib.rs+++ b/crates/common/src/lib.rs@@ -8,6 +8,7 @@pub mod fsx;pub mod git;+pub mod http;pub mod jsp;pub mod proc;#[cfg(feature = "seed")]diff --git a/crates/context/Cargo.toml b/crates/context/Cargo.tomlindex 580c41500..518910746 100644--- a/crates/context/Cargo.toml+++ b/crates/context/Cargo.toml@@ -24,5 +24,9 @@ flate2 = { version = "1", default-features = false, features = ["zlib-rs"] }ureq = { version = "2", default-features = false, features = ["tls", "json", "socks-proxy"] }rustls-native-certs = "0.8"webpki-roots = "1"-tiny_http = "0.12"unicode-normalization = "0.1.25"++[dev-dependencies]+# Stand-in upstream APIs in unit tests only; the runtime servers use+# impeccable_common::http.+tiny_http = "0.12"diff --git a/crates/context/src/component_review/server.rs b/crates/context/src/component_review/server.rsindex c08df87c4..1eede7074 100644--- a/crates/context/src/component_review/server.rs+++ b/crates/context/src/component_review/server.rs@@ -2,10 +2,10 @@ use super::{manifest::{digest, relative},store,};+use impeccable_common::http::{Exchange, Request, Response, SerialServer};use impeccable_common::Io;use serde_json::{json, Value};use std::{- io::Read,path::Path,sync::atomic::{AtomicBool, Ordering},time::{Duration, Instant},@@ -14,8 +14,11 @@ use std::{const JS: &str = include_str!("../../assets/component-review.js");const HTML: &str = "<!doctype html><html><head><meta charset='utf-8'><meta name='viewport' content='width=device-width,initial-scale=1'><title>Component review · Impeccable</title><link rel='stylesheet' href='/review.css'></head><body><main id='review'></main><script src='/review.js' defer></script></body></html>";const CSS: &str = concat!("@font-face{font-family:Albert Sans;src:url(/fonts/albertsans.ttf)}@font-face{font-family:Alumni Sans;src:url(/fonts/alumnisans.ttf)}@font-face{font-family:JetBrains Mono;src:url(/fonts/jetbrainsmono.ttf)}body{margin:0;color:var(--ks-text,#292929);background:var(--ks-paper,#fafafa);--font-sans:'Albert Sans',Arial,sans-serif;--font-display:'Alumni Sans',Arial,sans-serif;--font-mono:'JetBrains Mono',monospace}body>p{padding:24px;font:16px var(--font-sans)}", include_str!("../../../../ui/component-review/theme.css"));-fn respond(req: tiny_http::Request, code: u16, body: Vec<u8>, mime: &str, csp: &str) {- let mut response = tiny_http::Response::from_data(body).with_status_code(code);+/// Decision bodies over this are refused. The reader keeps one byte more so+/// an oversized body is told apart from one exactly at the limit.+const MAX_DECISION: usize = 512 * 1024;+fn respond(req: Exchange, code: u16, body: Vec<u8>, mime: &str, csp: &str) {+ let mut response = Response::new(code).bytes(body);for (k, v) in [("Content-Type", mime),("Cache-Control", "no-store"),@@ -23,20 +26,15 @@ fn respond(req: tiny_http::Request, code: u16, body: Vec<u8>, mime: &str, csp: &("Content-Security-Policy", csp),("Referrer-Policy", "no-referrer"),] {- response = response.with_header(tiny_http::Header::from_bytes(k, v).unwrap());+ response = response.header(k, v);}if mime.starts_with("font/") {- response = response.with_header(- tiny_http::Header::from_bytes("Access-Control-Allow-Origin", "*").unwrap(),- );+ response = response.header("Access-Control-Allow-Origin", "*");}- let _ = req.respond(response);+ req.respond(&response);}-fn header<'a>(req: &'a tiny_http::Request, name: &'static str) -> Option<&'a str> {- req.headers()- .iter()- .find(|h| h.field.equiv(name))- .map(|h| h.value.as_str())+fn header<'a>(req: &'a Request, name: &'static str) -> Option<&'a str> {+ req.header(name)}pub fn authorized(method: &str,@@ -92,13 +90,9 @@ pub struct Limits {/// one (idle timeout or interrupt). service.json never outlives the server.pub fn serve(dir: &Path, port: u16, io: &mut Io, limits: Limits, stop: Option<&AtomicBool>) -> Result<i32, String> {store::read(&dir.join("current.json"))?;- let server = tiny_http::Server::http(("127.0.0.1", port)).map_err(|e| e.to_string())?;- let _pool_nudge = crate::http_pool::keep_pool_moving(&server);- let actual = server- .server_addr()- .to_ip()- .ok_or("listener is not TCP")?- .port();+ let server =+ SerialServer::bind(("127.0.0.1", port), MAX_DECISION + 1).map_err(|e| e.to_string())?;+ let actual = server.port();let service = json!({"url":format!("http://127.0.0.1:{actual}/"),"pid":std::process::id()});store::write(&dir.join("service.json"), &service)?;struct Registration<'a>(&'a Path);@@ -130,17 +124,17 @@ pub fn serve(dir: &Path, port: u16, io: &mut Io, limits: Limits, stop: Option<&Areturn Ok(4);}let wait = limits.grace.min(limits.idle).min(Duration::from_secs(1));- let Some(mut req) = server.recv_timeout(wait).map_err(|e| e.to_string())? else {+ let Some(req) = server.recv_timeout(wait)? else {continue;};last = Instant::now();- let method = req.method().as_str().to_string();- let path = req.url().split('?').next().unwrap_or("").to_string();+ let method = req.request.method.clone();+ let path = req.request.target.split('?').next().unwrap_or("").to_string();if !authorized(&method,- header(&req, "Host"),- header(&req, "Origin"),- header(&req, "Sec-Fetch-Site"),+ header(&req.request, "Host"),+ header(&req.request, "Origin"),+ header(&req.request, "Sec-Fetch-Site"),actual,) {respond(@@ -177,20 +171,16 @@ pub fn serve(dir: &Path, port: u16, io: &mut Io, limits: Limits, stop: Option<&A"application/json",)),("POST", "/decision") => {- if !header(&req, "Content-Type")+ if !header(&req.request, "Content-Type").is_some_and(|t| t.starts_with("application/json")){return Err("expected application/json".into());}- let mut data = Vec::new();- req.as_reader()- .take(512 * 1024 + 1)- .read_to_end(&mut data)- .map_err(|e| e.to_string())?;- if data.len() > 512 * 1024 {+ let data = &req.request.body;+ if req.request.body_truncated || data.len() > MAX_DECISION {return Err("decision exceeds 512 KiB".into());}- let body: Value = serde_json::from_slice(&data).map_err(|e| e.to_string())?;+ let body: Value = serde_json::from_slice(data).map_err(|e| e.to_string())?;Ok((serde_json::to_vec(&store::submit(dir, &body)?).unwrap(),"application/json",diff --git a/crates/context/src/component_review/tests.rs b/crates/context/src/component_review/tests.rsindex a2e946f77..23019ccf1 100644--- a/crates/context/src/component_review/tests.rs+++ b/crates/context/src/component_review/tests.rs@@ -915,6 +915,33 @@ fn serve_exits_after_submission_or_idle_and_removes_its_service_record() {assert!(!dir.join("service.json").exists());}+// A browser loading the review opens a burst of sockets (fonts, script,+// packet, snapshot files) and keeps some idle. tiny_http 0.12 left part of+// such a burst unanswered (#1071); every request must get its answer.+#[test]+fn serve_answers_a_burst_of_connections() {+ let f = Fixture::new();+ let dir = f.prepare();+ let rx = serve_in_thread(&dir, 60_000);+ let started = std::time::Instant::now();+ let url = loop {+ if let Ok(s) = store::read(&dir.join("service.json")) {+ break s["url"].as_str().unwrap().to_string();+ }+ assert!(started.elapsed() < std::time::Duration::from_secs(10), "no service record");+ std::thread::sleep(std::time::Duration::from_millis(20));+ };+ let host = url.trim_start_matches("http://").trim_end_matches('/').to_string();+ let addr: std::net::SocketAddr = host.parse().unwrap();+ let request = format!("GET /review.css HTTP/1.1\r\nHost: {host}\r\n\r\n");+ for _ in 0..3 {+ assert_eq!(impeccable_common::http::burst_answered(addr, request.as_bytes(), 4, 8), 8, "connections stranded");+ }+ let state = store::read(&dir.join("current.json")).unwrap();+ store::submit(&dir, &approve(&state)).unwrap();+ assert_eq!(rx.recv_timeout(std::time::Duration::from_secs(5)).unwrap(), Ok(0));+}+#[test]fn status_hides_dead_servers_and_serve_refuses_sessions_without_a_browser() {let f = Fixture::new();diff --git a/crates/context/src/http_pool.rs b/crates/context/src/http_pool.rsdeleted file mode 100644index b73c7412d..000000000--- a/crates/context/src/http_pool.rs+++ /dev/null@@ -1,107 +0,0 @@-//! Keeps tiny_http's connection pool from stranding a connection.-//!-//! tiny_http 0.12 hands every accepted connection to a `TaskPool` task that-//! serves that connection's requests until the client closes it. `spawn`-//! queues the task instead of starting a thread whenever the pool's count of-//! idle workers is above zero, but that count only drops once a woken worker-//! takes the queue lock again. A burst of connections that arrives faster-//! than the idle workers wake (a browser opening one socket per image on a-//! decision page) can queue more tasks than there are workers. The workers-//! that did wake then sit in a keep-alive read on their own sockets, which a-//! browser holds open for minutes, and the leftover tasks never run: those-//! requests hang until the server exits. The decision page's missing-comp-//! probes hit this on Linux CI, where the burst outran the wakeups on every-//! failing run; the regression test below reproduces it on macOS too.-//!-//! The pool still makes progress whenever a new connection arrives: with no-//! idle worker it starts a thread that drains the queue after its own task,-//! and with an idle one the notify wakes it. So a guard opens and drops an-//! empty connection to the server on a short interval. The empty connection-//! reads EOF and never surfaces as a request, so request handling and idle-//! timers are unaffected, and a stranded connection is served within one-//! interval.--use std::net::{SocketAddr, TcpStream};-use std::sync::atomic::{AtomicBool, Ordering};-use std::sync::Arc;-use std::time::Duration;--const NUDGE_INTERVAL: Duration = Duration::from_millis(250);--/// Stops the nudge thread when dropped. Hold it for the server's lifetime.-pub struct PoolNudge {- stop: Arc<AtomicBool>,-}--impl Drop for PoolNudge {- fn drop(&mut self) {- self.stop.store(true, Ordering::SeqCst);- }-}--/// Starts nudging the server's pool. Returns `None` for a non-TCP listener.-pub fn keep_pool_moving(server: &tiny_http::Server) -> Option<PoolNudge> {- let addr = server.server_addr().to_ip()?;- Some(nudge(addr))-}--fn nudge(addr: SocketAddr) -> PoolNudge {- let stop = Arc::new(AtomicBool::new(false));- let flag = stop.clone();- std::thread::spawn(move || {- while !flag.load(Ordering::SeqCst) {- std::thread::sleep(NUDGE_INTERVAL);- if flag.load(Ordering::SeqCst) {- return;- }- if TcpStream::connect_timeout(&addr, Duration::from_secs(1)).is_err() {- return;- }- }- });- PoolNudge { stop }-}--#[cfg(test)]-mod tests {- use super::*;- use std::io::{Read, Write};-- // Every connection of a burst that lands before the pool's idle workers- // wake gets an answer. Without the nudge, some round of the six leaves- // all but four (the pool's floor of workers) unanswered on macOS too.- #[test]- fn a_burst_of_keep_alive_connections_is_fully_served() {- for _ in 0..6 {- let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();- let addr = listener.local_addr().unwrap();- // Connected before the server accepts, so its accept loop drains- // the backlog as one burst.- let mut socks: Vec<TcpStream> = (0..8).map(|_| TcpStream::connect(addr).unwrap()).collect();- let server = Arc::new(tiny_http::Server::from_listener(listener, None).unwrap());- let _nudge = keep_pool_moving(&server).unwrap();- let responder = server.clone();- std::thread::spawn(move || {- for rq in responder.incoming_requests() {- let _ = rq.respond(tiny_http::Response::empty(404));- }- });- for s in socks.iter_mut() {- s.write_all(b"GET /img/0 HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n").unwrap();- // Each nudge frees one stranded connection, so four take about a- // second; without the nudge they never answer.- s.set_read_timeout(Some(Duration::from_secs(5))).unwrap();- }- let answered = socks- .iter_mut()- .map(|s| {- let mut buf = [0u8; 64];- matches!(s.read(&mut buf), Ok(n) if n > 0)- })- .filter(|answered| *answered)- .count();- assert_eq!(answered, 8, "connections stranded in the pool");- server.unblock();- }- }-}diff --git a/crates/context/src/lib.rs b/crates/context/src/lib.rsindex 9317df51b..5d4cd0a68 100644--- a/crates/context/src/lib.rs+++ b/crates/context/src/lib.rs@@ -6,7 +6,6 @@ pub mod jsp;pub mod util;pub mod url;pub mod http;-pub mod http_pool;pub mod provider;pub mod hook_markers;pub mod target_args;diff --git a/crates/context/src/serve_question.rs b/crates/context/src/serve_question.rsindex b5a54a7e1..265ee69e8 100644--- a/crates/context/src/serve_question.rs+++ b/crates/context/src/serve_question.rs@@ -8,8 +8,13 @@ use crate::question_page::PAGE;use crate::util::{exists, is_dir, iso_now, json_compact, json_pretty, mtime_ms, now_ms, safe_read, utf16_len, Env};use impeccable_common::Io;use serde_json::{json, Map, Value};+use impeccable_common::http::{Response, SerialServer};use std::sync::{Arc, Mutex};+/// Ceiling on a POST body (`/answer`, `/build-path`, `/heartbeat`). The page+/// sends a few hundred bytes; the cap only bounds a hostile local client.+const MAX_REQUEST_BODY: usize = 8 * 1024 * 1024;+const NEXT_CLAIM_GRACE_MS: f64 = 10000.0;struct Args<'a> {@@ -1536,15 +1541,14 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {}}let port = if port_arg.is_finite() && port_arg >= 0.0 { port_arg as u16 } else { 0 };- let server = match tiny_http::Server::http(("127.0.0.1", port)) {+ let server = match SerialServer::bind(("127.0.0.1", port), MAX_REQUEST_BODY) {Ok(s) => s,Err(e) => {io.err(&format!("Error: listen EADDRINUSE: {}\n", e));return 1;}};- let actual_port = server.server_addr().to_ip().map(|a| a.port()).unwrap_or(port);- let _pool_nudge = crate::http_pool::keep_pool_moving(&server);+ let actual_port = server.port();let url = format!("http://127.0.0.1:{}/", actual_port);if a.has("detached-serve") {let _ = std::fs::create_dir_all(&qdir);@@ -1565,7 +1569,6 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {}let _ = io.stdout.flush();let started_at = now_ms();- let server = Arc::new(server);// Lifetime watchdog thread; exits the process like the JS setInterval.{let state = state.clone();@@ -1589,31 +1592,23 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {}});}- for mut request in server.incoming_requests() {- let method = request.method().as_str().to_string();- let raw_url = request.url().to_string();+ while let Some(mut request) = server.recv() {+ let method = request.request.method.clone();+ let raw_url = request.request.target.clone();// JS: every request passes the Host allowlist first; a foreign or// missing Host header is a 403.- let host = request- .headers()- .iter()- .find(|h| h.field.equiv("Host"))- .map(|h| h.value.as_str().to_string());+ let host = request.request.header("Host").map(str::to_string);if !allowed_host(host.as_deref(), actual_port) {- let _ = request.respond(tiny_http::Response::empty(403));+ request.respond(&Response::new(403));continue;}// JS: new URL(req.url, 'http://127.0.0.1'); a target the URL parser// rejects is a 400.let Some((path, query)) = parse_request_url(&raw_url) else {- let _ = request.respond(tiny_http::Response::empty(400));+ request.respond(&Response::new(400));continue;};- let origin = request- .headers()- .iter()- .find(|h| h.field.equiv("Origin"))- .map(|h| h.value.as_str().to_string());+ let origin = request.request.header("Origin").map(str::to_string);let mut st = state.lock().unwrap();if method == "GET" && path == "/" {if let Some(pending) = st.next_file() {@@ -1638,17 +1633,15 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {}}let html = st.page(st.awaiting_next);- let resp = tiny_http::Response::from_string(html)- .with_header(tiny_http::Header::from_bytes("content-type", "text/html; charset=utf-8").unwrap());- let _ = request.respond(resp);+ request.respond(&Response::new(200).header("content-type", "text/html; charset=utf-8").text(&html));continue;}if method == "POST" && path == "/heartbeat" {if let Some(code) = reject_detached_post(st.detached_key.as_deref(), origin.as_deref(), &query, actual_port) {- let _ = request.respond(tiny_http::Response::empty(code));+ request.respond(&Response::new(code));continue;}- let _ = request.respond(tiny_http::Response::empty(204));+ request.respond(&Response::new(204));st.last_beat_seen = Some(now_ms());if let Some(k) = st.detached_key.clone() {let now = now_ms();@@ -1664,9 +1657,7 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {}if method == "GET" && path == "/next-status" {let ready = st.next_file().map(|p| exists(&p)).unwrap_or(false);- let resp = tiny_http::Response::from_string(format!("{{\"ready\":{}}}", ready))- .with_header(tiny_http::Header::from_bytes("content-type", "application/json").unwrap());- let _ = request.respond(resp);+ request.respond(&Response::new(200).header("content-type", "application/json").text(&format!("{{\"ready\":{}}}", ready)));continue;}if method == "GET" && path.starts_with("/img/") {@@ -1689,11 +1680,10 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {"image/jpeg"};let bytes = std::fs::read(&abs).unwrap_or_default();- let resp = tiny_http::Response::from_data(bytes).with_header(tiny_http::Header::from_bytes("content-type", ty).unwrap());- let _ = request.respond(resp);+ request.respond(&Response::new(200).header("content-type", ty).bytes(bytes));}_ => {- let _ = request.respond(tiny_http::Response::empty(404));+ request.respond(&Response::new(404));}}continue;@@ -1716,14 +1706,14 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {if raw { "'none'" } else { "'unsafe-inline'" },if raw { "sandbox" } else { "sandbox allow-scripts" });- let mut resp = tiny_http::Response::from_string(body);+ let mut resp = Response::new(200).text(&body);for (k, v) in [("content-type", "text/html; charset=utf-8"), ("cache-control", "no-store"), ("x-content-type-options", "nosniff"), ("referrer-policy", "no-referrer"), ("content-security-policy", csp.as_str())] {- resp = resp.with_header(tiny_http::Header::from_bytes(k, v).unwrap());+ resp = resp.header(k, v);}- let _ = request.respond(resp);+ request.respond(&resp);}None => {- let _ = request.respond(tiny_http::Response::empty(404));+ request.respond(&Response::new(404));}}continue;@@ -1732,13 +1722,16 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {// JS: both handlers start with rejectDetachedPost (the /answer gate// from #555, the /build-path gate from the follow-up commit).if let Some(code) = reject_detached_post(st.detached_key.as_deref(), origin.as_deref(), &query, actual_port) {- let _ = request.respond(tiny_http::Response::empty(code));+ request.respond(&Response::new(code));continue;}- let mut body = String::new();- let _ = std::io::Read::read_to_string(request.as_reader(), &mut body);- let ok_resp = || tiny_http::Response::from_string("{\"ok\":true}")- .with_header(tiny_http::Header::from_bytes("content-type", "application/json").unwrap());+ if request.request.body_truncated {+ request.respond(&Response::new(413));+ continue;+ }+ // Read as UTF-8 or not at all, as `read_to_string` did.+ let body = String::from_utf8(std::mem::take(&mut request.request.body)).unwrap_or_default();+ let ok_resp = || Response::new(200).header("content-type", "application/json").text("{\"ok\":true}");if path == "/build-path" {let value = serde_json::from_str::<Value>(&body).ok().and_then(|v| v.get("value").and_then(|x| x.as_str()).map(|s| s.to_string()));if let Some(value) = value.filter(|v| v == "comp" || v == "code") {@@ -1758,10 +1751,10 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {// caller: the 200 reached the client (a separate process) while this// one could still be preempted before the write landed, so a poller// that trusted the 200 could look for the flip file and miss it.- let _ = request.respond(ok_resp());+ request.respond(&ok_resp());continue;}- let _ = request.respond(ok_resp());+ request.respond(&ok_resp());// /answerlet parsed = serde_json::from_str::<Value>(&body).ok().and_then(|v| v.as_object().cloned()).unwrap_or_default();let option_id = parsed.get("optionId").cloned().unwrap_or(Value::Null);@@ -1869,7 +1862,7 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {}continue;}- let _ = request.respond(tiny_http::Response::empty(404));+ request.respond(&Response::new(404));}0}@@ -3972,6 +3965,38 @@ mod tests {server.join().unwrap()}+ /// A decision page opens a socket per card image and keeps some idle.+ /// Under tiny_http 0.12 part of such a burst was never answered (#1071);+ /// every request must be.+ #[test]+ fn a_burst_of_connections_is_fully_served() {+ let dir = temp_project("burst");+ let qdir = dir.join(".impeccable/questions");+ let payload = json!({ "options": [{ "id": "a", "label": "A" }, { "id": "b", "label": "B" }] });+ std::fs::write(dir.join("payload.json"), payload.to_string()).unwrap();+ let cwd = dir.clone();+ let server = std::thread::spawn(move || {+ let (mut io, _cap) = Io::captured("", cwd, Env::from([("IMPECCABLE_SELF".to_string(), "impeccable".to_string()), ("IMPECCABLE_NO_TELEMETRY".to_string(), "1".to_string())]));+ let argv: Vec<String> = ["--payload", "payload.json", "--detached-serve", "--key", "pk", "--timeout", "0", "--no-open"].iter().map(|s| s.to_string()).collect();+ run(&argv, &mut io)+ });+ let deadline = now_ms() + 5000.0;+ let port = loop {+ if let Some(p) = read_state(&qdir.to_string_lossy(), "pk").and_then(|s| s.get("port").and_then(Value::as_u64)) {+ break p as u16;+ }+ assert!(now_ms() < deadline, "server never wrote its state");+ sleep_ms(20);+ };+ let addr: std::net::SocketAddr = ([127, 0, 0, 1], port).into();+ let request = format!("GET /img/0 HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\n\r\n");+ for _ in 0..3 {+ assert_eq!(impeccable_common::http::burst_answered(addr, request.as_bytes(), 4, 8), 8, "connections stranded");+ }+ ureq::post(&format!("http://127.0.0.1:{}/answer?key=pk", port)).send_string(&json!({ "optionId": "a" }).to_string()).unwrap();+ assert_eq!(server.join().unwrap(), 0);+ }+#[test]fn the_page_sends_the_choice_ping_itself_and_the_agent_rerun_becomes_a_no_op() {let dir = temp_project("picker-ping");diff --git a/crates/live/src/live_http.rs b/crates/live/src/live_http.rsindex b06f67305..81415b037 100644--- a/crates/live/src/live_http.rs+++ b/crates/live/src/live_http.rs@@ -1,996 +1,5 @@-//! A small HTTP/1.1 server core for the live helper: `TcpListener`, one thread-//! per connection, request parsing (headers + Content-Length / chunked body),-//! plain responses, and long-lived streamed responses (SSE, parked polls)-//! with client-close detection. Stands in for `node:http`; nothing here is-//! contract-visible except the routes `live_server` builds on it.+//! The live helper's HTTP core lives in `impeccable_common::http`, shared with+//! the decision page and component review servers. Re-exported here so+//! `crate::live_http::*` keeps naming it.-use std::collections::HashMap;-use std::io::{BufRead, BufReader, Read, Write};-use std::net::{Shutdown, TcpStream};-use std::sync::atomic::{AtomicBool, Ordering};-use std::sync::Arc;-use std::time::Duration;--pub struct Request {- pub method: String,- /// Raw request target (path + query), as `req.url` in Node.- pub target: String,- pub path: String,- pub query: Vec<(String, String)>,- /// Lower-cased header names.- pub headers: HashMap<String, String>,- pub body: Vec<u8>,- /// Set when the body exceeded `max_body` while reading (payload too large).- pub body_truncated: bool,-}--impl Request {- /// `url.searchParams.get(name)`: first value or None.- pub fn query_get(&self, name: &str) -> Option<&str> {- self.query- .iter()- .find(|(k, _)| k == name)- .map(|(_, v)| v.as_str())- }- pub fn header(&self, name: &str) -> Option<&str> {- self.headers- .get(&name.to_ascii_lowercase())- .map(String::as_str)- }-}--/// Percent-decode with `+` as space (`URLSearchParams` semantics), lossy UTF-8.-pub fn decode_query_component(s: &str) -> String {- let bytes = s.as_bytes();- let mut out: Vec<u8> = Vec::with_capacity(bytes.len());- let mut i = 0;- while i < bytes.len() {- match bytes[i] {- b'+' => {- out.push(b' ');- i += 1;- }- b'%' if i + 2 < bytes.len() => {- let h = (bytes[i + 1] as char).to_digit(16);- let l = (bytes[i + 2] as char).to_digit(16);- match (h, l) {- (Some(h), Some(l)) => {- out.push((h * 16 + l) as u8);- i += 3;- }- _ => {- out.push(b'%');- i += 1;- }- }- }- b => {- out.push(b);- i += 1;- }- }- }- String::from_utf8_lossy(&out).into_owned()-}--/// Split a request target into (pathname, query pairs). Mirrors what-/// `new URL(req.url, base)` exposes: the pathname is kept as sent (minus a-/// fragment), search params are decoded.-pub fn parse_target(target: &str) -> (String, Vec<(String, String)>) {- let no_frag = target.split('#').next().unwrap_or("");- let (path, query) = match no_frag.find('?') {- Some(i) => (&no_frag[..i], &no_frag[i + 1..]),- None => (no_frag, ""),- };- let mut pairs = Vec::new();- for part in query.split('&') {- if part.is_empty() {- continue;- }- let (k, v) = match part.find('=') {- Some(i) => (&part[..i], &part[i + 1..]),- None => (part, ""),- };- pairs.push((decode_query_component(k), decode_query_component(v)));- }- let path = if path.is_empty() {- "/".to_string()- } else {- normalize_path(path)- };- (path, pairs)-}--/// WHATWG URL path normalization for the cases that matter here: collapse-/// `.` and `..` segments.-fn normalize_path(p: &str) -> String {- let mut segs: Vec<&str> = Vec::new();- for seg in p.split('/').skip(1) {- match seg {- "." => {}- ".." => {- segs.pop();- }- s => segs.push(s),- }- }- let mut out = String::from("/");- out.push_str(&segs.join("/"));- if p.ends_with("/..") || p.ends_with("/.") {- if !out.ends_with('/') {- out.push('/');- }- }- out-}--/// Ceiling on the request head (request line + headers). Node's http server-/// rejects heads over ~16 KB by default (`maxHeaderSize`); this hand-rolled-/// parser used to accumulate header lines unbounded, letting a hostile-/// pre-auth localhost connection grow memory without limit. 64 KB keeps 4x-/// headroom over Node's default for legitimate local tooling while staying-/// finite. A head that exhausts the budget reads as EOF below, so the request-/// is dropped the way any malformed input is: connection closed, no panic.-const MAX_HEAD_BYTES: u64 = 64 * 1024;--/// Read one HTTP request from the stream. `max_body` caps the body read; a-/// body over the cap sets `body_truncated` (the caller answers 413).-pub fn read_request(stream: &TcpStream, max_body: usize) -> Option<Request> {- let mut reader = BufReader::new(stream.try_clone().ok()?).take(MAX_HEAD_BYTES);- let mut line = String::new();- // Request line (skip stray CRLFs between requests).- loop {- line.clear();- let n = reader.read_line(&mut line).ok()?;- if n == 0 {- return None;- }- if line.trim_end_matches(['\r', '\n']).is_empty() {- continue;- }- break;- }- let req_line = line.trim_end_matches(['\r', '\n']).to_string();- let mut parts = req_line.split_whitespace();- let method = parts.next()?.to_string();- let target = parts.next().unwrap_or("/").to_string();- let mut headers: HashMap<String, String> = HashMap::new();- loop {- line.clear();- let n = reader.read_line(&mut line).ok()?;- if n == 0 {- return None;- }- let l = line.trim_end_matches(['\r', '\n']);- if l.is_empty() {- break;- }- if let Some(i) = l.find(':') {- let name = l[..i].trim().to_ascii_lowercase();- let value = l[i + 1..].trim().to_string();- headers.entry(name).or_insert(value);- }- }- // The head is done; the body has its own cap (`max_body`), so drop the- // head budget and read the body from the underlying buffered stream.- let mut reader = reader.into_inner();- let mut body: Vec<u8> = Vec::new();- let mut truncated = false;- if let Some(len) = headers- .get("content-length")- .and_then(|v| v.trim().parse::<usize>().ok())- {- if len > max_body {- truncated = true;- // Drain what we can so the peer does not see a reset before the- // response, then stop.- let mut remaining = len;- let mut buf = [0u8; 8192];- let mut drained = 0usize;- while remaining > 0 && drained < max_body + 65536 {- let want = remaining.min(buf.len());- match reader.read(&mut buf[..want]) {- Ok(0) | Err(_) => break,- Ok(n) => {- remaining -= n;- drained += n;- }- }- }- } else {- body.resize(len, 0);- let mut read = 0;- while read < len {- match reader.read(&mut body[read..]) {- Ok(0) => break,- Ok(n) => read += n,- Err(_) => break,- }- }- body.truncate(read);- }- } else if headers- .get("transfer-encoding")- .map(|v| v.to_ascii_lowercase().contains("chunked"))- .unwrap_or(false)- {- loop {- line.clear();- if reader.read_line(&mut line).ok()? == 0 {- break;- }- let size_str = line.trim().split(';').next().unwrap_or("").trim();- let size = usize::from_str_radix(size_str, 16).unwrap_or(0);- if size == 0 {- // trailer + final CRLF- loop {- line.clear();- let n = reader.read_line(&mut line).unwrap_or(0);- if n == 0 || line.trim_end_matches(['\r', '\n']).is_empty() {- break;- }- }- break;- }- // The declared size comes straight off the wire: never allocate- // it up front (a huge chunk-size line would OOM-abort the process- // under panic=abort). Stream the chunk through a small buffer and- // stop as soon as the body would exceed the cap. The comparison- // must not add first: `body.len() + size` wraps when `size` is- // near usize::MAX, which would bypass the cap entirely.- if size > max_body.saturating_sub(body.len()) {- truncated = true;- break;- }- let mut remaining = size;- let mut buf = [0u8; 8192];- let mut eof = false;- while remaining > 0 {- let want = remaining.min(buf.len());- match reader.read(&mut buf[..want]) {- Ok(0) | Err(_) => {- eof = true;- break;- }- Ok(n) => {- remaining -= n;- body.extend_from_slice(&buf[..n]);- }- }- }- if eof {- break;- }- line.clear();- let _ = reader.read_line(&mut line);- }- }- let (path, query) = parse_target(&target);- Some(Request {- method,- target,- path,- query,- headers,- body,- body_truncated: truncated,- })-}--fn reason(status: u16) -> &'static str {- match status {- 200 => "OK",- 202 => "Accepted",- 204 => "No Content",- 400 => "Bad Request",- 401 => "Unauthorized",- 403 => "Forbidden",- 404 => "Not Found",- 409 => "Conflict",- 410 => "Gone",- 413 => "Payload Too Large",- 415 => "Unsupported Media Type",- 422 => "Unprocessable Entity",- 500 => "Internal Server Error",- _ => "",- }-}--/// A response under construction: status, headers (in insertion order), body.-pub struct Response {- pub status: u16,- pub headers: Vec<(String, String)>,- pub body: Vec<u8>,-}--impl Response {- pub fn new(status: u16) -> Response {- Response {- status,- headers: Vec::new(),- body: Vec::new(),- }- }- pub fn header(mut self, name: &str, value: &str) -> Response {- self.headers.push((name.to_string(), value.to_string()));- self- }- pub fn text(mut self, body: &str) -> Response {- self.body = body.as_bytes().to_vec();- self- }- pub fn bytes(mut self, body: Vec<u8>) -> Response {- self.body = body;- self- }- pub fn json(self, v: &serde_json::Value) -> Response {- let s = serde_json::to_string(v).unwrap_or_else(|_| "null".into());- self.header("Content-Type", "application/json").text(&s)- }-}--/// Write a complete response and close the connection.-pub fn send_response(stream: &mut TcpStream, cors: &[(String, String)], res: &Response) {- let mut head = format!("HTTP/1.1 {} {}\r\n", res.status, reason(res.status));- for (k, v) in cors {- head.push_str(&format!("{}: {}\r\n", k, v));- }- for (k, v) in &res.headers {- head.push_str(&format!("{}: {}\r\n", k, v));- }- head.push_str(&format!("Content-Length: {}\r\n", res.body.len()));- head.push_str("Connection: close\r\n\r\n");- let _ = stream.write_all(head.as_bytes());- let _ = stream.write_all(&res.body);- let _ = stream.flush();- let _ = stream.shutdown(Shutdown::Write);-}--/// A long-lived response written incrementally (chunked transfer encoding).-pub struct StreamResponse {- stream: TcpStream,- open: bool,-}--impl StreamResponse {- pub fn begin(- mut stream: TcpStream,- cors: &[(String, String)],- status: u16,- headers: &[(&str, &str)],- ) -> StreamResponse {- let mut head = format!("HTTP/1.1 {} {}\r\n", status, reason(status));- for (k, v) in cors {- head.push_str(&format!("{}: {}\r\n", k, v));- }- for (k, v) in headers {- head.push_str(&format!("{}: {}\r\n", k, v));- }- head.push_str("Transfer-Encoding: chunked\r\n\r\n");- let ok = stream.write_all(head.as_bytes()).is_ok() && stream.flush().is_ok();- StreamResponse { stream, open: ok }- }- /// Write one chunk; false when the client is gone.- pub fn write(&mut self, data: &[u8]) -> bool {- if !self.open || data.is_empty() {- return self.open;- }- let head = format!("{:x}\r\n", data.len());- let ok = self.stream.write_all(head.as_bytes()).is_ok()- && self.stream.write_all(data).is_ok()- && self.stream.write_all(b"\r\n").is_ok()- && self.stream.flush().is_ok();- if !ok {- self.open = false;- }- ok- }- pub fn end(&mut self) {- if self.open {- let _ = self.stream.write_all(b"0\r\n\r\n");- let _ = self.stream.flush();- self.open = false;- }- let _ = self.stream.shutdown(Shutdown::Both);- }- pub fn is_open(&self) -> bool {- self.open- }-}--/// Watch a connection for the peer closing it (Node's `req.on('close')`):-/// a reader thread blocks on the socket and calls `on_close` when the read-/// returns EOF or an error, unless `done` was set first (we closed it).-pub fn watch_close(- stream: &TcpStream,- done: Arc<AtomicBool>,- on_close: impl FnOnce() + Send + 'static,-) {- let Ok(mut clone) = stream.try_clone() else {- return;- };- let _ = clone.set_read_timeout(None);- std::thread::spawn(move || {- let mut buf = [0u8; 64];- loop {- match clone.read(&mut buf) {- Ok(0) | Err(_) => break,- Ok(_) => continue,- }- }- if !done.load(Ordering::SeqCst) {- on_close();- }- });-}--/// Set a per-`read` timeout on the socket. This is `SO_RCVTIMEO`: it bounds a-/// single blocked `read`, not the whole connection, and any byte received-/// resets it. It is a coarse backstop only; the total lane-hold bound is-/// [`read_request_deadline`], not this.-pub fn set_header_timeout(stream: &TcpStream) {- let _ = stream.set_read_timeout(Some(Duration::from_secs(60)));-}--/// Total wall-clock budget for reading one request (request line + headers +-/// body) from an accepted connection while it may still hold a place in the-/// turnstile lane.-///-/// [`set_header_timeout`] is per-`read` (`SO_RCVTIMEO`): any byte inside the-/// window resets it, so a slow-drip or silent peer keeps each `read` returning-/// `Ok(1)` (or blocking under the window) and `read_request` never returns,-/// pinning the connection's ticket and wedging every later mutation behind it.-/// This is one deadline across the whole read, enforced by a watchdog that-/// shuts the socket down when it elapses, turning that pre-auth lane-hold DoS-/// into a bounded, localhost-only stall (BLOCKER, triage D3). 10s is far more-/// than any legitimate local upload needs (the race test's ~4 KB over ~500 ms-/// finishes in a twentieth of it).-pub const READ_REQUEST_DEADLINE: Duration = Duration::from_secs(10);--/// Like [`read_request`] but bounded by a total wall-clock `deadline` across-/// the entire head+body read. A watchdog thread shuts the socket down if the-/// deadline elapses before the read finishes, so the blocked `read` returns and-/// `read_request` gives up (`None`, or a truncated body); the caller returns-/// and the ticket is released on `Drop`. A connection therefore cannot hold its-/// place in the lane past `deadline`, no matter how slowly (or never) it feeds-/// the socket.-pub fn read_request_deadline(- stream: &TcpStream,- max_body: usize,- deadline: Duration,-) -> Option<Request> {- // Bound the read at the socket as well as at the watchdog. Windows does- // not unblock a `recv` already parked in the kernel when another thread- // calls `shutdown` on the same socket, so there the watchdog alone cannot- // enforce the deadline and a silent connection held its ticket for the- // whole 60s header timeout. A read that makes no progress for the deadline- // now returns on every platform and the handler drops its ticket; the- // watchdog stays as the backstop for a connection that keeps trickling- // bytes without ever completing a request.- let prev_timeout = stream.read_timeout().ok().flatten();- let _ = stream.set_read_timeout(Some(deadline));- let done = Arc::new(AtomicBool::new(false));- let watch_stream = stream.try_clone().ok()?;- let watch_done = done.clone();- std::thread::spawn(move || {- // Poll the done flag so a fast, normal read frees the watchdog promptly- // instead of pinning a thread for the whole deadline.- let step = Duration::from_millis(50);- let mut waited = Duration::ZERO;- while waited < deadline {- if watch_done.load(Ordering::SeqCst) {- return;- }- std::thread::sleep(step);- waited += step;- }- if !watch_done.load(Ordering::SeqCst) {- // Unblock any in-progress read so `read_request` returns; the- // handler then drops its ticket and the connection closes.- let _ = watch_stream.shutdown(Shutdown::Both);- }- });- let req = read_request(stream, max_body);- done.store(true, Ordering::SeqCst);- let _ = stream.set_read_timeout(prev_timeout);- req-}--/// Serializes state-mutating request handlers in body-completion order.-///-/// Node's `http` server is single-threaded: a `/events` handler reads its-/// body, and its state mutation runs inside the `end` callback, atomically,-/// before any other request's `end` callback. So the server that creates a-/// session (`generate`) and the checkpoint that references it-/// (`generate_started`) never interleave; the checkpoint sees a store the-/// `generate` before it already wrote. This server is thread-per-connection,-/// so without help two concurrent POSTs race on who reaches the state lock-/// first, and a checkpoint whose small body finishes fast can overtake the-/// `generate` that creates its session, which turns into `404-/// unknown_session`.-///-/// This is the single FIFO those mutations pass through. The accept loop-/// hands every connection a ticket, so tickets are ordered by arrival. A-/// handler reads and parses its FULL request first (that stays parallel);-/// only then, before it touches server state, does it call-/// [`Ticket::wait_turn`], which blocks until every earlier ticket has been-/// released. There is **no timeout**: a checkpoint waits for the earlier-/// `generate` however long that `generate`'s body takes to arrive, so the-/// ordering is deterministic regardless of connection speed. A ticket is-/// released when its handler finishes, when the connection is about to park-/// (SSE, long polls) or run something long, or when the connection dies-/// (`Drop`) without producing a request. So a handler only ever blocks on-/// earlier connections that are alive and still working.-///-/// The wait is unbounded on purpose (ordering must not give up), so on its own-/// a slow-drip or silent connection accepted first would pin its ticket inside-/// [`read_request`] and wedge every later mutation forever. That is prevented-/// upstream, not here: a connection reads its request under a total wall-clock-/// deadline ([`read_request_deadline`]), so a peer that never finishes its-/// request is force-dropped and releases its ticket within that bound. A hung-/// or abandoned upload can delay a later mutation by at most the deadline, and-/// only from localhost.-///-/// Read-only routes (`/status`, `/health`, `/design-system*`, `/source`,-/// `/live.js`, `/detect.js`, `/`) and long-lived routes (SSE, parked polls)-/// do not sit in this lane: they release their ticket up front (read-only)-/// or as soon as they have registered and are about to park (long-lived),-/// so a mutation never waits on a stream and a stream never holds the lane.-pub struct Turnstile {- inner: std::sync::Mutex<TurnstileState>,- cv: std::sync::Condvar,-}--struct TurnstileState {- next: u64,- /// Tickets issued but not yet released.- outstanding: std::collections::BTreeSet<u64>,-}--impl Default for Turnstile {- fn default() -> Self {- Turnstile {- inner: std::sync::Mutex::new(TurnstileState {- next: 1,- outstanding: std::collections::BTreeSet::new(),- }),- cv: std::sync::Condvar::new(),- }- }-}--impl Turnstile {- fn state(&self) -> std::sync::MutexGuard<'_, TurnstileState> {- match self.inner.lock() {- Ok(g) => g,- Err(p) => p.into_inner(),- }- }-- /// Called by the accept loop, in accept order.- pub fn issue(self: &Arc<Self>) -> Ticket {- let mut st = self.state();- let n = st.next;- st.next += 1;- st.outstanding.insert(n);- Ticket {- turnstile: self.clone(),- n,- released: false,- }- }-- /// Block until every earlier outstanding ticket is released. No timeout:- /// ordering is by arrival and never gives up, so a later mutation cannot- /// overtake an earlier one whose body is still arriving. An earlier- /// connection that dies without producing a request releases its ticket- /// on `Drop`, so this wait only ever tracks earlier connections that are- /// alive and still working.- fn wait_turn(&self, n: u64) {- let mut st = self.state();- loop {- match st.outstanding.iter().next() {- Some(&first) if first < n => {}- _ => return,- }- st = match self.cv.wait(st) {- Ok(g) => g,- Err(p) => p.into_inner(),- };- }- }-- fn release(&self, n: u64) {- let mut st = self.state();- if st.outstanding.remove(&n) {- self.cv.notify_all();- }- }-}--/// One connection's place in the arrival order. Dropping it releases the-/// ticket, so a connection that never produces a request (EOF, timeout)-/// cannot hold later ones up: it leaves the lane the instant it dies.-pub struct Ticket {- turnstile: Arc<Turnstile>,- n: u64,- released: bool,-}--impl Ticket {- /// Wait for the connections accepted before this one to finish with- /// server state. Call before the first state access of a handler; a- /// no-op after `release`.- pub fn wait_turn(&self) {- if !self.released {- self.turnstile.wait_turn(self.n);- }- }- /// Let later connections proceed. Call before parking or before running- /// anything long that does not need arrival ordering.- pub fn release(&mut self) {- if !self.released {- self.released = true;- self.turnstile.release(self.n);- }- }-}--impl Drop for Ticket {- fn drop(&mut self) {- self.release();- }-}--#[cfg(test)]-mod tests {- use super::*;- use std::net::TcpListener;-- /// Run `read_request` on a raw request written by a client thread.- fn read_raw(raw: Vec<u8>, max_body: usize) -> Option<Request> {- let listener = TcpListener::bind("127.0.0.1:0").expect("bind");- let addr = listener.local_addr().expect("addr");- let client = std::thread::spawn(move || {- let mut s = TcpStream::connect(addr).expect("connect");- let _ = s.write_all(&raw);- let _ = s.flush();- let _ = s.shutdown(Shutdown::Write);- // Hold the read half open until the server is done.- let mut sink = Vec::new();- let _ = s.read_to_end(&mut sink);- });- let (stream, _) = listener.accept().expect("accept");- set_header_timeout(&stream);- let req = read_request(&stream, max_body);- drop(stream);- let _ = client.join();- req- }-- #[test]- fn chunked_body_is_decoded() {- let req = read_raw(- b"POST /x HTTP/1.1\r\nHost: h\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n3\r\nabc\r\n0\r\n\r\n".to_vec(),- 1024 * 1024,- )- .expect("request");- assert_eq!(req.body, b"helloabc");- assert!(!req.body_truncated);- }-- #[test]- fn huge_declared_chunk_size_is_capped_not_allocated() {- // A wire-supplied chunk size of ~256 TB must not be allocated up- // front (under panic=abort that OOM would take the server down); the- // request comes back truncated instead.- let req = read_raw(- b"POST /x HTTP/1.1\r\nHost: h\r\nTransfer-Encoding: chunked\r\n\r\nffffffffffff\r\nhi".to_vec(),- 1024 * 1024,- )- .expect("request");- assert!(req.body_truncated);- }-- #[test]- fn chunk_size_near_usize_max_does_not_wrap_past_cap() {- // After a 1-byte chunk, a declared size of usize::MAX made the old- // `body.len() + size > max_body` guard wrap to 0 and pass, bypassing- // the cap entirely. It must read as over-cap (truncated), not bypass.- let req = read_raw(- b"POST /x HTTP/1.1\r\nHost: h\r\nTransfer-Encoding: chunked\r\n\r\n1\r\na\r\nffffffffffffffff\r\nrest".to_vec(),- 1024 * 1024,- )- .expect("request");- assert!(req.body_truncated);- assert_eq!(req.body, b"a");- }-- #[test]- fn chunk_overflowing_cap_marks_truncated() {- let req = read_raw(- b"POST /x HTTP/1.1\r\nHost: h\r\nTransfer-Encoding: chunked\r\n\r\n4\r\nabcd\r\n4\r\nefgh\r\n0\r\n\r\n".to_vec(),- 6,- )- .expect("request");- assert!(req.body_truncated);- assert!(req.body.len() <= 6);- }-- #[test]- fn oversized_header_block_is_dropped() {- // A head past MAX_HEAD_BYTES reads as EOF: the request is dropped- // (connection closed) instead of accumulating headers unbounded.- let mut raw = b"GET / HTTP/1.1\r\nHost: h\r\n".to_vec();- raw.extend_from_slice(b"X-Big: ");- raw.extend(std::iter::repeat(b'a').take(2 * MAX_HEAD_BYTES as usize));- raw.extend_from_slice(b"\r\n\r\n");- assert!(read_raw(raw, 1024 * 1024).is_none());- }-- #[test]- fn head_under_cap_with_body_still_parses() {- // Sanity: the head budget must not eat into the body read.- let req = read_raw(- b"POST /x HTTP/1.1\r\nHost: h\r\nContent-Length: 5\r\n\r\nhello".to_vec(),- 1024 * 1024,- )- .expect("request");- assert_eq!(req.body, b"hello");- assert!(!req.body_truncated);- }-- /// The generate/generate_started race, reproduced end to end over real- /// sockets: a `generate` (accepted first) whose large body arrives in- /// slow, delayed chunks, and a small `generate_started` checkpoint- /// (accepted second) that reaches the server fast and races ahead.- ///- /// The checkpoint is the `unknown_session` gate: it must never run its- /// store lookup before the `generate` that creates the session has run- /// its insert, no matter how slowly the `generate` body dribbles in.- /// Under the old 250 ms accept-order grace, a body slower than the grace- /// let the checkpoint skip the still-arriving `generate` and observe an- /// empty store (`unknown_session`); with the grace gone the checkpoint- /// waits for the whole slow body. Looped so a flaky reorder would show.- #[test]- fn slow_generate_body_never_lets_checkpoint_see_unknown_session() {- use std::collections::HashSet;- use std::sync::mpsc;- use std::sync::Mutex;-- let iters: usize = std::env::var("IMPECCABLE_RACE_ITERS")- .ok()- .and_then(|v| v.parse().ok())- .unwrap_or(10);-- for iter in 0..iters {- let listener = TcpListener::bind("127.0.0.1:0").expect("bind");- let addr = listener.local_addr().expect("addr");- let turnstile = Arc::new(Turnstile::default());- // Stand-in for the session store: the ids `generate` has created.- let store: Arc<Mutex<HashSet<String>>> = Arc::new(Mutex::new(HashSet::new()));- // Server -> test: fires once the first connection is accepted, so- // the test only connects the checkpoint after the generate holds- // the earlier ticket (deterministic accept order).- let (accepted_tx, accepted_rx) = mpsc::channel::<()>();- // Checkpoint handler -> test: whether it saw the session.- let (result_tx, result_rx) = mpsc::channel::<bool>();-- let srv_turnstile = turnstile.clone();- let srv_store = store.clone();- let server = std::thread::spawn(move || {- let mut handlers = Vec::new();- for i in 0..2 {- let (stream, _) = listener.accept().expect("accept");- let _ = stream.set_nonblocking(false);- // Ticket issued in accept order, exactly as the live- // server's accept loop does.- let ticket = srv_turnstile.issue();- if i == 0 {- let _ = accepted_tx.send(());- }- let store = srv_store.clone();- let result_tx = result_tx.clone();- handlers.push(std::thread::spawn(move || {- // Ticket lives for the whole handler; dropped at the- // end -> released, letting the next mutation proceed.- let ticket = ticket;- set_header_timeout(&stream);- let Some(req) = read_request(&stream, 1024 * 1024) else {- return;- };- // Full body is read; only now take the ordering turn.- ticket.wait_turn();- let msg: serde_json::Value =- serde_json::from_slice(&req.body).unwrap_or(serde_json::Value::Null);- let ty = msg.get("type").and_then(|v| v.as_str()).unwrap_or("");- let id = msg- .get("id")- .and_then(|v| v.as_str())- .unwrap_or("")- .to_string();- if ty == "generate" {- store.lock().unwrap().insert(id);- } else {- let known = store.lock().unwrap().contains(&id);- let _ = result_tx.send(known);- }- }));- }- for h in handlers {- let _ = h.join();- }- });-- let sid = format!("sess-{iter}");- // The generate body is large; dribble it in chunks with sleeps so- // it takes ~300 ms to finish arriving (well past the old grace).- let gen_body = serde_json::json!({- "type": "generate",- "id": sid,- "pad": "x".repeat(4000),- })- .to_string();- let gen_head = format!(- "POST /events HTTP/1.1\r\nHost: h\r\nContent-Length: {}\r\n\r\n",- gen_body.len()- );-- let mut gen = TcpStream::connect(addr).expect("connect generate");- gen.set_nodelay(true).ok();- gen.write_all(gen_head.as_bytes()).expect("write gen head");- gen.flush().ok();- // Block until the server has accepted the generate connection, so- // the checkpoint that follows is guaranteed the later ticket.- accepted_rx.recv().expect("generate accepted");-- let gen_writer = std::thread::spawn(move || {- let bytes = gen_body.into_bytes();- // Six chunks with 100 ms gaps: the body finishes arriving- // ~500 ms after it starts, well past the old 250 ms grace, so- // a checkpoint that gave up on the grace would beat the insert.- for chunk in bytes.chunks(bytes.len() / 6 + 1) {- if gen.write_all(chunk).is_err() {- break;- }- let _ = gen.flush();- std::thread::sleep(Duration::from_millis(100));- }- // Hold the connection open until the server is done reading.- let mut sink = Vec::new();- let _ = gen.read_to_end(&mut sink);- });-- // The checkpoint: small, sent complete and fast, right away.- let cp_body = serde_json::json!({- "type": "checkpoint",- "id": sid,- "reason": "generate_started",- })- .to_string();- let cp_raw = format!(- "POST /events HTTP/1.1\r\nHost: h\r\nContent-Length: {}\r\n\r\n{}",- cp_body.len(),- cp_body- );- let mut cp = TcpStream::connect(addr).expect("connect checkpoint");- cp.set_nodelay(true).ok();- cp.write_all(cp_raw.as_bytes()).expect("write checkpoint");- cp.flush().ok();-- let known = result_rx- .recv_timeout(Duration::from_secs(10))- .expect("checkpoint handler ran");- assert!(- known,- "iteration {iter}: checkpoint observed unknown_session; \- the slow generate body was overtaken (ordering broke)",- );-- let mut sink = Vec::new();- let _ = cp.read_to_end(&mut sink);- let _ = gen_writer.join();- let _ = server.join();- }- }-- /// The Fable BLOCKER, reproduced at the lane level: a connection accepted- /// first that takes a ticket and then goes silent must not wedge a later- /// state-mutating request beyond the read deadline.- ///- /// The silent connection sends a partial head and never finishes, so its- /// handler blocks inside `read_request`. Under the plain per-`read` timeout- /// it held its ticket for the whole (up to 60s) window and every later- /// mutation waited on it with `wait_turn`'s NO-timeout condvar. With- /// `read_request_deadline` the silent connection is force-dropped at the- /// total deadline and drops its ticket, so the later request takes its turn- /// within the bound. Reverting to a deadline-less read makes this hang and- /// the timed `recv` below fail, so it stays a distinguishing test.- #[test]- fn silent_connection_holding_ticket_does_not_wedge_later_events_beyond_deadline() {- use std::sync::mpsc;-- // Short in-test deadline keeps the test fast; production uses- // READ_REQUEST_DEADLINE (10s).- let deadline = Duration::from_millis(400);-- let listener = TcpListener::bind("127.0.0.1:0").expect("bind");- let addr = listener.local_addr().expect("addr");- let turnstile = Arc::new(Turnstile::default());- // Server -> test: the first connection has been accepted (and holds the- // lower ticket), so the later request is guaranteed the higher ticket.- let (accepted_tx, accepted_rx) = mpsc::channel::<()>();- // Later handler -> test: it took its turn and ran.- let (ran_tx, ran_rx) = mpsc::channel::<()>();-- let srv_turnstile = turnstile.clone();- let server = std::thread::spawn(move || {- let mut handlers = Vec::new();- for i in 0..2 {- let (stream, _) = listener.accept().expect("accept");- let _ = stream.set_nonblocking(false);- // Ticket issued in accept order, as the live accept loop does.- let ticket = srv_turnstile.issue();- if i == 0 {- let _ = accepted_tx.send(());- }- let ran_tx = ran_tx.clone();- handlers.push(std::thread::spawn(move || {- let ticket = ticket;- set_header_timeout(&stream);- // The silent connection (i == 0) reads nothing complete and- // returns None at the deadline, dropping its ticket. The- // later one reads a full request, then takes its turn.- if read_request_deadline(&stream, 1024 * 1024, deadline).is_some() {- ticket.wait_turn();- let _ = ran_tx.send(());- }- }));- }- for h in handlers {- let _ = h.join();- }- });-- // Connection A (accepted first, lower ticket): a partial head, then- // silence. Kept open until the assertion so its ticket can only be- // released by the deadline, not by an early peer close.- let mut silent = TcpStream::connect(addr).expect("connect silent");- silent.set_nodelay(true).ok();- silent- .write_all(b"POST /events HTTP/1.1\r\nHost: h\r\n")- .expect("write partial head");- silent.flush().ok();- accepted_rx.recv().expect("silent connection accepted");-- // Connection B (accepted second, higher ticket): a complete request- // that must take its turn once the silent ticket is released.- let body = "{\"type\":\"checkpoint\"}";- let raw = format!(- "POST /events HTTP/1.1\r\nHost: h\r\nContent-Length: {}\r\n\r\n{}",- body.len(),- body- );- let mut later = TcpStream::connect(addr).expect("connect later");- later.set_nodelay(true).ok();- later.write_all(raw.as_bytes()).expect("write later");- later.flush().ok();-- // Must run within a small multiple of the deadline. Before the fix the- // silent ticket was held for the whole read window and this never- // arrived; generous slack absorbs CI scheduling. The Windows runner- // schedules the watchdog's polling loop against a ~15.6ms system timer- // while the whole crate's tests run in parallel, so it gets a wider- // margin; the bound stays far under the 60s read timeout a- // deadline-less read would hold the ticket for, so the test still- // distinguishes the fix from the regression.- let slack = if cfg!(windows) { deadline * 12 } else { deadline * 6 };- ran_rx.recv_timeout(slack).expect(- "later /events was wedged behind a silent connection past the read deadline",- );-- drop(silent);- drop(later);- let _ = server.join();- }-}+pub use impeccable_common::http::*;diff --git a/docs/ENGINE.md b/docs/ENGINE.mdindex 9a44ad42a..dcfcde616 100644--- a/docs/ENGINE.md+++ b/docs/ENGINE.md@@ -22,7 +22,8 @@ ENGINE_VERSION which engine release the launcher / npm shim downloadbrowser-bundle/ the page JS the in-page bundle is built fromcrates/cli the `impeccable` binary: verb router, exit codes- common Io handle (stdout/stderr/stdin/env/cwd), path + process helpers+ common Io handle (stdout/stderr/stdin/env/cwd), path + process helpers,+ the HTTP/1.1 server core (`http`) behind every local servercontext context, doctor, staleness, signals, concept-seed, pin, ...hook the design hook (hook, hook-before-edit, hook-admin)live live mode: server, wrap, accept, manual edits, Svelte/Vue@@ -65,6 +66,16 @@ release there; `IMPECCABLE_BIN=target/release/impeccable bun run fetch:engine`copies a local build), then `target/release/impeccable`, so a plain`cargo build --release -p impeccable` is enough.+Every local server the engine runs (`live-server`, the `serve-question`+decision page, `component-review serve`) is built on `impeccable_common::http`:+a `TcpListener`, one thread per connection, request parsing with a body cap,+and responses that close their connection. The live server drives connections+itself (turnstile ordering, SSE, parked polls); the other two take requests one+at a time from `SerialServer`, so their handlers stay single-threaded while no+connection waits on another to be read. That replaced tiny_http, whose 0.12+pool could leave part of a browser's connection burst unanswered (#1071). Add+new servers on the same core rather than another HTTP crate.+The frozen function-level vectors in `tests/oracle/vectors/calls/` replaythrough `impeccable_core::vectors::call` (`cargo test -p impeccable-core`),which is the union of foundation's dispatch arms and the core's.diff --git a/scripts/test-suites.mjs b/scripts/test-suites.mjsindex 58af15f6b..9be330f8b 100644--- a/scripts/test-suites.mjs+++ b/scripts/test-suites.mjs@@ -223,6 +223,9 @@ export const SUITES = {...COMMON_INFRA_PATTERNS,/^skill\/scripts\/live-browser/,/^ENGINE_VERSION$/,+ // The HTTP core the live server runs on (in common since the decision+ // page and component review share it) and live's re-export of it.+ /^crates\/(common\/src\/http|live\/src\/live_http)\.rs$/,/^tests\/framework-fixtures/,/^tests\/live-e2e(\.test\.mjs|\/)/,/^browser-bundle\/40-overlay\.js$/,