hitbox_http/predicates/mod.rs
1//! Predicates for determining HTTP request and response cacheability.
2//!
3//! Predicates evaluate HTTP messages and return [`Cacheable`] or [`NonCacheable`].
4//! They can be combined using logical operators from the [`conditions`] module.
5//!
6//! # Request vs Response Predicates
7//!
8//! - **Request predicates** decide whether to attempt a cache lookup
9//! - **Response predicates** decide whether to store a response in cache
10//!
11//! # Available Predicates
12//!
13//! ## Request Predicates ([`request`] module)
14//!
15//! | Predicate | Description |
16//! |-----------|-------------|
17//! | [`request::Method`] | Match by HTTP method |
18//! | [`request::Path`] | Match by URL path pattern |
19//! | [`request::Header`] | Match by request header |
20//! | [`request::Query`] | Match by query parameter |
21//! | [`request::Body`] | Match by request body content |
22//!
23//! ## Response Predicates ([`response`] module)
24//!
25//! | Predicate | Description |
26//! |-----------|-------------|
27//! | [`response::StatusCode`] | Match by status code or class |
28//! | [`response::Header`] | Match by response header |
29//! | [`response::Body`] | Match by response body content |
30//!
31//! # Combining Predicates
32//!
33//! Use [`PredicateExt`] to combine predicates:
34//!
35//! ```
36//! use hitbox::predicate::PredicateExt;
37//! use hitbox_http::predicates::request::Method;
38//! use hitbox_http::predicates::header::{Header, Operation};
39//!
40//! # use bytes::Bytes;
41//! # use http_body_util::Empty;
42//! # use hitbox::Neutral;
43//! # use hitbox::predicate::And;
44//! # use hitbox_http::CacheableHttpRequest;
45//! # type Subject = CacheableHttpRequest<Empty<Bytes>>;
46//! // Cache GET requests without Cache-Control: no-cache
47//! let predicate = Method::new(http::Method::GET).unwrap();
48//! # let _: &Method<Neutral<Subject>> = &predicate;
49//! let predicate = predicate.and(
50//! Header::new(Operation::Contains(
51//! http::header::CACHE_CONTROL,
52//! "no-cache".to_string(),
53//! )).not()
54//! );
55//! ```
56//!
57//! [`Cacheable`]: hitbox::predicate::PredicateResult::Cacheable
58//! [`NonCacheable`]: hitbox::predicate::PredicateResult::NonCacheable
59//! [`PredicateExt`]: conditions::PredicateExt
60
61use hitbox::Neutral;
62
63use crate::{CacheableHttpRequest, CacheableHttpResponse};
64
65pub mod body;
66pub mod conditions;
67pub mod header;
68pub mod request;
69pub mod response;
70pub mod version;
71
72/// A neutral predicate for HTTP requests that always returns `Cacheable`.
73///
74/// Use this when you want to cache all requests regardless of their properties,
75/// or as a starting point for predicate chains with `PredicateExt`.
76pub type NeutralRequestPredicate<ReqBody> = Neutral<CacheableHttpRequest<ReqBody>>;
77
78/// A neutral predicate for HTTP responses that always returns `Cacheable`.
79///
80/// Use this when you want to cache all responses regardless of their properties,
81/// or as a starting point for predicate chains with `PredicateExt`.
82pub type NeutralResponsePredicate<ResBody> = Neutral<CacheableHttpResponse<ResBody>>;