diff --git a/src/host.rs b/src/host.rs index 06ac7818..35a6b9fd 100644 --- a/src/host.rs +++ b/src/host.rs @@ -58,6 +58,9 @@ impl Host { ][..]).is_some() { return Err(ParseError::InvalidDomainCharacter) } + if domain.is_empty() { + return Err(ParseError::RelativeUrlWithoutBase) + } match parse_ipv4addr(&domain[..]) { Ok(Some(ipv4addr)) => Ok(Host::Ipv4(ipv4addr)), Ok(None) => Ok(Host::Domain(domain.to_ascii_lowercase())), diff --git a/tests/tests.rs b/tests/tests.rs index 45c403db..80c034e0 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -10,6 +10,7 @@ extern crate url; use std::net::{Ipv4Addr, Ipv6Addr}; use url::{Host, Url}; +use url::ParseError; #[test] fn new_file_paths() { @@ -80,6 +81,10 @@ fn new_directory_paths() { if cfg!(unix) { assert_eq!(Url::from_directory_path(Path::new("relative")), Err(())); assert_eq!(Url::from_directory_path(Path::new("../relative")), Err(())); + assert_eq!(Url::parse("relative"), Err(ParseError::RelativeUrlWithoutBase)); + assert_eq!(Url::parse("../relative"), Err(ParseError::RelativeUrlWithoutBase)); + assert_eq!(Url::parse("./relative"), Err(ParseError::RelativeUrlWithoutBase)); + assert_eq!(Url::parse("file://./foo"), Err(ParseError::RelativeUrlWithoutBase)); let url = Url::from_directory_path(Path::new("/foo/bar")).unwrap(); assert_eq!(url.host(), Some(&Host::Domain("".to_string())));