♻️ refactor: extract routing func
							parent
							
								
									e6273209f9
								
							
						
					
					
						commit
						de5338f4f2
					
				| 
						 | 
				
			
			@ -250,6 +250,31 @@ pub struct FileRoots <'a> {
 | 
			
		|||
	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
 | 
			
		||||
// HTML / handlebars
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue