From 57140dc828fc227c44b1acbf8c5eeda2d43bc0b8 Mon Sep 17 00:00:00 2001 From: Dominik Nakamura Date: Thu, 14 Oct 2021 18:59:14 +0900 Subject: [PATCH] Add the Cache-Control immutable directive --- src/common/cache_control.rs | 63 ++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/src/common/cache_control.rs b/src/common/cache_control.rs index 305361d3..00d656c4 100644 --- a/src/common/cache_control.rs +++ b/src/common/cache_control.rs @@ -45,14 +45,15 @@ pub struct CacheControl { bitflags! { struct Flags: u32 { - const NO_CACHE = 0b00000001; - const NO_STORE = 0b00000010; - const NO_TRANSFORM = 0b00000100; - const ONLY_IF_CACHED = 0b00001000; - const MUST_REVALIDATE = 0b00010000; - const PUBLIC = 0b00100000; - const PRIVATE = 0b01000000; - const PROXY_REVALIDATE = 0b10000000; + const NO_CACHE = 0b00000000_00000001; + const NO_STORE = 0b00000000_00000010; + const NO_TRANSFORM = 0b00000000_00000100; + const ONLY_IF_CACHED = 0b00000000_00001000; + const MUST_REVALIDATE = 0b00000000_00010000; + const PUBLIC = 0b00000000_00100000; + const PRIVATE = 0b00000000_01000000; + const PROXY_REVALIDATE = 0b00000000_10000000; + const IMMUTABLE = 0b00000001_00000000; } } @@ -90,6 +91,16 @@ impl CacheControl { self.flags.contains(Flags::ONLY_IF_CACHED) } + /// Check if the `immutable` directive is set. + pub fn immutable(&self) -> bool { + self.flags.contains(Flags::IMMUTABLE) + } + + /// Check if the `must-revalidate` directive is set. + pub fn must_revalidate(&self) -> bool { + self.flags.contains(Flags::MUST_REVALIDATE) + } + /// Check if the `public` directive is set. pub fn public(&self) -> bool { self.flags.contains(Flags::PUBLIC) @@ -100,6 +111,11 @@ impl CacheControl { self.flags.contains(Flags::PRIVATE) } + /// Check if the `proxy-revalidate` directive is set. + pub fn proxy_revalidate(&self) -> bool { + self.flags.contains(Flags::PROXY_REVALIDATE) + } + /// Get the value of the `max-age` directive if set. pub fn max_age(&self) -> Option { self.max_age.map(Into::into) @@ -146,9 +162,15 @@ impl CacheControl { self } - /// Set the `private` directive. - pub fn with_private(mut self) -> Self { - self.flags.insert(Flags::PRIVATE); + /// Set the `immutable` directive. + pub fn with_immutable(mut self) -> Self { + self.flags.insert(Flags::IMMUTABLE); + self + } + + /// Set the `must-revalidate` directive. + pub fn with_must_revalidate(mut self) -> Self { + self.flags.insert(Flags::MUST_REVALIDATE); self } @@ -158,6 +180,18 @@ impl CacheControl { self } + /// Set the `private` directive. + pub fn with_private(mut self) -> Self { + self.flags.insert(Flags::PRIVATE); + self + } + + /// Set the `proxy-revalidate` directive. + pub fn with_proxy_revalidate(mut self) -> Self { + self.flags.insert(Flags::PROXY_REVALIDATE); + self + } + /// Set the `max-age` directive. pub fn with_max_age(mut self, seconds: Duration) -> Self { self.max_age = Some(seconds.into()); @@ -227,6 +261,9 @@ impl FromIterator for FromIter { Directive::OnlyIfCached => { cc.flags.insert(Flags::ONLY_IF_CACHED); } + Directive::Immutable => { + cc.flags.insert(Flags::IMMUTABLE); + } Directive::MustRevalidate => { cc.flags.insert(Flags::MUST_REVALIDATE); } @@ -275,6 +312,7 @@ impl<'a> fmt::Display for Fmt<'a> { if_flag(Flags::NO_STORE, Directive::NoStore), if_flag(Flags::NO_TRANSFORM, Directive::NoTransform), if_flag(Flags::ONLY_IF_CACHED, Directive::OnlyIfCached), + if_flag(Flags::IMMUTABLE, Directive::Immutable), if_flag(Flags::MUST_REVALIDATE, Directive::MustRevalidate), if_flag(Flags::PUBLIC, Directive::Public), if_flag(Flags::PRIVATE, Directive::Private), @@ -322,6 +360,7 @@ enum Directive { MinFresh(u64), // response directives + Immutable, MustRevalidate, Public, Private, @@ -342,6 +381,7 @@ impl fmt::Display for Directive { Directive::MaxStale(secs) => return write!(f, "max-stale={}", secs), Directive::MinFresh(secs) => return write!(f, "min-fresh={}", secs), + Directive::Immutable => "immutable", Directive::MustRevalidate => "must-revalidate", Directive::Public => "public", Directive::Private => "private", @@ -361,6 +401,7 @@ impl FromStr for KnownDirective { "no-store" => Directive::NoStore, "no-transform" => Directive::NoTransform, "only-if-cached" => Directive::OnlyIfCached, + "immutable" => Directive::Immutable, "must-revalidate" => Directive::MustRevalidate, "public" => Directive::Public, "private" => Directive::Private,