From 5d98b3522e8e6906acbf9b3a1e867f27577acc85 Mon Sep 17 00:00:00 2001 From: Sayan Nandan Date: Thu, 11 Mar 2021 12:27:36 +0530 Subject: [PATCH] Enable string escaping with quotes for skysh This commit uses a Regex match iterator along with a few replace operations to enable the parsing of quoted strings from arguments. Previously, we simply ran a `split_whitespace()` to get the parts of the ActionGroup, but now we're using this new Regex which enables arguments like: 'SET me "sayan spaced"' to be passed and validated. Signed-off-by: Sayan Nandan --- AUTHORS.md | 2 +- Cargo.lock | 1 + libsky/Cargo.toml | 3 ++- libsky/src/terrapipe.rs | 22 +++++++++++++++++----- server/src/queryengine/mod.rs | 1 - 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/AUTHORS.md b/AUTHORS.md index 9a1704c2..daf2436a 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -1,3 +1,3 @@ # The Authors -Whenever we refer to "The Skytable Authors", "The Sky Authors", "The SkybaseDB Authors", "The SDB Authors", "The TerrabaseDB Authors", "The TDB Authors", or simply "The Authors", we are referring to: +Whenever we refer to "The Skytable Authors", "The Sky Authors", "The Skybase Authors", "The SkybaseDB Authors", "The SDB Authors", "The TerrabaseDB Authors", "The TDB Authors", or simply "The Authors", we are referring to: - Sayan (ohsayan at outlook dot com) \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 6991124d..f720ebe2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -319,6 +319,7 @@ version = "0.5.0" dependencies = [ "bytes", "lazy_static", + "regex", "termcolor", ] diff --git a/libsky/Cargo.toml b/libsky/Cargo.toml index de72e929..3fa55186 100644 --- a/libsky/Cargo.toml +++ b/libsky/Cargo.toml @@ -9,4 +9,5 @@ edition = "2018" [dependencies] lazy_static = "1.4.0" bytes = "1.0.1" -termcolor = "1.1.2" \ No newline at end of file +termcolor = "1.1.2" +regex = "1.4.3" \ No newline at end of file diff --git a/libsky/src/terrapipe.rs b/libsky/src/terrapipe.rs index 488c95fa..3031c04a 100644 --- a/libsky/src/terrapipe.rs +++ b/libsky/src/terrapipe.rs @@ -29,6 +29,11 @@ //! pub const ADDR: &'static str = "127.0.0.1"; +use std::str::FromStr; + +lazy_static::lazy_static! { + static ref RE: regex::Regex = regex::Regex::from_str(r#"("[^"]*"|'[^']*'|[\S]+)+"#).unwrap(); +} /// Response codes returned by the server #[derive(Debug, PartialEq)] @@ -130,11 +135,11 @@ pub fn proc_query(querystr: T) -> Vec where T: AsRef, { - // TODO(@ohsayan): Enable "" to be escaped - // let args: Vec<&str> = RE.find_iter(&querystr).map(|val| val.as_str()).collect(); - let querystr = querystr.as_ref().to_owned(); - let args: Vec<&str> = querystr.split_whitespace().collect(); - let mut bytes = Vec::with_capacity(querystr.len()); + let mut bytes = Vec::with_capacity(querystr.as_ref().len()); + let args: Vec = RE + .find_iter(&querystr.as_ref()) + .map(|val| val.as_str().replace("'", "").replace("\"", "").to_owned()) + .collect(); bytes.extend(b"#2\n*1\n#"); let arg_len_bytes = args.len().to_string().into_bytes(); let arg_len_bytes_len = (arg_len_bytes.len() + 1).to_string().into_bytes(); @@ -162,4 +167,11 @@ fn test_queryproc() { .to_owned(), proc_query(query) ); + let q_escaped = proc_query(r#"SET X 'sayan with spaces'"#); + assert_eq!( + "#2\n*1\n#2\n&3\n#3\nSET\n#1\nX\n#17\nsayan with spaces\n" + .as_bytes() + .to_owned(), + q_escaped + ); } diff --git a/server/src/queryengine/mod.rs b/server/src/queryengine/mod.rs index 09b690c5..dc4185df 100644 --- a/server/src/queryengine/mod.rs +++ b/server/src/queryengine/mod.rs @@ -72,7 +72,6 @@ mod tags { /// `MKSNAP` action tag pub const TAG_MKSNAP: &'static str = "MKSNAP"; } - /// Execute a simple(*) query pub async fn execute_simple(db: &CoreDB, con: &mut Con<'_>, buf: ActionGroup) -> TResult<()> { let first = match buf.get_first() {