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
use crate::state::{request_id, State};
use hyper::body::HttpBody;
use hyper::{Body, Response};
use log::trace;
use std::panic::RefUnwindSafe;
pub trait StaticResponseExtender: RefUnwindSafe {
type ResBody: HttpBody;
fn extend(state: &mut State, response: &mut Response<Self::ResBody>);
}
pub trait ResponseExtender<B>: RefUnwindSafe {
fn extend(&self, state: &mut State, response: &mut Response<B>);
}
impl<F, B> ResponseExtender<B> for F
where
F: Fn(&mut State, &mut Response<B>) + Send + Sync + RefUnwindSafe,
{
fn extend(&self, state: &mut State, res: &mut Response<B>) {
trace!(
"[{}] running closure based response extender",
request_id(state)
);
self(state, res);
}
}
pub struct NoopResponseExtender;
impl StaticResponseExtender for NoopResponseExtender {
type ResBody = Body;
fn extend(state: &mut State, _res: &mut Response<Body>) {
trace!(
"[{}] NoopResponseExtender invoked, does not make any changes to Response",
request_id(state)
);
trace!("[{}] no response body, no change made", request_id(state));
}
}
impl ResponseExtender<Body> for NoopResponseExtender {
fn extend(&self, state: &mut State, _res: &mut Response<Body>) {
trace!(
"[{}] NoopResponseExtender invoked on instance, does not make any changes to Response",
request_id(state)
);
trace!("[{}] no response body, no change made", request_id(state));
}
}