@@ -7,6 +7,7 @@ use util::{self, csv, Seconds};
77use HeaderValue ;
88
99/// `Cache-Control` header, defined in [RFC7234](https://tools.ietf.org/html/rfc7234#section-5.2)
10+ /// with extensions in [RFC8246](https://www.rfc-editor.org/rfc/rfc8246)
1011///
1112/// The `Cache-Control` header field is used to specify directives for
1213/// caches along the request/response chain. Such cache directives are
@@ -45,14 +46,15 @@ pub struct CacheControl {
4546
4647bitflags ! {
4748 struct Flags : u32 {
48- const NO_CACHE = 0b00000001 ;
49- const NO_STORE = 0b00000010 ;
50- const NO_TRANSFORM = 0b00000100 ;
51- const ONLY_IF_CACHED = 0b00001000 ;
52- const MUST_REVALIDATE = 0b00010000 ;
53- const PUBLIC = 0b00100000 ;
54- const PRIVATE = 0b01000000 ;
55- const PROXY_REVALIDATE = 0b10000000 ;
49+ const NO_CACHE = 0b000000001 ;
50+ const NO_STORE = 0b000000010 ;
51+ const NO_TRANSFORM = 0b000000100 ;
52+ const ONLY_IF_CACHED = 0b000001000 ;
53+ const MUST_REVALIDATE = 0b000010000 ;
54+ const PUBLIC = 0b000100000 ;
55+ const PRIVATE = 0b001000000 ;
56+ const PROXY_REVALIDATE = 0b010000000 ;
57+ const IMMUTABLE = 0b100000000 ;
5658 }
5759}
5860
@@ -100,6 +102,11 @@ impl CacheControl {
100102 self . flags . contains ( Flags :: PRIVATE )
101103 }
102104
105+ /// Check if the `immutable` directive is set.
106+ pub fn immutable ( & self ) -> bool {
107+ self . flags . contains ( Flags :: IMMUTABLE )
108+ }
109+
103110 /// Get the value of the `max-age` directive if set.
104111 pub fn max_age ( & self ) -> Option < Duration > {
105112 self . max_age . map ( Into :: into)
@@ -158,6 +165,12 @@ impl CacheControl {
158165 self
159166 }
160167
168+ /// Set the `immutable` directive.
169+ pub fn with_immutable ( mut self ) -> Self {
170+ self . flags . insert ( Flags :: IMMUTABLE ) ;
171+ self
172+ }
173+
161174 /// Set the `max-age` directive.
162175 pub fn with_max_age ( mut self , duration : Duration ) -> Self {
163176 self . max_age = Some ( duration. into ( ) ) ;
@@ -236,6 +249,9 @@ impl FromIterator<KnownDirective> for FromIter {
236249 Directive :: Private => {
237250 cc. flags . insert ( Flags :: PRIVATE ) ;
238251 }
252+ Directive :: Immutable => {
253+ cc. flags . insert ( Flags :: IMMUTABLE ) ;
254+ }
239255 Directive :: ProxyRevalidate => {
240256 cc. flags . insert ( Flags :: PROXY_REVALIDATE ) ;
241257 }
@@ -278,6 +294,7 @@ impl<'a> fmt::Display for Fmt<'a> {
278294 if_flag ( Flags :: MUST_REVALIDATE , Directive :: MustRevalidate ) ,
279295 if_flag ( Flags :: PUBLIC , Directive :: Public ) ,
280296 if_flag ( Flags :: PRIVATE , Directive :: Private ) ,
297+ if_flag ( Flags :: IMMUTABLE , Directive :: Immutable ) ,
281298 if_flag ( Flags :: PROXY_REVALIDATE , Directive :: ProxyRevalidate ) ,
282299 self . 0
283300 . max_age
@@ -325,6 +342,7 @@ enum Directive {
325342 MustRevalidate ,
326343 Public ,
327344 Private ,
345+ Immutable ,
328346 ProxyRevalidate ,
329347 SMaxAge ( u64 ) ,
330348}
@@ -345,6 +363,7 @@ impl fmt::Display for Directive {
345363 Directive :: MustRevalidate => "must-revalidate" ,
346364 Directive :: Public => "public" ,
347365 Directive :: Private => "private" ,
366+ Directive :: Immutable => "immutable" ,
348367 Directive :: ProxyRevalidate => "proxy-revalidate" ,
349368 Directive :: SMaxAge ( secs) => return write ! ( f, "s-maxage={}" , secs) ,
350369 } ,
@@ -364,6 +383,7 @@ impl FromStr for KnownDirective {
364383 "must-revalidate" => Directive :: MustRevalidate ,
365384 "public" => Directive :: Public ,
366385 "private" => Directive :: Private ,
386+ "immutable" => Directive :: Immutable ,
367387 "proxy-revalidate" => Directive :: ProxyRevalidate ,
368388 "" => return Err ( ( ) ) ,
369389 _ => match s. find ( '=' ) {
@@ -428,9 +448,18 @@ mod tests {
428448 ) ;
429449 }
430450
451+ #[ test]
452+ fn test_immutable ( ) {
453+ let cc = CacheControl :: new ( ) . with_immutable ( ) ;
454+ let headers = test_encode ( cc. clone ( ) ) ;
455+ assert_eq ! ( headers[ "cache-control" ] , "immutable" ) ;
456+ assert_eq ! ( test_decode:: <CacheControl >( & [ "immutable" ] ) . unwrap( ) , cc) ;
457+ assert ! ( cc. immutable( ) ) ;
458+ }
459+
431460 #[ test]
432461 fn test_parse_bad_syntax ( ) {
433- assert_eq ! ( test_decode:: <CacheControl >( & [ "max-age=lolz" ] ) , None , ) ;
462+ assert_eq ! ( test_decode:: <CacheControl >( & [ "max-age=lolz" ] ) , None ) ;
434463 }
435464
436465 #[ test]
0 commit comments