122 lines
4.9 KiB
Rust
122 lines
4.9 KiB
Rust
//! 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<u64>,
|
|
/// Body bytes per second, shared by all connections of the server.
|
|
pub throttle_total: Option<u64>,
|
|
/// Wait before the reply starts.
|
|
pub latency_ms: Option<u64>,
|
|
/// After this many body bytes, say nothing more for `stall_ms` (default one hour).
|
|
pub stall_after: Option<u64>,
|
|
pub stall_ms: Option<u64>,
|
|
/// After this many body bytes, close the connection cleanly.
|
|
pub cut_after: Option<u64>,
|
|
/// After this many body bytes, reset the connection (TCP RST).
|
|
pub reset_after: Option<u64>,
|
|
/// Answer with this status instead of the file (403, 404, 410, 416, 429, 500, 503 ...).
|
|
pub status: Option<u16>,
|
|
/// `Retry-After` seconds on the status answer.
|
|
pub retry_after: Option<u64>,
|
|
/// Give the status only to the first N requests of the path.
|
|
pub status_count: Option<u32>,
|
|
/// Added to the declared `Content-Length` (the body keeps its real size).
|
|
pub wrong_length: Option<i64>,
|
|
/// 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<u64>,
|
|
/// From request N+1 on the path serves a new version: other bytes, ETag and date.
|
|
pub version_after_requests: Option<u32>,
|
|
/// 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<u64>,
|
|
/// ... or after this many requests. Dead links answer `expire_status` (default 403).
|
|
pub expire_requests: Option<u32>,
|
|
pub expire_status: Option<u16>,
|
|
/// More open connections than this from one client get 429.
|
|
pub max_conns: Option<usize>,
|
|
}
|
|
|
|
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<Faults> {
|
|
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),
|
|
_ => {}
|
|
}
|
|
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,
|
|
} };
|
|
}
|
|
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
|
|
)
|
|
}
|
|
}
|
|
|
|
#[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)));
|
|
}
|
|
}
|