static_file_resolver/impl.rs
1use super::*;
2
3impl Default for StaticFileResolver<'_> {
4 /// Builds the resolver that is used when none is given.
5 ///
6 /// # Returns
7 ///
8 /// - `Self` - The default resolver.
9 fn default() -> Self {
10 Self {
11 root: Path::new("."),
12 index: INDEX_DEFAULT,
13 }
14 }
15}
16
17impl StaticFileResolver<'_> {
18 /// The file a path names under the root, when the path is allowed.
19 ///
20 /// A path that climbs out of the root is refused, so a request cannot
21 /// reach a file the root does not hold.
22 ///
23 /// # Arguments
24 ///
25 /// - `&str` - The path of the file, relative to the root.
26 ///
27 /// # Returns
28 ///
29 /// - `Option<PathBuf>` - The file, when the path is allowed.
30 pub fn resolve(&self, path: &str) -> Option<PathBuf> {
31 let mut file: PathBuf = self.get_root().to_path_buf();
32 if path.is_empty() {
33 file.push(self.get_index());
34 return Some(file);
35 }
36 for segment in path.split(SEPARATOR_SLASH) {
37 if segment.is_empty()
38 || segment == SEGMENT_CURRENT
39 || segment == SEGMENT_PARENT
40 || segment.contains(SEPARATOR_BACKSLASH)
41 {
42 return None;
43 }
44 file.push(segment);
45 }
46 Some(file)
47 }
48}