//! Faults: set live on the server for a path, or on one request through the query string. /// Every field is off by default. The query names are the field names: /// `/data/1/1000?cut_after=500&latency_ms=200`. Booleans take `1`. #[derive(Clone, Debug, Default, PartialEq)] pub struct Faults { /// Body bytes per second, for each connection. pub throttle_conn: Option, /// Body bytes per second, shared by all connections of the server. pub throttle_total: Option, /// Wait before the reply starts. pub latency_ms: Option, /// After this many body bytes, say nothing more for `stall_ms` (default one hour). pub stall_after: Option, pub stall_ms: Option, /// After this many body bytes, close the connection cleanly. pub cut_after: Option, /// After this many body bytes, reset the connection (TCP RST). pub reset_after: Option, /// Answer with this status instead of the file (403, 404, 410, 416, 429, 500, 503 ...). pub status: Option, /// `Retry-After` seconds on the status answer. pub retry_after: Option, /// Give the status only to the first N requests of the path. pub status_count: Option, /// Added to the declared `Content-Length` (the body keeps its real size). pub wrong_length: Option, /// Answer `Range` requests with the whole file and 200. pub ignore_range: bool, /// After this many body bytes the content switches to other bytes. pub change_after: Option, /// From request N+1 on the path serves a new version: other bytes, ETag and date. pub version_after_requests: Option, /// Claim `Content-Encoding: gzip` (the body stays plain). pub gzip: bool, /// The link dies this many seconds after its first request ... pub expire_secs: Option, /// ... or after this many requests. Dead links answer `expire_status` (default 403). pub expire_requests: Option, pub expire_status: Option, /// More open connections than this from one client get 429. pub max_conns: Option, /// Served by this many "nodes" in turn (request number modulo N): the same bytes, but each its own Last-Modified and /// nginx-style weak ETag `W/"-"`, and If-Range is checked against the node's own. pub farm: Option, /// On a farm, odd nodes serve other bytes of the same size. pub liar: bool, } impl Faults { pub fn status(code: u16) -> Faults { Faults { status: Some(code), ..Faults::default() } } /// Faults named in a query string; `None` for a bad name or value. pub fn from_query(query: &str) -> Option { let mut f = Faults::default(); for (k, v) in query.split('&').filter(|p| !p.is_empty()).filter_map(|p| p.split_once('=')) { f.set(k, v)?; } Some(f) } /// Set one field by name. Names that aren't faults (like `disp`) are not an error. fn set(&mut self, k: &str, v: &str) -> Option<()> { macro_rules! num { ($f:ident) => { self.$f = Some(v.parse().ok()?) }; } match k { "throttle_conn" => num!(throttle_conn), "throttle_total" => num!(throttle_total), "latency_ms" => num!(latency_ms), "stall_after" => num!(stall_after), "stall_ms" => num!(stall_ms), "cut_after" => num!(cut_after), "reset_after" => num!(reset_after), "status" => num!(status), "retry_after" => num!(retry_after), "status_count" => num!(status_count), "wrong_length" => num!(wrong_length), "ignore_range" => self.ignore_range = v == "1", "change_after" => num!(change_after), "version_after_requests" => num!(version_after_requests), "gzip" => self.gzip = v == "1", "expire_secs" => num!(expire_secs), "expire_requests" => num!(expire_requests), "expire_status" => num!(expire_status), "max_conns" => num!(max_conns), "farm" => num!(farm), "liar" => self.liar = v == "1", _ => {} } Some(()) } /// `other` wins where it sets something. pub fn merged(&self, other: &Faults) -> Faults { macro_rules! pick { ($($f:ident),*) => { Faults { $($f: other.$f.clone().or_else(|| self.$f.clone()),)* ignore_range: self.ignore_range || other.ignore_range, gzip: self.gzip || other.gzip, liar: self.liar || other.liar, } }; } pick!( throttle_conn, throttle_total, latency_ms, stall_after, stall_ms, cut_after, reset_after, status, retry_after, status_count, wrong_length, change_after, version_after_requests, expire_secs, expire_requests, expire_status, max_conns, farm ) } } #[cfg(test)] mod tests { use super::*; #[test] fn query_and_merge() { let q = Faults::from_query("cut_after=5&ignore_range=1&disp=star").unwrap(); assert_eq!(q.cut_after, Some(5)); assert!(q.ignore_range); assert!(Faults::from_query("cut_after=x").is_none()); let base = Faults { latency_ms: Some(1), cut_after: Some(9), ..Faults::default() }; let m = base.merged(&q); assert_eq!((m.latency_ms, m.cut_after), (Some(1), Some(5))); } }