Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Url::from_file_path() incorrect handling of backslash on linux #901

Closed
nayeemrmn opened this issue Jan 31, 2024 · 0 comments · Fixed by #902
Closed

Url::from_file_path() incorrect handling of backslash on linux #901

nayeemrmn opened this issue Jan 31, 2024 · 0 comments · Fixed by #902

Comments

@nayeemrmn
Copy link

nayeemrmn commented Jan 31, 2024

Also affects Url::from_directory_path().

Backslashes in paths get left as-is. They should be percent-encoded to %5C. Currently that backslash would change to a forward-slash if the output were to be stringified and re-parsed with Url::parse(). See the following roundtrip:

fn main() {
  let path = std::path::PathBuf::from("/a/b/c\\d");
  let url = url::Url::from_file_path(&path).unwrap();
  println!("{}", url.as_str()); // file:///a/b/c\d
  let url2 = url::Url::parse(url.as_str()).unwrap();
  println!("{}", url2.as_str()); // file:///a/b/c/d
  let path2 = url2.to_file_path().unwrap();
  println!("{}", path2.display()); // /a/b/c/d
  //                         Expected /a/b/c\d
}

Expected:

fn main() {
  let path = std::path::PathBuf::from("/a/b/c\\d");
  let url = url::Url::from_file_path(&path).unwrap();
  println!("{}", url.as_str()); // file:///a/b/c%5Cd
  let url2 = url::Url::parse(url.as_str()).unwrap();
  println!("{}", url2.as_str()); // file:///a/b/c%5Cd
  let path2 = url2.to_file_path().unwrap();
  println!("{}", path2.display()); // /a/b/c\d
}

On Windows they get swapped to forward-slashes, which is correct for Windows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant