use std::fmt::Display; use url::Url; /// Check out whether a URL is valid or not pub fn check_url(url: impl Display) -> bool { match Url::parse(&url.to_string()) { Ok(u) => { if u.scheme() != "http" && u.scheme() != "https" { log::debug!("URL is invalid, scheme is not http or https!"); return false; } if u.port_or_known_default() != Some(443) && u.port_or_known_default() != Some(80) { log::debug!("URL is invalid, port is not 80 or 443!"); return false; } true } Err(e) => { log::debug!("URL is invalid, could not be parsed! {e}"); false } } } #[cfg(test)] mod test { use crate::utils::url_utils::check_url; #[test] fn valid_url_ubuntu() { assert!(check_url( "https://releases.ubuntu.com/22.04.3/ubuntu-22.04.3-desktop-amd64.iso" )); } #[test] fn valid_url_with_port_ubuntu() { assert!(check_url( "https://releases.ubuntu.com:443/22.04.3/ubuntu-22.04.3-desktop-amd64.iso" )); } #[test] fn invalid_valid_url_ftp() { assert!(!check_url( "ftp://releases.ubuntu.com/22.04.3/ubuntu-22.04.3-desktop-amd64.iso" )); } #[test] fn invalid_valid_bad_port() { assert!(!check_url( "http://releases.ubuntu.com:81/22.04.3/ubuntu-22.04.3-desktop-amd64.iso" )); } }