diff --git a/src/html.rs b/src/html.rs index 16db4ab..e6ded28 100644 --- a/src/html.rs +++ b/src/html.rs @@ -1,7 +1,7 @@ use crate::http::retrieve_asset; use crate::js::attr_is_event_handler; use crate::utils::{ - data_to_data_url, is_valid_url, resolve_css_imports, resolve_url, url_has_protocol, + data_to_data_url, is_http_url, resolve_css_imports, resolve_url, url_has_protocol, }; use html5ever::interface::QualName; use html5ever::parse_document; @@ -377,7 +377,7 @@ pub fn walk_and_embed_assets( if &attr.name.local == "action" { let attr_value = attr.value.trim(); // Modify action to be a full URL - if !is_valid_url(attr_value) { + if !is_http_url(attr_value) { let href_full_url = resolve_url(&url, attr_value).unwrap_or_default(); attr.value.clear(); diff --git a/src/main.rs b/src/main.rs index 183545f..40928a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ mod macros; use crate::args::AppArgs; use monolith::html::{html_to_dom, stringify_document, walk_and_embed_assets}; use monolith::http::retrieve_asset; -use monolith::utils::is_valid_url; +use monolith::utils::is_http_url; use reqwest::blocking::Client; use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT}; use std::collections::HashMap; @@ -47,7 +47,7 @@ impl Output { fn main() { let app_args = AppArgs::get(); - if !is_valid_url(app_args.url_target.as_str()) { + if !is_http_url(app_args.url_target.as_str()) { eprintln!( "Only HTTP and HTTPS URLs are allowed but got: {}", &app_args.url_target diff --git a/src/tests/utils.rs b/src/tests/utils.rs index 94a33fc..335e7e4 100644 --- a/src/tests/utils.rs +++ b/src/tests/utils.rs @@ -1,5 +1,5 @@ use crate::utils::{ - clean_url, data_to_data_url, detect_mimetype, is_data_url, is_valid_url, resolve_url, + clean_url, data_to_data_url, detect_mimetype, is_data_url, is_http_url, resolve_url, url_has_protocol, }; use url::ParseError; @@ -71,16 +71,16 @@ fn test_url_has_protocol() { } #[test] -fn test_is_valid_url() { +fn test_is_http_url() { // succeeding - assert!(is_valid_url("https://www.rust-lang.org/")); - assert!(is_valid_url("http://kernel.org")); + assert!(is_http_url("https://www.rust-lang.org/")); + assert!(is_http_url("http://kernel.org")); // failing - assert!(!is_valid_url("//kernel.org")); - assert!(!is_valid_url("./index.html")); - assert!(!is_valid_url("some-local-page.htm")); - assert!(!is_valid_url("ftp://1.2.3.4/www/index.html")); - assert!(!is_valid_url( + assert!(!is_http_url("//kernel.org")); + assert!(!is_http_url("./index.html")); + assert!(!is_http_url("some-local-page.htm")); + assert!(!is_http_url("ftp://1.2.3.4/www/index.html")); + assert!(!is_http_url( "data:text/html;base64,V2VsY29tZSBUbyBUaGUgUGFydHksIDxiPlBhbDwvYj4h" )); } diff --git a/src/utils.rs b/src/utils.rs index 747cc74..d247a36 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -93,12 +93,12 @@ pub fn is_data_url>(url: T) -> Result { Url::parse(url.as_ref()).and_then(|u| Ok(u.scheme() == "data")) } -pub fn is_valid_url>(path: T) -> bool { +pub fn is_http_url>(path: T) -> bool { REGEX_URL.is_match(path.as_ref()) } pub fn resolve_url, U: AsRef>(from: T, to: U) -> Result { - let result = if is_valid_url(to.as_ref()) { + let result = if is_http_url(to.as_ref()) { to.as_ref().to_string() } else { Url::parse(from.as_ref())?