Skip to content

Commit a4b56c0

Browse files
committed
Handle file scheme type when setting host and hostname.
1 parent 6b50daa commit a4b56c0

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
@@ -968,10 +968,32 @@ impl<'a> Parser<'a> {
968968
Ok((host, input))
969969
}
970970

971-
pub fn parse_file_host<'i>(
971+
pub fn get_file_host<'i>(input: Input<'i>) -> ParseResult<(Host<String>, Input)> {
972+
let (_, host_str, remaining) = Parser::file_host(input)?;
973+
let host = Host::parse(&host_str)?;
974+
Ok((host, remaining))
975+
}
976+
977+
fn parse_file_host<'i>(
972978
&mut self,
973979
input: Input<'i>,
974980
) -> ParseResult<(bool, HostInternal, Input<'i>)> {
981+
let (has_host, host_str, remaining) = Parser::file_host(input)?;
982+
let host = if host_str.is_empty() {
983+
HostInternal::None
984+
} else {
985+
match Host::parse(&host_str)? {
986+
Host::Domain(ref d) if d == "localhost" => HostInternal::None,
987+
host => {
988+
write!(&mut self.serialization, "{}", host).unwrap();
989+
host.into()
990+
}
991+
}
992+
};
993+
Ok((has_host, host, remaining))
994+
}
995+
996+
pub fn file_host<'i>(input: Input<'i>) -> ParseResult<(bool, String, Input<'i>)> {
975997
// Undo the Input abstraction here to avoid allocating in the common case
976998
// where the host part of the input does not contain any tab or newline
977999
let input_str = input.chars.as_str();
@@ -1000,20 +1022,9 @@ impl<'a> Parser<'a> {
10001022
}
10011023
}
10021024
if is_windows_drive_letter(host_str) {
1003-
return Ok((false, HostInternal::None, input));
1025+
return Ok((false, "".to_string(), input));
10041026
}
1005-
let host = if host_str.is_empty() {
1006-
HostInternal::None
1007-
} else {
1008-
match Host::parse(host_str)? {
1009-
Host::Domain(ref d) if d == "localhost" => HostInternal::None,
1010-
host => {
1011-
write!(&mut self.serialization, "{}", host).unwrap();
1012-
host.into()
1013-
}
1014-
}
1015-
};
1016-
Ok((true, host, remaining))
1027+
Ok((true, host_str.to_string(), remaining))
10171028
}
10181029

10191030
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)