rama_http/layer/set_header/request/
header.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use crate::{HeaderName, HeaderValue, Request};
use rama_core::Context;
use std::{
    future::{ready, Future},
    marker::PhantomData,
};

/// Trait for producing header values.
///
/// Used by [`SetRequestHeader`] and [`SetResponseHeader`].
///
/// This trait is implemented for closures with the correct type signature. Typically users will
/// not have to implement this trait for their own types.
///
/// It is also implemented directly for [`HeaderValue`]. When a fixed header value should be added
/// to all responses, it can be supplied directly to the middleware.
pub trait MakeHeaderValue<S, B>: Send + Sync + 'static {
    /// Try to create a header value from the request or response.
    fn make_header_value(
        &self,
        ctx: Context<S>,
        req: Request<B>,
    ) -> impl Future<Output = (Context<S>, Request<B>, Option<HeaderValue>)> + Send + '_;
}

/// Functional version of [`MakeHeaderValue`].
pub trait MakeHeaderValueFn<S, B, A>: Send + Sync + 'static {
    /// Try to create a header value from the request or response.
    fn call(
        &self,
        ctx: Context<S>,
        req: Request<B>,
    ) -> impl Future<Output = (Context<S>, Request<B>, Option<HeaderValue>)> + Send + '_;
}

impl<F, Fut, S, B> MakeHeaderValueFn<S, B, ()> for F
where
    S: Clone + Send + Sync + 'static,
    B: Send + 'static,
    F: Fn() -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Option<HeaderValue>> + Send + 'static,
{
    async fn call(
        &self,
        ctx: Context<S>,
        req: Request<B>,
    ) -> (Context<S>, Request<B>, Option<HeaderValue>) {
        let maybe_value = self().await;
        (ctx, req, maybe_value)
    }
}

impl<F, Fut, S, B> MakeHeaderValueFn<S, B, ((), B)> for F
where
    S: Clone + Send + Sync + 'static,
    B: Send + 'static,
    F: Fn(Request<B>) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = (Request<B>, Option<HeaderValue>)> + Send + 'static,
{
    async fn call(
        &self,
        ctx: Context<S>,
        req: Request<B>,
    ) -> (Context<S>, Request<B>, Option<HeaderValue>) {
        let (req, maybe_value) = self(req).await;
        (ctx, req, maybe_value)
    }
}

impl<F, Fut, S, B> MakeHeaderValueFn<S, B, (Context<S>,)> for F
where
    S: Clone + Send + Sync + 'static,
    B: Send + 'static,
    F: Fn(Context<S>) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = (Context<S>, Option<HeaderValue>)> + Send + 'static,
{
    async fn call(
        &self,
        ctx: Context<S>,
        req: Request<B>,
    ) -> (Context<S>, Request<B>, Option<HeaderValue>) {
        let (ctx, maybe_value) = self(ctx).await;
        (ctx, req, maybe_value)
    }
}

impl<F, Fut, S, B> MakeHeaderValueFn<S, B, (Context<S>, B)> for F
where
    F: Fn(Context<S>, Request<B>) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = (Context<S>, Request<B>, Option<HeaderValue>)> + Send + 'static,
{
    fn call(
        &self,
        ctx: Context<S>,
        req: Request<B>,
    ) -> impl Future<Output = (Context<S>, Request<B>, Option<HeaderValue>)> + Send + '_ {
        self(ctx, req)
    }
}

/// The public wrapper type for [`MakeHeaderValueFn`].
pub struct BoxMakeHeaderValueFn<F, A> {
    f: F,
    _marker: PhantomData<fn(A) -> ()>,
}

impl<F, A> BoxMakeHeaderValueFn<F, A> {
    /// Create a new [`BoxMakeHeaderValueFn`].
    pub const fn new(f: F) -> Self {
        Self {
            f,
            _marker: PhantomData,
        }
    }
}

impl<F, A> Clone for BoxMakeHeaderValueFn<F, A>
where
    F: Clone,
{
    fn clone(&self) -> Self {
        Self {
            f: self.f.clone(),
            _marker: PhantomData,
        }
    }
}

impl<F, A> std::fmt::Debug for BoxMakeHeaderValueFn<F, A>
where
    F: std::fmt::Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BoxMakeHeaderValueFn")
            .field("f", &self.f)
            .finish()
    }
}

impl<S, B, A, F> MakeHeaderValue<S, B> for BoxMakeHeaderValueFn<F, A>
where
    A: Send + 'static,
    F: MakeHeaderValueFn<S, B, A>,
{
    fn make_header_value(
        &self,
        ctx: Context<S>,
        req: Request<B>,
    ) -> impl Future<Output = (Context<S>, Request<B>, Option<HeaderValue>)> + Send + '_ {
        self.f.call(ctx, req)
    }
}

impl<S, B> MakeHeaderValue<S, B> for HeaderValue
where
    S: Clone + Send + Sync + 'static,
    B: Send + 'static,
{
    fn make_header_value(
        &self,
        ctx: Context<S>,
        req: Request<B>,
    ) -> impl Future<Output = (Context<S>, Request<B>, Option<HeaderValue>)> + Send + '_ {
        ready((ctx, req, Some(self.clone())))
    }
}

impl<S, B> MakeHeaderValue<S, B> for Option<HeaderValue>
where
    S: Clone + Send + Sync + 'static,
    B: Send + 'static,
{
    fn make_header_value(
        &self,
        ctx: Context<S>,
        req: Request<B>,
    ) -> impl Future<Output = (Context<S>, Request<B>, Option<HeaderValue>)> + Send + '_ {
        ready((ctx, req, self.clone()))
    }
}

#[derive(Debug, Clone, Copy)]
pub(super) enum InsertHeaderMode {
    Override,
    Append,
    IfNotPresent,
}

impl InsertHeaderMode {
    pub(super) async fn apply<S, B, M>(
        self,
        header_name: &HeaderName,
        ctx: Context<S>,
        req: Request<B>,
        make: &M,
    ) -> (Context<S>, Request<B>)
    where
        B: Send + 'static,
        M: MakeHeaderValue<S, B>,
    {
        match self {
            InsertHeaderMode::Override => {
                let (ctx, mut req, maybe_value) = make.make_header_value(ctx, req).await;
                if let Some(value) = maybe_value {
                    req.headers_mut().insert(header_name.clone(), value);
                }
                (ctx, req)
            }
            InsertHeaderMode::IfNotPresent => {
                if !req.headers().contains_key(header_name) {
                    let (ctx, mut req, maybe_value) = make.make_header_value(ctx, req).await;
                    if let Some(value) = maybe_value {
                        req.headers_mut().insert(header_name.clone(), value);
                    }
                    (ctx, req)
                } else {
                    (ctx, req)
                }
            }
            InsertHeaderMode::Append => {
                let (ctx, mut req, maybe_value) = make.make_header_value(ctx, req).await;
                if let Some(value) = maybe_value {
                    req.headers_mut().append(header_name.clone(), value);
                }
                (ctx, req)
            }
        }
    }
}