hyper_staticfile/util/
requested_path.rs1use std::path::{Component, Path, PathBuf};
2
3#[inline]
4fn decode_percents(string: &str) -> String {
5 percent_encoding::percent_decode_str(string)
6 .decode_utf8_lossy()
7 .into_owned()
8}
9
10fn sanitize_path(path: &Path) -> PathBuf {
11 path.components()
12 .fold(PathBuf::new(), |mut result, p| match p {
13 Component::Normal(x) => {
14 if Path::new(&x)
17 .components()
18 .all(|c| matches!(c, Component::Normal(_)))
19 {
20 result.push(x);
21 }
22 result
23 }
24 Component::ParentDir => {
25 result.pop();
26 result
27 }
28 _ => result,
29 })
30}
31
32pub struct RequestedPath {
34 pub sanitized: PathBuf,
36 pub is_dir_request: bool,
38}
39
40impl RequestedPath {
41 pub fn resolve(request_path: &str) -> Self {
43 let is_dir_request = request_path.as_bytes().last() == Some(&b'/');
44 let request_path = PathBuf::from(decode_percents(request_path));
45 RequestedPath {
46 sanitized: sanitize_path(&request_path),
47 is_dir_request,
48 }
49 }
50}