use iron::modifier::Modifier; use iron::prelude::*; use iron::AfterMiddleware; /// Applies a modifier to every request that starts with a given path. pub struct Prefix { prefix: Vec, modifier: M, } impl Prefix { pub fn new(prefix: P, modifier: M) -> Prefix where P: IntoIterator, S: AsRef, { Prefix { prefix: prefix.into_iter().map(|s| s.as_ref().into()).collect(), modifier, } } fn prefix_matches(&self, path: &[&str]) -> bool { if self.prefix.len() > path.len() { return false; } path.iter() .zip(self.prefix.iter()) .all(|(path, prefix)| path == prefix) } } impl AfterMiddleware for Prefix where M: Clone + Modifier + Send + Sync + 'static, { fn after(&self, req: &mut Request<'_, '_>, mut res: Response) -> IronResult { if self.prefix_matches(&req.url.path()) { self.modifier.clone().modify(&mut res); } Ok(res) } }