Skip to content

Commit

Permalink
Handle updating the hostname when the first path segment is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
theskim committed Nov 20, 2024
1 parent 21cada8 commit 8c6bac3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
9 changes: 8 additions & 1 deletion url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2129,7 +2129,7 @@ impl Url {
} else {
self.host_end
};
let suffix = self.slice(old_suffix_pos..).to_owned();
let mut suffix = self.slice(old_suffix_pos..).to_owned();
self.serialization.truncate(self.host_start as usize);
if !self.has_authority() {
debug_assert!(self.slice(self.scheme_end..self.host_start) == ":");
Expand All @@ -2143,6 +2143,13 @@ impl Url {
self.host_end = to_u32(self.serialization.len()).unwrap();
self.host = host.into();

// Adjust serialization to switch from host to empty segment
if suffix.starts_with("/.//") {
suffix.drain(.."/.".len());
// pathname should be "//p" not "p" given that the first segment was empty
self.path_start -= "//".len() as u32;
}

if let Some(new_port) = opt_new_port {
self.port = new_port;
if let Some(port) = new_port {
Expand Down
2 changes: 0 additions & 2 deletions url/tests/expected_failures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
<file:/.//p>
<http://example.net/path> set hostname to <example.com:8080>
<http://example.net:8080/path> set hostname to <example.com:>
<non-spec:/.//p> set hostname to <h>
<non-spec:/.//p> set hostname to <>
<foo:///some/path> set pathname to <>
<file:///var/log/system.log> set href to <http://0300.168.0xF0>
<file://monkey/> set pathname to <\\\\>
Expand Down
5 changes: 5 additions & 0 deletions url/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,7 @@ fn serde_error_message() {

#[test]
fn test_can_be_a_base_with_set_path() {
use url::quirks;
let mut url = Url::parse("web+demo:/").unwrap();
assert!(!url.cannot_be_a_base());

Expand All @@ -1396,6 +1397,10 @@ fn test_can_be_a_base_with_set_path() {
assert_eq!(segments, vec!["", "not-a-host"]);

assert_eq!(url.as_str(), "web+demo:/.//not-a-host");
quirks::set_hostname(&mut url, "test").unwrap();
assert_eq!(url.as_str(), "web+demo://test//not-a-host");
quirks::set_hostname(&mut url, "").unwrap();
assert_eq!(url.as_str(), "web+demo:////not-a-host");
}

#[test]
Expand Down

0 comments on commit 8c6bac3

Please sign in to comment.