@@ -4,7 +4,7 @@ use crate::Status;
4
4
use std:: time:: Duration ;
5
5
6
6
/// An HTTP `Cache-Control` directive.
7
- #[ derive( Debug ) ]
7
+ #[ derive( Debug , Clone ) ]
8
8
pub enum CacheDirective {
9
9
/// The response body will not change over time.
10
10
Immutable ,
@@ -76,30 +76,31 @@ impl CacheDirective {
76
76
// sense.
77
77
pub ( crate ) fn from_str ( s : & str ) -> crate :: Result < Option < Self > > {
78
78
use CacheDirective :: * ;
79
- let parts = s. split ( '=' ) ;
79
+ let mut parts = s. split ( '=' ) ;
80
+ let next = parts. next ( ) . unwrap ( ) . clone ( ) ;
80
81
81
- let get_dur = || -> crate :: Result < Duration > {
82
+ let mut get_dur = || -> crate :: Result < Duration > {
82
83
let dur = parts. next ( ) . status ( 400 ) ?;
83
84
let dur: u64 = dur. parse ( ) . status ( 400 ) ?;
84
85
Ok ( Duration :: new ( dur, 0 ) )
85
86
} ;
86
87
87
88
// 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 {
89
90
"no-cache" => Some ( NoCache ) ,
90
91
"no-store" => Some ( NoStore ) ,
91
92
"no-transform" => Some ( NoTransform ) ,
92
93
"only-if-cached" => Some ( OnlyIfCached ) ,
93
94
"must-revalidate" => Some ( MustRevalidate ) ,
94
- "no-cache" => Some ( NoCache ) ,
95
- "no-store" => Some ( NoStore ) ,
96
- "no-transform" => Some ( NoTransform ) ,
97
95
"public" => Some ( Public ) ,
98
96
"private" => Some ( Private ) ,
99
97
"proxy-revalidate" => Some ( ProxyRevalidate ) ,
100
98
"max-age" => Some ( MaxAge ( get_dur ( ) ?) ) ,
101
99
"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
+ }
103
104
None => Some ( MaxStale ( None ) ) ,
104
105
} ,
105
106
"min-fresh=<seconds>" => Some ( MinFresh ( get_dur ( ) ?) ) ,
@@ -128,13 +129,13 @@ impl From<CacheDirective> for HeaderValue {
128
129
NoCache => h ( format ! ( "no-cache" ) ) ,
129
130
NoStore => h ( format ! ( "no-store" ) ) ,
130
131
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 ( ) ) ) ,
138
139
}
139
140
}
140
141
}
0 commit comments