diff options
author | Nadrieril | 2019-08-31 22:37:48 +0200 |
---|---|---|
committer | Nadrieril | 2019-08-31 22:37:48 +0200 |
commit | befc4cda44a4f1e26aa0a301dfc92b455cbcbf18 (patch) | |
tree | e7866fde1008271c41f3f35fc48ae918a74f67d0 /dhall_syntax | |
parent | 774d1affe13b50945517ccc43f9d4e771841e4a2 (diff) |
Don't URL-decode path segments
Diffstat (limited to '')
-rw-r--r-- | dhall_syntax/Cargo.toml | 2 | ||||
-rw-r--r-- | dhall_syntax/src/parser.rs | 27 | ||||
-rw-r--r-- | dhall_syntax/src/printer.rs | 22 |
3 files changed, 28 insertions, 23 deletions
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<String>; 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<Vec<String>>; 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<SubExpr: Display> Display for Import<SubExpr> { 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<SubExpr: Display> Display for Import<SubExpr> { 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)? |