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
//! A caching middleware that follows HTTP caching rules.
//! By default it uses [`cacache`](https://github.com/zkat/cacache-rs) as the backend cache manager.
//!
//! ## Example - Surf (requires feature: `client-surf`)
//!
//! ```ignore
//! use http_cache::{Cache, CacheMode, CACacheManager};
//!
//! #[async_std::main]
//! async fn main() -> surf::Result<()> {
//! let req = surf::get("https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching");
//! surf::client()
//! .with(Cache {
//! mode: CacheMode::Default,
//! cache_manager: CACacheManager::default(),
//! })
//! .send(req)
//! .await?;
//! Ok(())
//! }
//!
//! ```
//! ## Example - Reqwest (requires feature: `client-reqwest`)
//!
//! ```ignore
//! use reqwest::Client;
//! use reqwest_middleware::{ClientBuilder, Result};
//! use http_cache::{Cache, CacheMode, CACacheManager};
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//! let client = ClientBuilder::new(Client::new())
//! .with(Cache {
//! mode: CacheMode::Default,
//! cache_manager: CACacheManager::default(),
//! })
//! .build();
//! client
//! .get("https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching")
//! .send()
//! .await?;
//! Ok(())
//! }
//! ```
#![forbid(unsafe_code, future_incompatible)]
#![deny(
missing_docs,
missing_debug_implementations,
missing_copy_implementations,
nonstandard_style,
unused_qualifications,
rustdoc::missing_doc_code_examples
)]
mod error;
mod managers;
mod middleware;
pub use error::CacheError;
#[cfg(feature = "manager-cacache")]
pub use managers::cacache::CACacheManager;
use http::{header::CACHE_CONTROL, request, response, StatusCode};
use std::{collections::HashMap, str::FromStr, time::SystemTime};
use http_cache_semantics::{AfterResponse, BeforeRequest, CachePolicy};
use serde::{Deserialize, Serialize};
use url::Url;
/// A `Result` typedef to use with the `CacheError` type
pub type Result<T> = std::result::Result<T, CacheError>;
/// Represents an HTTP version
#[derive(Debug, Copy, Clone, 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.iter() {
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
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
pub fn must_revalidate(&self) -> bool {
if let Some(val) = self.headers.get(CACHE_CONTROL.as_str()) {
val.as_str().to_lowercase().contains("must-revalidate")
} else {
false
}
}
}
/// Describes the functionality required for interfacing with HTTP client middleware
#[async_trait::async_trait]
pub(crate) trait Middleware {
fn is_method_get_head(&self) -> bool;
fn policy(&self, response: &HttpResponse) -> Result<CachePolicy>;
fn update_headers(&mut self, parts: request::Parts) -> Result<()>;
fn set_no_cache(&mut self) -> Result<()>;
fn parts(&self) -> Result<request::Parts>;
fn url(&self) -> Result<&Url>;
fn method(&self) -> Result<String>;
async fn remote_fetch(&mut self) -> Result<HttpResponse>;
}
/// A trait providing methods for storing, reading, and removing cache records.
#[async_trait::async_trait]
pub trait CacheManager {
/// 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<()>;
}
/// Similar to [make-fetch-happen cache options](https://github.com/npm/make-fetch-happen#--optscache).
/// Passed in when the [`Cache`] 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. (Can only be used when request’s mode is "same-origin".
/// Any cached redirects will be followed assuming request’s redirect mode is "follow"
/// and the redirects do not violate request’s mode.)
OnlyIfCached,
}
/// Caches requests according to http spec
#[derive(Debug, Clone)]
pub struct Cache<T: CacheManager + Send + Sync + 'static> {
/// Determines the manager behavior
pub mode: CacheMode,
/// Manager instance that implements the CacheManager trait
pub cache_manager: T,
}
impl<T: CacheManager + Send + Sync + 'static> Cache<T> {
#[allow(dead_code)]
pub(crate) 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 middleware.remote_fetch().await;
}
if let Some(store) = self
.cache_manager
.get(&middleware.method()?, middleware.url()?)
.await?
{
let (mut res, policy) = store;
let res_url = res.url.clone();
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;
//
#[allow(clippy::manual_range_contains)]
if warning_code >= 100 && warning_code < 200 {
res.remove_warning();
}
}
match self.mode {
CacheMode::Default => {
self.conditional_fetch(middleware, res, policy).await
}
CacheMode::NoCache => {
middleware.set_no_cache()?;
self.conditional_fetch(middleware, res, policy).await
}
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, 112, "Disconnected operation");
Ok(res)
}
_ => self.remote_fetch(&mut middleware).await,
}
} else {
match self.mode {
CacheMode::OnlyIfCached => {
// ENOTCACHED
return Ok(HttpResponse {
body: b"GatewayTimeout".to_vec(),
headers: Default::default(),
status: 504,
url: middleware.url()?.clone(),
version: HttpVersion::Http11,
});
}
_ => self.remote_fetch(&mut middleware).await,
}
}
}
#[allow(dead_code)]
async fn remote_fetch(
&self,
middleware: &mut impl Middleware,
) -> Result<HttpResponse> {
let res = middleware.remote_fetch().await?;
let policy = middleware.policy(&res)?;
let is_cacheable = middleware.is_method_get_head()
&& self.mode != CacheMode::NoStore
&& self.mode != CacheMode::Reload
&& res.status == 200
&& policy.is_storable();
if is_cacheable {
Ok(self
.cache_manager
.put(&middleware.method()?, middleware.url()?, res, policy)
.await?)
} else if !middleware.is_method_get_head() {
self.cache_manager
.delete(&middleware.method()?, middleware.url()?)
.await?;
Ok(res)
} else {
Ok(res)
}
}
#[allow(dead_code)]
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)?;
return Ok(cached_res);
}
BeforeRequest::Stale { request: parts, matches } => {
if matches {
middleware.update_headers(parts)?;
}
}
}
let req_url = middleware.url()?.clone();
match middleware.remote_fetch().await {
Ok(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.clone(),
111,
"Revalidation failed",
);
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)?;
}
}
let res = self
.cache_manager
.put(
&middleware.method()?,
&req_url,
cached_res,
policy,
)
.await?;
Ok(res)
} else {
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");
Ok(cached_res)
}
}
}
}
}
