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
230
231
232
233
//! What web-net tells the rest of the system, and where it asks whether a
//! request may continue.
//!
//! A security component that cannot report what it sees is a detour, not a
//! defence. Every refusal, denial, and failure is reported through
//! [`Events`], so a detection or gate layer hears about a probe the moment
//! web-net turns it away. Events carry ids, codes, and statuses, never
//! request bytes.
use ;
use crateRequestId;
/// What kind of thing happened.
///
/// ```
/// use std::sync::Arc;
/// use std::sync::atomic::{AtomicU32, Ordering};
/// use web_net::{App, Event, EventKind, Events, get};
///
/// #[derive(Default)]
/// struct Refusals(AtomicU32);
/// impl Events for Refusals {
/// fn event(&self, e: &Event<'_>) {
/// if e.kind == EventKind::Refused {
/// self.0.fetch_add(1, Ordering::Relaxed);
/// }
/// }
/// }
///
/// let refusals = Arc::new(Refusals::default());
/// let app = App::builder().events(Arc::clone(&refusals)).build()?;
/// serve_net::testing::respond(&app, b"GET /missing HTTP/1.1\r\nHost: localhost\r\n\r\n");
/// assert_eq!(refusals.0.load(Ordering::Relaxed), 1);
/// # Ok::<(), web_net::BuildError>(())
/// ```
/// One event.
///
/// ```
/// use std::sync::{Arc, Mutex};
/// use web_net::{App, Event, Events, get};
///
/// #[derive(Default)]
/// struct Codes(Mutex<Vec<(u16, &'static str)>>);
/// impl Events for Codes {
/// fn event(&self, e: &Event<'_>) {
/// self.0.lock().unwrap().push((e.status, e.code));
/// }
/// }
///
/// let codes = Arc::new(Codes::default());
/// let app = App::builder().events(Arc::clone(&codes)).route(get("/", |cx| cx.text(200, "hi"))).build()?;
/// serve_net::testing::respond(&app, b"GET /missing HTTP/1.1\r\nHost: localhost\r\n\r\n");
/// assert_eq!(*codes.0.lock().unwrap(), [(404, "route.none")]);
/// # Ok::<(), web_net::BuildError>(())
/// ```
/// Where web-net reports. Every method has an empty default; the default
/// implementation, [`NoEvents`], reports nothing.
///
/// Called on the request path: keep it to a counter or a queue push.
///
/// ```
/// use std::sync::atomic::{AtomicU32, Ordering};
/// use web_net::{Event, Events};
///
/// // Counts every event; the app reports refusals, denials, and failures here.
/// #[derive(Default)]
/// struct Counter(AtomicU32);
/// impl Events for Counter {
/// fn event(&self, _: &Event<'_>) {
/// self.0.fetch_add(1, Ordering::Relaxed);
/// }
/// }
///
/// let app = web_net::App::builder().events(Counter::default()).build()?;
/// # let _ = app;
/// # Ok::<(), web_net::BuildError>(())
/// ```
/// Reports nothing.
///
/// ```
/// use web_net::{App, NoEvents};
///
/// // The default; setting it again turns reporting off.
/// let app = App::builder().events(NoEvents).build()?;
/// # let _ = app;
/// # Ok::<(), web_net::BuildError>(())
/// ```
;
/// A sink the application keeps a handle to, to read what it counted.
///
/// ```
/// use std::sync::Arc;
/// use std::sync::atomic::{AtomicU32, Ordering};
/// use web_net::{App, Event, Events, get};
///
/// #[derive(Default)]
/// struct Refusals(AtomicU32);
/// impl Events for Refusals {
/// fn event(&self, _: &Event<'_>) {
/// self.0.fetch_add(1, Ordering::Relaxed);
/// }
/// }
///
/// let seen = Arc::new(Refusals::default());
/// let app = App::builder()
/// .events(Arc::clone(&seen))
/// .route(get("/", |cx| cx.text(200, "hi")))
/// .build()?;
/// serve_net::testing::respond(&app, b"GET /missing HTTP/1.1\r\nHost: localhost\r\n\r\n");
/// assert_eq!(seen.0.load(Ordering::Relaxed), 1);
/// # Ok::<(), web_net::BuildError>(())
/// ```
/// A point in web-net's pipeline where a checkpoint runs.
///
/// ```
/// use serve_net::{Envelope, Verdict};
/// use web_net::{App, Checkpoint, Stage, get};
///
/// // Stops requests once their access check passed.
/// struct AfterAccess;
/// impl Checkpoint for AfterAccess {
/// fn check(&self, _: &Envelope, at: Stage) -> Verdict {
/// if at == Stage::Authorized { Verdict::Deny { status: 503 } } else { Verdict::Pass }
/// }
/// }
///
/// let app = App::builder().checkpoint(AfterAccess).route(get("/", |cx| cx.text(200, "hi"))).build()?;
/// let r = serve_net::testing::respond(&app, b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
/// assert_eq!(r.status, 503);
/// # Ok::<(), web_net::BuildError>(())
/// ```
/// Revocation in flight: a ban, a revoked session, or a lockdown that
/// arrives while a request is being handled. serve-net's own checkpoint
/// covers admission and the moment before the reply leaves; this one covers
/// the points in between that only web-net knows.
///
/// A checkpoint backed by remote state fails closed (denies) when the
/// remote cannot be reached.
///
/// ```
/// use std::sync::Arc;
/// use std::sync::atomic::{AtomicBool, Ordering};
/// use serve_net::testing::respond;
/// use serve_net::{Envelope, Verdict};
/// use web_net::{App, Checkpoint, Stage, get};
///
/// #[derive(Default)]
/// struct Lockdown(AtomicBool);
/// impl Checkpoint for Lockdown {
/// fn check(&self, _: &Envelope, _: Stage) -> Verdict {
/// if self.0.load(Ordering::Relaxed) { Verdict::Deny { status: 503 } } else { Verdict::Pass }
/// }
/// }
///
/// let lockdown = Arc::new(Lockdown::default());
/// let app = App::builder().checkpoint(Arc::clone(&lockdown)).route(get("/", |cx| cx.text(200, "hi"))).build()?;
/// let req = b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n";
/// assert_eq!(respond(&app, req).status, 200);
/// lockdown.0.store(true, Ordering::Relaxed);
/// assert_eq!(respond(&app, req).status, 503);
/// # Ok::<(), web_net::BuildError>(())
/// ```
/// A checkpoint shared with the code that revokes.