Skip to content

Commit

Permalink
Remove url and replace with urlencoding
Browse files Browse the repository at this point in the history
(Also, run `cargo fmt`)
  • Loading branch information
joshuamegnauth54 committed Jun 16, 2024
1 parent 15a15f8 commit 67fb256
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ coinit_speed_over_memory = []

[dependencies]
log = "0.4"
urlencoding = "2.1.3"

[dev-dependencies]
serial_test = { version = "2.0.0", default-features = false }
Expand All @@ -47,7 +46,7 @@ chrono = { version = "0.4.31", optional = true, default-features = false, featur
] }
libc = "0.2.149"
scopeguard = "1.2.0"
url = "2.4.1"
urlencoding = "2.1.3"
once_cell = "1.18.0"

[target.'cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))'.dependencies]
Expand Down
33 changes: 24 additions & 9 deletions src/freedesktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::{
ffi::{OsStrExt, OsStringExt},
fs::PermissionsExt,
},
path::{Path, PathBuf},
path::{Component, Path, PathBuf},
};

use log::{debug, warn};
Expand Down Expand Up @@ -595,13 +595,28 @@ fn decode_uri_path(path: impl AsRef<Path>) -> PathBuf {
// passed into the `url` functions.
// Simply parsing the path doesn't work because of the possibility of invalid Unicode.
// URL encoding the entire path doesn't work because back slashes will be encoded too
// Thus, the easiest way is to manually encode each segment of the path and recombine
// Thus, the easiest way is to manually decode each segment of the path and recombine
path.as_ref().iter().map(|part| OsString::from_vec(urlencoding::decode_binary(part.as_bytes()).to_vec())).collect()
}

fn encode_uri_path(absolute_file_path: impl AsRef<Path>) -> String {
let url = url::Url::from_file_path(absolute_file_path.as_ref()).unwrap();
url.path().to_owned()
fn encode_uri_path(path: impl AsRef<Path>) -> String {
// `iter()` cannot be used here because it yields '/' in certain situations, such as
// for root directories.
// Slashes would be encoded and thus mess up the path
let path: PathBuf = path
.as_ref()
.components()
.map(|component| {
// Only encode names and not '/', 'C:\', et cetera
if let Component::Normal(part) = component {
urlencoding::encode_binary(part.as_bytes()).to_string()
} else {
component.as_os_str().to_str().expect("Path components such as '/' are valid Unicode").to_owned()
}
})
.collect();

path.to_str().expect("URL encoded bytes is valid Unicode").to_owned()
}

#[derive(Eq, PartialEq, Debug)]
Expand Down Expand Up @@ -868,7 +883,7 @@ mod tests {
use std::{
collections::{hash_map::Entry, HashMap},
env,
ffi::{OsString, OsStr},
ffi::{OsStr, OsString},
fmt,
fs::File,
os::unix,
Expand Down Expand Up @@ -993,9 +1008,9 @@ mod tests {
// Add invalid UTF-8 byte
let mut bytes = base.into_encoded_bytes();
bytes.push(168);

// SAFETY:
// * OsString is produced in part from a valid OsStr
// * OsString is produced in part from a valid OsStr
// * OsString does not have to be valid Unicode
// * The string isn't written to disk or transferred anywhere where it may be read by a
// different Rust version than produced it
Expand All @@ -1008,7 +1023,7 @@ mod tests {
assert_eq!(fake.as_encoded_bytes(), path.as_os_str().as_encoded_bytes());

// Shouldn't panic
encode_uri_path(&path);
encode_uri_path(&path);
}

//////////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 67fb256

Please sign in to comment.