Skip to content

Commit

Permalink
Add optional quotes to query values
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealLorenz committed Dec 2, 2023
1 parent 987b6fd commit e4c318d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions rq-core/src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ 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 }

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 }
Expand Down
20 changes: 20 additions & 0 deletions rq-core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ impl<'i> From<Pair<'i, Rule>> 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::<HashMap<String, String>>()
Expand Down Expand Up @@ -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");
}
}

0 comments on commit e4c318d

Please sign in to comment.