correct is_valid_url to is_http_url

This commit is contained in:
Sunshine 2020-02-12 01:59:21 -05:00
parent 5c8d75539b
commit 928664dc88
No known key found for this signature in database
GPG key ID: B80CA68703CD8AB1
4 changed files with 15 additions and 15 deletions

View file

@ -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();

View file

@ -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

View file

@ -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"
));
}

View file

@ -93,12 +93,12 @@ pub fn is_data_url<T: AsRef<str>>(url: T) -> Result<bool, ParseError> {
Url::parse(url.as_ref()).and_then(|u| Ok(u.scheme() == "data"))
}
pub fn is_valid_url<T: AsRef<str>>(path: T) -> bool {
pub fn is_http_url<T: AsRef<str>>(path: T) -> bool {
REGEX_URL.is_match(path.as_ref())
}
pub fn resolve_url<T: AsRef<str>, U: AsRef<str>>(from: T, to: U) -> Result<String, ParseError> {
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())?