0.34.6_repo_cleanup
This commit is contained in:
1 parent
ce1a90397c
commit
133d2f0f91
273 files changed
+5969
-6926
No files matched your search
@@ -0,0 +1,101 @@
|
||||
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,
|
||||
},
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
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 { .. } => {
|
||||
crate::cache::request_policy(method, headers, body_empty, path, has_query, settings)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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("range") {
|
||||
crate::cache::RangeSelection::Unsupported
|
||||
} else {
|
||||
crate::cache::RangeSelection::None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
Reference in new issue
Block a user