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 <nandansayan@outlook.com>
next
Sayan Nandan 4 years ago
parent e051a0376b
commit 5d98b3522e
No known key found for this signature in database
GPG Key ID: C31EFD7DDA12AEE0

@ -1,3 +1,3 @@
# The Authors # 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) - Sayan (ohsayan at outlook dot com)

1
Cargo.lock generated

@ -319,6 +319,7 @@ version = "0.5.0"
dependencies = [ dependencies = [
"bytes", "bytes",
"lazy_static", "lazy_static",
"regex",
"termcolor", "termcolor",
] ]

@ -10,3 +10,4 @@ edition = "2018"
lazy_static = "1.4.0" lazy_static = "1.4.0"
bytes = "1.0.1" bytes = "1.0.1"
termcolor = "1.1.2" termcolor = "1.1.2"
regex = "1.4.3"

@ -29,6 +29,11 @@
//! //!
pub const ADDR: &'static str = "127.0.0.1"; 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 /// Response codes returned by the server
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -130,11 +135,11 @@ pub fn proc_query<T>(querystr: T) -> Vec<u8>
where where
T: AsRef<str>, T: AsRef<str>,
{ {
// TODO(@ohsayan): Enable "" to be escaped let mut bytes = Vec::with_capacity(querystr.as_ref().len());
// let args: Vec<&str> = RE.find_iter(&querystr).map(|val| val.as_str()).collect(); let args: Vec<String> = RE
let querystr = querystr.as_ref().to_owned(); .find_iter(&querystr.as_ref())
let args: Vec<&str> = querystr.split_whitespace().collect(); .map(|val| val.as_str().replace("'", "").replace("\"", "").to_owned())
let mut bytes = Vec::with_capacity(querystr.len()); .collect();
bytes.extend(b"#2\n*1\n#"); bytes.extend(b"#2\n*1\n#");
let arg_len_bytes = args.len().to_string().into_bytes(); let arg_len_bytes = args.len().to_string().into_bytes();
let arg_len_bytes_len = (arg_len_bytes.len() + 1).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(), .to_owned(),
proc_query(query) 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
);
} }

@ -72,7 +72,6 @@ mod tags {
/// `MKSNAP` action tag /// `MKSNAP` action tag
pub const TAG_MKSNAP: &'static str = "MKSNAP"; pub const TAG_MKSNAP: &'static str = "MKSNAP";
} }
/// Execute a simple(*) query /// Execute a simple(*) query
pub async fn execute_simple(db: &CoreDB, con: &mut Con<'_>, buf: ActionGroup) -> TResult<()> { pub async fn execute_simple(db: &CoreDB, con: &mut Con<'_>, buf: ActionGroup) -> TResult<()> {
let first = match buf.get_first() { let first = match buf.get_first() {

Loading…
Cancel
Save