skp_cache_http/
cache_control.rs1use std::time::Duration;
2
3#[derive(Debug, Clone, Default, PartialEq, Eq)]
5pub struct CacheControl {
6 pub max_age: Option<Duration>,
8 pub s_maxage: Option<Duration>,
10 pub no_cache: bool,
12 pub no_store: bool,
14 pub private: bool,
16 pub public: bool,
18 pub must_revalidate: bool,
20 pub stale_while_revalidate: Option<Duration>,
22}
23
24impl CacheControl {
25 pub fn parse(header: &str) -> Self {
27 let mut cc = Self::default();
28 for directive in header.split(',') {
29 let directive = directive.trim();
30 if directive.eq_ignore_ascii_case("no-cache") { cc.no_cache = true; }
31 else if directive.eq_ignore_ascii_case("no-store") { cc.no_store = true; }
32 else if directive.eq_ignore_ascii_case("private") { cc.private = true; }
33 else if directive.eq_ignore_ascii_case("public") { cc.public = true; }
34 else if directive.eq_ignore_ascii_case("must-revalidate") { cc.must_revalidate = true; }
35 else if let Some(val) = directive.strip_prefix("max-age=") {
36 if let Ok(secs) = val.parse::<u64>() { cc.max_age = Some(Duration::from_secs(secs)); }
37 }
38 else if let Some(val) = directive.strip_prefix("s-maxage=") {
39 if let Ok(secs) = val.parse::<u64>() { cc.s_maxage = Some(Duration::from_secs(secs)); }
40 }
41 else if let Some(val) = directive.strip_prefix("stale-while-revalidate=") {
42 if let Ok(secs) = val.parse::<u64>() { cc.stale_while_revalidate = Some(Duration::from_secs(secs)); }
43 }
44 }
45 cc
46 }
47}