pub enum Operation {
Limit {
bytes: usize,
},
Plain(PlainOperation),
Jq {
filter: JqExpression,
operation: JqOperation,
},
}Expand description
Matching operations for HTTP body content.
§Caveats
Body predicates may consume bytes from the stream. The body is transitioned
to BufferedBody::Partial or BufferedBody::Complete after evaluation.
§Examples
§Size Limit
Only cache responses smaller than 1MB:
use hitbox_http::predicates::body::Operation;
let op = Operation::Limit { bytes: 1024 * 1024 };§Plain Text Matching
Cache only if body contains a success marker:
use bytes::Bytes;
use hitbox_http::predicates::body::{Operation, PlainOperation};
let op = Operation::Plain(PlainOperation::Contains(Bytes::from("\"success\":true")));Cache only if body starts with JSON array:
use bytes::Bytes;
use hitbox_http::predicates::body::{Operation, PlainOperation};
let op = Operation::Plain(PlainOperation::Starts(Bytes::from("[")));Cache only if body matches a regex pattern:
use hitbox_http::predicates::body::{Operation, PlainOperation};
let regex = regex::bytes::Regex::new(r#""status":\s*"(ok|success)""#).unwrap();
let op = Operation::Plain(PlainOperation::RegExp(regex));§JQ (JSON) Matching
Cache only if response has non-empty items array:
use hitbox_http::predicates::body::{Operation, JqExpression, JqOperation};
let op = Operation::Jq {
filter: JqExpression::compile(".items | length > 0").unwrap(),
operation: JqOperation::Eq(serde_json::Value::Bool(true)),
};Cache only if user role exists:
use hitbox_http::predicates::body::{Operation, JqExpression, JqOperation};
let op = Operation::Jq {
filter: JqExpression::compile(".user.role").unwrap(),
operation: JqOperation::Exist,
};Cache only if status is one of allowed values:
use hitbox_http::predicates::body::{Operation, JqExpression, JqOperation};
let op = Operation::Jq {
filter: JqExpression::compile(".status").unwrap(),
operation: JqOperation::In(vec![
serde_json::json!("published"),
serde_json::json!("approved"),
]),
};Variants§
Limit
Use when you need to limit cached response sizes.
Best for preventing cache bloat from large responses like file downloads.
Reads up to bytes + 1 to determine if the limit is exceeded.
Plain(PlainOperation)
Use when matching raw body bytes without parsing.
Best for text responses, checking signatures, or simple content matching.
Jq
Use when caching depends on JSON content structure or values.
Best for APIs where cacheability depends on response data (e.g., user roles, feature flags).
Fields
filter: JqExpressionThe compiled JQ filter expression.
operation: JqOperationThe operation to apply to the filter result.
Implementations§
Source§impl Operation
impl Operation
Sourcepub async fn check<B>(
&self,
body: BufferedBody<B>,
) -> PredicateResult<BufferedBody<B>>
pub async fn check<B>( &self, body: BufferedBody<B>, ) -> PredicateResult<BufferedBody<B>>
Check if the operation matches the body.
Returns PredicateResult::Cacheable if the operation is satisfied,
PredicateResult::NonCacheable otherwise.