-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
lib.rs
554 lines (520 loc) · 20.6 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#![forbid(unsafe_code, future_incompatible)]
#![deny(
missing_docs,
missing_debug_implementations,
missing_copy_implementations,
nonstandard_style,
unused_qualifications,
unused_import_braces,
unused_extern_crates,
trivial_casts,
trivial_numeric_casts
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
//! A caching middleware that follows HTTP caching rules, thanks to
//! [`http-cache-semantics`](https://github.com/kornelski/rusty-http-cache-semantics).
//! By default, it uses [`cacache`](https://github.com/zkat/cacache-rs) as the backend cache manager.
//!
//! ## Features
//!
//! The following features are available. By default `manager-cacache` is enabled.
//!
//! - `manager-cacache` (default): enable [cacache](https://github.com/zkat/cacache-rs),
//! a high-performance disk cache, backend manager.
//! - `manager-moka` (disabled): enable [moka](https://github.com/moka-rs/moka),
//! a high-performance in-memory cache, backend manager.
//! - `with-http-types` (disabled): enable [http-types](https://github.com/http-rs/http-types)
//! type conversion support
mod error;
mod managers;
use std::{
collections::HashMap, convert::TryFrom, fmt, str::FromStr, time::SystemTime,
};
use http::{header::CACHE_CONTROL, request, response, StatusCode};
use http_cache_semantics::{AfterResponse, BeforeRequest, CachePolicy};
use serde::{Deserialize, Serialize};
use url::Url;
pub use error::{CacheError, Result};
#[cfg(feature = "manager-cacache")]
pub use managers::cacache::CACacheManager;
#[cfg(feature = "manager-moka")]
pub use managers::moka::MokaManager;
// Exposing the moka cache for convenience, renaming to avoid naming conflicts
#[cfg(feature = "manager-moka")]
#[cfg_attr(docsrs, doc(cfg(feature = "manager-moka")))]
pub use moka::future::{Cache as MokaCache, CacheBuilder as MokaCacheBuilder};
// Custom headers used to indicate cache status (hit or miss)
/// `x-cache` header: Value will be HIT if the response was served from cache, MISS if not
pub const XCACHE: &str = "x-cache";
/// `x-cache-lookup` header: Value will be HIT if a response existed in cache, MISS if not
pub const XCACHELOOKUP: &str = "x-cache-lookup";
/// Represents a basic cache status
/// Used in the custom headers `x-cache` and `x-cache-lookup`
#[derive(Debug, Copy, Clone)]
pub enum HitOrMiss {
/// Yes, there was a hit
HIT,
/// No, there was no hit
MISS,
}
impl fmt::Display for HitOrMiss {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::HIT => write!(f, "HIT"),
Self::MISS => write!(f, "MISS"),
}
}
}
/// Represents an HTTP version
#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[non_exhaustive]
pub enum HttpVersion {
/// HTTP Version 0.9
#[serde(rename = "HTTP/0.9")]
Http09,
/// HTTP Version 1.0
#[serde(rename = "HTTP/1.0")]
Http10,
/// HTTP Version 1.1
#[serde(rename = "HTTP/1.1")]
Http11,
/// HTTP Version 2.0
#[serde(rename = "HTTP/2.0")]
H2,
/// HTTP Version 3.0
#[serde(rename = "HTTP/3.0")]
H3,
}
/// A basic generic type that represents an HTTP response
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct HttpResponse {
/// HTTP response body
pub body: Vec<u8>,
/// HTTP response headers
pub headers: HashMap<String, String>,
/// HTTP response status code
pub status: u16,
/// HTTP response url
pub url: Url,
/// HTTP response version
pub version: HttpVersion,
}
impl HttpResponse {
/// Returns `http::response::Parts`
pub fn parts(&self) -> Result<response::Parts> {
let mut converted =
response::Builder::new().status(self.status).body(())?;
{
let headers = converted.headers_mut();
for header in &self.headers {
headers.insert(
http::header::HeaderName::from_str(header.0.as_str())?,
http::HeaderValue::from_str(header.1.as_str())?,
);
}
}
Ok(converted.into_parts().0)
}
/// Returns the status code of the warning header if present
#[must_use]
pub fn warning_code(&self) -> Option<usize> {
self.headers.get("warning").and_then(|hdr| {
hdr.as_str().chars().take(3).collect::<String>().parse().ok()
})
}
/// Adds a warning header to a response
pub fn add_warning(&mut self, url: &Url, code: usize, message: &str) {
// warning = "warning" ":" 1#warning-value
// warning-value = warn-code SP warn-agent SP warn-text [SP warn-date]
// warn-code = 3DIGIT
// warn-agent = ( host [ ":" port ] ) | pseudonym
// ; the name or pseudonym of the server adding
// ; the warning header, for use in debugging
// warn-text = quoted-string
// warn-date = <"> HTTP-date <">
// (https://tools.ietf.org/html/rfc2616#section-14.46)
self.headers.insert(
"warning".to_string(),
format!(
"{} {} {:?} \"{}\"",
code,
url.host().expect("Invalid URL"),
message,
httpdate::fmt_http_date(SystemTime::now())
),
);
}
/// Removes a warning header from a response
pub fn remove_warning(&mut self) {
self.headers.remove("warning");
}
/// Update the headers from `http::response::Parts`
pub fn update_headers(&mut self, parts: &response::Parts) -> Result<()> {
for header in parts.headers.iter() {
self.headers.insert(
header.0.as_str().to_string(),
header.1.to_str()?.to_string(),
);
}
Ok(())
}
/// Checks if the Cache-Control header contains the must-revalidate directive
#[must_use]
pub fn must_revalidate(&self) -> bool {
self.headers.get(CACHE_CONTROL.as_str()).map_or(false, |val| {
val.as_str().to_lowercase().contains("must-revalidate")
})
}
/// Adds the custom `x-cache` header to the response
pub fn cache_status(&mut self, hit_or_miss: HitOrMiss) {
self.headers.insert(XCACHE.to_string(), hit_or_miss.to_string());
}
/// Adds the custom `x-cache-lookup` header to the response
pub fn cache_lookup_status(&mut self, hit_or_miss: HitOrMiss) {
self.headers.insert(XCACHELOOKUP.to_string(), hit_or_miss.to_string());
}
}
/// A trait providing methods for storing, reading, and removing cache records.
#[async_trait::async_trait]
pub trait CacheManager: Send + Sync + 'static {
/// Attempts to pull a cached response and related policy from cache.
async fn get(
&self,
method: &str,
url: &Url,
) -> Result<Option<(HttpResponse, CachePolicy)>>;
/// Attempts to cache a response and related policy.
async fn put(
&self,
method: &str,
url: &Url,
res: HttpResponse,
policy: CachePolicy,
) -> Result<HttpResponse>;
/// Attempts to remove a record from cache.
async fn delete(&self, method: &str, url: &Url) -> Result<()>;
}
/// Describes the functionality required for interfacing with HTTP client middleware
#[async_trait::async_trait]
pub trait Middleware: Send {
/// Determines if the request method is either GET or HEAD
fn is_method_get_head(&self) -> bool;
/// Returns a new cache policy with default options
fn policy(&self, response: &HttpResponse) -> Result<CachePolicy>;
/// Returns a new cache policy with custom options
fn policy_with_options(
&self,
response: &HttpResponse,
options: CacheOptions,
) -> Result<CachePolicy>;
/// Attempts to update the request headers with the passed `http::request::Parts`
fn update_headers(&mut self, parts: &request::Parts) -> Result<()>;
/// Attempts to force the "no-cache" directive on the request
fn force_no_cache(&mut self) -> Result<()>;
/// Attempts to construct `http::request::Parts` from the request
fn parts(&self) -> Result<request::Parts>;
/// Attempts to determine the requested url
fn url(&self) -> Result<Url>;
/// Attempts to determine the request method
fn method(&self) -> Result<String>;
/// Attempts to fetch an upstream resource and return an [`HttpResponse`]
async fn remote_fetch(&mut self) -> Result<HttpResponse>;
}
/// Similar to [make-fetch-happen cache options](https://github.com/npm/make-fetch-happen#--optscache).
/// Passed in when the [`HttpCache`] struct is being built.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CacheMode {
/// Will inspect the HTTP cache on the way to the network.
/// If there is a fresh response it will be used.
/// If there is a stale response a conditional request will be created,
/// and a normal request otherwise.
/// It then updates the HTTP cache with the response.
/// If the revalidation request fails (for example, on a 500 or if you're offline),
/// the stale response will be returned.
Default,
/// Behaves as if there is no HTTP cache at all.
NoStore,
/// Behaves as if there is no HTTP cache on the way to the network.
/// Ergo, it creates a normal request and updates the HTTP cache with the response.
Reload,
/// Creates a conditional request if there is a response in the HTTP cache
/// and a normal request otherwise. It then updates the HTTP cache with the response.
NoCache,
/// Uses any response in the HTTP cache matching the request,
/// not paying attention to staleness. If there was no response,
/// it creates a normal request and updates the HTTP cache with the response.
ForceCache,
/// Uses any response in the HTTP cache matching the request,
/// not paying attention to staleness. If there was no response,
/// it returns a network error.
OnlyIfCached,
}
impl TryFrom<http::Version> for HttpVersion {
type Error = CacheError;
fn try_from(value: http::Version) -> Result<Self> {
Ok(match value {
http::Version::HTTP_09 => Self::Http09,
http::Version::HTTP_10 => Self::Http10,
http::Version::HTTP_11 => Self::Http11,
http::Version::HTTP_2 => Self::H2,
http::Version::HTTP_3 => Self::H3,
_ => return Err(CacheError::BadVersion),
})
}
}
impl From<HttpVersion> for http::Version {
fn from(value: HttpVersion) -> Self {
match value {
HttpVersion::Http09 => Self::HTTP_09,
HttpVersion::Http10 => Self::HTTP_10,
HttpVersion::Http11 => Self::HTTP_11,
HttpVersion::H2 => Self::HTTP_2,
HttpVersion::H3 => Self::HTTP_3,
}
}
}
#[cfg(feature = "http-types")]
impl TryFrom<http_types::Version> for HttpVersion {
type Error = CacheError;
fn try_from(value: http_types::Version) -> Result<Self> {
Ok(match value {
http_types::Version::Http0_9 => Self::Http09,
http_types::Version::Http1_0 => Self::Http10,
http_types::Version::Http1_1 => Self::Http11,
http_types::Version::Http2_0 => Self::H2,
http_types::Version::Http3_0 => Self::H3,
_ => return Err(CacheError::BadVersion),
})
}
}
#[cfg(feature = "http-types")]
impl From<HttpVersion> for http_types::Version {
fn from(value: HttpVersion) -> Self {
match value {
HttpVersion::Http09 => Self::Http0_9,
HttpVersion::Http10 => Self::Http1_0,
HttpVersion::Http11 => Self::Http1_1,
HttpVersion::H2 => Self::Http2_0,
HttpVersion::H3 => Self::Http3_0,
}
}
}
/// Options struct provided by
/// [`http-cache-semantics`](https://github.com/kornelski/rusty-http-cache-semantics).
pub use http_cache_semantics::CacheOptions;
/// Caches requests according to http spec.
#[derive(Debug, Clone)]
pub struct HttpCache<T: CacheManager> {
/// Determines the manager behavior.
pub mode: CacheMode,
/// Manager instance that implements the [`CacheManager`] trait.
/// By default, a manager implementation with [`cacache`](https://github.com/zkat/cacache-rs)
/// as the backend has been provided, see [`CACacheManager`].
pub manager: T,
/// Override the default cache options.
pub options: Option<CacheOptions>,
}
#[allow(dead_code)]
impl<T: CacheManager> HttpCache<T> {
/// Attempts to run the passed middleware along with the cache
pub async fn run(
&self,
mut middleware: impl Middleware,
) -> Result<HttpResponse> {
let is_cacheable = middleware.is_method_get_head()
&& self.mode != CacheMode::NoStore
&& self.mode != CacheMode::Reload;
if !is_cacheable {
return self.remote_fetch(&mut middleware).await;
}
let method = middleware.method()?.to_uppercase();
let url = middleware.url()?;
if let Some(store) = self.manager.get(&method, &url).await? {
let (mut res, policy) = store;
res.cache_lookup_status(HitOrMiss::HIT);
if let Some(warning_code) = res.warning_code() {
// https://tools.ietf.org/html/rfc7234#section-4.3.4
//
// If a stored response is selected for update, the cache MUST:
//
// * delete any warning header fields in the stored response with
// warn-code 1xx (see Section 5.5);
//
// * retain any warning header fields in the stored response with
// warn-code 2xx;
//
if (100..200).contains(&warning_code) {
res.remove_warning();
}
}
match self.mode {
CacheMode::Default => {
self.conditional_fetch(middleware, res, policy).await
}
CacheMode::NoCache => {
middleware.force_no_cache()?;
let mut res = self.remote_fetch(&mut middleware).await?;
res.cache_lookup_status(HitOrMiss::HIT);
Ok(res)
}
CacheMode::ForceCache | CacheMode::OnlyIfCached => {
// 112 Disconnected operation
// SHOULD be included if the cache is intentionally disconnected from
// the rest of the network for a period of time.
// (https://tools.ietf.org/html/rfc2616#section-14.46)
res.add_warning(
&res.url.clone(),
112,
"Disconnected operation",
);
res.cache_status(HitOrMiss::HIT);
Ok(res)
}
_ => self.remote_fetch(&mut middleware).await,
}
} else {
match self.mode {
CacheMode::OnlyIfCached => {
// ENOTCACHED
let mut res = HttpResponse {
body: b"GatewayTimeout".to_vec(),
headers: HashMap::default(),
status: 504,
url: middleware.url()?,
version: HttpVersion::Http11,
};
res.cache_status(HitOrMiss::MISS);
res.cache_lookup_status(HitOrMiss::MISS);
Ok(res)
}
_ => self.remote_fetch(&mut middleware).await,
}
}
}
async fn remote_fetch(
&self,
middleware: &mut impl Middleware,
) -> Result<HttpResponse> {
let mut res = middleware.remote_fetch().await?;
res.cache_status(HitOrMiss::MISS);
res.cache_lookup_status(HitOrMiss::MISS);
let policy = match self.options {
Some(options) => middleware.policy_with_options(&res, options)?,
None => middleware.policy(&res)?,
};
let is_get_head = middleware.is_method_get_head();
let is_cacheable = is_get_head
&& self.mode != CacheMode::NoStore
&& self.mode != CacheMode::Reload
&& res.status == 200
&& policy.is_storable();
let url = middleware.url()?;
let method = middleware.method()?.to_uppercase();
if is_cacheable {
Ok(self.manager.put(&method, &url, res, policy).await?)
} else if !is_get_head {
self.manager.delete("GET", &url).await.ok();
Ok(res)
} else {
Ok(res)
}
}
async fn conditional_fetch(
&self,
mut middleware: impl Middleware,
mut cached_res: HttpResponse,
mut policy: CachePolicy,
) -> Result<HttpResponse> {
let before_req =
policy.before_request(&middleware.parts()?, SystemTime::now());
match before_req {
BeforeRequest::Fresh(parts) => {
cached_res.update_headers(&parts)?;
cached_res.cache_status(HitOrMiss::HIT);
cached_res.cache_lookup_status(HitOrMiss::HIT);
return Ok(cached_res);
}
BeforeRequest::Stale { request: parts, matches } => {
if matches {
middleware.update_headers(&parts)?;
}
}
}
let req_url = middleware.url()?;
match middleware.remote_fetch().await {
Ok(mut cond_res) => {
let status = StatusCode::from_u16(cond_res.status)?;
if status.is_server_error() && cached_res.must_revalidate() {
// 111 Revalidation failed
// MUST be included if a cache returns a stale response
// because an attempt to revalidate the response failed,
// due to an inability to reach the server.
// (https://tools.ietf.org/html/rfc2616#section-14.46)
cached_res.add_warning(
&req_url,
111,
"Revalidation failed",
);
cached_res.cache_status(HitOrMiss::HIT);
Ok(cached_res)
} else if cond_res.status == 304 {
let after_res = policy.after_response(
&middleware.parts()?,
&cond_res.parts()?,
SystemTime::now(),
);
match after_res {
AfterResponse::Modified(new_policy, parts)
| AfterResponse::NotModified(new_policy, parts) => {
policy = new_policy;
cached_res.update_headers(&parts)?;
}
}
cached_res.cache_status(HitOrMiss::HIT);
cached_res.cache_lookup_status(HitOrMiss::HIT);
let method = middleware.method()?.to_uppercase();
let res = self
.manager
.put(&method, &req_url, cached_res, policy)
.await?;
Ok(res)
} else if cond_res.status == 200 {
let policy = match self.options {
Some(options) => middleware
.policy_with_options(&cond_res, options)?,
None => middleware.policy(&cond_res)?,
};
cond_res.cache_status(HitOrMiss::MISS);
cond_res.cache_lookup_status(HitOrMiss::HIT);
let method = middleware.method()?.to_uppercase();
let res = self
.manager
.put(&method, &req_url, cond_res, policy)
.await?;
Ok(res)
} else {
cached_res.cache_status(HitOrMiss::HIT);
Ok(cached_res)
}
}
Err(e) => {
if cached_res.must_revalidate() {
Err(e)
} else {
// 111 Revalidation failed
// MUST be included if a cache returns a stale response
// because an attempt to revalidate the response failed,
// due to an inability to reach the server.
// (https://tools.ietf.org/html/rfc2616#section-14.46)
cached_res.add_warning(
&req_url,
111,
"Revalidation failed",
);
cached_res.cache_status(HitOrMiss::HIT);
Ok(cached_res)
}
}
}
}
}