tower_etag_cache/response/
mod.rs

1use http::HeaderMap;
2use pin_project::pin_project;
3
4#[cfg(feature = "http-body-impl")]
5pub mod http_body_impl;
6
7/// `http::Response` body type of [`EtagCache`](crate::EtagCache)
8#[pin_project(project = EtagCacheResBodyProj)]
9pub enum EtagCacheResBody<ResBody, TResBody> {
10    Miss(#[pin] TResBody),
11    Passthrough(#[pin] ResBody),
12
13    /// 304 response. Should return empty http body
14    Hit,
15}
16
17impl<ResBody, TResBody> EtagCacheResBody<ResBody, TResBody> {
18    pub fn hit_resp(headers: HeaderMap) -> http::Result<http::Response<Self>> {
19        let mut builder = http::response::Builder::new().status(http::StatusCode::NOT_MODIFIED);
20        *builder.headers_mut().unwrap() = headers;
21        builder.body(Self::Hit)
22    }
23
24    pub fn passthrough_resp(resp: http::Response<ResBody>) -> http::Response<Self> {
25        let (parts, body) = resp.into_parts();
26        http::Response::from_parts(parts, Self::Passthrough(body))
27    }
28
29    pub fn miss_resp(resp: http::Response<TResBody>) -> http::Response<Self> {
30        let (parts, body) = resp.into_parts();
31        http::Response::from_parts(parts, Self::Miss(body))
32    }
33}