Skip to content

Commit c940f57

Browse files
committed
init cache control
1 parent 79a7c5f commit c940f57

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

src/cache/cache_control.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,47 @@
1+
use std::time::Duration;
12
/// An HTTP `Cache-Control` directive.
23
#[derive(Debug)]
34
pub enum CacheDirective {
5+
/// The response body will not change over time.
46
Immutable,
7+
/// The maximum amount of time a resource is considered fresh.
58
MaxAge(Duration),
9+
/// Indicates the client will accept a stale response.
610
MaxStale(Option<Duration>),
11+
/// A response that will still be fresh for at least the specified duration.
712
MinFresh(Duration),
13+
/// Once a response is stale, a fresh response must be retrieved.
814
MustRevalidate,
15+
/// The response may be cached, but must always be revalidated before being used.
916
NoCache,
17+
/// The response may not be cached.
1018
NoStore,
19+
/// An intermediate cache or proxy cannot edit the response body,
20+
/// Content-Encoding, Content-Range, or Content-Type.
1121
NoTransform,
22+
/// Do not use the network for a response.
1223
OnlyIfCached,
24+
/// The response may be stored only by a browser's cache, even if the
25+
/// response is normally non-cacheable
1326
Private,
27+
/// Like must-revalidate, but only for shared caches (e.g., proxies).
1428
ProxyRevalidate,
29+
/// The response may be stored by any cache, even if the response is normally
30+
/// non-cacheable.
1531
Public,
32+
/// Overrides max-age or the Expires header, but only for shared caches.
1633
SMaxAge(Duration),
34+
/// The client will accept a stale response if retrieving a fresh one fails.
1735
StaleIfError(Duration),
36+
/// Indicates the client will accept a stale response, while asynchronously
37+
/// checking in the background for a fresh one.
1838
StaleWhileRevalidate(Duration),
1939
}
2040

2141
impl CacheDirective {
2242
/// Check whether this directive is valid in an HTTP request.
2343
pub fn is_req(&self) -> bool {
24-
use Self::*;
44+
use CacheDirective::*;
2545
match self {
2646
MaxAge(_) | MaxStale(_) | MinFresh(_) | NoCache | NoStore | NoTransform
2747
| OnlyIfCached => true,
@@ -31,10 +51,19 @@ impl CacheDirective {
3151

3252
/// Check whether this directive is valid in an HTTP response.
3353
pub fn is_res(&self) -> bool {
34-
use Self::*;
54+
use CacheDirective::*;
3555
match self {
36-
MustRevalidate | NoCache | NoStore | NoTransform | Public | Private
37-
| ProxyRevalidate | MaxAge(_) | SMaxAge(_) => true,
56+
MustRevalidate
57+
| NoCache
58+
| NoStore
59+
| NoTransform
60+
| Public
61+
| Private
62+
| ProxyRevalidate
63+
| MaxAge(_)
64+
| SMaxAge(_)
65+
| StaleIfError(_)
66+
| StaleWhileRevalidate(_) => true,
3867
_ => false,
3968
}
4069
}

src/cache/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
//! HTTP caching.
2+
13
mod cache_control;
4+
5+
pub use cache_control::CacheDirective;

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ pub mod url {
117117
#[macro_use]
118118
mod utils;
119119

120+
pub mod cache;
120121
pub mod headers;
121122
pub mod mime;
122123

0 commit comments

Comments
 (0)