Skip to content

Commit

Permalink
Normalize URL paths: convert /.//p, /..//p, and //p to p
Browse files Browse the repository at this point in the history
  • Loading branch information
theskim committed Jun 18, 2024
1 parent 3d6dbbb commit d4bea43
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
26 changes: 26 additions & 0 deletions url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,32 @@ impl Url {
);
}
});

// To handle cases like <non-spec:/> set pathname to </..//p>
// For instance, //p should be converted to /..//p here
// At this point, we would get "non-spec://p" for serialization
// and "/..//p" for path
if let Some(path_pos) = path.rfind("//") {
if let Some(serialization_pos) = self.serialization.rfind("//") {
const PATH_INCREMENT: usize = 2; // length of "/."

if path_pos + PATH_INCREMENT <= path.len()
&& serialization_pos + PATH_INCREMENT <= self.serialization.len()
{
let rest_path = &path[(path_pos + PATH_INCREMENT)..];
let rest_serialization =
&self.serialization[(serialization_pos + PATH_INCREMENT)..];

// rest should be the same
if rest_path == rest_serialization {
self.serialization
.replace_range(serialization_pos.., &format!("/.//{}", rest_path));
self.path_start += PATH_INCREMENT as u32;
}
}
}
}

self.restore_after_path(old_after_path_pos, &after_path);
}

Expand Down
3 changes: 0 additions & 3 deletions url/tests/expected_failures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,4 @@
<file://monkey/> set pathname to <\\\\>
<file:///unicorn> set pathname to <//\\/>
<file:///unicorn> set pathname to <//monkey/..//>
<non-spec:/> set pathname to </.//p>
<non-spec:/> set pathname to </..//p>
<non-spec:/> set pathname to <//p>
<non-spec:/.//> set pathname to <p>

0 comments on commit d4bea43

Please sign in to comment.