@@ -7,6 +7,7 @@ use util::{self, csv, Seconds};
7
7
use HeaderValue ;
8
8
9
9
/// `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)
10
11
///
11
12
/// The `Cache-Control` header field is used to specify directives for
12
13
/// caches along the request/response chain. Such cache directives are
@@ -45,14 +46,15 @@ pub struct CacheControl {
45
46
46
47
bitflags ! {
47
48
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 ;
56
58
}
57
59
}
58
60
@@ -100,6 +102,11 @@ impl CacheControl {
100
102
self . flags . contains ( Flags :: PRIVATE )
101
103
}
102
104
105
+ /// Check if the `immutable` directive is set.
106
+ pub fn immutable ( & self ) -> bool {
107
+ self . flags . contains ( Flags :: IMMUTABLE )
108
+ }
109
+
103
110
/// Get the value of the `max-age` directive if set.
104
111
pub fn max_age ( & self ) -> Option < Duration > {
105
112
self . max_age . map ( Into :: into)
@@ -158,6 +165,12 @@ impl CacheControl {
158
165
self
159
166
}
160
167
168
+ /// Set the `immutable` directive.
169
+ pub fn with_immutable ( mut self ) -> Self {
170
+ self . flags . insert ( Flags :: IMMUTABLE ) ;
171
+ self
172
+ }
173
+
161
174
/// Set the `max-age` directive.
162
175
pub fn with_max_age ( mut self , duration : Duration ) -> Self {
163
176
self . max_age = Some ( duration. into ( ) ) ;
@@ -236,6 +249,9 @@ impl FromIterator<KnownDirective> for FromIter {
236
249
Directive :: Private => {
237
250
cc. flags . insert ( Flags :: PRIVATE ) ;
238
251
}
252
+ Directive :: Immutable => {
253
+ cc. flags . insert ( Flags :: IMMUTABLE ) ;
254
+ }
239
255
Directive :: ProxyRevalidate => {
240
256
cc. flags . insert ( Flags :: PROXY_REVALIDATE ) ;
241
257
}
@@ -278,6 +294,7 @@ impl<'a> fmt::Display for Fmt<'a> {
278
294
if_flag ( Flags :: MUST_REVALIDATE , Directive :: MustRevalidate ) ,
279
295
if_flag ( Flags :: PUBLIC , Directive :: Public ) ,
280
296
if_flag ( Flags :: PRIVATE , Directive :: Private ) ,
297
+ if_flag ( Flags :: IMMUTABLE , Directive :: Immutable ) ,
281
298
if_flag ( Flags :: PROXY_REVALIDATE , Directive :: ProxyRevalidate ) ,
282
299
self . 0
283
300
. max_age
@@ -325,6 +342,7 @@ enum Directive {
325
342
MustRevalidate ,
326
343
Public ,
327
344
Private ,
345
+ Immutable ,
328
346
ProxyRevalidate ,
329
347
SMaxAge ( u64 ) ,
330
348
}
@@ -345,6 +363,7 @@ impl fmt::Display for Directive {
345
363
Directive :: MustRevalidate => "must-revalidate" ,
346
364
Directive :: Public => "public" ,
347
365
Directive :: Private => "private" ,
366
+ Directive :: Immutable => "immutable" ,
348
367
Directive :: ProxyRevalidate => "proxy-revalidate" ,
349
368
Directive :: SMaxAge ( secs) => return write ! ( f, "s-maxage={}" , secs) ,
350
369
} ,
@@ -364,6 +383,7 @@ impl FromStr for KnownDirective {
364
383
"must-revalidate" => Directive :: MustRevalidate ,
365
384
"public" => Directive :: Public ,
366
385
"private" => Directive :: Private ,
386
+ "immutable" => Directive :: Immutable ,
367
387
"proxy-revalidate" => Directive :: ProxyRevalidate ,
368
388
"" => return Err ( ( ) ) ,
369
389
_ => match s. find ( '=' ) {
@@ -428,9 +448,18 @@ mod tests {
428
448
) ;
429
449
}
430
450
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
+
431
460
#[ test]
432
461
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 ) ;
434
463
}
435
464
436
465
#[ test]
0 commit comments