From ce6ca748929bfbaa8efb64d270fb3a68e0996ccb Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Wed, 25 May 2016 22:32:32 +0200 Subject: [PATCH] Have Url::from_file_path not generate an URL with an empty path. Fix #197. --- Cargo.toml | 2 +- src/lib.rs | 8 +++++++- tests/unit.rs | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d8c501dc..e0f6596a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "url" -version = "1.1.0" +version = "1.1.1" authors = ["The rust-url developers"] description = "URL library for Rust, based on the WHATWG URL Standard" diff --git a/src/lib.rs b/src/lib.rs index f1f487c2..7a38e07b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1243,11 +1243,17 @@ fn path_to_file_url_segments(path: &Path, serialization: &mut String) -> Result< if !path.is_absolute() { return Err(()) } + let mut empty = true; // skip the root component for component in path.components().skip(1) { + empty = false; serialization.push('/'); serialization.extend(percent_encode( - component.as_os_str().as_bytes(), PATH_SEGMENT_ENCODE_SET)) + component.as_os_str().as_bytes(), PATH_SEGMENT_ENCODE_SET)); + } + if empty { + // An URL’s path must not be empty. + serialization.push('/'); } Ok(()) } diff --git a/tests/unit.rs b/tests/unit.rs index 09df7efa..55906b8e 100644 --- a/tests/unit.rs +++ b/tests/unit.rs @@ -259,3 +259,12 @@ fn issue_61() { assert_eq!(url.port_or_known_default(), Some(443)); url.assert_invariants(); } + +#[test] +/// https://github.com/servo/rust-url/issues/197 +fn issue_197() { + let mut url = Url::from_file_path("/").unwrap(); + url.assert_invariants(); + assert_eq!(url, Url::parse("file:///").unwrap()); + url.path_segments_mut().unwrap().pop_if_empty(); +}