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
pub mod filter;
mod router;
pub use filter::*;
pub use router::{DetectMatched, Router};
use std::collections::HashMap;
use std::sync::Arc;
use crate::http::{Request, Response};
use crate::{Depot, Handler};
#[doc(hidden)]
pub type PathParams = HashMap<String, String>;
#[doc(hidden)]
#[derive(Debug, Eq, PartialEq)]
pub struct PathState {
pub(crate) url_path: String,
pub(crate) cursor: usize,
pub(crate) params: PathParams,
}
impl PathState {
pub fn new(url_path: &str) -> Self {
let url_path = url_path.trim_start_matches('/').trim_end_matches('/');
PathState {
url_path: decode_url_path_safely(url_path),
cursor: 0,
params: PathParams::new(),
}
}
pub(crate) fn ended(&self) -> bool {
self.cursor >= self.url_path.len()
}
}
fn decode_url_path_safely(path: &str) -> String {
percent_encoding::percent_decode_str(path)
.decode_utf8_lossy()
.to_string()
}
pub struct FlowCtrl {
is_ceased: bool,
cursor: usize,
pub(crate) handlers: Vec<Arc<dyn Handler>>,
}
impl FlowCtrl {
#[inline]
pub fn new(handlers: Vec<Arc<dyn Handler>>) -> Self {
FlowCtrl {
is_ceased: false,
cursor: 0,
handlers,
}
}
#[inline]
pub fn has_next(&self) -> bool {
self.cursor < self.handlers.len() && !self.handlers.is_empty()
}
#[inline]
pub async fn call_next(&mut self, req: &mut Request, depot: &mut Depot, res: &mut Response) -> bool {
if let Some(code) = res.status_code() {
if code.is_redirection() || !code.is_success() {
self.skip_reset();
return false;
}
}
if let Some(handler) = self.handlers.get(self.cursor) {
self.cursor += 1;
handler.clone().handle(req, depot, res, self).await;
true
} else {
false
}
}
#[inline]
pub fn skip_reset(&mut self) {
self.cursor = self.handlers.len()
}
#[inline]
pub fn is_ceased(&self) -> bool {
self.is_ceased
}
#[inline]
pub fn cease(&mut self) {
self.skip_reset();
self.is_ceased = true;
}
}