From e4c318d287a24fb94f6ada2911902ef6776e5ca1 Mon Sep 17 00:00:00 2001 From: TheRealLorenz Date: Sat, 2 Dec 2023 12:25:06 +0100 Subject: [PATCH] Add optional quotes to query values --- rq-core/src/grammar.pest | 4 ++-- rq-core/src/parser.rs | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/rq-core/src/grammar.pest b/rq-core/src/grammar.pest index af2045b..9b38a2f 100644 --- a/rq-core/src/grammar.pest +++ b/rq-core/src/grammar.pest @@ -8,7 +8,7 @@ request = { } request_line = _{ (method ~ " "+)? ~ uri ~ query? ~ (" "+ ~ version)? ~ NEWLINE } -uri = { (!(whitespace| "?") ~ ANY)+ } +uri = { (!(whitespace | "?") ~ ANY)+ } method = { ("GET" | "DELETE" | "POST" | "PUT") } version = { "HTTP/" ~ ("0.9" | "1.0" | "1.1" | "2.0" | "3.0") } whitespace = _{ " " | "\t" | NEWLINE } @@ -16,7 +16,7 @@ whitespace = _{ " " | "\t" | NEWLINE } query = { "?" ~ query_item ~ ("&" ~ query_item)* } query_item = { query_name ~ "=" ~ query_value } query_name = { (!(NEWLINE | "=") ~ ANY)+ } -query_value = { (!(NEWLINE | "&") ~ ANY)+ } +query_value = { (PUSH("'" | "\"") ~ (!PEEK ~ ANY)* ~ POP) | (!(NEWLINE | "&" | " ") ~ ANY)+ } headers = { header+ } header = { header_name ~ ":" ~ whitespace ~ header_value ~ NEWLINE } diff --git a/rq-core/src/parser.rs b/rq-core/src/parser.rs index 2d2d2c6..400f094 100644 --- a/rq-core/src/parser.rs +++ b/rq-core/src/parser.rs @@ -92,6 +92,12 @@ impl<'i> From> for HttpRequest { let key = pairs.next().unwrap().as_str().to_string(); let value = pairs.next().unwrap().as_str().to_string(); + for c in ['\'', '"'] { + if value.starts_with(c) && value.ends_with(c) { + return (key, value.trim_matches(c).to_string()); + } + } + (key, value) }) .collect::>() @@ -279,4 +285,18 @@ authorization: token assert_eq!(file.requests[0].query.get("foo").unwrap(), "bar"); assert_eq!(file.requests[0].query.get("baz").unwrap(), "2"); } + + #[test] + fn test_query_params_with_quotes() { + let input = r#" +POST test.dev?foo=" bar"&baz=' &ciao' HTTP/1.0 +authorization: token + +"#; + let file = assert_parses(input); + assert_eq!(file.requests.len(), 1); + assert_eq!(file.requests[0].query.len(), 2); + assert_eq!(file.requests[0].query.get("foo").unwrap(), " bar"); + assert_eq!(file.requests[0].query.get("baz").unwrap(), " &ciao"); + } }