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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Per-location request matchers.
//
// A `Matcher` is a list of `MatchPredicate`s evaluated with AND
// semantics: every predicate must accept the request for the
// matcher to accept. Within a single predicate (method-set,
// header-value list, query-value list), the listed alternatives
// combine with OR semantics.
//
// Matchers are attached to `location` blocks. When a location has
// a matcher and the matcher rejects a request, the router falls
// through to the next-best candidate (next-shortest matching
// prefix, in declaration order on ties) instead of dispatching.
use hyper::Method;
use hyper::Request;
use hyper::header::HeaderName;
use regex::Regex;
/// A matcher's predicate list. AND across the list; OR within
/// each predicate's value set. An empty `predicates` accepts
/// every request -- callers store `Option<Arc<Matcher>>` so they
/// can avoid building an empty one.
pub struct Matcher {
pub predicates: Vec<MatchPredicate>,
}
pub enum MatchPredicate {
/// Request method must be one of the listed methods.
Method(Vec<Method>),
/// Named header's value must satisfy one of the listed
/// `HeaderMatch`es. A missing header always fails;
/// `HeaderAbsent` is the explicit form for "match when
/// missing".
Header {
name: HeaderName,
values: Vec<HeaderMatch>,
},
/// Matches precisely when the named header is missing from
/// the request. An empty value counts as present, in line
/// with hyper's HeaderMap.
HeaderAbsent { name: HeaderName },
/// Named query parameter must equal one of the listed
/// values. The parameter is looked up in the request's
/// raw query string (first occurrence wins). Missing
/// parameter fails the predicate.
Query {
name: String,
values: Vec<String>,
},
/// URI path must match at least one of the configured
/// regexes. Patterns are evaluated unanchored, so operators
/// who want a whole-path match should include `^...$`
/// themselves.
Path(Vec<Regex>),
/// Inverts a group of predicates. The inner list is
/// AND-evaluated and the whole result negated, so
/// `not { method "GET"; header "X" "y" }` matches when it
/// is *not* the case that both inner predicates hold.
Not(Vec<MatchPredicate>),
}
/// One alternative for a header-value predicate.
pub enum HeaderMatch {
/// Exact byte-for-byte comparison against the header value.
Exact(String),
/// Anchored regex match against the header value.
Regex(Regex),
}
impl Matcher {
/// `true` iff every predicate accepts the request.
pub fn matches<B>(&self, req: &Request<B>) -> bool {
self.predicates.iter().all(|p| p.matches(req))
}
}
impl MatchPredicate {
fn matches<B>(&self, req: &Request<B>) -> bool {
match self {
MatchPredicate::Method(methods) => {
methods.iter().any(|m| m == req.method())
}
MatchPredicate::Header { name, values } => {
match req.headers().get(name) {
Some(v) => match v.to_str() {
Ok(s) => values.iter().any(|m| match m {
HeaderMatch::Exact(want) => want == s,
HeaderMatch::Regex(re) => re.is_match(s),
}),
// Non-UTF-8 header values can't match any
// configured pattern -- both string-exact and
// regex paths require a `&str`.
Err(_) => false,
},
None => false,
}
}
MatchPredicate::HeaderAbsent { name } => {
!req.headers().contains_key(name)
}
MatchPredicate::Query { name, values } => {
let q = match req.uri().query() {
Some(q) => q,
None => return false,
};
// First-occurrence semantics keeps behaviour
// predictable when the same key appears more
// than once.
for pair in q.split('&') {
let mut it = pair.splitn(2, '=');
let k = it.next().unwrap_or("");
let v = it.next().unwrap_or("");
if k == name {
return values.iter().any(|w| w == v);
}
}
false
}
MatchPredicate::Path(patterns) => {
let path = req.uri().path();
patterns.iter().any(|re| re.is_match(path))
}
MatchPredicate::Not(inner) => {
// AND inside, then invert. An empty inner list
// shouldn't reach runtime (the parser rejects it)
// but if it does, treat it as a vacuous truth
// that the negation flips to `false`.
let inner_all = inner.iter().all(|p| p.matches(req));
!inner_all
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use hyper::Request;
fn req(method: &str, uri: &str, headers: &[(&str, &str)])
-> Request<()>
{
let mut b = Request::builder()
.method(method)
.uri(uri);
for (k, v) in headers {
b = b.header(*k, *v);
}
b.body(()).unwrap()
}
#[test]
fn method_predicate_or_within_list() {
let m = Matcher {
predicates: vec![MatchPredicate::Method(vec![
Method::POST,
Method::PUT,
])],
};
assert!(m.matches(&req("POST", "/", &[])));
assert!(m.matches(&req("PUT", "/", &[])));
assert!(!m.matches(&req("GET", "/", &[])));
}
#[test]
fn header_predicate_exact_and_regex() {
let m = Matcher {
predicates: vec![MatchPredicate::Header {
name: HeaderName::from_static("x-api-version"),
values: vec![
HeaderMatch::Exact("v1".to_string()),
HeaderMatch::Regex(
Regex::new("^v[23]$").unwrap()
),
],
}],
};
assert!(m.matches(
&req("GET", "/", &[("X-API-Version", "v1")])
));
assert!(m.matches(
&req("GET", "/", &[("X-API-Version", "v2")])
));
assert!(m.matches(
&req("GET", "/", &[("X-API-Version", "v3")])
));
assert!(!m.matches(
&req("GET", "/", &[("X-API-Version", "v4")])
));
// Missing header -> no match.
assert!(!m.matches(&req("GET", "/", &[])));
}
#[test]
fn query_predicate_first_occurrence_wins() {
let m = Matcher {
predicates: vec![MatchPredicate::Query {
name: "format".to_string(),
values: vec!["json".to_string()],
}],
};
assert!(m.matches(&req("GET", "/?format=json", &[])));
assert!(m.matches(&req("GET", "/?a=1&format=json", &[])));
assert!(!m.matches(&req("GET", "/?format=xml", &[])));
// First-occurrence: a later `format=json` does not save
// an earlier `format=xml`.
assert!(!m.matches(
&req("GET", "/?format=xml&format=json", &[])
));
// Missing query string entirely.
assert!(!m.matches(&req("GET", "/", &[])));
}
#[test]
fn predicates_combine_with_and() {
let m = Matcher {
predicates: vec![
MatchPredicate::Method(vec![Method::POST]),
MatchPredicate::Header {
name: HeaderName::from_static("x-tenant"),
values: vec![HeaderMatch::Exact(
"acme".to_string()
)],
},
],
};
assert!(m.matches(
&req("POST", "/", &[("X-Tenant", "acme")])
));
// Method fails.
assert!(!m.matches(
&req("GET", "/", &[("X-Tenant", "acme")])
));
// Header fails.
assert!(!m.matches(
&req("POST", "/", &[("X-Tenant", "other")])
));
}
#[test]
fn empty_predicate_list_accepts() {
// Defensive: the config parser refuses to build an empty
// matcher, but the evaluator must still degrade safely if
// one slips through (e.g. via a programmatic builder).
let m = Matcher { predicates: vec![] };
assert!(m.matches(&req("GET", "/", &[])));
}
#[test]
fn header_absent_predicate() {
let m = Matcher {
predicates: vec![MatchPredicate::HeaderAbsent {
name: HeaderName::from_static("authorization"),
}],
};
assert!(m.matches(&req("GET", "/", &[])));
// Even an empty header counts as present (hyper
// distinguishes the two), so the predicate fails.
assert!(!m.matches(
&req("GET", "/", &[("Authorization", "")])
));
assert!(!m.matches(
&req("GET", "/", &[("Authorization", "Bearer x")])
));
}
#[test]
fn path_predicate_is_unanchored() {
// Patterns are matched unanchored, so a bare `admin`
// matches anywhere in the path -- including the
// middle of a segment. This is documented behaviour;
// operators who want strict boundaries write
// explicit `^...$` or use word boundaries.
let m = Matcher {
predicates: vec![MatchPredicate::Path(vec![
Regex::new("admin").unwrap(),
])],
};
assert!(m.matches(&req("GET", "/admin/users", &[])));
assert!(m.matches(&req("GET", "/api/admin", &[])));
assert!(m.matches(&req("GET", "/uber/admin/x", &[])));
// Substring match: even `/sysadmin/x` hits because
// the regex isn't anchored.
assert!(m.matches(&req("GET", "/sysadmin/x", &[])));
assert!(!m.matches(&req("GET", "/users", &[])));
}
#[test]
fn path_predicate_anchors_when_caret_dollar() {
// Operators control anchoring themselves. `^/admin$`
// matches only the exact path.
let m = Matcher {
predicates: vec![MatchPredicate::Path(vec![
Regex::new("^/admin$").unwrap(),
])],
};
assert!(m.matches(&req("GET", "/admin", &[])));
assert!(!m.matches(&req("GET", "/admin/users", &[])));
assert!(!m.matches(&req("GET", "/sysadmin", &[])));
}
#[test]
fn path_predicate_or_within_list() {
let m = Matcher {
predicates: vec![MatchPredicate::Path(vec![
Regex::new(r"\.jpg$").unwrap(),
Regex::new(r"\.png$").unwrap(),
])],
};
assert!(m.matches(&req("GET", "/img/cat.jpg", &[])));
assert!(m.matches(&req("GET", "/img/cat.png", &[])));
assert!(!m.matches(&req("GET", "/img/cat.gif", &[])));
}
#[test]
fn not_predicate_inverts_and_of_inner() {
// not { method GET } -> matches anything except GET.
let m = Matcher {
predicates: vec![MatchPredicate::Not(vec![
MatchPredicate::Method(vec![Method::GET]),
])],
};
assert!(m.matches(&req("POST", "/", &[])));
assert!(!m.matches(&req("GET", "/", &[])));
}
#[test]
fn not_predicate_and_of_multiple_inner() {
// not { method GET; header X y } -> matches unless both
// (method is GET AND X=y); so a GET with X=other still
// matches because the inner AND is false.
let m = Matcher {
predicates: vec![MatchPredicate::Not(vec![
MatchPredicate::Method(vec![Method::GET]),
MatchPredicate::Header {
name: HeaderName::from_static("x-blocked"),
values: vec![HeaderMatch::Exact(
"yes".to_string()
)],
},
])],
};
// Both inner predicates true -> outer negation fails.
assert!(!m.matches(
&req("GET", "/", &[("X-Blocked", "yes")])
));
// Method matches but header doesn't -> inner AND false
// -> negation matches.
assert!(m.matches(
&req("GET", "/", &[("X-Blocked", "no")])
));
// Neither matches -> negation matches.
assert!(m.matches(&req("POST", "/", &[])));
}
#[test]
fn non_utf8_header_value_never_matches() {
let mut r: Request<()> = Request::builder()
.method("GET")
.uri("/")
.body(())
.unwrap();
r.headers_mut().insert(
HeaderName::from_static("x-bin"),
hyper::header::HeaderValue::from_bytes(b"\xff\xfe")
.unwrap(),
);
let m = Matcher {
predicates: vec![MatchPredicate::Header {
name: HeaderName::from_static("x-bin"),
values: vec![HeaderMatch::Regex(
Regex::new(".*").unwrap()
)],
}],
};
assert!(!m.matches(&r));
}
}