add multi-root

main
Trisha 2022-03-25 16:14:22 -05:00
parent 436adb98ef
commit fb9b0c67f5
1 changed files with 29 additions and 12 deletions

View File

@ -257,21 +257,38 @@ struct RoutedPath <'a> {
impl <'a> FileRoots <'a> { impl <'a> FileRoots <'a> {
fn route (self, input: &'a str) -> Result <Option <RoutedPath>, FileServerError> { 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 // TODO: There is totally a dir traversal attack in here somewhere
let encoded_path = &path [0..]; if let Some (path) = input.strip_prefix ("/dirs/") {
if let Some ((dir, path)) = path.split_once ('/') {
let root = match self.dirs.get (dir) {
None => return Ok (None),
Some (x) => x,
};
let path_s = percent_decode (path.as_bytes ()).decode_utf8 ().map_err (FileServerError::PathNotUtf8)?;
return Ok (Some (RoutedPath {
root,
path_s,
}));
}
else {
return Ok (None);
}
}
if let Some (path) = input.strip_prefix ("/files/") {
let encoded_path = &path [0..];
let path_s = percent_decode (encoded_path.as_bytes ()).decode_utf8 ().map_err (FileServerError::PathNotUtf8)?;
return Ok (Some (RoutedPath {
root: self.files,
path_s,
}));
}
let path_s = percent_decode (encoded_path.as_bytes ()).decode_utf8 ().map_err (FileServerError::PathNotUtf8)?; return Ok (None);
Ok (Some (RoutedPath {
root: self.files,
path_s,
}))
} }
} }