♻️ refactor: extract routing func

main
Trisha 2022-03-25 15:47:34 -05:00
parent e6273209f9
commit de5338f4f2
1 changed files with 25 additions and 0 deletions

View File

@ -250,6 +250,31 @@ pub struct FileRoots <'a> {
pub dirs: &'a BTreeMap <String, PathBuf>, pub dirs: &'a BTreeMap <String, PathBuf>,
} }
struct RoutedPath <'a> {
root: &'a Path,
path: std::borrow::Cow <'a, str>,
}
impl <'a> FileRoots <'a> {
fn route (self, input: &'a str) -> Result <Option <RoutedPath>, FileServerError> {
let path = match input.strip_prefix ("/files/") {
Some (x) => x,
None => return Ok (None),
};
// TODO: There is totally a dir traversal attack in here somewhere
let encoded_path = &path [0..];
let path = percent_decode (encoded_path.as_bytes ()).decode_utf8 ().map_err (FileServerError::PathNotUtf8)?;
Ok (Some (RoutedPath {
root: self.files,
path,
}))
}
}
// Handle the requests internally without knowing anything about PTTH or // Handle the requests internally without knowing anything about PTTH or
// HTML / handlebars // HTML / handlebars