Skip to content

Commit 21cada8

Browse files
committed
Fix mutating path of URL without authority (idempotency, empty path segments)
1 parent da64903 commit 21cada8

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

url/src/path_segments.rs

+40-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl<'a> PathSegmentsMut<'a> {
239239
I::Item: AsRef<str>,
240240
{
241241
let scheme_type = SchemeType::from(self.url.scheme());
242-
let path_start = self.url.path_start as usize;
242+
let mut path_start = self.url.path_start as usize;
243243
self.url.mutate(|parser| {
244244
parser.context = parser::Context::PathSegmentSetter;
245245
for segment in segments {
@@ -253,7 +253,44 @@ impl<'a> PathSegmentsMut<'a> {
253253
{
254254
parser.serialization.push('/');
255255
}
256-
let mut has_host = true; // FIXME account for this?
256+
257+
let mut path_empty = false;
258+
259+
// Check ':' and then see if the next character is '/'
260+
let mut has_host = if let Some(index) = parser.serialization.find(":") {
261+
if parser.serialization.len() > index + 1
262+
&& parser.serialization.as_bytes().get(index + 1) == Some(&b'/')
263+
{
264+
let rest = &parser.serialization[(index + ":/".len())..];
265+
let host_part = rest.split('/').next().unwrap_or("");
266+
path_empty = rest.is_empty();
267+
!host_part.is_empty() && !host_part.contains('@')
268+
} else {
269+
false
270+
}
271+
} else {
272+
false
273+
};
274+
275+
// For cases where normalization is applied across both the serialization and the path.
276+
// Append "/." immediately after the scheme (up to ":")
277+
// This is done if three conditions are met.
278+
// https://url.spec.whatwg.org/#url-serializing
279+
// 1. The host is null
280+
// 2. The url's path length is greater than 1
281+
// 3. the first segment of the URL's path is an empty string
282+
if !has_host && segment.len() > 1 && path_empty {
283+
if let Some(index) = parser.serialization.find(":") {
284+
if parser.serialization.len() == index + 2
285+
&& parser.serialization.as_bytes().get(index + 1) == Some(&b'/')
286+
{
287+
// Append an extra '/' to ensure that "/./path" becomes "/.//path"
288+
parser.serialization.insert_str(index + ":".len(), "/./");
289+
path_start += "/.".len();
290+
}
291+
}
292+
}
293+
257294
parser.parse_path(
258295
scheme_type,
259296
&mut has_host,
@@ -262,6 +299,7 @@ impl<'a> PathSegmentsMut<'a> {
262299
);
263300
}
264301
});
302+
self.url.path_start = path_start as u32;
265303
self
266304
}
267305
}

url/tests/unit.rs

+38
Original file line numberDiff line numberDiff line change
@@ -1379,3 +1379,41 @@ fn serde_error_message() {
13791379
r#"relative URL without a base: "§invalid#+#*Ä" at line 1 column 25"#
13801380
);
13811381
}
1382+
1383+
#[test]
1384+
fn test_can_be_a_base_with_set_path() {
1385+
let mut url = Url::parse("web+demo:/").unwrap();
1386+
assert!(!url.cannot_be_a_base());
1387+
1388+
url.set_path("//not-a-host");
1389+
assert_eq!(url.path(), "//not-a-host");
1390+
1391+
let segments: Vec<_> = url
1392+
.path_segments()
1393+
.expect("should have path segments")
1394+
.collect();
1395+
1396+
assert_eq!(segments, vec!["", "not-a-host"]);
1397+
1398+
assert_eq!(url.as_str(), "web+demo:/.//not-a-host");
1399+
}
1400+
1401+
#[test]
1402+
fn test_can_be_a_base_with_path_segments_mut() {
1403+
let mut url = Url::parse("web+demo:/").unwrap();
1404+
assert!(!url.cannot_be_a_base());
1405+
1406+
url.path_segments_mut()
1407+
.expect("should have path segments")
1408+
.push("")
1409+
.push("not-a-host");
1410+
1411+
assert_eq!(url.as_str(), "web+demo:/.//not-a-host");
1412+
assert_eq!(url.path(), "//not-a-host");
1413+
1414+
let segments: Vec<_> = url
1415+
.path_segments()
1416+
.expect("should have path segments")
1417+
.collect();
1418+
assert_eq!(segments, vec!["", "not-a-host"]);
1419+
}

0 commit comments

Comments
 (0)