mithril_aggregator/file_uploaders/
url_sanitizer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use anyhow::{anyhow, Context};
use reqwest::Url;

use mithril_common::StdResult;

/// Sanitize URL path by removing empty segments and adding trailing slash
pub fn sanitize_url_path(url: &Url) -> StdResult<Url> {
    let segments_non_empty = url
        .path_segments()
        .map(|s| s.into_iter().filter(|s| !s.is_empty()).collect::<Vec<_>>())
        .unwrap_or_default();
    let mut url = url.clone();
    {
        let mut url_path_segments = url
            .path_segments_mut()
            .map_err(|e| anyhow!("error parsing URL: {e:?}"))
            .with_context(|| "while sanitizing URL path: {url}")?;
        let url_path_segments_cleared = url_path_segments.clear();
        for segment in segments_non_empty {
            url_path_segments_cleared.push(segment);
        }
        url_path_segments_cleared.push("");
    }

    Ok(url)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sanitize_url_path() {
        let url = Url::parse("http://example.com/a//b/c.ext?test=123").unwrap();
        assert_eq!(
            "http://example.com/a/b/c.ext/?test=123",
            sanitize_url_path(&url).unwrap().as_str()
        );

        let url = Url::parse("http://example.com/a//b/c.ext").unwrap();
        assert_eq!(
            "http://example.com/a/b/c.ext/",
            sanitize_url_path(&url).unwrap().as_str()
        );

        let url = Url::parse("http://example.com/a//b/c").unwrap();
        assert_eq!(
            "http://example.com/a/b/c/",
            sanitize_url_path(&url).unwrap().as_str()
        );

        let url = Url::parse("http://example.com/").unwrap();
        assert_eq!(
            "http://example.com/",
            sanitize_url_path(&url).unwrap().as_str()
        );

        let url = Url::parse("http://example.com").unwrap();
        assert_eq!(
            "http://example.com/",
            sanitize_url_path(&url).unwrap().as_str()
        );
    }
}