132 lines
3.3 KiB
Rust
132 lines
3.3 KiB
Rust
use crate::cache::{RequestPolicy, Settings};
|
|
use http::{HeaderMap, Method};
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
pub(super) enum CachePlan {
|
|
Disabled(&'static str),
|
|
Enabled {
|
|
force_cache: bool,
|
|
ignore_cookie: bool,
|
|
vcl_cookie_lookup: bool,
|
|
},
|
|
}
|
|
|
|
impl CachePlan {
|
|
pub(super) fn new(
|
|
disabled_reason: Option<&'static str>,
|
|
settings: &Settings,
|
|
path: &str,
|
|
force_match: impl FnOnce() -> bool,
|
|
) -> Self {
|
|
if let Some(reason) = disabled_reason {
|
|
return Self::Disabled(reason);
|
|
}
|
|
let force_cache = force_match();
|
|
Self::Enabled {
|
|
force_cache,
|
|
ignore_cookie: !force_cache && settings.ignore_cookie_for_path(path),
|
|
vcl_cookie_lookup: false,
|
|
}
|
|
}
|
|
|
|
pub(super) fn with_vcl_cookie_lookup(mut self, allowed: bool) -> Self {
|
|
if let Self::Enabled {
|
|
vcl_cookie_lookup, ..
|
|
} = &mut self
|
|
{
|
|
*vcl_cookie_lookup = allowed;
|
|
}
|
|
self
|
|
}
|
|
|
|
pub(super) fn vcl_cookie_lookup(self) -> bool {
|
|
matches!(
|
|
self,
|
|
Self::Enabled {
|
|
vcl_cookie_lookup: true,
|
|
..
|
|
}
|
|
)
|
|
}
|
|
|
|
pub(super) fn force_cache(self) -> bool {
|
|
matches!(
|
|
self,
|
|
Self::Enabled {
|
|
force_cache: true,
|
|
..
|
|
}
|
|
)
|
|
}
|
|
|
|
pub(super) fn ignore_cookie(self) -> bool {
|
|
matches!(
|
|
self,
|
|
Self::Enabled {
|
|
ignore_cookie: true,
|
|
..
|
|
}
|
|
)
|
|
}
|
|
|
|
pub(super) fn request_policy(
|
|
self,
|
|
method: &Method,
|
|
headers: &HeaderMap,
|
|
body_empty: bool,
|
|
path: &str,
|
|
has_query: bool,
|
|
settings: &Settings,
|
|
) -> RequestPolicy {
|
|
match self {
|
|
Self::Disabled(reason) => RequestPolicy::Bypass(reason),
|
|
Self::Enabled {
|
|
force_cache: true, ..
|
|
} => crate::cache::forced_request_policy(method, headers, body_empty),
|
|
Self::Enabled {
|
|
vcl_cookie_lookup, ..
|
|
} => crate::cache::request_policy_with_vcl_cookie(
|
|
method,
|
|
headers,
|
|
body_empty,
|
|
path,
|
|
has_query,
|
|
settings,
|
|
vcl_cookie_lookup,
|
|
),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(super) fn combine_policy(
|
|
original: RequestPolicy,
|
|
rewritten: impl FnOnce() -> RequestPolicy,
|
|
) -> RequestPolicy {
|
|
if matches!(original, RequestPolicy::Bypass(_)) {
|
|
return original;
|
|
}
|
|
match (original, rewritten()) {
|
|
(_, RequestPolicy::Bypass(reason)) => RequestPolicy::Bypass(reason),
|
|
(RequestPolicy::Revalidate(reason), _) => RequestPolicy::Revalidate(reason),
|
|
(_, RequestPolicy::Revalidate(reason)) => RequestPolicy::Revalidate(reason),
|
|
_ => RequestPolicy::Use,
|
|
}
|
|
}
|
|
|
|
pub(super) fn range_selection(
|
|
cache_eligible: bool,
|
|
method: &Method,
|
|
headers: &HeaderMap,
|
|
) -> crate::cache::RangeSelection {
|
|
if cache_eligible {
|
|
crate::cache::request_range(method, headers)
|
|
} else if headers.contains_key(::http::header::RANGE) {
|
|
crate::cache::RangeSelection::Unsupported
|
|
} else {
|
|
crate::cache::RangeSelection::None
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests;
|