Skip to content

Commit 89def95

Browse files
committed
Handle file scheme type when setting host and hostname.
1 parent 2803c0d commit 89def95

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

src/parser.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -986,10 +986,32 @@ impl<'a> Parser<'a> {
986986
Ok((host, input))
987987
}
988988

989-
pub(crate) fn parse_file_host<'i>(
989+
pub fn get_file_host<'i>(input: Input<'i>) -> ParseResult<(Host<String>, Input)> {
990+
let (_, host_str, remaining) = Parser::file_host(input)?;
991+
let host = Host::parse(&host_str)?;
992+
Ok((host, remaining))
993+
}
994+
995+
fn parse_file_host<'i>(
990996
&mut self,
991997
input: Input<'i>,
992998
) -> ParseResult<(bool, HostInternal, Input<'i>)> {
999+
let (has_host, host_str, remaining) = Parser::file_host(input)?;
1000+
let host = if host_str.is_empty() {
1001+
HostInternal::None
1002+
} else {
1003+
match Host::parse(&host_str)? {
1004+
Host::Domain(ref d) if d == "localhost" => HostInternal::None,
1005+
host => {
1006+
write!(&mut self.serialization, "{}", host).unwrap();
1007+
host.into()
1008+
}
1009+
}
1010+
};
1011+
Ok((has_host, host, remaining))
1012+
}
1013+
1014+
pub fn file_host<'i>(input: Input<'i>) -> ParseResult<(bool, String, Input<'i>)> {
9931015
// Undo the Input abstraction here to avoid allocating in the common case
9941016
// where the host part of the input does not contain any tab or newline
9951017
let input_str = input.chars.as_str();
@@ -1018,20 +1040,9 @@ impl<'a> Parser<'a> {
10181040
}
10191041
}
10201042
if is_windows_drive_letter(host_str) {
1021-
return Ok((false, HostInternal::None, input));
1043+
return Ok((false, "".to_string(), input));
10221044
}
1023-
let host = if host_str.is_empty() {
1024-
HostInternal::None
1025-
} else {
1026-
match Host::parse(host_str)? {
1027-
Host::Domain(ref d) if d == "localhost" => HostInternal::None,
1028-
host => {
1029-
write!(&mut self.serialization, "{}", host).unwrap();
1030-
host.into()
1031-
}
1032-
}
1033-
};
1034-
Ok((true, host, remaining))
1045+
Ok((true, host_str.to_string(), remaining))
10351046
}
10361047

10371048
pub fn parse_port<P>(

src/quirks.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ pub fn set_host(url: &mut Url, new_host: &str) -> Result<(), ()> {
107107
let opt_port;
108108
{
109109
let scheme = url.scheme();
110-
let result = Parser::parse_host(Input::new(new_host), SchemeType::from(scheme));
110+
let scheme_type = SchemeType::from(scheme);
111+
let result = if scheme_type == SchemeType::File {
112+
Parser::get_file_host(Input::new(new_host))
113+
} else {
114+
Parser::parse_host(Input::new(new_host), scheme_type)
115+
};
111116
match result {
112117
Ok((h, remaining)) => {
113118
host = h;
@@ -155,7 +160,12 @@ pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
155160
if url.cannot_be_a_base() {
156161
return Err(());
157162
}
158-
let result = Parser::parse_host(Input::new(new_hostname), SchemeType::from(url.scheme()));
163+
let scheme_type = SchemeType::from(url.scheme());
164+
let result = if scheme_type == SchemeType::File {
165+
Parser::get_file_host(Input::new(new_hostname))
166+
} else {
167+
Parser::parse_host(Input::new(new_hostname), scheme_type)
168+
};
159169
if let Ok((host, _remaining)) = result {
160170
if let Host::Domain(h) = &host {
161171
if h.is_empty() {

0 commit comments

Comments
 (0)