From a269edfcc42291b19d7a101cfae472c975da17f4 Mon Sep 17 00:00:00 2001 From: Daniel Robertson Date: Sun, 21 Feb 2016 02:36:03 +0000 Subject: [PATCH] Do not panic when parsing '.' in URL host: #166 When parsing a url such as file://./foo the parser should not panic. --- src/host.rs | 3 +++ tests/tests.rs | 5 +++++ 2 files changed, 8 insertions(+) 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())));