use crate::Error;
use crate::parse::{
CanonicalHost, CanonicalPort, PathPlus, PrePath, parse_path_plus, parse_pre_path, parse_query_plus,
write_canonical_path,
};
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub struct Parts {
pub pre_path: PrePath,
pub path_plus: PathPlus,
pub needs_slash: bool,
pub needs_host_rewrite: bool,
}
impl Parts {
pub const fn needs_port_rewrite(self) -> bool {
self.pre_path.port_len != self.pre_path.canonical_port_len()
}
pub const fn needs_path_rewrite(self) -> bool {
self.path_plus.path_len != self.path_plus.canonical_path_len
}
pub const fn needs_rewrite(self) -> bool {
self.needs_host_rewrite || self.needs_port_rewrite() || self.needs_path_rewrite()
}
pub const fn is_normalized(self) -> bool {
!self.needs_slash && !self.needs_rewrite()
}
pub fn normalized_len(self, len: usize) -> usize {
let dropped: usize = self.path_plus.path_len - self.path_plus.canonical_path_len;
((len - self.pre_path.len()) - dropped) + self.pre_path.canonical_len() + (self.needs_slash as usize)
}
pub const fn slash_index(self) -> usize {
self.pre_path.len()
}
}
pub fn write_normalized(s: &str, parts: Parts, url: &mut String) {
let pre_path: PrePath = parts.pre_path;
url.push_str(&s[..pre_path.host_start()]);
if let Some(ip) = pre_path.ip {
url.push_str(CanonicalHost::new(ip).as_str());
} else {
url.push_str(pre_path.host_str(s));
}
if let Some(port) = pre_path.port {
url.push_str(CanonicalPort::new(port).as_str());
}
let after_authority: &str = &s[pre_path.len()..];
if parts.needs_slash {
url.push('/');
url.push_str(after_authority);
} else {
let (path, path_plus) = after_authority.split_at(parts.path_plus.path_len);
write_canonical_path(path, url);
url.push_str(path_plus);
}
}
pub fn parse_parts(s: &str) -> Result<Parts, Error> {
let pre_path: PrePath = parse_pre_path(s)?;
let needs_host_rewrite: bool = pre_path.needs_host_rewrite(s);
let after_authority: &str = &s[pre_path.len()..];
let needs_slash: bool = !after_authority.starts_with('/');
let path_plus: PathPlus = if needs_slash {
parse_query_plus(after_authority)?
} else {
parse_path_plus(after_authority)?
};
Ok(Parts {
pre_path,
path_plus,
needs_slash,
needs_host_rewrite,
})
}
#[cfg(test)]
mod tests {
use crate::Error;
use crate::Error::{InvalidHost, InvalidPath, InvalidQuery, InvalidScheme};
use crate::parse::{Parts, parse_parts, write_normalized};
type Summary = (bool, usize, usize, usize);
fn parts_of(s: &str) -> Result<Summary, Error> {
parse_parts(s).map(|p: Parts| {
(
p.needs_slash,
p.slash_index(),
p.path_plus.path_len,
p.path_plus.query_len,
)
})
}
#[test]
fn fn_parse_parts() {
let test_cases: &[(&str, Result<Summary, Error>)] = &[
("http://host", Ok((true, 11, 1, 0))),
("http://host/", Ok((false, 11, 1, 0))),
("http://host/p", Ok((false, 11, 2, 0))),
("http://host?q", Ok((true, 11, 1, 2))),
("http://host#f", Ok((true, 11, 1, 0))),
("http://host?q#f", Ok((true, 11, 1, 2))),
("http://host:80", Ok((true, 14, 1, 0))),
("http://host:80?q", Ok((true, 14, 1, 2))),
("http://host:80/p?q", Ok((false, 14, 2, 2))),
("http://[::1]?q", Ok((true, 12, 1, 2))),
("http://[::1]:80#f", Ok((true, 15, 1, 0))),
("no-scheme", Err(InvalidScheme)),
("http://ho!st?q", Err(InvalidHost)),
("http://host/p q", Err(InvalidPath)),
("http://host?q q", Err(InvalidQuery)),
];
for (url, expected) in test_cases {
let result: Result<Summary, Error> = parts_of(url);
assert_eq!(result, *expected, "url={}", url);
}
}
#[test]
fn fn_write_normalized() {
let test_cases: &[(&str, &str)] = &[
("http://host/p?q#f", "http://host/p?q#f"),
("http://host", "http://host/"),
("http://host?q#f", "http://host/?q#f"),
("http://host:0080/p", "http://host:80/p"),
("http://host:/p?q", "http://host/p?q"),
("http://[0:0:0:0:0:0:0:1]:0080/a/../b?q#f", "http://[::1]:80/b?q#f"),
("HTTP://HOST/P", "HTTP://HOST/P"),
];
for (input, expected) in test_cases {
let parts: Parts = parse_parts(input).unwrap();
let mut result: String = String::new();
write_normalized(input, parts, &mut result);
assert_eq!(result, *expected, "input={}", input);
assert_eq!(parts.normalized_len(input.len()), result.len(), "input={}", input);
assert_eq!(parts.is_normalized(), *input == result, "input={}", input);
}
}
}