kutil_http/cache/middleware/
configuration.rs

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