kute-cli 0.9.2

Command line interface for the Kannaka Universal Tokenization Engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//! A small HTTP service over the same five read-only operations as the MCP
//! server, and through the same code: `encode`, `decode`, `inspect`,
//! `compare`, `discover`, plus `capabilities`. Version 1 of the interface
//! lives under `/v1/`.
//!
//! It is written directly on `std::net` so that every bound is in this
//! file: one request per connection, a fixed number of workers, a fixed
//! queue, a deadline on reading, a cap on the header block and on the body.
//! It does not speak TLS and does not authenticate anyone, so it listens
//! on the loopback interface unless told otherwise, and on loopback it
//! answers only requests addressed to a loopback host name.

use crate::mcp::Server;
use serde_json::{json, Value};
use std::io::{Read, Write};
use std::net::{SocketAddr, TcpListener, TcpStream};
use std::sync::mpsc::{sync_channel, Receiver, TrySendError};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

/// The operations a POST may name.
pub const OPERATIONS: [&str; 5] = ["encode", "decode", "inspect", "compare", "discover"];

/// Most bytes of request line and headers.
const MAX_HEAD_BYTES: usize = 16 * 1024;

/// What the service may cost, apart from the per-call limits of the
/// operations themselves.
#[derive(Clone, Debug)]
pub struct Options {
    /// Requests handled at once.
    pub workers: usize,
    /// Accepted connections that may wait for a worker. Past it, a
    /// connection is answered 503 and closed.
    pub queue: usize,
    /// A request must arrive whole within this time, and its answer must
    /// be taken within it.
    pub timeout: Duration,
    /// Longest request body.
    pub max_body_bytes: u64,
    /// Answer requests whatever host they name. Off, only loopback names
    /// are answered, which keeps a web page from reaching the service
    /// through a name it controls.
    pub any_host: bool,
    /// Serve the Studio page under `/studio`.
    pub studio: bool,
    /// Give the files of the held vocabularies to whoever asks, under
    /// `/v1/vocabularies`. Off, those paths do not exist.
    pub registry: bool,
    /// Answer the five operations. Off, the service is a registry and
    /// nothing else: no caller can make it compute.
    pub operations: bool,
    /// Other registries to ask for a vocabulary this one does not hold.
    pub federation: Option<Arc<Federation>>,
    /// The status file of a peer on this machine, read for `GET /v1/peer`.
    pub peer_status: Option<std::path::PathBuf>,
}

/// Asking other registries, and what that may cost this one.
///
/// A vocabulary this registry does not hold is asked for upstream, in
/// the order the upstreams were named, checked as `kute fetch` checks
/// one (parsed, valid, the digest recomputed, test vectors run), and
/// kept in memory to be given again. Every bound is here: one question
/// upstream at a time, however many callers ask; a deadline and a size
/// limit on each; a cache of fixed size; and a short memory of digests
/// nobody had, so that asking for them again costs nothing.
pub struct Federation {
    pub upstreams: Vec<String>,
    /// Longest wait for one upstream.
    pub timeout: Duration,
    /// Largest file taken from an upstream.
    pub max_bytes: u64,
    /// Most bytes of upstream files kept. The oldest go first.
    pub cache_bytes: u64,
    /// What an upstream file must pass, beyond being the content its
    /// digest names: a signature from a key this registry trusts. With
    /// none, a file is passed on whoever signed it or nobody.
    check: Option<UpstreamCheck>,
    asking: Mutex<()>,
    kept: Mutex<std::collections::VecDeque<(String, Arc<[u8]>)>>,
    absent: Mutex<std::collections::VecDeque<(String, Instant)>>,
    /// For each upstream, when it last failed. One that failed is not
    /// asked again for a while, so a slow or broken upstream named
    /// first costs its timeout once and not on every question.
    failed: Mutex<Vec<Option<Instant>>>,
}

pub type UpstreamCheck = Box<dyn Fn(&kute::Vocabulary) -> Result<(), String> + Send + Sync>;

impl std::fmt::Debug for Federation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Federation").field("upstreams", &self.upstreams).field("check", &self.check.is_some()).finish_non_exhaustive()
    }
}

/// How long a digest that no upstream had is remembered as absent.
const ABSENT_FOR: Duration = Duration::from_secs(60);
/// How long an upstream that failed, stalled or sent something else is left alone.
const REST_FOR: Duration = Duration::from_secs(30);
/// How many such digests are remembered.
const ABSENT_KEPT: usize = 1024;

fn locked<T>(mutex: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
    mutex.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
}

impl Federation {
    pub fn new(upstreams: Vec<String>, timeout: Duration, max_bytes: u64, cache_bytes: u64, check: Option<UpstreamCheck>) -> kute::Result<Self> {
        for upstream in &upstreams {
            crate::registry::parse_url(upstream)?;
        }
        let failed = Mutex::new(vec![None; upstreams.len()]);
        Ok(Self { upstreams, timeout, max_bytes, cache_bytes, check, asking: Mutex::new(()), kept: Default::default(), absent: Default::default(), failed })
    }

    fn cached(&self, digest: &str) -> Option<Arc<[u8]>> {
        locked(&self.kept).iter().find(|(d, _)| d == digest).map(|(_, file)| file.clone())
    }

    /// The file with this digest from the cache or, when `ask` is set,
    /// from the first upstream that gives it. `Ok(None)` when nobody has it.
    fn find(&self, engine: &kute::Engine, digest: &str, ask: bool) -> Result<Option<Arc<[u8]>>, Reply> {
        if let Some(file) = self.cached(digest) {
            return Ok(Some(file));
        }
        if !ask {
            return Ok(None);
        }
        {
            let mut absent = locked(&self.absent);
            absent.retain(|(_, when)| when.elapsed() < ABSENT_FOR);
            if absent.iter().any(|(d, _)| d == digest) {
                return Ok(None);
            }
        }
        // One question upstream at a time. A caller who arrives meanwhile
        // is told to come back, and its worker is free at once: callers
        // asking for digests nobody has cannot hold every worker.
        let _asking = match self.asking.try_lock() {
            Ok(guard) => guard,
            Err(std::sync::TryLockError::Poisoned(poisoned)) => poisoned.into_inner(),
            Err(std::sync::TryLockError::WouldBlock) => return Err(problem(503, "Service Unavailable", "busy", "this registry is asking another for a vocabulary; try again")),
        };
        if let Some(file) = self.cached(digest) {
            return Ok(Some(file));
        }
        let mut every_upstream_said_no = true;
        for (i, upstream) in self.upstreams.iter().enumerate() {
            if locked(&self.failed)[i].is_some_and(|when| when.elapsed() < REST_FOR) {
                // Not asked, so it has not said no.
                every_upstream_said_no = false;
                continue;
            }
            match crate::registry::take(engine, upstream, digest, self.max_bytes, self.timeout, true) {
                Ok(crate::registry::Taken::Found(vocabulary, _)) => {
                    // The right content without a signature this registry
                    // requires is not kept: the next upstream may have it signed.
                    if self.check.as_ref().is_some_and(|check| check(&vocabulary).is_err()) {
                        every_upstream_said_no = false;
                        locked(&self.failed)[i] = Some(Instant::now());
                        continue;
                    }
                    // Written out again here, not passed on as it came.
                    let Ok(file) = kute::vocab::to_vec(&vocabulary) else { continue };
                    let file: Arc<[u8]> = file.into();
                    if file.len() as u64 <= self.cache_bytes {
                        let mut kept = locked(&self.kept);
                        kept.push_back((digest.to_string(), file.clone()));
                        while kept.iter().map(|(_, f)| f.len() as u64).sum::<u64>() > self.cache_bytes {
                            kept.pop_front();
                        }
                    }
                    return Ok(Some(file));
                }
                Ok(crate::registry::Taken::Absent(_)) => {}
                // An upstream that failed or sent something else is passed
                // over, and is not taken as having said the file is absent.
                Err(_) => {
                    every_upstream_said_no = false;
                    locked(&self.failed)[i] = Some(Instant::now());
                }
            }
        }
        if every_upstream_said_no {
            let mut absent = locked(&self.absent);
            if absent.len() >= ABSENT_KEPT {
                absent.pop_front();
            }
            absent.push_back((digest.to_string(), Instant::now()));
        }
        Ok(None)
    }
}

/// The Studio page: three files compiled into the binary. The page loads
/// nothing else and calls only `/v1/` on the origin it came from.
const STUDIO: [(&str, &str, &str); 3] = [
    ("/studio", "text/html; charset=utf-8", include_str!("../studio/index.html")),
    ("/studio/studio.css", "text/css; charset=utf-8", include_str!("../studio/studio.css")),
    ("/studio/studio.js", "text/javascript; charset=utf-8", include_str!("../studio/studio.js")),
];

/// What the page may do, enforced by the browser: scripts and styles from
/// this origin only, requests to this origin only, no frames, no forms
/// posting elsewhere, nothing else at all.
const STUDIO_POLICY: &str = "default-src 'none'; script-src 'self'; style-src 'self'; connect-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'";

impl Default for Options {
    fn default() -> Self {
        Self { workers: 4, queue: 16, timeout: Duration::from_secs(10), max_body_bytes: 3 << 20, any_host: false, studio: false, registry: false, operations: true, federation: None, peer_status: None }
    }
}

struct Reply {
    status: u16,
    reason: &'static str,
    body: Value,
    allow: Option<&'static str>,
    /// A file of the Studio page, as (content type, text), sent instead of `body`.
    page: Option<(&'static str, &'static str)>,
    /// A vocabulary file, sent instead of `body`.
    file: Option<Box<[u8]>>,
}

fn problem(status: u16, reason: &'static str, code: &str, message: impl Into<String>) -> Reply {
    Reply { status, reason, body: json!({ "error": { "code": code, "message": message.into() } }), allow: None, page: None, file: None }
}

fn ok(body: Value) -> Reply {
    Reply { status: 200, reason: "OK", body, allow: None, page: None, file: None }
}

struct Request {
    method: String,
    path: String,
    host: Option<String>,
    content_type: Option<String>,
    body: Vec<u8>,
    /// Another registry is asking on a caller's behalf.
    via: bool,
}

fn loopback_name(host: &str) -> bool {
    let name = if let Some(rest) = host.strip_prefix('[') {
        // [::1] or [::1]:port, and nothing else after the bracket
        match rest.split_once(']') {
            Some((name, after)) if after.is_empty() || after.strip_prefix(':').is_some_and(|port| port.chars().all(|c| c.is_ascii_digit())) => name,
            _ => return false,
        }
    } else {
        host.rsplit_once(':').map_or(host, |(name, port)| if port.chars().all(|c| c.is_ascii_digit()) { name } else { host })
    };
    name.eq_ignore_ascii_case("localhost") || name.parse::<std::net::IpAddr>().is_ok_and(|ip| ip.is_loopback())
}

/// Read one request within the deadline and the size limits.
fn read_request(stream: &mut TcpStream, options: &Options) -> Result<Request, Reply> {
    let deadline = Instant::now() + options.timeout;
    let late = || problem(408, "Request Timeout", "timeout", "the request did not arrive in time");
    let mut buffer: Vec<u8> = Vec::with_capacity(1024);
    let mut chunk = [0u8; 4096];
    let head_end = loop {
        if let Some(at) = buffer.windows(4).position(|w| w == b"\r\n\r\n") {
            break at;
        }
        if buffer.len() > MAX_HEAD_BYTES {
            return Err(problem(431, "Request Header Fields Too Large", "headers_too_large", format!("the request line and headers may take {MAX_HEAD_BYTES} bytes")));
        }
        let left = deadline.checked_duration_since(Instant::now()).filter(|d| !d.is_zero()).ok_or_else(late)?;
        stream.set_read_timeout(Some(left)).map_err(|_| late())?;
        match stream.read(&mut chunk) {
            Ok(0) => return Err(problem(400, "Bad Request", "bad_request", "the connection closed before the request was complete")),
            Ok(n) => buffer.extend_from_slice(&chunk[..n]),
            Err(_) => return Err(late()),
        }
    };
    if head_end > MAX_HEAD_BYTES {
        return Err(problem(431, "Request Header Fields Too Large", "headers_too_large", format!("the request line and headers may take {MAX_HEAD_BYTES} bytes")));
    }
    let bad = |what: &str| problem(400, "Bad Request", "bad_request", what.to_string());
    let head = std::str::from_utf8(&buffer[..head_end]).map_err(|_| bad("the request line and headers are not text"))?;
    let mut lines = head.split("\r\n");
    let mut first = lines.next().unwrap_or("").split(' ');
    let (method, target, version) = (first.next().unwrap_or(""), first.next().unwrap_or(""), first.next().unwrap_or(""));
    if method.is_empty() || !target.starts_with('/') || !matches!(version, "HTTP/1.0" | "HTTP/1.1") || first.next().is_some() {
        return Err(bad("the request line is not HTTP/1.x"));
    }
    let (mut host, mut content_type, mut length, mut chunked, mut via) = (None, None, None, false, false);
    for line in lines {
        let (name, value) = line.split_once(':').ok_or_else(|| bad("a header line has no colon"))?;
        // A name is a token: no space before the colon, no continuation
        // line. Something in front of this service that read such a line
        // another way would disagree with it about where the request ends.
        if name.is_empty() || !name.bytes().all(|b| b.is_ascii_alphanumeric() || b"!#$%&'*+-.^_`|~".contains(&b)) {
            return Err(bad("a header name is not a token"));
        }
        let value = value.trim_matches([' ', '\t']);
        if value.bytes().any(|b| (b < 0x20 && b != b'\t') || b == 0x7f) {
            return Err(bad("a header value holds a control character"));
        }
        match name.to_ascii_lowercase().as_str() {
            "host" => {
                if host.is_some() {
                    return Err(bad("two Host headers"));
                }
                host = Some(value.to_string());
            }
            "content-type" => content_type = Some(value.to_ascii_lowercase()),
            "content-length" => {
                // Digits only: `parse` alone would take "+5".
                let n: u64 = Some(value).filter(|v| !v.is_empty() && v.bytes().all(|b| b.is_ascii_digit())).and_then(|v| v.parse().ok()).ok_or_else(|| bad("Content-Length is not a number"))?;
                if length.is_some_and(|earlier| earlier != n) {
                    return Err(bad("two different Content-Length headers"));
                }
                length = Some(n);
            }
            "transfer-encoding" => chunked = true,
            "kute-registry-via" => via = true,
            _ => {}
        }
    }
    if chunked {
        return Err(problem(411, "Length Required", "length_required", "send the body with Content-Length, not a transfer encoding"));
    }
    let path = target.split('?').next().unwrap_or("").to_string();
    let mut body = buffer[head_end + 4..].to_vec();
    let length = match (method, length) {
        (_, Some(n)) => n,
        ("POST", None) => return Err(problem(411, "Length Required", "length_required", "a POST needs Content-Length")),
        (_, None) => 0,
    };
    if length > options.max_body_bytes {
        return Err(problem(413, "Content Too Large", "too_large", format!("the body is {length} bytes; the limit is {}", options.max_body_bytes)));
    }
    let length = length as usize;
    if body.len() > length {
        return Err(bad("more bytes than Content-Length; one request per connection"));
    }
    while body.len() < length {
        let left = deadline.checked_duration_since(Instant::now()).filter(|d| !d.is_zero()).ok_or_else(late)?;
        stream.set_read_timeout(Some(left)).map_err(|_| late())?;
        let want = chunk.len().min(length - body.len());
        match stream.read(&mut chunk[..want]) {
            Ok(0) => return Err(bad("the connection closed before the body was complete")),
            Ok(n) => body.extend_from_slice(&chunk[..n]),
            Err(_) => return Err(late()),
        }
    }
    Ok(Request { method: method.to_string(), path, host, content_type, body, via })
}

fn route(server: &Server, request: &Request, options: &Options) -> Reply {
    if !options.any_host && !request.host.as_deref().is_some_and(loopback_name) {
        return problem(403, "Forbidden", "forbidden_host", "this service answers only requests addressed to localhost");
    }
    if let Some((_, content_type, text)) = STUDIO.iter().find(|(path, _, _)| options.studio && *path == request.path) {
        if request.method != "GET" {
            return Reply { allow: Some("GET"), ..problem(405, "Method Not Allowed", "method_not_allowed", "the page is read with GET") };
        }
        return Reply { page: Some((content_type, text)), ..ok(Value::Null) };
    }
    let operation = match request.path.strip_prefix("/v1/") {
        Some(rest) => rest,
        None => return problem(404, "Not Found", "not_found", "the interface is under /v1/"),
    };
    if operation == "capabilities" {
        if request.method != "GET" {
            return Reply { allow: Some("GET"), ..problem(405, "Method Not Allowed", "method_not_allowed", "capabilities is read with GET") };
        }
        let mut held = match server.call("inspect", &json!({})) {
            Ok(v) => v,
            Err(message) => return problem(500, "Internal Server Error", "internal", message),
        };
        held["api"] = json!("v1");
        held["software"] = json!(format!("kute {}", kute::KUTE_VERSION));
        held["operations"] = if options.operations { json!(OPERATIONS) } else { json!([]) };
        held["registry"] = json!(options.registry);
        held["peer_status"] = json!(options.peer_status.is_some());
        held["limits"]["max_body_bytes"] = json!(options.max_body_bytes);
        return ok(held);
    }
    if operation == "peer" {
        let Some(path) = &options.peer_status else {
            return problem(404, "Not Found", "not_found", "this service was not told of a peer: start it with --peer-status FILE");
        };
        if request.method != "GET" {
            return Reply { allow: Some("GET"), ..problem(405, "Method Not Allowed", "method_not_allowed", "the peer's status is read with GET; nothing can be sent to the peer from here") };
        }
        return ok(crate::peer_status::read(path));
    }
    if operation == "vocabularies" || operation.starts_with("vocabularies/") {
        // Without the option the paths do not exist, and the answer does
        // not say what is held.
        if !options.registry {
            return problem(404, "Not Found", "not_found", "this service does not give vocabulary files");
        }
        if request.method != "GET" {
            return Reply { allow: Some("GET"), ..problem(405, "Method Not Allowed", "method_not_allowed", "vocabularies are read with GET; nothing can be sent to this registry") };
        }
        let Some(digest) = operation.strip_prefix("vocabularies/") else {
            let mut listing = server.registry_list();
            // Who else this registry asks. What they hold is theirs to say.
            listing["upstreams"] = json!(options.federation.as_ref().map(|f| f.upstreams.clone()).unwrap_or_default());
            listing["upstream_files_need_a_trusted_signature"] = json!(options.federation.as_ref().is_some_and(|f| f.check.is_some()));
            return ok(listing);
        };
        // Only a digest reaches the lookup: a file is named by the hash
        // of its content and by nothing else.
        if !crate::registry::is_digest(digest) {
            return problem(404, "Not Found", "not_found", "a vocabulary is asked for by its digest: sha256: and 64 lowercase hex digits");
        }
        return match server.registry_file(digest) {
            Some(Ok(file)) => Reply { file: Some(file.into_boxed_slice()), ..ok(Value::Null) },
            Some(Err(_)) => problem(500, "Internal Server Error", "internal", "the vocabulary could not be written out"),
            None => {
                // Not held here. A registry asking for another is answered
                // from what is here and kept, and no further.
                let found = match &options.federation {
                    Some(federation) => federation.find(server.engine(), digest, !request.via),
                    None => Ok(None),
                };
                match found {
                    Ok(Some(file)) => Reply { file: Some(file.to_vec().into_boxed_slice()), ..ok(Value::Null) },
                    Ok(None) => problem(404, "Not Found", "not_found", "no vocabulary with that digest is held here"),
                    Err(reply) => reply,
                }
            }
        };
    }
    if !OPERATIONS.contains(&operation) || !options.operations {
        return problem(404, "Not Found", "not_found", format!("there is no operation {operation:?}"));
    }
    if request.method != "POST" {
        return Reply { allow: Some("POST"), ..problem(405, "Method Not Allowed", "method_not_allowed", "operations are called with POST") };
    }
    // A JSON content type cannot be sent across origins by a web page
    // without the browser asking first, and this service never agrees.
    if !request.content_type.as_deref().is_some_and(|t| t == "application/json" || t.starts_with("application/json;")) {
        return problem(415, "Unsupported Media Type", "unsupported_media_type", "send Content-Type: application/json");
    }
    let arguments: Value = match serde_json::from_slice(&request.body) {
        Ok(v) => v,
        Err(e) => return problem(400, "Bad Request", "bad_json", format!("the body is not JSON: {e}")),
    };
    // A panic in an operation must not end the worker: with every worker
    // gone the service would accept connections and answer none. No input
    // is known to cause one.
    match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| server.call(operation, &arguments))) {
        Ok(Ok(result)) => ok(result),
        Ok(Err(message)) => problem(422, "Unprocessable Content", "refused", message),
        Err(_) => problem(500, "Internal Server Error", "internal", "the operation failed unexpectedly"),
    }
}

fn answer(stream: &mut TcpStream, reply: &Reply, options: &Options) {
    let (content_type, body) = match (reply.page, &reply.file) {
        (Some((content_type, text)), _) => (content_type, text.as_bytes().to_vec()),
        (None, Some(file)) => ("application/json", file.to_vec()),
        (None, None) => ("application/json", serde_json::to_vec(&reply.body).unwrap_or_else(|_| b"{}".to_vec())),
    };
    let mut head = format!(
        "HTTP/1.1 {} {}\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\nConnection: close\r\nCache-Control: no-store\r\nX-Content-Type-Options: nosniff\r\n",
        reply.status,
        reply.reason,
        body.len()
    );
    if reply.status == 503 {
        head.push_str("Retry-After: 1\r\n");
    }
    if reply.page.is_some() {
        head.push_str(&format!("Content-Security-Policy: {STUDIO_POLICY}\r\nReferrer-Policy: no-referrer\r\nX-Frame-Options: DENY\r\n"));
    }
    if let Some(allow) = reply.allow {
        head.push_str(&format!("Allow: {allow}\r\n"));
    }
    head.push_str("\r\n");
    let _ = stream.set_write_timeout(Some(options.timeout));
    if stream.write_all(head.as_bytes()).is_ok() && stream.write_all(&body).is_ok() {
        let _ = stream.flush();
    }
    let _ = stream.shutdown(std::net::Shutdown::Write);
    // A request refused before it was read to its end leaves bytes
    // unread, and closing over unread bytes resets the connection on
    // some systems (seen on macOS), which can take the answer with it.
    // So read what is left and throw it away: at most 1 MiB and half a
    // second, then close whatever the client does. The answer is safe
    // only if all of what was sent is read, so the limit is the largest
    // unread request an answer is guaranteed to survive.
    let started = Instant::now();
    let _ = stream.set_read_timeout(Some(Duration::from_millis(50)));
    let (mut scrap, mut discarded) = ([0u8; 4096], 0usize);
    while discarded < 1_048_576 && started.elapsed() < Duration::from_millis(500) {
        match stream.read(&mut scrap) {
            Ok(0) | Err(_) => break,
            Ok(n) => discarded += n,
        }
    }
}

fn serve_one(server: &Server, mut stream: TcpStream, options: &Options) {
    let reply = match read_request(&mut stream, options) {
        Ok(request) => route(server, &request, options),
        Err(reply) => reply,
    };
    answer(&mut stream, &reply, options);
}

fn work(server: Arc<Server>, jobs: Arc<Mutex<Receiver<TcpStream>>>, options: Options) {
    loop {
        let next = jobs.lock().map(|queue| queue.recv());
        match next {
            Ok(Ok(stream)) => serve_one(&server, stream, &options),
            _ => return,
        }
    }
}

/// Serve until the listener fails. Each connection carries one request.
pub fn serve(server: Server, listener: TcpListener, options: Options) -> kute::Result<()> {
    let server = Arc::new(server);
    let (send, receive) = sync_channel::<TcpStream>(options.queue.max(1));
    let receive = Arc::new(Mutex::new(receive));
    for _ in 0..options.workers.max(1) {
        let (server, receive, options) = (server.clone(), receive.clone(), options.clone());
        std::thread::spawn(move || work(server, receive, options));
    }
    for stream in listener.incoming() {
        let stream = match stream {
            Ok(s) => s,
            Err(_) => continue,
        };
        match send.try_send(stream) {
            Ok(()) => {}
            Err(TrySendError::Full(mut stream)) => {
                // Answered here, without reading: reading is what a worker is for.
                let busy = Reply { ..problem(503, "Service Unavailable", "busy", "every worker is busy and the queue is full; try again") };
                let brief = Options { timeout: Duration::from_secs(1), ..options.clone() };
                answer(&mut stream, &busy, &brief);
            }
            Err(TrySendError::Disconnected(_)) => return Err(kute::KuteError::Backend("the workers stopped".into())),
        }
    }
    Ok(())
}

/// Whether an address is on the loopback interface.
pub fn is_loopback(address: &SocketAddr) -> bool {
    address.ip().is_loopback()
}