@@ -4,7 +4,7 @@ use crate::Status;
44use std:: time:: Duration ;
55
66/// An HTTP `Cache-Control` directive.
7- #[ derive( Debug ) ]
7+ #[ derive( Debug , Clone ) ]
88pub enum CacheDirective {
99 /// The response body will not change over time.
1010 Immutable ,
@@ -76,30 +76,31 @@ impl CacheDirective {
7676 // sense.
7777 pub ( crate ) fn from_str ( s : & str ) -> crate :: Result < Option < Self > > {
7878 use CacheDirective :: * ;
79- let parts = s. split ( '=' ) ;
79+ let mut parts = s. split ( '=' ) ;
80+ let next = parts. next ( ) . unwrap ( ) . clone ( ) ;
8081
81- let get_dur = || -> crate :: Result < Duration > {
82+ let mut get_dur = || -> crate :: Result < Duration > {
8283 let dur = parts. next ( ) . status ( 400 ) ?;
8384 let dur: u64 = dur. parse ( ) . status ( 400 ) ?;
8485 Ok ( Duration :: new ( dur, 0 ) )
8586 } ;
8687
8788 // This won't panic because each input string has at least one part.
88- let res = match parts . next ( ) . unwrap ( ) {
89+ let res = match next {
8990 "no-cache" => Some ( NoCache ) ,
9091 "no-store" => Some ( NoStore ) ,
9192 "no-transform" => Some ( NoTransform ) ,
9293 "only-if-cached" => Some ( OnlyIfCached ) ,
9394 "must-revalidate" => Some ( MustRevalidate ) ,
94- "no-cache" => Some ( NoCache ) ,
95- "no-store" => Some ( NoStore ) ,
96- "no-transform" => Some ( NoTransform ) ,
9795 "public" => Some ( Public ) ,
9896 "private" => Some ( Private ) ,
9997 "proxy-revalidate" => Some ( ProxyRevalidate ) ,
10098 "max-age" => Some ( MaxAge ( get_dur ( ) ?) ) ,
10199 "max-stale" => match parts. next ( ) {
102- Some ( secs) => Some ( MaxStale ( Some ( get_dur ( ) ?) ) ) ,
100+ Some ( secs) => {
101+ let dur: u64 = secs. parse ( ) . status ( 400 ) ?;
102+ Some ( MaxStale ( Some ( Duration :: new ( dur, 0 ) ) ) )
103+ }
103104 None => Some ( MaxStale ( None ) ) ,
104105 } ,
105106 "min-fresh=<seconds>" => Some ( MinFresh ( get_dur ( ) ?) ) ,
@@ -128,13 +129,13 @@ impl From<CacheDirective> for HeaderValue {
128129 NoCache => h ( format ! ( "no-cache" ) ) ,
129130 NoStore => h ( format ! ( "no-store" ) ) ,
130131 NoTransform => h ( format ! ( "no-transform" ) ) ,
131- OnlyIfCached => h ( format ! ( "immutable " ) ) ,
132- Private => h ( format ! ( "immutable " ) ) ,
133- ProxyRevalidate => h ( format ! ( "immutable " ) ) ,
134- Public => h ( format ! ( "immutable " ) ) ,
135- SMaxAge ( dur) => h ( format ! ( "immutable" ) ) ,
136- StaleIfError ( dur) => h ( format ! ( "immutable" ) ) ,
137- StaleWhileRevalidate ( dur) => h ( format ! ( "immutable" ) ) ,
132+ OnlyIfCached => h ( format ! ( "only-if-cached " ) ) ,
133+ Private => h ( format ! ( "private " ) ) ,
134+ ProxyRevalidate => h ( format ! ( "proxy-revalidate " ) ) ,
135+ Public => h ( format ! ( "public " ) ) ,
136+ SMaxAge ( dur) => h ( format ! ( "s-max-age={}" , dur . as_secs ( ) ) ) ,
137+ StaleIfError ( dur) => h ( format ! ( "stale-if-error={}" , dur . as_secs ( ) ) ) ,
138+ StaleWhileRevalidate ( dur) => h ( format ! ( "stale-while-revalidate={}" , dur . as_secs ( ) ) ) ,
138139 }
139140 }
140141}
0 commit comments