Skip to content

Commit

Permalink
Merge pull request #86 from erickt/panic
Browse files Browse the repository at this point in the history
Don't panic if passed in a large max age.
  • Loading branch information
SergioBenitez authored May 7, 2017
2 parents 32d74f8 + ee18b79 commit 9587854
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::cmp;
use std::error::Error;
use std::ascii::AsciiExt;
use std::str::Utf8Error;
Expand Down Expand Up @@ -152,7 +153,12 @@ fn parse_inner<'c>(s: &str, decode: bool) -> Result<Cookie<'c>, ParseError> {
// max age as 0 seconds.
cookie.max_age = match v.parse() {
Ok(val) if val <= 0 => Some(Duration::zero()),
Ok(val) => Some(Duration::seconds(val)),
Ok(val) => {
// Don't panic if the max age seconds is greater than what's supported by
// `Duration`.
let val = cmp::min(val, Duration::max_value().num_seconds());
Some(Duration::seconds(val))
}
Err(_) => continue,
};
}
Expand Down Expand Up @@ -392,4 +398,13 @@ mod tests {

assert_eq!(cookie, expected);
}

#[test]
fn do_not_panic_on_large_max_ages() {
let max_seconds = Duration::max_value().num_seconds();
let expected = Cookie::build("foo", "bar")
.max_age(Duration::seconds(max_seconds))
.finish();
assert_eq_parse!(format!(" foo=bar; Max-Age={:?}", max_seconds + 1), expected);
}
}

0 comments on commit 9587854

Please sign in to comment.