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
use anymap::AnyMap;
use StatusCode;
use header::Headers;
use context::Context;
use response::Data;
use server::Global;
pub struct FilterContext<'a> {
pub storage: &'a mut AnyMap,
pub global: &'a Global,
}
pub trait ContextFilter: Send + Sync {
fn modify(&self, context: FilterContext, request_context: &mut Context) -> ContextAction;
}
#[derive(Clone)]
pub enum ContextAction {
Next,
Abort(StatusCode)
}
impl<'a> ContextAction {
pub fn next() -> ContextAction {
ContextAction::Next
}
pub fn abort(status: StatusCode) -> ContextAction {
ContextAction::Abort(status)
}
}
pub trait ResponseFilter: Send + Sync {
fn begin(&self, context: FilterContext, status: StatusCode, headers: &mut Headers) ->
(StatusCode, ResponseAction);
fn write<'a>(&'a self, context: FilterContext, content: Option<Data<'a>>) -> ResponseAction;
fn end(&self, context: FilterContext) -> ResponseAction;
}
#[derive(Clone)]
pub enum ResponseAction<'a> {
Next(Option<Data<'a>>),
SilentAbort,
Abort(String)
}
impl<'a> ResponseAction<'a> {
pub fn next<T: Into<Data<'a>>>(data: Option<T>) -> ResponseAction<'a> {
ResponseAction::Next(data.map(|d| d.into()))
}
pub fn silent_abort() -> ResponseAction<'a> {
ResponseAction::SilentAbort
}
pub fn abort(message: String) -> ResponseAction<'a> {
ResponseAction::Abort(message)
}
}