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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use failure::Fail;
#[cfg(feature = "regular_expression")]
use regex::Regex;
use std::cell::RefCell;
use url::{ParseError, SyntaxViolation, Url};

#[cfg(test)]
use std::path::Path;

#[derive(Clone, Debug, Display, Fail, PartialEq)]
pub enum UrlError {
    ParseError(ParseError),
    SyntaxViolation(SyntaxViolation),
    JsonFragmentError(String),
}

impl From<ParseError> for UrlError {
    fn from(error: ParseError) -> Self {
        UrlError::ParseError(error)
    }
}

impl From<SyntaxViolation> for UrlError {
    fn from(error: SyntaxViolation) -> Self {
        UrlError::SyntaxViolation(error)
    }
}

#[cfg(feature = "regular_expression")]
fn get_invalid_fragment_part_according_to_json_pointer_rules(url: &Url) -> Option<String> {
    // Checks https://tools.ietf.org/html/rfc6901 rules
    let regex = Regex::new("#.*(~([^01]|$))").unwrap();
    regex.captures(url.as_str()).and_then(|captures| Some(String::from(&captures[1])))
}

#[cfg(not(feature = "regular_expression"))]
fn get_invalid_fragment_part_according_to_json_pointer_rules(url: &Url) -> Option<String> {
    // Checks https://tools.ietf.org/html/rfc6901 rules
    // This check could have been done with a Regex but this requires regex dependency which adds ~20KB in the
    // final built library, which looks a bit too much as we are not using regex for anything else.
    //
    // As we're not sure about the real impact of having regex as dependency of this library
    // we're implementing a non-regex based solution, the code is a bit more verbose and so it's possible that it will
    // be selected a single solution.
    let fragment = url.fragment().unwrap_or("/");
    let mut next_character_error: Option<String> = None;

    let _ = fragment.chars().collect::<Vec<char>>().windows(2).any(|window| {
        let current = window[0];
        let next = window[1];

        if current == '~' && next != '0' && next != '1' {
            next_character_error = Some(format!("{}{}", current, next));
            return true;
        }
        false
    });

    if next_character_error.is_some() {
        next_character_error
    } else if fragment.ends_with('~') {
        Some("~".to_string())
    } else {
        None
    }
}

pub(crate) fn parse_and_normalize_url<R: AsRef<str>>(url: R) -> Result<Url, UrlError> {
    let syntax_violations = RefCell::new(Vec::<SyntaxViolation>::new());
    let mut url = Url::options()
        .syntax_violation_callback(Some(&|v| syntax_violations.borrow_mut().push(v)))
        .parse(url.as_ref())?;
    if let Some(violation) = syntax_violations.borrow().first() {
        Err(*violation)?
    }

    let cloned_url = url.clone();
    let fragments = cloned_url.fragment().unwrap_or("").trim_end_matches('/');

    let owned_fragment = if fragments.is_empty() {
        "/".to_string()
    } else if fragments.starts_with('/') {
        fragments.to_string()
    } else {
        format!("/{}", fragments)
    };

    url.set_fragment(Some(owned_fragment.as_str()));

    if let Some(invalid_fragment) = get_invalid_fragment_part_according_to_json_pointer_rules(&url) {
        return Err(UrlError::JsonFragmentError(invalid_fragment));
    }

    Ok(url)
}

pub(crate) fn normalize_url_for_cache(url: &Url) -> Url {
    let mut clone_url = url.clone();
    clone_url.set_fragment(Some("/"));
    clone_url
}

#[allow(dead_code)]
#[cfg(test)]
pub(crate) fn test_data_file_path(path: &str) -> String {
    let repository_path = Path::new(file!()).canonicalize().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
    String::from(
        path.split('/')
            .collect::<Vec<_>>()
            .iter()
            .fold(repository_path.join("test-data"), |iter_path, &path_path| iter_path.join(path_path))
            .canonicalize()
            .unwrap()
            .to_str()
            .unwrap(),
    )
}

#[allow(dead_code)]
#[cfg(test)]
pub(crate) fn test_data_file_url(path: &str) -> String {
    Url::from_file_path(dbg!(test_data_file_path(path))).unwrap().to_string()
}

#[cfg(test)]
mod tests {
    use super::{parse_and_normalize_url, UrlError};
    use test_case_derive::test_case;
    use url::{ParseError, SyntaxViolation};

    #[test_case("memory://", "memory:///#/" :: "url_with_no_path_no_fragment")]
    #[test_case("memory://#", "memory:///#/" :: "url_with_no_path")]
    #[test_case("memory:///", "memory:///#/" :: "url_with_no_fragment")]
    #[test_case("memory:///#", "memory:///#/" :: "url_with_path_and_fragment")]
    #[test_case("memory:///#/", "memory:///#/" :: "url_with_path_and_fragment_normalized")]
    #[test_case("memory:///#fragment", "memory:///#/fragment" :: "url_with_path_and_not_empty_fragment_1")]
    #[test_case("memory:///#/fragment", "memory:///#/fragment" :: "url_with_path_and_not_empty_fragment_2")]
    #[test_case("memory:///#/fragment/", "memory:///#/fragment" :: "url_with_path_and_not_empty_fragment_3")]
    fn test_parse_and_normalize_url_valid_case(url_str: &str, expected_result_str: &str) {
        assert_eq!(parse_and_normalize_url(url_str).unwrap().as_str(), expected_result_str);
    }

    #[test_case("http:///", UrlError::ParseError(ParseError::EmptyHost))]
    #[test_case("http://300.0.0.0/", UrlError::ParseError(ParseError::InvalidIpv4Address))]
    #[test_case("memory://#/\0a", UrlError::SyntaxViolation(SyntaxViolation::NullInFragment))]
    #[test_case("http:/example", UrlError::SyntaxViolation(SyntaxViolation::ExpectedDoubleSlash))]
    #[test_case("memory://#/~", UrlError::JsonFragmentError(String::from("~")))]
    #[test_case("memory://#/~a", UrlError::JsonFragmentError(String::from("~a")))]
    #[test_case("memory://#/~0/~1/~c", UrlError::JsonFragmentError(String::from("~c")))]
    fn test_parse_and_normalize_url_invalid_case(url_str: &str, expected_err: UrlError) {
        assert_eq!(parse_and_normalize_url(url_str).unwrap_err(), expected_err);
    }
    //    #[allow(clippy::redundant_closure)]
    //    #[test_case("", vec![] :: "empty fragment")]
    //    #[test_case("#", vec![] :: "Only pound in fragment")]
    //    #[test_case("/", vec![] :: "Only slash in fragment")]
    //    #[test_case("#/", vec![] :: "Pound and slash in fragment")]
    //    #[test_case("/a/~0/b/~1/c", vec!["a", "~", "b", "/", "c"] :: "Complex fragment not starting with pound")]
    //    #[test_case("#/a/~0/b/~1/c", vec!["a", "~", "b", "/", "c"] :: "Complex fragment starting with pound")]
    //    fn test_extract_fragment_components_from_fragment_string(url_str: &str, expected_result: Vec<&str>) {
    //        assert_eq!(
    //            extract_fragment_components_from_fragment_string(url_str),
    //            expected_result.iter().map(|part: &&str| part.to_string()).collect::<Vec<String>>()
    //        );
    //    }
    //
    //    #[allow(clippy::redundant_closure)]
    //    #[test_case("memory://", vec![] :: "Bare minimal url")]
    //    #[test_case("memory://#/", vec![] :: "Minimal url with fragment")]
    //    #[test_case("memory://#/a/~0/b/~1/c", vec!["a", "~", "b", "/", "c"] :: "Complex fragment")]
    //    fn test_extract_fragment_components(url_str: &str, expected_result: Vec<&str>) {
    //        assert_eq!(
    //            extract_fragment_components(&parse_and_normalize_url(url_str).ok().unwrap()),
    //            expected_result.iter().map(|part: &&str| part.to_string()).collect::<Vec<String>>()
    //        );
    //    }
    //
    //    #[test_case("memory://", vec![], "memory:///#")]
    //    #[test_case("memory://", vec!["a"], "memory:///#/a")]
    //    #[test_case("memory://#/a/b", vec!["c"], "memory:///#/a/b/c")]
    //    fn test_append_fragment_components(url_str: &str, components_to_add: Vec<&str>, expected_result: &str) {
    //        assert_eq!(
    //            append_fragment_components(&parse_and_normalize_url(url_str).ok().unwrap(), components_to_add).as_str(),
    //            expected_result,
    //        );
    //    }
}