Allow double quoted cookie value in parse

RFC6265 defines cookie value as
cookie-value      = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE )

This fix unquotes cookie value if it's double quoted so that it can
allow either format of cookie value.
This commit is contained in:
ichyo 2019-10-01 00:00:44 +09:00
parent 5169d306ae
commit 8218f10989
1 changed files with 16 additions and 0 deletions

View File

@ -86,6 +86,14 @@ fn name_val_decoded(
Ok((name, val))
}
fn unquote_value(val: &str) -> &str {
if val.len() >= 2 && val.starts_with('"') && val.ends_with('"') {
&val[1..val.len() - 1]
} else {
val
}
}
// This function does the real parsing but _does not_ set the `cookie_string` in
// the returned cookie object. This only exists so that the borrow to `s` is
// returned at the end of the call, allowing the `cookie_string` field to be
@ -107,6 +115,8 @@ fn parse_inner<'c>(s: &str, decode: bool) -> Result<Cookie<'c>, ParseError> {
return Err(ParseError::EmptyName);
}
let value = unquote_value(value);
// Create a cookie with all of the defaults. We'll fill things in while we
// iterate through the parameters below.
let (name, value) = if decode {
@ -399,6 +409,12 @@ mod tests {
);
}
#[test]
fn parse_double_quoted_value() {
let expected = Cookie::new("foo", "bar");
assert_eq_parse!("foo=\"bar\"", expected);
}
#[test]
fn odd_characters() {
let expected = Cookie::new("foo", "b%2Fr");