Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the Cache-Control immutable directive #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
63 changes: 52 additions & 11 deletions src/common/cache_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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<Duration> {
self.max_age.map(Into::into)
Expand Down Expand Up @@ -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
}

Expand All @@ -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());
Expand Down Expand Up @@ -227,6 +261,9 @@ impl FromIterator<KnownDirective> 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);
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -322,6 +360,7 @@ enum Directive {
MinFresh(u64),

// response directives
Immutable,
MustRevalidate,
Public,
Private,
Expand All @@ -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",
Expand All @@ -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,
Expand Down