diff --git a/src/files/path.rs b/src/files/path.rs index e8e894a83b7fb7deb495b9b7b77ae4e21ede7b6f..e0fb0b734ee49cad88f9a26226e0a099da0221e7 100644 --- a/src/files/path.rs +++ b/src/files/path.rs @@ -1,5 +1,5 @@ use snafu::{ResultExt, Snafu}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; #[derive(Snafu, Debug)] pub enum Error { @@ -21,9 +21,9 @@ pub enum Error { /// Note: This function does not check that the path resolves somewhere outside /// of the base path. pub fn resolve_path_within_tree( - base_path: &PathBuf, - current_path: &PathBuf, - path: &PathBuf, + base_path: &Path, + current_path: &Path, + path: &Path, ) -> Result<PathBuf, Error> { let new_path = if path.is_absolute() { base_path.join(path.strip_prefix("/").context(StripPrefix)?) diff --git a/src/files/path_context.rs b/src/files/path_context.rs index 9f2bbbd16c399264c8ccd72c2953b643cb5a4c4a..00744bd3f8a2bb3d4014bbf0bb8f6aba6b223955 100644 --- a/src/files/path_context.rs +++ b/src/files/path_context.rs @@ -10,7 +10,7 @@ use std::{ collections::HashMap, convert::{TryFrom, TryInto}, fmt::Debug, - path::PathBuf, + path::{Path, PathBuf}, }; #[derive(Snafu, Debug)] @@ -44,7 +44,9 @@ enum PathMatcher { } impl PathMatcher { - pub fn matches_path(&self, input_path: &PathBuf) -> bool { + pub fn matches_path<P: AsRef<Path>>(&self, input_path: P) -> bool { + let input_path = input_path.as_ref(); + match self { Self::Static { path } => input_path.eq(path), Self::Prefix { prefix } => input_path.starts_with(prefix), @@ -88,7 +90,7 @@ pub struct PathContext { } impl PathContext { - pub fn matches_path(&self, input_path: &PathBuf) -> bool { + pub fn matches_path<P: AsRef<Path>>(&self, input_path: P) -> bool { if let Some(matcher) = &self.matcher { matcher.matches_path(input_path) } else { @@ -260,7 +262,7 @@ mod tests { PathMatcher::Static { path: "hello/world.png".into(), } - .matches_path(&"hello/world.png".into()), + .matches_path(&"hello/world.png"), true ); @@ -268,7 +270,7 @@ mod tests { PathMatcher::Static { path: "hello/world.tar.gz".into(), } - .matches_path(&"hello/world.png".into()), + .matches_path(&"hello/world.png"), false ); } @@ -279,7 +281,7 @@ mod tests { PathMatcher::Prefix { prefix: "hello/".into(), } - .matches_path(&"hello/world.png".into()), + .matches_path(&"hello/world.png"), true ); @@ -287,7 +289,7 @@ mod tests { PathMatcher::Prefix { prefix: "hello/bye".into(), } - .matches_path(&"hello/world.png".into()), + .matches_path(&"hello/world.png"), false ); } @@ -310,7 +312,7 @@ mod tests { PathMatcher::Regex { regex: Regex::new(r"\.png$").unwrap(), } - .matches_path(&"hello/world.tar.gz".into()), + .matches_path(&"hello/world.tar.gz"), false ); } diff --git a/src/files/service.rs b/src/files/service.rs index 6dbaf0f0a214d500b7acfcce4b8813a69efe08d7..263e8497d0c24ef2091b7a2b4066a17699384035 100644 --- a/src/files/service.rs +++ b/src/files/service.rs @@ -20,7 +20,7 @@ use snafu::Snafu; use std::{ convert::TryInto, io, - path::PathBuf, + path::{Path, PathBuf}, rc::Rc, sync::{Arc, RwLock}, task::{Context, Poll}, @@ -169,7 +169,7 @@ impl FilesService { RequestContext { path: serve_dir.join(&path_from_request), serve_dir, - path_context: self.get_path_context(&path_from_request.into()), + path_context: self.get_path_context(&path_from_request), } }; @@ -402,7 +402,9 @@ impl FilesService { Ok((canonical_path, serve_dir, path_from_request)) } - fn get_path_context(&self, path_from_request: &PathBuf) -> PathContext { + fn get_path_context<P: AsRef<Path>>(&self, path_from_request: P) -> PathContext { + let path_from_request = path_from_request.as_ref(); + for path_context in self.path_contexts.as_ref() { if path_context.matches_path(path_from_request) { debug!( @@ -458,7 +460,7 @@ impl Service for FilesService { Err(e) => return self.handle_early_err(e, req), }; - let path_context = self.get_path_context(&path_from_request.into()); + let path_context = self.get_path_context(&path_from_request); let request_context = RequestContext { path,