Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/common/cache_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl Flags {
const PRIVATE: Self = Self { bits: 0b001000000 };
const PROXY_REVALIDATE: Self = Self { bits: 0b010000000 };
const IMMUTABLE: Self = Self { bits: 0b100000000 };
const MUST_UNDERSTAND: Self = Self { bits: 0b1000000000 };

fn empty() -> Self {
Self { bits: 0 }
Expand Down Expand Up @@ -121,6 +122,10 @@ impl CacheControl {
pub fn immutable(&self) -> bool {
self.flags.contains(Flags::IMMUTABLE)
}
/// Check if the `must_understand` directive is set.
pub fn must_understand(&self) -> bool {
self.flags.contains(Flags::MUST_UNDERSTAND)
}

/// Get the value of the `max-age` directive if set.
pub fn max_age(&self) -> Option<Duration> {
Expand Down Expand Up @@ -186,6 +191,11 @@ impl CacheControl {
self
}

/// Set the `must_understand` directive.
pub fn with_must_understand(mut self) -> Self {
self.flags.insert(Flags::MUST_UNDERSTAND);
self
}
/// Set the `max-age` directive.
pub fn with_max_age(mut self, duration: Duration) -> Self {
self.max_age = Some(duration.into());
Expand Down Expand Up @@ -258,6 +268,9 @@ impl FromIterator<KnownDirective> for FromIter {
Directive::MustRevalidate => {
cc.flags.insert(Flags::MUST_REVALIDATE);
}
Directive::MustUnderstand => {
cc.flags.insert(Flags::MUST_UNDERSTAND);
}
Directive::Public => {
cc.flags.insert(Flags::PUBLIC);
}
Expand Down Expand Up @@ -310,6 +323,7 @@ impl<'a> fmt::Display for Fmt<'a> {
if_flag(Flags::PUBLIC, Directive::Public),
if_flag(Flags::PRIVATE, Directive::Private),
if_flag(Flags::IMMUTABLE, Directive::Immutable),
if_flag(Flags::MUST_UNDERSTAND, Directive::MustUnderstand),
if_flag(Flags::PROXY_REVALIDATE, Directive::ProxyRevalidate),
self.0
.max_age
Expand Down Expand Up @@ -355,6 +369,7 @@ enum Directive {

// response directives
MustRevalidate,
MustUnderstand,
Public,
Private,
Immutable,
Expand All @@ -376,6 +391,7 @@ impl fmt::Display for Directive {
Directive::MinFresh(secs) => return write!(f, "min-fresh={}", secs),

Directive::MustRevalidate => "must-revalidate",
Directive::MustUnderstand => "must-understand",
Directive::Public => "public",
Directive::Private => "private",
Directive::Immutable => "immutable",
Expand All @@ -399,6 +415,7 @@ impl FromStr for KnownDirective {
"public" => Directive::Public,
"private" => Directive::Private,
"immutable" => Directive::Immutable,
"must-understand" => Directive::MustUnderstand,
"proxy-revalidate" => Directive::ProxyRevalidate,
"" => return Err(()),
_ => match s.find('=') {
Expand Down Expand Up @@ -472,6 +489,18 @@ mod tests {
assert!(cc.immutable());
}

#[test]
fn test_must_understand() {
let cc = CacheControl::new().with_must_understand();
let headers = test_encode(cc.clone());
assert_eq!(headers["cache-control"], "must-understand");
assert_eq!(
test_decode::<CacheControl>(&["must-understand"]).unwrap(),
cc
);
assert!(cc.must_understand());
}

#[test]
fn test_parse_bad_syntax() {
assert_eq!(test_decode::<CacheControl>(&["max-age=lolz"]), None);
Expand Down