Skip to main content

hitbox_http/
cache_status.rs

1//! Cache status extension for HTTP responses.
2//!
3//! This module provides the [`CacheStatusExt`] implementation for HTTP responses,
4//! allowing cache status information to be attached as headers.
5
6use hitbox::{CacheStatus, CacheStatusExt};
7use http::{HeaderValue, header::HeaderName};
8use hyper::body::Body as HttpBody;
9
10use crate::CacheableHttpResponse;
11
12/// Default header name for cache status (HIT/MISS/STALE).
13///
14/// The value is `x-cache-status`. Use builder methods on cache middleware
15/// to customize the header name.
16pub const DEFAULT_CACHE_STATUS_HEADER: HeaderName = HeaderName::from_static("x-cache-status");
17
18impl<ResBody> CacheStatusExt for CacheableHttpResponse<ResBody>
19where
20    ResBody: HttpBody,
21{
22    type Config = HeaderName;
23
24    fn cache_status(&mut self, status: CacheStatus, config: &Self::Config) {
25        let value = match status {
26            CacheStatus::Hit => HeaderValue::from_static("HIT"),
27            CacheStatus::Miss => HeaderValue::from_static("MISS"),
28            CacheStatus::Stale => HeaderValue::from_static("STALE"),
29        };
30        self.parts.headers.insert(config.clone(), value);
31    }
32}