use std::collections::{HashMap, VecDeque};
use std::io::Write;
use std::time::{Duration, Instant};
use axum::body::Body;
use datumesh_core::json::{self, Value};
use http_body_util::BodyExt;
use crate::bundle::{self, BundleError};
use crate::envelope::{self, Cache, Reply};
use crate::mesh::Inner;
pub(crate) const MAX_BODY: usize = 16 * 1024 * 1024;
pub(crate) const MAX_RECEIPTS: usize = 50_000;
const DOMAIN_TIMEOUT: Duration = Duration::from_secs(5);
const HOUR: Duration = Duration::from_secs(3600);
const PER_HOST: usize = 10;
const PER_MESH: usize = 60;
#[derive(Default)]
pub(crate) struct Limits {
hosts: HashMap<String, VecDeque<Instant>>,
mesh: VecDeque<Instant>,
}
impl Limits {
fn admit(&mut self, host: &str, now: Instant) -> Result<(), u64> {
fn window(q: &mut VecDeque<Instant>, max: usize, now: Instant) -> Result<(), u64> {
while q.front().is_some_and(|t| now.duration_since(*t) >= HOUR) {
q.pop_front();
}
if q.len() >= max {
let oldest = q.front().copied().unwrap_or(now);
return Err(HOUR.saturating_sub(now.duration_since(oldest)).as_secs().max(1));
}
Ok(())
}
let Limits { hosts, mesh } = self;
hosts.retain(|_, q| q.back().is_some_and(|t| now.duration_since(*t) < HOUR));
let q = hosts.entry(host.to_string()).or_default();
window(q, PER_HOST, now)?;
window(mesh, PER_MESH, now)?;
q.push_back(now);
mesh.push_back(now);
Ok(())
}
}
enum BodyRead {
Bytes(Vec<u8>),
TooLarge,
Broken,
}
async fn read_capped(mut body: Body) -> BodyRead {
let mut out = Vec::new();
let mut too_large = false;
while let Some(frame) = body.frame().await {
let Ok(frame) = frame else { return BodyRead::Broken };
if let Ok(data) = frame.into_data() {
if too_large {
continue;
}
if out.len() + data.len() > MAX_BODY {
too_large = true;
out = Vec::new();
} else {
out.extend_from_slice(&data);
}
}
}
if too_large {
BodyRead::TooLarge
} else {
BodyRead::Bytes(out)
}
}
pub(crate) async fn post(inner: std::sync::Arc<Inner>, body: Body) -> Reply {
let bytes = match read_capped(body).await {
BodyRead::Bytes(b) => b,
BodyRead::TooLarge => {
let r = refusal(413, "E_TOO_LARGE", "a bundle is at most 16 MiB".into());
log(&inner, "-", "E_TOO_LARGE", 0, 0);
return r;
}
BodyRead::Broken => {
log(&inner, "-", "E_BUNDLE_FORMAT", 0, 0);
return refusal(400, "E_BUNDLE_FORMAT", "the body could not be read".into());
}
};
let i2 = inner.clone();
match inner.pool.clone().run(move || attempt(&i2, &bytes)).await {
Some(r) => r,
None => envelope::error_reply(datumesh_core::ErrorCode::EvalUnavailable),
}
}
#[derive(Default)]
struct Seen {
host: String,
functions: usize,
receipts: usize,
}
fn attempt(inner: &Inner, bytes: &[u8]) -> Reply {
let mut seen = Seen { host: "-".into(), ..Seen::default() };
let (reply, result) = check(inner, bytes, &mut seen);
log(inner, &seen.host, &result, seen.functions, seen.receipts);
reply
}
fn refusal(status: u16, code: &str, detail: String) -> Reply {
let body =
Value::Object(vec![("error".into(), Value::String(code.into())), ("detail".into(), Value::String(detail))]);
Reply::json(status, body.to_jcs(), Cache::NoStore)
}
fn text<'a>(v: &'a Value, k: &str) -> Option<&'a str> {
v.get(k).and_then(Value::as_str)
}
fn check(inner: &Inner, bytes: &[u8], seen: &mut Seen) -> (Reply, String) {
let fail = |status: u16, code: &str, detail: String| (refusal(status, code, detail), code.to_string());
let doc = match json::parse(bytes) {
Ok(d @ Value::Object(_)) => d,
Ok(_) => return fail(400, "E_BUNDLE_FORMAT", "a bundle is a JSON object".into()),
Err(e) => return fail(400, "E_BUNDLE_FORMAT", format!("not JSON: {e}")),
};
let Some(namespace) = text(&doc, "namespace") else {
return fail(400, "E_BUNDLE_FORMAT", "a bundle has a namespace".into());
};
let host = namespace.split('/').next().unwrap_or("").to_string();
if namespace.split('/').count() < 2 || host.is_empty() {
return fail(400, "E_BUNDLE_FORMAT", "the namespace has a host and a name".into());
}
seen.host = host.clone();
let names: Vec<(String, String)> = match doc.get("names") {
Some(Value::Array(a)) => {
a.iter().filter_map(|n| Some((text(n, "name")?.to_string(), text(n, "fn_hash")?.to_string()))).collect()
}
_ => Vec::new(),
};
seen.functions = names.len();
seen.receipts = match doc.get("receipts") {
Some(Value::Array(a)) => a.len(),
_ => 0,
};
if seen.receipts > MAX_RECEIPTS {
return fail(413, "E_TOO_LARGE", format!("a bundle has at most {MAX_RECEIPTS} receipts"));
}
let own = inner.config.owns(&host);
if let Some(name) = outside_namespace(&doc, namespace) {
return fail(422, "E_NOT_OWNED", format!("{name} is not under {namespace}/"));
}
if !own && !inner.config.publish_domains.allows(&host) {
return fail(403, "E_DOMAIN_NOT_ALLOWED", format!("{host} may not publish to this mesh"));
}
if let Err(wait) = inner.publish_limits.lock().unwrap_or_else(|p| p.into_inner()).admit(&host, Instant::now()) {
return (envelope::rate_limited_reply(wait), "rate_limited".into());
}
let keydoc: Vec<u8> = if own {
match inner.store.namespace(&host) {
Ok(Some(n)) => n.keydoc,
_ => return fail(422, "E_KEY_DOC", format!("the store holds no key document of {host}")),
}
} else {
match inner.foreign.client().key_document_live(&host, DOMAIN_TIMEOUT) {
Ok(b) => b,
Err(e) => return fail(422, "E_KEY_UNAVAILABLE", e.to_string()),
}
};
match bundle::prove(&*inner.store, &inner.config.host, &*inner.clock, bytes, &keydoc) {
Ok(()) => {}
Err(e) => return refused(&e),
}
let owned = vec![host.clone()];
let imported = match bundle::import_published(
&*inner.store,
&inner.config.host,
&owned,
&*inner.clock,
bytes,
Some(&keydoc),
) {
Ok(i) => i,
Err(e) => return refused(&e),
};
if !own {
if let Err(e) = inner.foreign.host(&host) {
return fail(500, "error", e);
}
}
let origin = inner.config.origin();
let functions: Vec<Value> = names
.iter()
.map(|(name, fh)| {
Value::Object(vec![
("name".into(), Value::String(name.clone())),
("fn_hash".into(), Value::String(fh.clone())),
("page".into(), Value::String(format!("{origin}/fn/{name}"))),
])
})
.collect();
let body = Value::Object(vec![
("imported".into(), json::parse(&imported.to_jcs()).unwrap_or(Value::Null)),
("names".into(), Value::Array(names.iter().map(|(n, _)| Value::String(n.clone())).collect())),
("functions".into(), Value::Array(functions)),
]);
(Reply::json(201, body.to_jcs(), Cache::NoStore), "created".into())
}
fn outside_namespace(doc: &Value, namespace: &str) -> Option<String> {
let prefix = format!("{namespace}/");
let mut names: Vec<&str> = Vec::new();
for member in ["names", "catalog"] {
if let Some(Value::Array(a)) = doc.get(member) {
names.extend(a.iter().filter_map(|n| text(n, "name")));
}
}
names.into_iter().find(|n| !n.starts_with(&prefix)).map(str::to_string)
}
fn refused(e: &BundleError) -> (Reply, String) {
let (status, code, detail) = match e {
BundleError::Format(m) => (400, "E_BUNDLE_FORMAT", m.clone()),
BundleError::NotOwned(m) => (422, "E_NOT_OWNED", m.clone()),
BundleError::Manifest(m) if m.starts_with("the signature does not verify") => {
(422, "E_BUNDLE_SIGNATURE", m.clone())
}
BundleError::Manifest(m) => (422, "E_KEY_DOC", m.clone()),
BundleError::Descriptor(m) => (422, "E_BUNDLE_DESCRIPTOR", m.clone()),
BundleError::Receipt(m) => (422, "E_BUNDLE_RECEIPT", m.clone()),
BundleError::TxInFuture { receipt, tx, now } => {
(422, "E_TX_IN_FUTURE", format!("receipt {receipt}: tx {tx} is later than the mesh clock {now}"))
}
BundleError::Bind(m) if m.contains("at or after the clock") => (422, "E_BACKDATED", m.clone()),
BundleError::Bind(m) => (422, "E_BIND", m.clone()),
BundleError::Store(m) if m.contains("at or after") => (422, "E_BACKDATED", m.clone()),
BundleError::Tests(m) => {
let code = match m.split_once(':').map(|(c, _)| c) {
Some("E_TESTS_FAILING") => "E_TESTS_FAILING",
Some("E_TESTS_MISSING") => "E_TESTS_MISSING",
_ => "E_BUNDLE_TESTS",
};
(422, code, m.clone())
}
other => (500, "error", other.to_string()),
};
(refusal(status, code, detail), code.to_string())
}
fn log(inner: &Inner, host: &str, result: &str, functions: usize, receipts: usize) {
let Some(dir) = &inner.config.log_dir else { return };
let line = format!("{} host={host} result={result} functions={functions} receipts={receipts}\n", inner.now());
let written = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(dir.join("bundles.log"))
.and_then(|mut f| f.write_all(line.as_bytes()));
if let Err(e) = written {
tracing::warn!(target: "datumesh_http", error = %e, "bundles.log: the attempt is not logged");
}
}