From 106ecb093f0be88411965d47975b4301c7c79f06 Mon Sep 17 00:00:00 2001 From: Shubham Singh Date: Thu, 3 Dec 2026 19:19:50 +0530 Subject: [PATCH 1/2] fix(serve): honor --cors for the decision server The --cors flag reached the LLM server but was dropped when serve routed a decision model to its local decision endpoint. Pass the flag through to the decision server, answer OPTIONS with 204, and add the same Access-Control headers the SDK LLM server uses to every response, including 400/404/413/431 and 500. Without --cors the behavior is unchanged. Validated with the GLiNER 2.5 Decide model: preflight returns 204 with the headers, POST returns the same decision JSON with the headers, and error responses carry them too. Browser-level CORS validation was not performed. --- src/commands/cmd_serve.rs | 65 ++++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 11 deletions(-) diff --git a/src/commands/cmd_serve.rs b/src/commands/cmd_serve.rs index 62e1c75b..d345cb58 100644 --- a/src/commands/cmd_serve.rs +++ b/src/commands/cmd_serve.rs @@ -101,7 +101,7 @@ fn run_serve( // Decision checkpoints get their own endpoint: rac_server is LLM-only, // so a decision model reference serves POST /v1/decisions in-process. if !reference.is_empty() && is_decision_reference(reference) { - return run_serve_decisions(options, reference, host, port); + return run_serve_decisions(options, reference, host, port, cors); } let model = match ensure_model_ready( @@ -327,11 +327,24 @@ fn parse_head(head: &[u8]) -> Result<(String, String, usize), String> { /// Best-effort response on a nonblocking connection, with one size-dependent /// absolute write budget shared by the headers and body. Progress never /// renews it; responses that cannot finish within the capped budget close. +/// When `cors` is set, every response carries the same headers the SDK's LLM +/// server adds in its pre-routing handler, error responses included. #[cfg(wally_has_server)] -fn write_response(stream: &mut std::net::TcpStream, status: u16, reason: &str, body: &str) { +fn write_response( + stream: &mut std::net::TcpStream, + status: u16, + reason: &str, + body: &str, + cors: bool, +) { use std::io::{ErrorKind, Write}; + let cors_headers = if cors { + "Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: GET, POST, OPTIONS\r\nAccess-Control-Allow-Headers: Content-Type, Authorization\r\n" + } else { + "" + }; let head = format!( - "HTTP/1.1 {status} {reason}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + "HTTP/1.1 {status} {reason}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n{cors_headers}\r\n", body.len() ); let transfer = @@ -369,6 +382,7 @@ fn json_error(message: &str) -> String { fn handle_decisions_connection( stream: &mut std::net::TcpStream, loaded: &crate::commands::cmd_decisions::LoadedDecisions, + cors: bool, ) { let header_deadline = std::time::Instant::now() + SERVE_HEADER_TIMEOUT; let mut raw: Vec = Vec::new(); @@ -384,6 +398,7 @@ fn handle_decisions_connection( 431, "Request Header Fields Too Large", &json_error("headers too large"), + cors, ); return; } @@ -397,6 +412,7 @@ fn handle_decisions_connection( 408, "Request Timeout", &json_error("request timed out"), + cors, ); return; } @@ -406,18 +422,24 @@ fn handle_decisions_connection( let (method, path, length) = match parse_head(&raw[..head_end]) { Ok(parsed) => parsed, Err(message) => { - write_response(stream, 400, "Bad Request", &json_error(&message)); + write_response(stream, 400, "Bad Request", &json_error(&message), cors); return; } }; + // Preflight, before any method/path routing, the way the SDK's LLM server + // answers it. Without --cors, OPTIONS keeps falling through to the 404. + if cors && method == "OPTIONS" { + write_response(stream, 204, "No Content", "", cors); + return; + } if path == "/health" && method == "GET" { let mut json = crate::io::output::JsonWriter::new(); json.begin_object().field_str("status", "ok").end_object(); - write_response(stream, 200, "OK", json.str()); + write_response(stream, 200, "OK", json.str(), cors); return; } if method != "POST" || path != "/v1/decisions" { - write_response(stream, 404, "Not Found", &json_error("not found")); + write_response(stream, 404, "Not Found", &json_error("not found"), cors); return; } if length > MAX_SERVE_BODY_BYTES { @@ -426,6 +448,7 @@ fn handle_decisions_connection( 413, "Content Too Large", &json_error("request body exceeds 1 MiB"), + cors, ); return; } @@ -441,6 +464,7 @@ fn handle_decisions_connection( 408, "Request Timeout", &json_error("request timed out"), + cors, ); return; } @@ -454,6 +478,7 @@ fn handle_decisions_connection( 400, "Bad Request", &json_error("truncated request body"), + cors, ); return; } @@ -466,6 +491,7 @@ fn handle_decisions_connection( 400, "Bad Request", &json_error("invalid request JSON"), + cors, ); return; } @@ -480,6 +506,7 @@ fn handle_decisions_connection( "request has fields the decisions contract does not define: {}", unknown.join(", ") )), + cors, ); return; } @@ -493,12 +520,13 @@ fn handle_decisions_connection( &json_error(&format!( "request does not match the decisions contract: {error}" )), + cors, ); return; } }; if let Err(error) = crate::commands::cmd_decisions::validate(&request) { - write_response(stream, 400, "Bad Request", &json_error(&error)); + write_response(stream, 400, "Bad Request", &json_error(&error), cors); return; } if SERVE_STOP.load(Ordering::SeqCst) { @@ -513,19 +541,34 @@ fn handle_decisions_connection( 200, "OK", &crate::io::json::dump(&response.to_json()), + cors, ); } Err(message) => { - write_response(stream, 500, "Internal Server Error", &json_error(&message)); + write_response( + stream, + 500, + "Internal Server Error", + &json_error(&message), + cors, + ); } } } /// Serve a local decision checkpoint over HTTP: one model loaded once, then /// POST /v1/decisions per request plus GET /health. Mirrors the CLI's -/// validation so the same body behaves the same in both. +/// validation so the same body behaves the same in both. `--cors` adds the +/// same headers the SDK's LLM server applies and answers preflight before +/// routing. #[cfg(wally_has_server)] -fn run_serve_decisions(options: &GlobalOptions, reference: &str, host: &str, port: u16) -> i32 { +fn run_serve_decisions( + options: &GlobalOptions, + reference: &str, + host: &str, + port: u16, + cors: bool, +) -> i32 { if bootstrap::bootstrap(options).is_err() { return 1; } @@ -574,7 +617,7 @@ fn run_serve_decisions(options: &GlobalOptions, reference: &str, host: &str, por error_line(&format!("could not set connection non-blocking: {error}")); continue; } - handle_decisions_connection(&mut stream, &loaded); + handle_decisions_connection(&mut stream, &loaded, cors); } Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => { std::thread::sleep(std::time::Duration::from_millis(50)); From 63bd3400c080290333a771012f55dd84304cde39 Mon Sep 17 00:00:00 2001 From: Shubham Singh Date: Fri, 4 Dec 2026 19:38:33 +0530 Subject: [PATCH 2/2] fix(serve): omit content length from 204 responses RFC 9110 section 8.6 forbids Content-Length on 204 responses, including an explicit zero. Keep the normal JSON response framing for other statuses. Verified eight raw TCP exchanges with a real decision model and all five existing serve tests. Browser behavior remains unverified. --- src/commands/cmd_serve.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/commands/cmd_serve.rs b/src/commands/cmd_serve.rs index d345cb58..c63e8d8c 100644 --- a/src/commands/cmd_serve.rs +++ b/src/commands/cmd_serve.rs @@ -343,9 +343,14 @@ fn write_response( } else { "" }; + // RFC 9110 section 8.6 forbids Content-Length on 204, even when zero. + let content_length = if status == 204 { + String::new() + } else { + format!("Content-Length: {}\r\n", body.len()) + }; let head = format!( - "HTTP/1.1 {status} {reason}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n{cors_headers}\r\n", - body.len() + "HTTP/1.1 {status} {reason}\r\nContent-Type: application/json\r\n{content_length}Connection: close\r\n{cors_headers}\r\n" ); let transfer = std::time::Duration::from_secs_f64(body.len() as f64 / SERVE_WRITE_BYTES_PER_SECOND as f64);