use std::collections::{BTreeMap, BTreeSet};
use std::sync::atomic::Ordering;
use std::time::{Duration, Instant};
use datumesh_core::json::{self, Value};
use datumesh_core::lexical;
use datumesh_core::{KeyDocument, Keys};
use datumesh_store::{ListedHost, Listing, ListingWrite, Store, SubjectKeys};
use crate::envelope::{Cache, Reply};
pub const DEFAULT_INTERVAL: Duration = Duration::from_secs(21_600);
pub const MAX_HOSTS: usize = 64;
pub const UNREACHABLE_DROP_AFTER: Duration = Duration::from_secs(7 * 86_400);
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Limits {
pub names: usize,
pub bytes: u64,
pub time: Duration,
}
impl Default for Limits {
fn default() -> Self {
Limits { names: 2_000, bytes: 32 << 20, time: Duration::from_secs(120) }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Cut {
Names,
Bytes,
Time,
Unreached,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FetchedName {
pub name: String,
pub fn_hash: String,
pub subjects: Vec<SubjectKeys>,
pub descriptor: Vec<u8>,
pub label: String,
pub description: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Fetch {
Fetched {
key_id: String,
names: Vec<FetchedName>,
refused: Vec<String>,
cut: Option<Cut>,
},
Unreachable {
detail: String,
},
KeyDocFailed {
detail: String,
},
MeshDocFailed {
detail: String,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Outcome {
Ok,
KeyDocFailed,
MeshDocFailed,
Cut,
Unreachable,
}
impl Outcome {
pub fn as_str(self) -> &'static str {
match self {
Outcome::Ok => "ok",
Outcome::KeyDocFailed => "key_document_failed",
Outcome::MeshDocFailed => "mesh_document_failed",
Outcome::Cut => "cut",
Outcome::Unreachable => "unreachable",
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Applied {
pub outcome: Outcome,
pub cut: Option<Cut>,
pub added: Vec<String>,
pub changed: Vec<String>,
pub dropped: Vec<String>,
pub listed: usize,
pub writes: u64,
}
impl Applied {
pub fn changed_any(&self) -> bool {
!(self.added.is_empty() && self.changed.is_empty() && self.dropped.is_empty())
}
}
#[derive(Debug)]
pub struct ListingError {
detail: String,
}
impl std::fmt::Display for ListingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "listing: {}", self.detail)
}
}
impl std::error::Error for ListingError {}
pub fn origin_of(config: &crate::Config, host: &str) -> String {
match config.peers.get(host) {
Some(origin) => origin.clone(),
None => format!("https://{host}"),
}
}
enum Stop {
Unreachable(String),
Bytes,
}
struct Run {
agent: ureq::Agent,
started: Instant,
limits: Limits,
bytes: u64,
}
impl Run {
fn new(limits: &Limits) -> Run {
let agent: ureq::Agent = ureq::Agent::config_builder()
.max_redirects(0)
.http_status_as_error(false)
.user_agent(concat!("datumesh-http/", env!("CARGO_PKG_VERSION")))
.build()
.into();
Run { agent, started: Instant::now(), limits: limits.clone(), bytes: 0 }
}
fn left(&self) -> Option<Duration> {
self.limits.time.checked_sub(self.started.elapsed()).filter(|d| !d.is_zero())
}
fn get(&mut self, url: &str) -> Result<(u16, Vec<u8>), Stop> {
let Some(left) = self.left() else { return Err(Stop::Unreachable("the run's time is spent".into())) };
let resp = self
.agent
.get(url)
.config()
.timeout_global(Some(left))
.build()
.call()
.map_err(|e| Stop::Unreachable(format!("{url}: {e}")))?;
let status = resp.status().as_u16();
if status >= 500 {
return Err(Stop::Unreachable(format!("{url}: status {status}")));
}
let room = self.limits.bytes.saturating_sub(self.bytes);
let body = resp.into_body().with_config().limit(room.saturating_add(1)).read_to_vec().map_err(|e| match e {
ureq::Error::BodyExceedsLimit(_) => Stop::Bytes,
e => Stop::Unreachable(format!("{url}: {e}")),
})?;
self.bytes += body.len() as u64;
if self.bytes > self.limits.bytes {
return Err(Stop::Bytes);
}
Ok((status, body))
}
}
fn instant_key(s: &str) -> String {
let s = s.strip_suffix('Z').unwrap_or(s);
let (whole, frac) = s.split_once('.').unwrap_or((s, ""));
let frac = &frac[..frac.len().min(9)];
format!("{whole}.{frac:0<9}")
}
fn key_valid_at(doc: &KeyDocument, now: &str) -> Option<String> {
let now = instant_key(now);
doc.keys
.keys
.iter()
.find(|k| instant_key(&k.valid_from) <= now && k.valid_to.as_deref().is_none_or(|t| now < instant_key(t)))
.map(|k| k.id.clone())
}
fn next_page(host: &str, origin: &str, next: &str) -> Option<String> {
let slice = format!("/slice/{host}/");
if next.starts_with(&slice) {
return Some(format!("{origin}{next}"));
}
let rest = next.strip_prefix(&format!("https://{host}"))?;
rest.starts_with(&slice).then(|| format!("{origin}{rest}"))
}
type Members = BTreeMap<String, BTreeSet<SubjectKeys>>;
fn catalogue(run: &mut Run, host: &str, origin: &str, doc: &KeyDocument) -> Result<(Members, Option<Cut>), Stop> {
let mut keys = Keys::new();
keys.insert_document(doc.clone());
let mut out: BTreeMap<String, BTreeSet<SubjectKeys>> = BTreeMap::new();
let mut url = format!("{origin}/slice/{host}/catalog/subject/source?domain=*&measure=*&rank=*®ion=*&thing=*");
let source_ns = format!("{host}/catalog/subject");
let mut pages = 0usize;
loop {
let (status, body) = match run.get(&url) {
Ok(x) => x,
Err(Stop::Bytes) => return Ok((out, Some(Cut::Bytes))),
Err(Stop::Unreachable(detail)) => {
if pages == 0 {
return Err(Stop::Unreachable(detail));
}
let cut = if run.left().is_none() { Cut::Time } else { Cut::Unreached };
return Ok((out, Some(cut)));
}
};
let page = if status == 200 { json::parse(&body).ok() } else { None };
let Some(page) = page else {
if pages == 0 {
return Err(Stop::Unreachable(format!("{url}: status {status}")));
}
return Ok((out, Some(Cut::Unreached)));
};
pages += 1;
if let Some(Value::Array(members)) = page.get("members") {
for m in members {
let Some(receipt) = m.get("receipt") else { continue };
let r = datumesh_core::verify(&receipt.to_jcs(), &keys, None, None);
if r.error().is_some() {
continue;
}
let Some(cell) = receipt.get("cell") else { continue };
let is_source = cell.get("fn").and_then(Value::as_str) == Some("source")
&& cell.get("ns").and_then(Value::as_str) == Some(source_ns.as_str());
if !is_source {
continue;
}
let arg = |k: &str| -> Option<String> {
cell.get("args")?.get(k)?.get("value")?.as_str().map(str::to_string)
};
let (Some(d), Some(t), Some(me), Some(rg)) =
(arg("domain"), arg("thing"), arg("measure"), arg("region"))
else {
continue;
};
if cell.get("args").and_then(|a| a.get("rank")).is_none() {
continue;
}
let value = receipt.get("value");
let name = value.and_then(|v| v.as_str().or_else(|| v.get("value").and_then(Value::as_str)));
let Some(name) = name else { continue };
let under = name.starts_with(&format!("{host}/"));
if under && lexical::check_function_name(name, "name").is_ok() {
out.entry(name.to_string()).or_default().insert(SubjectKeys::new(&d, &t, &me, &rg));
}
}
}
match page.get("next") {
None | Some(Value::Null) => break,
Some(Value::String(next)) => match next_page(host, origin, next) {
Some(u) => url = u,
None => return Ok((out, Some(Cut::Unreached))),
},
Some(_) => return Ok((out, Some(Cut::Unreached))),
}
}
Ok((out, None))
}
enum NameStep {
Got(FetchedName),
Refused,
NotReached,
}
fn name_step(run: &mut Run, origin: &str, name: &str, subjects: Vec<SubjectKeys>) -> Result<NameStep, Stop> {
let base = format!("{origin}/");
let (status, d) = run.get(&format!("{base}describe/{name}"))?;
if status == 404 {
return Ok(NameStep::Refused);
}
if status != 200 {
return Ok(NameStep::NotReached);
}
let fn_hash = json::parse(&d)
.ok()
.and_then(|v| v.get("fn_hash").and_then(Value::as_str).map(str::to_string))
.filter(|h| lexical::check_hex(h, 64, "fn_hash").is_ok());
let Some(fn_hash) = fn_hash else { return Ok(NameStep::Refused) };
let (status, bytes) = run.get(&format!("{base}descriptor/{fn_hash}"))?;
if status != 200 {
return Ok(NameStep::NotReached);
}
if datumesh_core::function_hash(&bytes).ok().as_deref() != Some(fn_hash.as_str()) {
return Ok(NameStep::Refused);
}
let (status, f) = run.get(&format!("{base}fn/{name}?format=json"))?;
let page = if status == 200 { json::parse(&f).ok() } else { None };
let Some(v) = page else { return Ok(NameStep::NotReached) };
let label = v.get("label").and_then(Value::as_str).unwrap_or_default().to_string();
let description = v.get("description").and_then(Value::as_str).unwrap_or_default().to_string();
if description.is_empty() {
return Ok(NameStep::NotReached);
}
Ok(NameStep::Got(FetchedName { name: name.to_string(), fn_hash, subjects, descriptor: bytes, label, description }))
}
fn first_cut(cut: &mut Option<Cut>, c: Cut) {
if cut.is_none() {
*cut = Some(c);
}
}
pub fn fetch(host: &str, origin: &str, limits: &Limits, now: &str) -> Fetch {
let mut run = Run::new(limits);
let unreachable = |s: Stop| match s {
Stop::Unreachable(detail) => Fetch::Unreachable { detail },
Stop::Bytes => Fetch::Unreachable { detail: "the byte limit, before the key document".into() },
};
let (status, body) = match run.get(&format!("{origin}/.well-known/datumesh.json")) {
Ok(x) => x,
Err(s) => return unreachable(s),
};
let doc = match datumesh_core::key_document_from_response(host, status, Some(&body)) {
Ok(d) if status == 200 => d,
Ok(_) => return Fetch::KeyDocFailed { detail: format!("status {status}") },
Err(e) => return Fetch::KeyDocFailed { detail: e.to_string() },
};
let Some(key_id) = key_valid_at(&doc, now) else {
return Fetch::KeyDocFailed { detail: format!("no key of {host} is valid at {now}") };
};
let proto = json::parse(&body).ok().and_then(|v| v.get("proto").and_then(Value::as_str).map(str::to_string));
let (status, mbody) = match run.get(&format!("{origin}/.well-known/datumesh-mesh.json")) {
Ok(x) => x,
Err(s) => return unreachable(s),
};
if status != 200 {
return Fetch::MeshDocFailed { detail: format!("status {status}") };
}
let Ok(m) = json::parse(&mbody) else { return Fetch::MeshDocFailed { detail: "not JSON".into() } };
if m.get("host").and_then(Value::as_str) != Some(host) || m.get("proto").and_then(Value::as_str) != proto.as_deref()
{
return Fetch::MeshDocFailed { detail: "disagrees with the key document on host or proto".into() };
}
let (subjects, mut cut) = match catalogue(&mut run, host, origin, &doc) {
Ok(x) => x,
Err(s) => return unreachable(s),
};
let mut all: Vec<(String, Vec<SubjectKeys>)> =
subjects.into_iter().map(|(n, s)| (n, s.into_iter().collect())).collect();
if all.len() > limits.names {
all.truncate(limits.names);
first_cut(&mut cut, Cut::Names);
}
let mut names = Vec::new();
let mut refused = Vec::new();
for (name, subjects) in all {
if run.left().is_none() {
first_cut(&mut cut, Cut::Time);
break;
}
match name_step(&mut run, origin, &name, subjects) {
Ok(NameStep::Got(n)) => names.push(n),
Ok(NameStep::Refused) => refused.push(name),
Ok(NameStep::NotReached) => first_cut(&mut cut, Cut::Unreached),
Err(Stop::Bytes) => {
first_cut(&mut cut, Cut::Bytes);
break;
}
Err(Stop::Unreachable(_)) => {
if run.left().is_none() {
first_cut(&mut cut, Cut::Time);
break;
}
first_cut(&mut cut, Cut::Unreached);
}
}
}
Fetch::Fetched { key_id, names, refused, cut }
}
const MAX_CREATED_SUBJECTS: usize = 64;
pub fn apply<S: Store + ?Sized>(store: &S, host: &str, fetch: &Fetch, now: &str) -> Result<Applied, ListingError> {
let se = |e: &dyn std::fmt::Display| ListingError { detail: e.to_string() };
let mut applied = Applied {
outcome: Outcome::Ok,
cut: None,
added: Vec::new(),
changed: Vec::new(),
dropped: Vec::new(),
listed: 0,
writes: 0,
};
let held: Vec<Listing> = store.listings_of(host).map_err(|e| se(&e))?;
let existing: Vec<String> = held.iter().map(|l| l.name.clone()).collect();
let mut drop_all = false;
let mut drop_set: BTreeSet<String> = BTreeSet::new();
match fetch {
Fetch::Fetched { key_id, names, refused, cut } => {
let mut verified = BTreeSet::new();
let offered: BTreeSet<&str> = names.iter().map(|n| n.name.as_str()).collect();
let mut refuse: BTreeSet<String> = refused.iter().cloned().collect();
let stored: BTreeMap<&str, &Listing> = held.iter().map(|l| (l.name.as_str(), l)).collect();
let bound = crate::catalog_build::bound_subjects(store).map_err(|e| se(&e))?;
let staying =
|l: &Listing| cut.is_some() && !offered.contains(l.name.as_str()) && !refused.contains(&l.name);
let mut created: BTreeSet<SubjectKeys> = held
.iter()
.filter(|l| staying(l))
.flat_map(|l| l.subjects.iter().cloned())
.filter(|s| !bound.contains(s))
.collect();
let mut cap_cut = false;
let mut ordered: Vec<&FetchedName> = names.iter().collect();
ordered.sort_by(|a, b| a.name.cmp(&b.name));
for n in ordered {
let under = n.name.starts_with(&format!("{host}/"));
let ok =
under && datumesh_core::function_hash(&n.descriptor).ok().as_deref() == Some(n.fn_hash.as_str());
if !ok {
refuse.insert(n.name.clone());
continue;
}
let mut subjects = n.subjects.clone();
if cut.is_some() {
if let Some(s) = stored.get(n.name.as_str()) {
subjects.extend(s.subjects.iter().cloned());
}
}
subjects.sort();
subjects.dedup();
let new: Vec<SubjectKeys> =
subjects.iter().filter(|s| !bound.contains(*s) && !created.contains(*s)).cloned().collect();
if created.len() + new.len() > MAX_CREATED_SUBJECTS {
cap_cut = true;
continue;
}
let l = Listing {
host: host.to_string(),
name: n.name.clone(),
fn_hash: n.fn_hash.clone(),
subjects,
descriptor: n.descriptor.clone(),
key_id: key_id.clone(),
label: n.label.clone(),
description: n.description.clone(),
fetched_at: now.to_string(),
listed_at: now.to_string(),
};
match store.put_listing(&l) {
Ok(ListingWrite::Unchanged) => {}
Ok(ListingWrite::Inserted) => {
applied.added.push(n.name.clone());
applied.writes += 1;
}
Ok(ListingWrite::Changed) => {
applied.changed.push(n.name.clone());
applied.writes += 1;
}
Err(datumesh_store::Error::Invalid(_)) => {
refuse.insert(n.name.clone());
continue;
}
Err(e) => return Err(se(&e)),
}
if store.set_listing_unreached(&n.name, None).map_err(|e| se(&e))? {
applied.writes += 1;
}
created.extend(new);
verified.insert(n.name.clone());
}
let cut = cut.or(cap_cut.then_some(Cut::Names));
match cut {
None => drop_set.extend(existing.iter().filter(|e| !verified.contains(*e)).cloned()),
Some(c) => {
drop_set.extend(existing.iter().filter(|e| refuse.contains(*e)).cloned());
for name in existing.iter().filter(|e| !offered.contains(e.as_str()) && !refuse.contains(*e)) {
let since = match store.listing_unreached(name).map_err(|e| se(&e))? {
Some(since) => since,
None => {
store.set_listing_unreached(name, Some(now)).map_err(|e| se(&e))?;
applied.writes += 1;
now.to_string()
}
};
let due = crate::keynames::add_seconds(&since, UNREACHABLE_DROP_AFTER.as_secs());
if due.is_some_and(|d| d.as_str() <= now) {
drop_set.insert(name.clone());
}
}
applied.cut = Some(c);
applied.outcome = Outcome::Cut;
}
}
let day = &now[..10];
let row = ListedHost { host: host.to_string(), last_seen: Some(day.to_string()), unreachable_since: None };
if store.put_listed_host(&row).map_err(|e| se(&e))? {
applied.writes += 1;
}
}
Fetch::KeyDocFailed { .. } => {
applied.outcome = Outcome::KeyDocFailed;
drop_all = true;
}
Fetch::MeshDocFailed { .. } => {
applied.outcome = Outcome::MeshDocFailed;
drop_all = true;
}
Fetch::Unreachable { .. } => {
applied.outcome = Outcome::Unreachable;
let row = store.listed_host(host).map_err(|e| se(&e))?;
match row {
None => {
let r = ListedHost {
host: host.to_string(),
last_seen: None,
unreachable_since: Some(now.to_string()),
};
store.put_listed_host(&r).map_err(|e| se(&e))?;
applied.writes += 1;
}
Some(r) => match r.unreachable_since.as_deref() {
None => {
let r2 = ListedHost { unreachable_since: Some(now.to_string()), ..r };
store.put_listed_host(&r2).map_err(|e| se(&e))?;
applied.writes += 1;
}
Some(since) => {
let due = crate::keynames::add_seconds(since, UNREACHABLE_DROP_AFTER.as_secs());
if due.is_some_and(|d| d.as_str() <= now) {
drop_all = true;
}
}
},
}
}
}
if drop_all {
drop_set.extend(existing.iter().cloned());
}
for name in &drop_set {
if store.delete_listing(name).map_err(|e| se(&e))? {
applied.dropped.push(name.clone());
applied.writes += 1;
}
}
applied.listed = store.listings_of(host).map_err(|e| se(&e))?.len();
Ok(applied)
}
pub fn run_host<S: Store + ?Sized>(
store: &S,
host: &str,
origin: &str,
limits: &Limits,
now: &str,
) -> Result<Applied, ListingError> {
let f = fetch(host, origin, limits, now);
apply(store, host, &f, now)
}
fn active(inner: &crate::mesh::Inner) -> bool {
!inner.config.listed_meshes.is_empty()
}
fn under_listed(inner: &crate::mesh::Inner, word: &str) -> bool {
word.split_once('/').is_some_and(|(host, _)| inner.config.listed_meshes.iter().any(|h| h == host))
}
pub(crate) fn lookup(inner: &crate::mesh::Inner, name: &str) -> Result<Option<Listing>, ListingError> {
inner.listing_lookups.fetch_add(1, Ordering::Relaxed);
inner.store.listing(name).map_err(|e| ListingError { detail: e.to_string() })
}
pub(crate) fn checked(inner: &crate::mesh::Inner, name: &str) -> Result<Option<Listing>, ListingError> {
if !active(inner) {
return Ok(None);
}
lookup(inner, name)
}
pub(crate) fn listed(inner: &crate::mesh::Inner, name: &str) -> Option<Listing> {
checked(inner, name).ok().flatten()
}
fn resolve_alias(text: &str, alias: &dyn Fn(&str) -> Option<String>) -> String {
let Some(rest) = text.strip_prefix('@') else { return text.to_string() };
let end = rest.find(['(', '/', '?', ' ']).unwrap_or(rest.len());
match alias(&rest[..end]) {
Some(target) => format!("{target}{}", &rest[end..]),
None => text.to_string(),
}
}
pub(crate) fn checked_in(
inner: &crate::mesh::Inner,
bindings: &dyn crate::call::Bindings,
alias: &dyn Fn(&str) -> Option<String>,
text: &str,
) -> Result<Option<Listing>, ListingError> {
if !active(inner) {
return Ok(None);
}
let text = resolve_alias(text.strip_prefix("cell://").unwrap_or(text), alias);
let mut ends: Vec<usize> =
text.char_indices().filter(|(_, c)| matches!(c, '/' | '(' | '?' | ' ')).map(|(i, _)| i).collect();
ends.push(text.len());
for e in ends {
if under_listed(inner, &text[..e]) {
if let Some(l) = bindings.listing(&text[..e])? {
return Ok(Some(l));
}
}
}
Ok(None)
}
pub(crate) fn checked_words(inner: &crate::mesh::Inner, text: &str) -> Result<Option<Listing>, ListingError> {
if !active(inner) {
return Ok(None);
}
let words: BTreeSet<&str> =
text.split(|c: char| !(c.is_alphanumeric() || "._/-".contains(c))).filter(|w| under_listed(inner, w)).collect();
for w in words {
if let Some(l) = lookup(inner, w)? {
return Ok(Some(l));
}
}
Ok(None)
}
pub(crate) fn checked_formula(inner: &crate::mesh::Inner, body: &[u8]) -> Result<Option<Listing>, ListingError> {
if !active(inner) {
return Ok(None);
}
let formula = json::parse(body).ok().and_then(|v| v.get("formula").and_then(Value::as_str).map(str::to_string));
match formula {
Some(f) => checked_words(inner, &f),
None => Ok(None),
}
}
pub(crate) fn unavailable() -> Reply {
crate::envelope::error_reply(datumesh_core::ErrorCode::EvalUnavailable)
}
pub(crate) fn refusal(l: &Listing) -> Reply {
let body = Value::Object(vec![
("outcome".into(), Value::String("not_covered".into())),
("error".into(), Value::String("E_LISTED_ELSEWHERE".into())),
("mesh".into(), Value::String(mesh_of(&l.host))),
("name".into(), Value::String(l.name.clone())),
]);
Reply::json(404, body.to_jcs(), Cache::NoStore)
}
fn mesh_of(host: &str) -> String {
format!("https://{host}")
}
fn first_note(inner: &crate::mesh::Inner, name: &str) -> Option<String> {
let built = crate::catalog_view::built(inner)?;
let subject = &built.places.get(name)?.subject;
let first = built.firsts.get(subject)?;
if first == name {
return None;
}
let l = listed(inner, first)?;
let last_seen = inner.store.listed_host(&l.host).ok().flatten().and_then(|r| r.last_seen).unwrap_or_default();
Some(format!(
"<p>The source's own function is listed here and ranks first in its subject (last seen {}): \
<a href=\"{}\"><code>{}</code></a>.</p>\n",
escape(&last_seen),
escape(&crate::fnpage::page_url(inner, &l.name)),
escape(&l.name)
))
}
pub(crate) fn relay_note(inner: &crate::mesh::Inner, d: &Value) -> Option<String> {
first_note(inner, d.get("name").or_else(|| d.get("fn"))?.as_str()?)
}
pub(crate) fn takeover_note(inner: &crate::mesh::Inner, relays: &[String]) -> String {
let mut notes: Vec<String> = Vec::new();
for r in relays {
if let Some(n) = first_note(inner, r) {
if !notes.contains(&n) {
notes.push(n);
}
}
}
notes.concat()
}
fn listed_members(store: &dyn Store, l: &Listing) -> Vec<(String, Value)> {
let last_seen = store.listed_host(&l.host).ok().flatten().and_then(|r| r.last_seen).unwrap_or_default();
let descriptor = json::parse(&l.descriptor).unwrap_or(Value::Null);
let s = |v: &str| Value::String(v.to_string());
vec![
("outcome".into(), s("ok")),
("kind".into(), s("function")),
("fn".into(), s(&l.name)),
("listed".into(), Value::Bool(true)),
("mesh".into(), s(&mesh_of(&l.host))),
("publisher".into(), s(&l.host)),
("address".into(), s(&format!("{}/fn/{}", mesh_of(&l.host), l.name))),
("last_seen".into(), s(&last_seen)),
("fn_hash".into(), s(&l.fn_hash)),
("label".into(), s(&l.label)),
("description".into(), s(&l.description)),
("descriptor".into(), descriptor),
]
}
pub(crate) fn describe_reply(store: &dyn Store, l: &Listing) -> Reply {
Reply::json(200, Value::Object(listed_members(store, l)).to_jcs(), Cache::Public(300))
}
fn escape(s: &str) -> String {
s.replace('&', "&").replace('<', "<").replace('>', ">").replace('"', """)
}
pub(crate) fn fn_reply(store: &dyn Store, l: &Listing, json_form: bool) -> Reply {
if json_form {
return describe_reply(store, l);
}
let members = listed_members(store, l);
let last_seen = members.iter().find(|(k, _)| k == "last_seen").and_then(|(_, v)| v.as_str()).unwrap_or_default();
let address = mesh_of(&l.host) + "/fn/" + &l.name;
let html = format!(
"<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\">\
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>{name}</title></head>\
<body><h1>{name}</h1><p>{description}</p>\
<p>Listed from <a href=\"{address}\">{mesh}</a>, last seen {last_seen}. This mesh describes the function; \
it does not serve its values.</p></body></html>",
name = escape(&l.name),
description = escape(&l.description),
address = escape(&address),
mesh = escape(&mesh_of(&l.host)),
last_seen = escape(last_seen),
);
Reply {
status: 200,
body: html.into_bytes().into(),
content_type: "text/html; charset=utf-8",
cache: Cache::Public(300),
canonical: None,
retry_after: None,
}
}