datumesh-http 0.4.3

The Datumesh engine behind HTTPS: cell envelopes, batches, slices (JSON and Arrow), receipts, jobs, verification and cross-mesh resolution; the datumesh-mesh server.
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
//! The store-less client of other meshes (for `datumesh-local` and for this mesh's cross-mesh
//! reads): fetch a host's key document and mesh document, its bindings, a cell's envelope (by
//! call, URI or pin), slice pages, batches, receipts, descriptors, manifests, blobs and
//! versions, and **check** each with the core: the envelope against the request
//! (`check_answer`, `check_slice_answer`, `check_batch`), every receipt's signature against
//! its signer's key document (fetched from the signer's own host, never from the mesh that
//! served it), and content-addressed bytes against their hash. Nothing is stored; the import
//! into a store is [`crate::foreign::Foreign`]'s, a layer on top.
//!
//! **Blocking.** The client is synchronous (ureq, rustls with the Mozilla roots, no redirects,
//! a time limit per request, bodies bounded): this mesh's engine reads foreign cells from
//! inside an evaluation, which runs on a blocking thread, and `datumesh-local`'s CLI is
//! synchronous too. An async caller runs it on `tokio::task::spawn_blocking`, as the mesh's
//! own handlers do with the engine. [`Client`] is cheap to clone and shares its caches.
//!
//! **Caches.** Key documents (default 300 s; a key id the cached document lacks refetches it,
//! at most every 10 s) and mesh documents (300 s), in memory. Nothing else is cached here.
//!
//! **Allowlist.** With [`ClientConfig::allowed`] set, only those hosts are fetched from
//! (anything else is refused with `E_INPUT_UNAVAILABLE` before any request).

use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use datumesh_core::json::{self, Value};
use datumesh_core::{
    check_answer, check_batch, check_slice_answer, format_reference, keys_from_response, lexical, parse_reference,
    resolve_mesh, Answer, Batch, ErrorCode, ForeignDetail, FunctionDescriptor, KeyDoc, Keys, Manifest, MeshDoc,
    Receipt, Reference, Request, Retrieval, Slice, SliceRequest,
};

use crate::foreign::KeyCache;

/// The largest body read from another mesh.
const MAX_BODY: u64 = 64 << 20;
/// The largest key or mesh document read (SPEC 9.3's suggested bound).
const MAX_DOC: u64 = 256 << 10;

/// A failure of the client: the code of the `error` envelope a mesh would give for it, and why.
#[derive(Clone, Debug)]
pub struct ClientError {
    /// `E_INPUT_UNAVAILABLE` (the other mesh is down, answered otherwise, or is not allowed),
    /// `E_MESH_DOC`, `E_KEY_UNAVAILABLE` or `E_KEY_DOC` (resolving its host), `E_ENVELOPE` (an
    /// answer that fails the check), or what checking a receipt gave.
    pub code: ErrorCode,
    /// Why.
    pub detail: String,
}

impl ClientError {
    pub(crate) fn new(code: ErrorCode, detail: impl Into<String>) -> ClientError {
        ClientError { code, detail: detail.into() }
    }
    pub(crate) fn unavailable(detail: impl Into<String>) -> ClientError {
        ClientError::new(ErrorCode::InputUnavailable, detail)
    }

    /// `E_FOREIGN_UNAVAILABLE` (spec-0.4.3): no HTTP response came back from the other mesh.
    /// `detail` starts with the envelope's `detail` name, then `": "` and why.
    pub(crate) fn foreign(detail: ForeignDetail, why: impl std::fmt::Display) -> ClientError {
        ClientError::new(ErrorCode::ForeignUnavailable, format!("{}: {why}", detail.as_str()))
    }

    /// For `E_FOREIGN_UNAVAILABLE` (spec-0.4.3), the envelope's `detail` (`offline`, `network`,
    /// `dns`, `tls`, `timeout`, `refused`): the start of [`ClientError::detail`]. `None` for
    /// every other code. Since 0.2.1.
    pub fn foreign_detail(&self) -> Option<ForeignDetail> {
        if self.code != ErrorCode::ForeignUnavailable {
            return None;
        }
        ForeignDetail::from_name(self.detail.split(':').next().unwrap_or(""))
    }
}

/// Why no HTTP response came back (spec-0.4.3's `detail` of `E_FOREIGN_UNAVAILABLE`), when the
/// failure is one of those; `None` for a failure after a response began, or of another kind.
fn transport_detail(e: &ureq::Error) -> Option<ForeignDetail> {
    use std::io::ErrorKind;
    Some(match e {
        ureq::Error::HostNotFound => ForeignDetail::Dns,
        ureq::Error::Timeout(_) => ForeignDetail::Timeout,
        // The system resolver's failure arrives as an I/O error, not `HostNotFound`.
        ureq::Error::Io(io) if io.to_string().contains("failed to lookup address information") => ForeignDetail::Dns,
        // Windows' resolver (CONTRACT-1.8b H3): WSAHOST_NOT_FOUND (11001) to WSANO_DATA (11004),
        // or its text.
        ureq::Error::Io(io)
            if io.raw_os_error().is_some_and(|c| (11001..=11004).contains(&c))
                || io.to_string().to_ascii_lowercase().contains("no such host is known") =>
        {
            ForeignDetail::Dns
        }
        ureq::Error::Io(io) => match io.kind() {
            ErrorKind::ConnectionRefused => ForeignDetail::Refused,
            ErrorKind::TimedOut => ForeignDetail::Timeout,
            _ => ForeignDetail::Network,
        },
        ureq::Error::ConnectionFailed => ForeignDetail::Network,
        ureq::Error::Tls(_) | ureq::Error::Rustls(_) | ureq::Error::Pem(_) | ureq::Error::TlsRequired => {
            ForeignDetail::Tls
        }
        _ => return None,
    })
}

impl std::fmt::Display for ClientError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {}", self.code.as_str(), self.detail)
    }
}

impl std::error::Error for ClientError {}

/// The client's configuration.
#[non_exhaustive]
#[derive(Clone, Debug)]
pub struct ClientConfig {
    /// Origins to use instead of `https://<host>` for some hosts (tests, private networks).
    pub peers: BTreeMap<String, String>,
    /// When set, the only hosts fetched from.
    pub allowed: Option<Vec<String>>,
    /// The time limit of one request (default 10 s).
    pub timeout: Duration,
    /// Key documents' lifetime in the cache (default 300 s).
    pub key_ttl: Duration,
    /// The shortest interval between two fetches of one host's key document (default 10 s).
    pub key_refetch: Duration,
    /// Mesh documents' lifetime in the cache (default 300 s).
    pub mesh_ttl: Duration,
}

impl Default for ClientConfig {
    fn default() -> Self {
        ClientConfig {
            peers: BTreeMap::new(),
            allowed: None,
            timeout: Duration::from_secs(10),
            key_ttl: Duration::from_secs(300),
            key_refetch: Duration::from_secs(10),
            mesh_ttl: Duration::from_secs(300),
        }
    }
}

/// A binding another mesh states (SPEC 9.7).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RemoteBinding {
    /// The function hash.
    pub fn_hash: String,
    /// When the binding began.
    pub since: String,
}

/// An answer fetched and checked: its HTTP status, its body as received, and the checked answer.
#[derive(Clone, Debug)]
pub struct Fetched {
    /// The HTTP status.
    pub status: u16,
    /// The body.
    pub body: Vec<u8>,
    /// The answer, checked against the request.
    pub answer: Answer,
}

/// One version a mesh lists (SPEC 9.8).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct VersionEntry {
    /// The version's `tx`.
    pub tx: String,
    /// Its receipt's `asof`.
    pub asof: String,
    /// Its receipt hash.
    pub receipt: String,
}

/// A page of the versions endpoint (SPEC 9.8).
#[derive(Clone, Debug)]
pub struct Versions {
    /// The cell (4.4).
    pub cell: Value,
    /// The versions, ascending by `tx`.
    pub versions: Vec<VersionEntry>,
    /// The following page's URL.
    pub next: Option<String>,
}

struct Inner {
    agent: ureq::Agent,
    cfg: ClientConfig,
    keys: Arc<KeyCache>,
    meshes: Mutex<HashMap<String, (MeshDoc, Instant)>>,
}

/// The client (see the module documentation).
#[derive(Clone)]
pub struct Client {
    inner: Arc<Inner>,
}

impl std::fmt::Debug for Client {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Client").field("allowed", &self.inner.cfg.allowed).finish_non_exhaustive()
    }
}

/// Percent-encodes a value as SPEC 5.1 says (literal characters as themselves).
pub fn pct(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for b in s.bytes() {
        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~' | b':') {
            out.push(b as char);
        } else {
            out.push_str(&format!("%{b:02X}"));
        }
    }
    out
}

fn host_of(name: &str) -> &str {
    name.split('/').next().unwrap_or(name)
}

impl Client {
    /// A client with this configuration.
    pub fn new(cfg: ClientConfig) -> Client {
        Client::with_keys(cfg, Arc::new(KeyCache::default()))
    }

    /// A client sharing a key cache (the mesh's, so `POST /verify` sees what it fetched).
    pub fn with_keys(cfg: ClientConfig, keys: Arc<KeyCache>) -> Client {
        let agent: ureq::Agent = ureq::Agent::config_builder()
            .max_redirects(0)
            .http_status_as_error(false)
            .timeout_global(Some(cfg.timeout))
            .user_agent(concat!("datumesh-http/", env!("CARGO_PKG_VERSION")))
            .build()
            .into();
        Client { inner: Arc::new(Inner { agent, cfg, keys, meshes: Mutex::new(HashMap::new()) }) }
    }

    /// The key cache.
    pub fn keys(&self) -> &Arc<KeyCache> {
        &self.inner.keys
    }

    /// Whether `host` may be fetched from.
    pub fn allowed(&self, host: &str) -> bool {
        self.inner.cfg.allowed.as_ref().is_none_or(|a| a.iter().any(|h| h == host))
    }

    fn check_host(&self, host: &str) -> Result<(), ClientError> {
        if lexical::check_dns_name(host, "$").is_err() {
            return Err(ClientError::new(ErrorCode::KeyUnavailable, format!("{host:?} is not a DNS name")));
        }
        if !self.allowed(host) {
            return Err(ClientError::unavailable(format!("{host} is not an allowed host")));
        }
        Ok(())
    }

    /// `GET url` (an `https://<host>/...` URL; the origin replaced for a configured peer):
    /// status and body, at most `limit` bytes.
    pub fn get(&self, url: &str, limit: u64) -> Result<(u16, Vec<u8>), String> {
        self.request(url, None, limit)
    }

    fn request(&self, url: &str, post: Option<&[u8]>, limit: u64) -> Result<(u16, Vec<u8>), String> {
        self.request_with_detail(url, post, limit).map_err(|(m, _)| m)
    }

    /// A request to another mesh whose failure is a [`ClientError`]: `E_FOREIGN_UNAVAILABLE`
    /// (spec-0.4.3) with its detail (`offline`, `network`, `dns`, `tls`, `timeout`, `refused`)
    /// when no HTTP response came back, `E_INPUT_UNAVAILABLE` for a failure after a response
    /// began (a body that could not be read, a host not allowed). A mesh that answered, with
    /// any status, is not this function's failure. Since the cross-mesh failure change.
    fn fetch(&self, url: &str, post: Option<&[u8]>, limit: u64) -> Result<(u16, Vec<u8>), ClientError> {
        self.request_with_detail(url, post, limit).map_err(|(m, d)| match d {
            Some(d) => ClientError::foreign(d, m),
            None => ClientError::unavailable(m),
        })
    }

    /// [`request`](Client::request), its failure with the `E_FOREIGN_UNAVAILABLE` detail when no
    /// HTTP response came back.
    fn request_with_detail(
        &self,
        url: &str,
        post: Option<&[u8]>,
        limit: u64,
    ) -> Result<(u16, Vec<u8>), (String, Option<ForeignDetail>)> {
        let mut target = url.to_string();
        if let Some(rest) = url.strip_prefix("https://") {
            let host = rest.split(['/', '?']).next().unwrap_or("");
            if !self.allowed(host) {
                return Err((format!("{host} is not an allowed host"), None));
            }
            if let Some(origin) = self.inner.cfg.peers.get(host) {
                target = format!("{origin}{}", &rest[host.len()..]);
            }
        }
        let resp = match post {
            None => self.inner.agent.get(&target).call(),
            Some(b) => self.inner.agent.post(&target).header("content-type", "application/json").send(b),
        };
        let mut resp = resp.map_err(|e| (format!("{url}: {e}"), transport_detail(&e)))?;
        let status = resp.status().as_u16();
        let body =
            resp.body_mut().with_config().limit(limit).read_to_vec().map_err(|e| (format!("{url}: {e}"), None))?;
        Ok((status, body))
    }

    /// The key document of `host` (SPEC 9.3), from the cache while it is fresh, else fetched;
    /// `refresh` fetches it again unless it was fetched within the refetch interval.
    pub fn key_document(&self, host: &str, refresh: bool) -> Result<KeyDoc, ClientError> {
        self.key_document_with(host, refresh)
    }

    /// [`key_document`](Client::key_document) of a function's publisher, before this mesh
    /// computes the function (its declarations, SPEC 8.5): a fetch that got no HTTP response
    /// is `E_FOREIGN_UNAVAILABLE` (spec-0.4.3), its detail first in
    /// [`ClientError::detail`] ([`ClientError::foreign_detail`]). Since 0.2.1.
    pub fn publisher_key_document(&self, host: &str) -> Result<KeyDoc, ClientError> {
        self.key_document_with(host, false)
    }

    fn key_document_with(&self, host: &str, refresh: bool) -> Result<KeyDoc, ClientError> {
        if let Some((doc, _, at)) = self.inner.keys.get(host) {
            let age = at.elapsed();
            if (!refresh && age < self.inner.cfg.key_ttl) || (refresh && age < self.inner.cfg.key_refetch) {
                return Ok(doc);
            }
        }
        self.check_host(host)?;
        let url = format!("https://{host}/.well-known/datumesh.json");
        let (status, body) = self.request_with_detail(&url, None, MAX_DOC).map_err(|(e, d)| match d {
            Some(d) => ClientError::foreign(d, e),
            None => ClientError::new(ErrorCode::KeyUnavailable, e),
        })?;
        let doc = keys_from_response(host, status, if status == 200 { Some(&body) } else { None })
            .map_err(|e| ClientError::new(e.code(), e.to_string()))?;
        self.inner.keys.insert(host, &body).map_err(|e| ClientError::new(e.code(), e.to_string()))?;
        Ok(doc)
    }

    /// The bytes of `host`'s key document, fetched now (not from the cache) and given at most
    /// `within` (the domain proof of `POST /bundles`, Build 2 Part 4, P1: 5 s). Nothing is cached.
    /// A failure to get any HTTP answer, or a non-200 answer, is `E_KEY_UNAVAILABLE`.
    pub fn key_document_live(&self, host: &str, within: Duration) -> Result<Vec<u8>, ClientError> {
        self.check_host(host)?;
        let url = format!("https://{host}/.well-known/datumesh.json");
        let mut target = url.clone();
        if let Some(origin) = self.inner.cfg.peers.get(host) {
            target = format!("{origin}/.well-known/datumesh.json");
        }
        let resp = self
            .inner
            .agent
            .get(&target)
            .config()
            .timeout_global(Some(within))
            .build()
            .call()
            .map_err(|e| ClientError::new(ErrorCode::KeyUnavailable, format!("{url}: {e}")))?;
        let mut resp = resp;
        if resp.status().as_u16() != 200 {
            return Err(ClientError::new(
                ErrorCode::KeyUnavailable,
                format!("{url}: status {}", resp.status().as_u16()),
            ));
        }
        resp.body_mut()
            .with_config()
            .limit(MAX_DOC)
            .read_to_vec()
            .map_err(|e| ClientError::new(ErrorCode::KeyUnavailable, format!("{url}: {e}")))
    }

    /// The bytes of `host`'s key document as last fetched.
    pub fn key_document_bytes(&self, host: &str) -> Option<Vec<u8>> {
        self.inner.keys.get(host).map(|(_, b, _)| b)
    }

    /// The mesh document of `host`, resolved with its key document (SPEC 9.6), cached.
    pub fn resolve(&self, host: &str) -> Result<MeshDoc, ClientError> {
        if let Some((m, at)) = self.inner.meshes.lock().unwrap_or_else(|p| p.into_inner()).get(host) {
            if at.elapsed() < self.inner.cfg.mesh_ttl {
                return Ok(m.clone());
            }
        }
        self.check_host(host)?;
        type Got = Result<(u16, Vec<u8>), (String, Option<ForeignDetail>)>;
        let get = |path: &str| -> Got {
            let url = format!("https://{host}/.well-known/{path}");
            self.request_with_detail(&url, None, MAX_DOC)
        };
        let keys: Got = get("datumesh.json");
        let mesh: Got = match &keys {
            Ok((200, _)) => get("datumesh-mesh.json"),
            _ => Err((String::new(), None)),
        };
        fn ret(r: &Got) -> Retrieval<'_> {
            match r {
                Ok((s, b)) => Retrieval::Response { status: *s, body: Some(b.as_slice()) },
                Err(_) => Retrieval::Failed,
            }
        }
        let resolved = resolve_mesh(host, ret(&keys), ret(&mesh)).map_err(|e| {
            // No response from the host: E_FOREIGN_UNAVAILABLE with why.
            for r in [&keys, &mesh] {
                if let Err((why, Some(d))) = r {
                    return ClientError::foreign(*d, why);
                }
            }
            let detail = match (&keys, &mesh) {
                (Err((e, _)), _) | (_, Err((e, _))) if !e.is_empty() => e.clone(),
                _ => e.to_string(),
            };
            ClientError::new(e.code(), detail)
        })?;
        if let Ok((_, body)) = &keys {
            let _ = self.inner.keys.insert(host, body);
        }
        let m = resolved.mesh;
        self.inner
            .meshes
            .lock()
            .unwrap_or_else(|p| p.into_inner())
            .insert(host.to_string(), (m.clone(), Instant::now()));
        Ok(m)
    }

    fn endpoint(&self, host: &str, name: &str, values: &[(&str, &str)]) -> Result<String, ClientError> {
        self.resolve(host)?
            .url(name, values)
            .ok_or_else(|| ClientError::new(ErrorCode::MeshDoc, format!("{host} has no {name} endpoint")))
    }

    /// The binding of `name` that `host`'s bindings resource states as of `asof` (now when
    /// `None`), SPEC 9.7.
    pub fn binding(&self, host: &str, name: &str, asof: Option<&str>) -> Result<Option<RemoteBinding>, ClientError> {
        let mesh = self.resolve(host)?;
        let mut url = format!("{}?name={}", mesh.bindings, pct(name));
        if let Some(a) = asof {
            url.push_str(&format!("&asof={}", pct(a)));
        }
        let (status, body) = self.fetch(&url, None, MAX_DOC)?;
        if status != 200 {
            return Err(ClientError::unavailable(format!("{url}: status {status}")));
        }
        let v = json::parse(&body).map_err(|e| ClientError::unavailable(format!("{url}: {e}")))?;
        let Some(b) = v.get("bindings").and_then(|b| b.get(name)) else {
            if v.get("bindings").and_then(Value::as_object).is_none() {
                return Err(ClientError::unavailable(format!("{url}: no bindings object")));
            }
            return Ok(None);
        };
        let h = b.get("fn_hash").and_then(Value::as_str).unwrap_or("");
        let since = b.get("since").and_then(Value::as_str).unwrap_or("");
        if lexical::check_hex(h, 64, "$").is_err() || lexical::check_instant(since, "$").is_err() {
            return Err(ClientError::unavailable(format!("{url}: malformed binding")));
        }
        if asof.is_some_and(|a| since > a) {
            return Err(ClientError::unavailable(format!("{url}: a binding since after the asof")));
        }
        Ok(Some(RemoteBinding { fn_hash: h.to_string(), since: since.to_string() }))
    }

    /// The descriptor `fn_hash` names, from `host` (its hash checked).
    pub fn descriptor(&self, host: &str, fn_hash: &str) -> Result<Vec<u8>, ClientError> {
        let url = self.endpoint(host, "descriptor", &[("hash", fn_hash)])?;
        let (status, body) = self.fetch(&url, None, MAX_BODY)?;
        if status != 200 {
            return Err(ClientError::new(ErrorCode::FnUnavailable, format!("{url}: status {status}")));
        }
        let d = FunctionDescriptor::from_json(&body).map_err(|e| ClientError::new(e.code(), e.to_string()))?;
        if d.hash() != fn_hash {
            return Err(ClientError::new(ErrorCode::FnHashMismatch, format!("{url} gave the descriptor {}", d.hash())));
        }
        Ok(body)
    }

    /// The keys of the signers named in a body (a receipt, an envelope, a page or a batch):
    /// each signer's key document, from its own host.
    pub fn keys_for_body(&self, body: &[u8]) -> Keys {
        let mut hosts = BTreeSet::new();
        fn walk(v: &Value, hosts: &mut BTreeSet<String>, depth: usize) {
            if depth > 8 {
                return;
            }
            match v {
                Value::Object(m) => {
                    for (k, x) in m {
                        if k == "computed_by" {
                            if let Some(s) = x.as_str() {
                                hosts.insert(s.split('#').next().unwrap_or("").to_string());
                            }
                        } else {
                            walk(x, hosts, depth + 1);
                        }
                    }
                }
                Value::Array(a) => a.iter().for_each(|x| walk(x, hosts, depth + 1)),
                _ => {}
            }
        }
        if let Ok(v) = json::parse(body) {
            walk(&v, &mut hosts, 0);
        }
        let mut keys = Keys::new();
        for h in hosts {
            match self.key_document(&h, false) {
                Ok(d) => keys.insert(d),
                Err(e) => keys.insert_result(&h, Err(datumesh_core::Error::new(e.code, "$", e.detail))),
            }
        }
        keys
    }

    /// Runs `check` with the signers' keys, fetching them again once when a key id is unknown
    /// (a rotation).
    fn checked<T>(&self, body: &[u8], check: impl Fn(&Keys) -> datumesh_core::Result<T>) -> Result<T, ClientError> {
        let keys = self.keys_for_body(body);
        match check(&keys) {
            Err(e) if matches!(e.code(), ErrorCode::KeyUnknown | ErrorCode::KeyNotValid) => {
                let mut hosts = BTreeSet::new();
                if let Ok(v) = json::parse(body) {
                    collect_signers(&v, &mut hosts);
                }
                for h in hosts {
                    let _ = self.key_document(&h, true);
                }
                check(&self.keys_for_body(body)).map_err(|e| ClientError::new(e.code(), e.to_string()))
            }
            r => r.map_err(|e| ClientError::new(e.code(), e.to_string())),
        }
    }

    /// A receipt by its hash from `host`: its hash checked and its signature verified (SPEC
    /// 10.1 steps 1-6) against its signer's key document.
    pub fn receipt(&self, host: &str, hash: &str) -> Result<Vec<u8>, ClientError> {
        let url = self.endpoint(host, "receipt", &[("hash", hash)])?;
        let (status, body) = self.fetch(&url, None, MAX_BODY)?;
        if status != 200 {
            return Err(ClientError::unavailable(format!("{url}: status {status}")));
        }
        let h = self.checked(&body, |keys| {
            let r = datumesh_core::verify(&body, keys, None, None);
            match r.error() {
                Some(e) => Err(e),
                None => Ok(r.hash.unwrap_or_default()),
            }
        })?;
        if h != hash {
            return Err(ClientError::unavailable(format!("{url} gave the receipt {h}")));
        }
        Ok(body)
    }

    /// A slice manifest from `host` (its entry checked).
    pub fn manifest(&self, host: &str, slice_id: &str, members_hash: &str) -> Result<Vec<u8>, ClientError> {
        let url = self.endpoint(host, "manifest", &[("slice_id", slice_id), ("members_hash", members_hash)])?;
        let (status, body) = self.fetch(&url, None, MAX_BODY)?;
        if status != 200 {
            return Err(ClientError::unavailable(format!("{url}: status {status}")));
        }
        let m = Manifest::from_json(&body).map_err(|e| ClientError::new(e.code(), e.to_string()))?;
        if m.entry() != format!("slice:{slice_id}:{members_hash}") {
            return Err(ClientError::unavailable(format!("{url} gave the manifest {}", m.entry())));
        }
        Ok(body)
    }

    /// Blob content by SHA-256 from `host` (its hash checked).
    pub fn blob(&self, host: &str, sha256: &str) -> Result<Vec<u8>, ClientError> {
        let url = self.endpoint(host, "blob", &[("sha256", sha256)])?;
        let (status, body) = self.fetch(&url, None, MAX_BODY)?;
        if status != 200 {
            return Err(ClientError::unavailable(format!("{url}: status {status}")));
        }
        if datumesh_core::sha256_hex(&body) != sha256 {
            return Err(ClientError::new(ErrorCode::BlobMismatch, format!("{url}: content of another hash")));
        }
        Ok(body)
    }

    /// The envelope for `rest` (a cell URI's rest or a call, as the cell endpoint takes it)
    /// from `host`, checked against `request` (5.5; the host asked is added to it).
    pub fn cell(&self, host: &str, rest: &str, request: &Request) -> Result<Fetched, ClientError> {
        let url = self.endpoint(host, "cell", &[("cell", rest)])?;
        let (status, body) = self.fetch(&url, None, MAX_BODY)?;
        let req =
            request.clone().with_mesh(host).map_err(|e| ClientError::new(ErrorCode::Envelope, format!("{e:?}")))?;
        let answer = self.checked(&body, |keys| check_answer(status, &body, keys, &req))?;
        Ok(Fetched { status, body, answer })
    }

    /// The envelope of a reference (pinned or not) from `host`: its URI written by the core,
    /// the request it describes made from it (the cell id needs the function hash: the pin,
    /// else the binding `host` states as of the reference's asof).
    pub fn reference(&self, host: &str, reference: &Reference) -> Result<Fetched, ClientError> {
        let fn_hash = match &reference.fn_hash {
            Some(h) => h.clone(),
            None => {
                let name = format!("{}/{}", reference.ns, reference.fn_name);
                self.binding(host, &name, reference.asof.as_deref())?
                    .ok_or_else(|| ClientError::new(ErrorCode::NameUnbound, name.clone()))?
                    .fn_hash
            }
        };
        let cell = reference.resolve(&fn_hash).map_err(|e| ClientError::new(e.code(), e.to_string()))?;
        let mut request = Request::new(&cell.id(), reference.asof.as_deref())
            .map_err(|e| ClientError::new(ErrorCode::Envelope, format!("{e:?}")))?;
        if let Some(p) = &reference.fn_hash {
            request = request.with_pin(p).map_err(|e| ClientError::new(ErrorCode::Envelope, format!("{e:?}")))?;
        }
        let uri = format_reference(reference);
        self.cell(host, &uri["cell://".len()..], &request)
    }

    /// The slice a slice URI's rest denotes on `host`: the descriptor of its pin, or of the
    /// name's binding (as of its asof), parsed against.
    pub fn slice_of(&self, host: &str, rest: &str) -> Result<(Slice, Reference), ClientError> {
        let uri = format!("cell://{rest}");
        let path = rest.split('?').next().unwrap_or(rest);
        let query = rest.split_once('?').map(|(_, q)| q).unwrap_or("");
        let pin = query.split('&').find_map(|p| p.strip_prefix("fn_hash=")).map(str::to_string);
        let asof = query.split('&').next_back().and_then(|p| p.strip_prefix("asof=")).map(str::to_string);
        let fn_hash = match pin {
            Some(p) => p,
            None => {
                self.binding(host, path, asof.as_deref())?
                    .ok_or_else(|| ClientError::new(ErrorCode::NameUnbound, path.to_string()))?
                    .fn_hash
            }
        };
        let d = FunctionDescriptor::from_json(&self.descriptor(host, &fn_hash)?)
            .map_err(|e| ClientError::new(e.code(), e.to_string()))?;
        let r =
            parse_reference(&uri, &d.signature.to_value()).map_err(|e| ClientError::new(e.code(), e.to_string()))?;
        let cell = r.resolve(&fn_hash).map_err(|e| ClientError::new(e.code(), e.to_string()))?;
        let slice = Slice::from_value(&cell.to_value()).map_err(|e| ClientError::new(e.code(), e.to_string()))?;
        Ok((slice, r))
    }

    /// A page of a slice from `host` (5.6): the first page of `rest` (a slice URI's rest), or,
    /// with `next`, the page that URL names (which must be on `host`), checked against the
    /// slice `rest` denotes and, for a following page, the cursor `after`.
    pub fn slice_page(
        &self,
        host: &str,
        rest: &str,
        next: Option<&str>,
        after: Option<BTreeMap<String, datumesh_core::Scalar>>,
    ) -> Result<Fetched, ClientError> {
        let (slice, r) = self.slice_of(host, rest)?;
        let url = match next {
            Some(n) => {
                if !n.starts_with(&format!("https://{host}/")) {
                    return Err(ClientError::new(ErrorCode::Envelope, format!("{n} is not on {host}")));
                }
                n.to_string()
            }
            None => self.endpoint(host, "slice", &[("cell", rest)])?,
        };
        let (status, body) = self.fetch(&url, None, MAX_BODY)?;
        let mut req = SliceRequest::new(slice.id(), r.asof.as_deref())
            .and_then(|q| q.with_mesh(host))
            .map_err(|e| ClientError::new(ErrorCode::Envelope, format!("{e:?}")))?;
        if let Some(p) = &r.fn_hash {
            req = req.with_pin(p).map_err(|e| ClientError::new(ErrorCode::Envelope, format!("{e:?}")))?;
        }
        if let Some(a) = after {
            req = req.with_after(a);
        }
        let answer = self.checked(&body, |keys| check_slice_answer(status, &body, keys, &req))?;
        Ok(Fetched { status, body, answer })
    }

    /// The first page of a slice (see [`Client::slice_page`]).
    pub fn slice(&self, host: &str, rest: &str, next: Option<&str>) -> Result<Fetched, ClientError> {
        self.slice_page(host, rest, next, None)
    }

    /// A batch (5.7) to `host`: the requests' rests and the request each describes, checked as
    /// a whole and item by item.
    pub fn batch(&self, host: &str, items: &[String], requests: &[Request]) -> Result<Batch, ClientError> {
        let url = self.endpoint(host, "cells", &[])?;
        let body = Value::Array(items.iter().map(|i| Value::String(i.clone())).collect()).to_jcs();
        let (status, resp) = self.fetch(&url, Some(&body), MAX_BODY)?;
        let reqs: Vec<Request> =
            requests.iter().map(|r| r.clone().with_mesh(host).unwrap_or_else(|_| r.clone())).collect();
        self.checked(&resp, |keys| check_batch(status, &resp, keys, &reqs))
    }

    /// The versions of a stored cell from `host` (9.8): `rest` is its URI's rest without asof.
    pub fn versions(&self, host: &str, rest: &str) -> Result<Versions, ClientError> {
        let url = self.endpoint(host, "versions", &[("cell", rest)])?;
        self.versions_at(&url)
    }

    /// A page of the versions endpoint at `url` (a `next`).
    pub fn versions_at(&self, url: &str) -> Result<Versions, ClientError> {
        let (status, body) = self.fetch(url, None, MAX_BODY)?;
        let v = json::parse(&body).map_err(|e| ClientError::unavailable(format!("{url}: {e}")))?;
        if status != 200 {
            let code = v
                .get("error")
                .and_then(Value::as_str)
                .and_then(ErrorCode::from_code)
                .unwrap_or(ErrorCode::InputUnavailable);
            return Err(ClientError::new(code, format!("{url}: status {status}")));
        }
        let cell = v.get("cell").cloned().ok_or_else(|| ClientError::unavailable(format!("{url}: no cell")))?;
        let mut versions = Vec::new();
        if let Some(Value::Array(vs)) = v.get("versions") {
            for x in vs {
                let s = |k: &str| x.get(k).and_then(Value::as_str).map(str::to_string);
                match (s("tx"), s("asof"), s("receipt")) {
                    (Some(tx), Some(asof), Some(receipt)) => versions.push(VersionEntry { tx, asof, receipt }),
                    _ => return Err(ClientError::unavailable(format!("{url}: a malformed version"))),
                }
            }
        }
        Ok(Versions { cell, versions, next: v.get("next").and_then(Value::as_str).map(str::to_string) })
    }

    /// A receipt fetched by the client, parsed.
    pub fn parse_receipt(bytes: &[u8]) -> Result<Receipt, ClientError> {
        Receipt::from_json(bytes).map_err(|e| ClientError::new(e.code(), e.to_string()))
    }
}

fn collect_signers(v: &Value, hosts: &mut BTreeSet<String>) {
    match v {
        Value::Object(m) => {
            for (k, x) in m {
                if k == "computed_by" {
                    if let Some(s) = x.as_str() {
                        hosts.insert(s.split('#').next().unwrap_or("").to_string());
                    }
                } else {
                    collect_signers(x, hosts);
                }
            }
        }
        Value::Array(a) => a.iter().for_each(|x| collect_signers(x, hosts)),
        _ => {}
    }
}

/// The host of a function name.
pub fn name_host(name: &str) -> &str {
    host_of(name)
}

#[cfg(test)]
mod transport_detail_tests {
    use super::*;
    use std::io::{self, ErrorKind};

    /// CONTRACT-1.8b H3, test 1: a Windows resolver error code (WSAHOST_NOT_FOUND, 11001) is `dns`.
    #[test]
    fn a_windows_resolver_code_is_dns() {
        let e = ureq::Error::Io(io::Error::from_raw_os_error(11001));
        assert_eq!(transport_detail(&e), Some(ForeignDetail::Dns));
    }

    /// CONTRACT-1.8b H3, test 2: the text "No such host is known" is `dns`.
    #[test]
    fn no_such_host_is_known_is_dns() {
        let e = ureq::Error::Io(io::Error::other("No such host is known. (os error 11001)"));
        assert_eq!(transport_detail(&e), Some(ForeignDetail::Dns));
    }

    /// CONTRACT-1.8b H3, test 3: everything else is unchanged.
    #[test]
    fn a_refused_connection_stays_refused() {
        let e = ureq::Error::Io(io::Error::from(ErrorKind::ConnectionRefused));
        assert_eq!(transport_detail(&e), Some(ForeignDetail::Refused));
    }
}