From befc4cda44a4f1e26aa0a301dfc92b455cbcbf18 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 31 Aug 2019 22:37:48 +0200 Subject: Don't URL-decode path segments --- dhall_syntax/Cargo.toml | 2 +- dhall_syntax/src/parser.rs | 27 ++++++++++++++++++++++----- dhall_syntax/src/printer.rs | 22 +++++----------------- 3 files changed, 28 insertions(+), 23 deletions(-) (limited to 'dhall_syntax') diff --git a/dhall_syntax/Cargo.toml b/dhall_syntax/Cargo.toml index 08d0195..1da10c7 100644 --- a/dhall_syntax/Cargo.toml +++ b/dhall_syntax/Cargo.toml @@ -10,7 +10,7 @@ doctest = false [dependencies] itertools = "0.8.0" -percent-encoding = "1.0.1" +percent-encoding = "2.1.0" pest = "2.1" either = "1.5.2" take_mut = "0.2.2" diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index 24ecc1e..53fd68a 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -669,12 +669,29 @@ make_parser! { rule!(unquoted_path_component<&'a str>; captured_str!(s) => s); rule!(quoted_path_component<&'a str>; captured_str!(s) => s); rule!(path_component; children!( - [unquoted_path_component(s)] => { - percent_encoding::percent_decode(s.as_bytes()) - .decode_utf8_lossy() - .into_owned() + [unquoted_path_component(s)] => s.to_string(), + [quoted_path_component(s)] => { + const RESERVED: &percent_encoding::AsciiSet = + &percent_encoding::CONTROLS + .add(b'=').add(b':').add(b'/').add(b'?') + .add(b'#').add(b'[').add(b']').add(b'@') + .add(b'!').add(b'$').add(b'&').add(b'\'') + .add(b'(').add(b')').add(b'*').add(b'+') + .add(b',').add(b';'); + s.chars() + .map(|c| { + // Percent-encode ascii chars + if c.is_ascii() { + percent_encoding::utf8_percent_encode( + &c.to_string(), + RESERVED, + ).to_string() + } else { + c.to_string() + } + }) + .collect() }, - [quoted_path_component(s)] => s.to_string(), )); rule!(path>; children!( [path_component(components)..] => { diff --git a/dhall_syntax/src/printer.rs b/dhall_syntax/src/printer.rs index 8571d11..f3dc648 100644 --- a/dhall_syntax/src/printer.rs +++ b/dhall_syntax/src/printer.rs @@ -360,15 +360,9 @@ impl Display for Import { use FilePrefix::*; use ImportLocation::*; use ImportMode::*; - let fmt_remote_path_component = |s: &str| -> String { - use percent_encoding::{ - utf8_percent_encode, PATH_SEGMENT_ENCODE_SET, - }; - utf8_percent_encode(s, PATH_SEGMENT_ENCODE_SET).to_string() - }; - let fmt_local_path_component = |s: &str| -> String { + let quote_if_needed = |s: &str| -> String { if s.chars().all(|c| c.is_ascii_alphanumeric()) { - s.to_owned() + s.to_string() } else { format!("\"{}\"", s) } @@ -383,19 +377,13 @@ impl Display for Import { Absolute => "", }; write!(f, "{}/", prefix)?; - let path: String = path - .iter() - .map(|c| fmt_local_path_component(c.as_ref())) - .join("/"); + let path: String = + path.iter().map(|c| quote_if_needed(&*c)).join("/"); f.write_str(&path)?; } Remote(url) => { write!(f, "{}://{}/", url.scheme, url.authority,)?; - let path: String = url - .path - .iter() - .map(|c| fmt_remote_path_component(c.as_ref())) - .join("/"); + let path: String = url.path.iter().join("/"); f.write_str(&path)?; if let Some(q) = &url.query { write!(f, "?{}", q)? -- cgit v1.2.3