hitbox-http
HTTP caching primitives for the Hitbox framework.
This crate provides the building blocks for caching HTTP requests and responses: predicates to determine cacheability, extractors to generate cache keys, and body utilities for transparent request/response handling.
Core Concepts
-
Predicate: Evaluates whether a request or response should be cached. Returns
CacheableorNonCacheable. -
Extractor: Generates cache key parts from HTTP components (method, path, headers, query parameters, body).
-
[
CacheableSubject]: A trait that allows predicates and extractors to work uniformly with both requests and responses. -
[
BufferedBody]: A body wrapper with three states (Complete,Partial,Passthrough) enabling transparent caching without disrupting the HTTP stream.
Quickstart
use Duration;
use PolicyConfig;
use PredicateExt;
use Endpoint;
use ;
# use Bytes;
# use Empty;
// Build a cache configuration for an endpoint
let config = builder
// Skip cache when Cache-Control: no-cache is present
.request_predicate
// Only cache successful responses
.response_predicate
// Build cache key from method, path parameters, and query
.extractor
// Cache for 5 minutes
.policy
.build;
# let _: = config;
Predicates
Predicates determine whether a request or response is cacheable.
Request Predicates
| Predicate | Description |
|---|---|
[predicates::request::Method] |
Match by HTTP method (GET, POST, etc.) |
[predicates::request::Path] |
Match by path pattern |
[predicates::request::Header] |
Match by header presence or value |
[predicates::request::Query] |
Match by query parameter |
[predicates::request::Body] |
Match by request body content |
Response Predicates
| Predicate | Description |
|---|---|
[predicates::response::StatusCode] |
Match by status code or class |
[predicates::response::Header] |
Match by header presence or value |
[predicates::response::Body] |
Match by response body content |
Combining Predicates
Use PredicateExt methods to combine predicates:
use PredicateExt;
use ;
# use Bytes;
# use Empty;
# use Neutral;
# use CacheableHttpRequest;
# type Subject = ;
// Skip cache when Cache-Control contains "no-cache"
let skip_no_cache = new;
# let _: & = &skip_no_cache;
let skip_no_cache = skip_no_cache.not;
// Skip cache when Authorization header exists
let skip_auth = new.not;
// Combine: cache only if BOTH conditions pass
let combined = skip_no_cache.and;
Extractors
Extractors generate cache key parts from HTTP components. Chain them using the builder pattern:
use ;
# use Bytes;
# use Empty;
# use ;
let extractor = new
.path
.query
.query;
# let _: = extractor;
| Extractor | Description |
|---|---|
[extractors::Method] |
Extract HTTP method |
[extractors::Path] |
Extract path parameters using patterns like /users/{id} |
[extractors::header] |
Extract header values |
[extractors::query] |
Extract query parameters |
[extractors::body] |
Extract from body (hash, JQ, regex) |
[extractors::Version] |
Extract HTTP version |
Main Types
- [
CacheableHttpRequest]: Wraps an HTTP request for cache evaluation. - [
CacheableHttpResponse]: Wraps an HTTP response for cache storage. - [
SerializableHttpResponse]: Serialized form of a response for cache backends.
Feature Flags
rkyv_format: Enables zero-copy deserialization using rkyv.