Skip to main content

tower_http_cache_plus/cache/middleware/
configuration.rs

1use super::{super::configuration::*, hooks::*};
2
3use kutil::http::*;
4
5/// Encodings in order from most preferred to least.
6///
7/// We are generally preferring to optimize for compute rather than bandwidth.
8///
9/// GZip and Deflate are almost identical, but we prefer GZip because it allows clients to check
10/// for errors.
11pub const ENCODINGS_BY_PREFERENCE: &[EncodingHeaderValue] = &[
12    EncodingHeaderValue::Brotli,
13    EncodingHeaderValue::GZip,
14    EncodingHeaderValue::Deflate,
15    EncodingHeaderValue::Zstandard,
16];
17
18//
19// MiddlewareCachingConfiguration
20//
21
22/// Middleware caching configuration.
23pub struct MiddlewareCachingConfiguration<RequestBodyT, CacheT, CacheKeyT> {
24    /// Cache.
25    pub cache: Option<CacheT>,
26
27    /// Cacheable by request (hook).
28    pub cacheable_by_request: Option<CacheableHook>,
29
30    /// Cacheable by response (hook).
31    pub cacheable_by_response: Option<CacheableHook>,
32
33    /// Cache key (hook).
34    pub cache_key: Option<CacheKeyHook<CacheKeyT, RequestBodyT>>,
35
36    /// Inner configuration.
37    pub inner: CachingConfiguration,
38}
39
40impl<RequestBodyT, CacheT, CacheKeyT> Default
41    for MiddlewareCachingConfiguration<RequestBodyT, CacheT, CacheKeyT>
42{
43    fn default() -> Self {
44        Self {
45            cache: None,
46            cacheable_by_request: None,
47            cacheable_by_response: None,
48            cache_key: None,
49            inner: CachingConfiguration {
50                min_body_size: 0,
51                max_body_size: 1024 * 1024, // 1 MiB
52                cacheable_by_default: true,
53                cache_duration: None,
54            },
55        }
56    }
57}
58
59impl<RequestBodyT, CacheT, CacheKeyT> Clone
60    for MiddlewareCachingConfiguration<RequestBodyT, CacheT, CacheKeyT>
61where
62    CacheT: Clone,
63{
64    fn clone(&self) -> Self {
65        // Unfortunately we can't get away with #[derive(Clone)]
66        // The culprit is the RequestBodyT generic param in CacheKeyHookContext
67        Self {
68            cache: self.cache.clone(),
69            cacheable_by_request: self.cacheable_by_request.clone(),
70            cacheable_by_response: self.cacheable_by_response.clone(),
71            cache_key: self.cache_key.clone(),
72            inner: self.inner.clone(),
73        }
74    }
75}
76
77//
78// MiddlewareEncodingConfiguration
79//
80
81/// Middleware encoding configuration.
82#[derive(Clone)]
83pub struct MiddlewareEncodingConfiguration {
84    /// Enabled encodings in order of preference.
85    pub enabled_encodings_by_preference: Option<Vec<EncodingHeaderValue>>,
86
87    /// Encodable by request (hook).
88    pub encodable_by_request: Option<EncodableHook>,
89
90    /// Encodable by response (hook).
91    pub encodable_by_response: Option<EncodableHook>,
92
93    /// Inner configuration.
94    pub inner: EncodingConfiguration,
95}
96
97impl Default for MiddlewareEncodingConfiguration {
98    fn default() -> Self {
99        Self {
100            enabled_encodings_by_preference: Some(ENCODINGS_BY_PREFERENCE.into()),
101            encodable_by_request: None,
102            encodable_by_response: None,
103            inner: EncodingConfiguration {
104                min_body_size: 0,
105                encodable_by_default: true,
106                keep_identity_encoding: true,
107            },
108        }
109    }
110}