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
use std::fmt::{self, Formatter};

use crate::http::uri::Scheme;
use crate::http::{Method, Request};
use crate::routing::{Filter, PathState};

/// Filter by request method
#[derive(Clone, PartialEq, Eq)]
pub struct MethodFilter(pub Method);
impl MethodFilter {
    /// Create a new `MethodFilter`.
    pub fn new(method: Method) -> Self {
        Self(method)
    }
}
impl Filter for MethodFilter {
    #[inline]
    fn filter(&self, req: &mut Request, _state: &mut PathState) -> bool {
        req.method() == self.0
    }
}
impl fmt::Debug for MethodFilter {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "method:{:?}", self.0)
    }
}

/// Filter by request uri scheme.
#[derive(Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct SchemeFilter {
    /// Scheme to filter.
    pub scheme: Scheme,
    /// When scheme is lack in request uri, use this value.
    pub lack: bool,
}
impl SchemeFilter {
    /// Create a new `SchemeFilter`.
    pub fn new(scheme: Scheme) -> Self {
        Self { scheme, lack: false }
    }
    /// Set lack value and return `Self`.
    pub fn lack(mut self, lack: bool) -> Self {
        self.lack = lack;
        self
    }
}
impl Filter for SchemeFilter {
    #[inline]
    fn filter(&self, req: &mut Request, _state: &mut PathState) -> bool {
        req.uri().scheme().map(|s| s == &self.scheme).unwrap_or(self.lack)
    }
}
impl fmt::Debug for SchemeFilter {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "scheme:{:?}", self.scheme)
    }
}

/// Filter by request uri host.
#[derive(Clone, PartialEq, Eq)]
pub struct HostFilter {
    /// Host to filter.
    pub host: String,
    /// When host is lack in request uri, use this value.
    pub lack: bool,
}
impl HostFilter {
    /// Create a new `HostFilter`.
    pub fn new(host: impl Into<String>) -> Self {
        Self {
            host: host.into(),
            lack: false,
        }
    }
    /// Set lack value and return `Self`.
    pub fn lack(mut self, lack: bool) -> Self {
        self.lack = lack;
        self
    }
}
impl Filter for HostFilter {
    #[inline]
    fn filter(&self, req: &mut Request, _state: &mut PathState) -> bool {
        // Http1, if `fix-http1-request-uri` feature is disabled, host is lack. so use header host instead.
        // https://github.com/hyperium/hyper/issues/1310
        #[cfg(feature = "fix-http1-request-uri")]
        let host = req.uri().authority().map(|a| a.as_str());
        #[cfg(not(feature = "fix-http1-request-uri"))]
        let host = req.uri().authority().map(|a| a.as_str()).or_else(|| {
            req.headers()
                .get(crate::http::header::HOST)
                .and_then(|h| h.to_str().ok())
        });
        host.map(|h| {
            if h.contains(':') {
                h.rsplit_once(':')
                    .expect("rsplit_once by ':' should not returns `None`")
                    .0
            } else {
                h
            }
        })
        .map(|h| h == self.host)
        .unwrap_or(self.lack)
    }
}
impl fmt::Debug for HostFilter {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "host:{:?}", self.host)
    }
}

/// Filter by request uri host.
#[derive(Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct PortFilter {
    /// Port to filter.
    pub port: u16,
    /// When port is lack in request uri, use this value.
    pub lack: bool,
}

impl PortFilter {
    /// Create a new `PortFilter`.
    pub fn new(port: u16) -> Self {
        Self { port, lack: false }
    }
    /// Set lack value and return `Self`.
    pub fn lack(mut self, lack: bool) -> Self {
        self.lack = lack;
        self
    }
}
impl Filter for PortFilter {
    #[inline]
    fn filter(&self, req: &mut Request, _state: &mut PathState) -> bool {
        // Http1, if `fix-http1-request-uri` feature is disabled, port is lack. so use header host instead.
        // https://github.com/hyperium/hyper/issues/1310
        #[cfg(feature = "fix-http1-request-uri")]
        let host = req.uri().authority().map(|a| a.as_str());
        #[cfg(not(feature = "fix-http1-request-uri"))]
        let host = req.uri().authority().map(|a| a.as_str()).or_else(|| {
            req.headers()
                .get(crate::http::header::HOST)
                .and_then(|h| h.to_str().ok())
        });
        host.map(|h| {
            if h.contains(':') {
                h.rsplit_once(':')
                    .expect("rsplit_once by ':' should not returns `None`")
                    .1
            } else {
                h
            }
        })
        .and_then(|p| p.parse::<u16>().ok())
        .map(|p| p == self.port)
        .unwrap_or(self.lack)
    }
}
impl fmt::Debug for PortFilter {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "port:{:?}", self.port)
    }
}