fix replacing urls i think

This commit is contained in:
mat 2024-07-17 05:44:41 +00:00
parent 732d95093c
commit 427da97c09

View File

@ -102,8 +102,10 @@ impl HostAndPath {
} else { } else {
return None; return None;
} }
} else if real_url.host.ends_with(&replace_from.host) {
replace_with.host.to_owned()
} else { } else {
replace_with.host.clone() return None;
} }
} else if real_url.host == replace_from.host { } else if real_url.host == replace_from.host {
replace_with.host.clone() replace_with.host.clone()
@ -113,17 +115,17 @@ impl HostAndPath {
// host matches, now check path // host matches, now check path
let new_path = if replace_from.path.ends_with('/') || replace_with.path.is_empty() { let new_path = if replace_from.path.ends_with('/') || replace_from.path.is_empty() {
if replace_with.path.ends_with('/') { if replace_with.path.ends_with('/') || replace_with.path.is_empty() {
if let Some(path_without_prefix) = real_url.path.strip_prefix(&replace_from.path) { if let Some(path_without_prefix) = real_url.path.strip_prefix(&replace_from.path) {
format!("{}{path_without_prefix}", replace_with.path) format!("{}{path_without_prefix}", replace_with.path)
} else { } else {
return None; return None;
} }
} else if replace_with.path.is_empty() { } else if real_url.path.starts_with(&replace_from.path) {
real_url.path.clone()
} else {
replace_with.path.clone() replace_with.path.clone()
} else {
return None;
} }
} else if real_url.path == replace_from.path { } else if real_url.path == replace_from.path {
replace_with.path.clone() replace_with.path.clone()
@ -229,4 +231,13 @@ mod tests {
"https://medium.com/asdf", "https://medium.com/asdf",
); );
} }
#[test]
fn test_non_matching_wildcard_to_absolute() {
test_replacement(
".medium.com",
"scribe.rip",
"https://example.com/asdf",
"https://example.com/asdf",
);
}
} }