From 19ebc6ce05c68b24225aac87317077407cf4ca45 Mon Sep 17 00:00:00 2001 From: GabrielDertoni Date: Mon, 11 Dec 2023 18:26:18 -0300 Subject: [PATCH] Fix incorrect parsing of Windows drive letter quirk (#889) * Fixes the parser to correctly parse "file:///C|/hello/world" into "file:///C:/hello/world" --- url/src/parser.rs | 11 +++++------ url/tests/unit.rs | 6 ++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/url/src/parser.rs b/url/src/parser.rs index 7d94d1d71..693c9f12d 100644 --- a/url/src/parser.rs +++ b/url/src/parser.rs @@ -1038,25 +1038,24 @@ impl<'a> Parser<'a> { &mut self, input: Input<'i>, ) -> ParseResult<(bool, HostInternal, Input<'i>)> { - let has_host; - let (_, host_str, remaining) = Parser::file_host(input)?; + let (has_host, host_str, remaining) = Parser::file_host(input)?; + if !has_host { + return Ok((false, HostInternal::None, remaining)); + } let host = if host_str.is_empty() { - has_host = false; HostInternal::None } else { match Host::parse(&host_str)? { Host::Domain(ref d) if d == "localhost" => { - has_host = false; HostInternal::None } host => { write!(&mut self.serialization, "{}", host).unwrap(); - has_host = true; host.into() } } }; - Ok((has_host, host, remaining)) + Ok((true, host, remaining)) } pub fn file_host(input: Input) -> ParseResult<(bool, String, Input)> { diff --git a/url/tests/unit.rs b/url/tests/unit.rs index c27f237ba..82ec422d8 100644 --- a/url/tests/unit.rs +++ b/url/tests/unit.rs @@ -1306,3 +1306,9 @@ fn issue_864() { url.set_path("x"); dbg!(&url); } + +#[test] +fn issue_889() { + let u = Url::parse("file:///C|/hello/world").unwrap(); + assert_eq!(u.as_str(), "file:///C:/hello/world"); +}