Apply percent-decoding to file-based URLs (#1541)

## Summary

Closes https://github.com/astral-sh/uv/issues/1537.
This commit is contained in:
Charlie Marsh 2024-02-16 16:11:16 -05:00 committed by GitHub
parent a97c207674
commit 01ffc36520
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 27 additions and 2 deletions

View file

@ -21,6 +21,7 @@ fs2 = { workspace = true }
junction = { workspace = true }
tempfile = { workspace = true }
tracing = { workspace = true }
urlencoding = { workspace = true }
[features]
default = []

View file

@ -31,14 +31,18 @@ impl<T: AsRef<Path>> Normalized for T {
///
/// On other platforms, this is a no-op.
pub fn normalize_url_path(path: &str) -> Cow<'_, str> {
// Apply percent-decoding to the URL.
let path = urlencoding::decode(path).unwrap_or(Cow::Borrowed(path));
// Return the path.
if cfg!(windows) {
Cow::Owned(
path.strip_prefix('/')
.unwrap_or(path)
.unwrap_or(&path)
.replace('/', std::path::MAIN_SEPARATOR_STR),
)
} else {
Cow::Borrowed(path)
path
}
}
@ -71,5 +75,17 @@ mod tests {
"./ferris/wheel-0.42.0.tar.gz"
);
}
if cfg!(windows) {
assert_eq!(
normalize_url_path("./wheel%20cache/wheel-0.42.0.tar.gz"),
".\\wheel cache\\wheel-0.42.0.tar.gz"
);
} else {
assert_eq!(
normalize_url_path("./wheel%20cache/wheel-0.42.0.tar.gz"),
"./wheel cache/wheel-0.42.0.tar.gz"
);
}
}
}