zentinel-proxy 0.6.28

A security-first reverse proxy built on Pingora with sleepable ops at the edge
Documentation
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
//! Route matching and selection module for Zentinel proxy
//!
//! This module implements the routing logic for matching incoming requests
//! to configured routes based on various criteria (path, host, headers, etc.)
//! with support for priority-based evaluation.

use dashmap::DashMap;
use prometheus::{register_int_counter, IntCounter};
use regex::Regex;
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, LazyLock};
use tracing::{debug, info, trace, warn};

/// Entries evicted from the route-match cache to enforce `route-cache-size`.
static ROUTE_CACHE_EVICTIONS: LazyLock<Option<IntCounter>> = LazyLock::new(|| {
    register_int_counter!(
        "zentinel_route_cache_evictions_total",
        "Route-match cache entries evicted to enforce route-cache-size"
    )
    .ok()
});

use zentinel_common::types::Priority;
use zentinel_common::RouteId;
use zentinel_config::{MatchCondition, RouteConfig, RoutePolicies};

/// Route matcher for efficient route selection
pub struct RouteMatcher {
    /// Routes sorted by priority (highest first)
    routes: Vec<CompiledRoute>,
    /// Default route ID if no match found
    default_route: Option<RouteId>,
    /// Cache for frequently matched routes (lock-free concurrent access)
    cache: Arc<RouteCache>,
    /// Whether any route requires header matching (optimization flag)
    needs_headers: bool,
    /// Whether any route requires query param matching (optimization flag)
    needs_query_params: bool,
}

/// Compiled route with pre-processed match conditions
struct CompiledRoute {
    /// Route configuration
    config: Arc<RouteConfig>,
    /// Route ID for quick lookup
    id: RouteId,
    /// Priority for ordering
    priority: Priority,
    /// Compiled match conditions
    matchers: Vec<CompiledMatcher>,
}

/// Compiled match condition for efficient evaluation
enum CompiledMatcher {
    /// Exact path match
    Path(String),
    /// Path prefix match
    PathPrefix(String),
    /// Regex path match
    PathRegex(Regex),
    /// Host match (exact or wildcard)
    Host(HostMatcher),
    /// Header presence or value match
    Header { name: String, value: Option<String> },
    /// HTTP method match
    Method(Vec<String>),
    /// Query parameter match
    QueryParam { name: String, value: Option<String> },
}

/// Host matching logic
enum HostMatcher {
    /// Exact host match
    Exact(String),
    /// Wildcard match (*.example.com)
    Wildcard { suffix: String },
    /// Regex match
    Regex(Regex),
}

/// Route cache for performance (lock-free concurrent access)
struct RouteCache {
    /// Cache entries (cache key -> route ID) - lock-free concurrent map
    entries: DashMap<String, RouteId>,
    /// Maximum cache size
    max_size: usize,
    /// Current entry count (approximate, for eviction decisions)
    entry_count: AtomicUsize,
    /// Cache hits counter
    hits: AtomicU64,
    /// Cache misses counter
    misses: AtomicU64,
}

impl RouteMatcher {
    /// Create a new route matcher from configuration with the default
    /// route-cache size (1000 entries).
    pub fn new(
        routes: Vec<RouteConfig>,
        default_route: Option<String>,
    ) -> Result<Self, RouteError> {
        Self::with_cache_size(routes, default_route, 1000)
    }

    /// Create a new route matcher with an explicit route-cache size
    /// (`system { route-cache-size N }`).
    pub fn with_cache_size(
        routes: Vec<RouteConfig>,
        default_route: Option<String>,
        cache_size: usize,
    ) -> Result<Self, RouteError> {
        info!(
            route_count = routes.len(),
            default_route = ?default_route,
            "Initializing route matcher"
        );

        let mut compiled_routes = Vec::new();

        for route in routes {
            trace!(
                route_id = %route.id,
                priority = ?route.priority,
                match_count = route.matches.len(),
                "Compiling route"
            );
            let compiled = CompiledRoute::compile(route)?;
            compiled_routes.push(compiled);
        }

        // Sort by priority (highest first), then by specificity
        compiled_routes.sort_by(|a, b| {
            b.priority
                .cmp(&a.priority)
                .then_with(|| b.specificity().cmp(&a.specificity()))
        });

        // Log final route order
        for (index, route) in compiled_routes.iter().enumerate() {
            debug!(
                route_id = %route.id,
                order = index,
                priority = ?route.priority,
                specificity = route.specificity(),
                "Route compiled and ordered"
            );
        }

        // Determine if any routes need headers or query params (optimization)
        let needs_headers = compiled_routes.iter().any(|r| {
            r.matchers
                .iter()
                .any(|m| matches!(m, CompiledMatcher::Header { .. }))
        });
        let needs_query_params = compiled_routes.iter().any(|r| {
            r.matchers
                .iter()
                .any(|m| matches!(m, CompiledMatcher::QueryParam { .. }))
        });

        info!(
            compiled_routes = compiled_routes.len(),
            needs_headers, needs_query_params, "Route matcher initialized"
        );

        Ok(Self {
            routes: compiled_routes,
            default_route: default_route.map(RouteId::new),
            cache: Arc::new(RouteCache::new(cache_size)),
            needs_headers,
            needs_query_params,
        })
    }

    /// Check if any route requires header matching
    #[inline]
    pub fn needs_headers(&self) -> bool {
        self.needs_headers
    }

    /// Check if any route requires query param matching
    #[inline]
    pub fn needs_query_params(&self) -> bool {
        self.needs_query_params
    }

    /// Match a request to a route
    pub fn match_request(&self, req: &RequestInfo<'_>) -> Option<RouteMatch> {
        trace!(
            method = %req.method,
            path = %req.path,
            host = %req.host,
            "Starting route matching"
        );

        // Check cache first (lock-free read, zero-allocation on hit)
        let cached = req.with_cache_key(|key| {
            self.cache.get(key).map(|r| {
                let route_id = r.clone();
                drop(r);
                route_id
            })
        });
        if let Some(route_id) = cached {
            trace!(
                route_id = %route_id,
                "Route cache hit"
            );
            if let Some(route) = self.find_route_by_id(&route_id) {
                debug!(
                    route_id = %route_id,
                    method = %req.method,
                    path = %req.path,
                    source = "cache",
                    "Route matched from cache"
                );
                return Some(RouteMatch {
                    route_id,
                    config: route.config.clone(),
                });
            }
        }

        // Record cache miss
        self.cache.record_miss();

        trace!(
            route_count = self.routes.len(),
            "Cache miss, evaluating routes"
        );

        // Evaluate routes in priority order
        for (index, route) in self.routes.iter().enumerate() {
            trace!(
                route_id = %route.id,
                route_index = index,
                priority = ?route.priority,
                matcher_count = route.matchers.len(),
                "Evaluating route"
            );

            if route.matches(req) {
                debug!(
                    route_id = %route.id,
                    method = %req.method,
                    path = %req.path,
                    host = %req.host,
                    priority = ?route.priority,
                    route_index = index,
                    "Route matched"
                );

                // Update cache — allocate key only on miss (rare after warmup)
                req.with_cache_key(|key| {
                    self.cache.insert(key.to_string(), route.id.clone());
                });

                trace!(
                    route_id = %route.id,
                    "Route added to cache"
                );

                return Some(RouteMatch {
                    route_id: route.id.clone(),
                    config: route.config.clone(),
                });
            }
        }

        // Use default route if configured
        if let Some(ref default_id) = self.default_route {
            debug!(
                route_id = %default_id,
                method = %req.method,
                path = %req.path,
                "Using default route (no explicit match)"
            );
            if let Some(route) = self.find_route_by_id(default_id) {
                return Some(RouteMatch {
                    route_id: default_id.clone(),
                    config: route.config.clone(),
                });
            }
        }

        debug!(
            method = %req.method,
            path = %req.path,
            host = %req.host,
            routes_evaluated = self.routes.len(),
            "No route matched"
        );
        None
    }

    /// Find a route by ID
    fn find_route_by_id(&self, id: &RouteId) -> Option<&CompiledRoute> {
        self.routes.iter().find(|r| r.id == *id)
    }

    /// Clear the route cache
    pub fn clear_cache(&self) {
        self.cache.clear();
    }

    /// Get cache statistics
    pub fn cache_stats(&self) -> CacheStats {
        CacheStats {
            entries: self.cache.len(),
            max_size: self.cache.max_size,
            hit_rate: self.cache.hit_rate(),
        }
    }
}

impl CompiledRoute {
    /// Compile a route configuration into an optimized matcher
    fn compile(config: RouteConfig) -> Result<Self, RouteError> {
        let mut matchers = Vec::new();

        for condition in &config.matches {
            let compiled = match condition {
                MatchCondition::Path(path) => CompiledMatcher::Path(path.clone()),
                MatchCondition::PathPrefix(prefix) => CompiledMatcher::PathPrefix(prefix.clone()),
                MatchCondition::PathRegex(pattern) => {
                    let regex = Regex::new(pattern).map_err(|e| RouteError::InvalidRegex {
                        pattern: pattern.clone(),
                        error: e.to_string(),
                    })?;
                    CompiledMatcher::PathRegex(regex)
                }
                MatchCondition::Host(host) => CompiledMatcher::Host(HostMatcher::parse(host)),
                MatchCondition::Header { name, value } => CompiledMatcher::Header {
                    name: name.to_lowercase(),
                    value: value.clone(),
                },
                MatchCondition::Method(methods) => {
                    CompiledMatcher::Method(methods.iter().map(|m| m.to_uppercase()).collect())
                }
                MatchCondition::QueryParam { name, value } => CompiledMatcher::QueryParam {
                    name: name.clone(),
                    value: value.clone(),
                },
            };
            matchers.push(compiled);
        }

        Ok(Self {
            id: RouteId::new(&config.id),
            priority: config.priority,
            config: Arc::new(config),
            matchers,
        })
    }

    /// Check if this route matches the request.
    ///
    /// Host matchers use OR logic (match any host), all other matchers use AND.
    /// This matches Gateway API semantics where multiple hostnames on an
    /// HTTPRoute are alternatives, not conjunctions.
    fn matches(&self, req: &RequestInfo<'_>) -> bool {
        // Partition matchers into host matchers and non-host matchers
        let mut has_host_matchers = false;
        let mut any_host_matched = false;

        for matcher in &self.matchers {
            match matcher {
                CompiledMatcher::Host(_) => {
                    has_host_matchers = true;
                    if matcher.matches(req) {
                        any_host_matched = true;
                    }
                }
                _ => {
                    if !matcher.matches(req) {
                        trace!(
                            route_id = %self.id,
                            matcher_type = ?matcher,
                            path = %req.path,
                            "Matcher did not match"
                        );
                        return false;
                    }
                }
            }
        }

        // If there are host matchers, at least one must match (OR logic)
        if has_host_matchers && !any_host_matched {
            trace!(
                route_id = %self.id,
                host = %req.host,
                "No host matcher matched"
            );
            return false;
        }

        true
    }

    /// Calculate route specificity for tie-breaking.
    ///
    /// Per Gateway API precedence rules:
    /// 1. Path specificity is primary (exact > longest prefix > regex)
    /// 2. Host specificity is secondary (exact > wildcard)
    /// 3. Header/method/query conditions add specificity
    ///
    /// Host matchers use OR logic, so multiple hosts don't increase
    /// specificity — we use the max host score, not the sum.
    fn specificity(&self) -> u32 {
        let mut path_score = 0u32;
        let mut host_score = 0u32;
        let mut condition_score = 0u32;

        for matcher in &self.matchers {
            match matcher {
                CompiledMatcher::Path(_) => path_score = path_score.max(10000),
                CompiledMatcher::PathRegex(_) => path_score = path_score.max(5000),
                CompiledMatcher::PathPrefix(p) => {
                    path_score = path_score.max(1000 + p.len() as u32)
                }
                CompiledMatcher::Host(host) => {
                    let s = match host {
                        HostMatcher::Exact(_) => 70,
                        HostMatcher::Regex(_) => 60,
                        HostMatcher::Wildcard { .. } => 50,
                    };
                    host_score = host_score.max(s);
                }
                CompiledMatcher::Header { value, .. } => {
                    condition_score += if value.is_some() { 30 } else { 20 };
                }
                CompiledMatcher::Method(_) => condition_score += 10,
                CompiledMatcher::QueryParam { value, .. } => {
                    condition_score += if value.is_some() { 25 } else { 15 };
                }
            }
        }

        path_score + host_score + condition_score
    }
}

impl CompiledMatcher {
    /// Check if this matcher matches the request
    fn matches(&self, req: &RequestInfo<'_>) -> bool {
        match self {
            Self::Path(path) => req.path == *path,
            Self::PathPrefix(prefix) => {
                if !req.path.starts_with(prefix) {
                    return false;
                }
                // Enforce segment boundary per Gateway API spec:
                // PathPrefix "/v2" must NOT match "/v2example", only "/v2", "/v2/", "/v2/anything"
                prefix == "/"
                    || req.path.len() == prefix.len()
                    || prefix.ends_with('/')
                    || req.path.as_bytes()[prefix.len()] == b'/'
                    || req.path.as_bytes()[prefix.len()] == b'?'
            }
            Self::PathRegex(regex) => regex.is_match(req.path),
            Self::Host(host_matcher) => host_matcher.matches(req.host),
            Self::Header { name, value } => {
                if let Some(header_value) = req.headers().get(name) {
                    value.as_ref().is_none_or(|v| header_value == v)
                } else {
                    false
                }
            }
            Self::Method(methods) => methods.iter().any(|m| m == req.method),
            Self::QueryParam { name, value } => {
                if let Some(param_value) = req.query_params().get(name) {
                    value.as_ref().is_none_or(|v| param_value == v)
                } else {
                    false
                }
            }
        }
    }
}

/// Normalize a host for comparison.
///
/// Three things, each of which was a way for a route to silently not match:
///
/// * **Port removed.** `Host: example.com:8080` must match `example.com`, per
///   the Gateway API spec.
/// * **Trailing dot removed.** `example.com.` names the same host as
///   `example.com`. Without this a restrictive host route could be skipped by
///   appending a dot, falling through to whatever permissive route follows.
/// * **Lowercased.** Host comparison is case-insensitive (RFC 3986 §3.2.2).
///   Both sides are normalized, so neither a mixed-case request nor a
///   mixed-case config silently matches nothing.
fn normalize_host(host: &str) -> String {
    // An IPv6 literal is bracketed and full of colons, so its port is
    // whatever follows the closing bracket. Splitting on the first colon
    // would reduce `[::1]:8080` to `[`.
    let without_port = if host.starts_with('[') {
        match host.find(']') {
            Some(end) => &host[..=end],
            None => host,
        }
    } else {
        host.split(':').next().unwrap_or(host)
    };

    let without_dot = without_port.strip_suffix('.').unwrap_or(without_port);
    without_dot.to_ascii_lowercase()
}

impl HostMatcher {
    /// Parse a host pattern into a matcher.
    ///
    /// The pattern is normalized the same way request hosts are, so a route
    /// written `host "Example.com"` matches `example.com`. Without that it
    /// matched nothing at all, silently.
    fn parse(pattern: &str) -> Self {
        let pattern = normalize_host(pattern);
        if let Some(suffix) = pattern.strip_prefix("*.") {
            // Wildcard pattern
            Self::Wildcard {
                suffix: suffix.to_string(),
            }
        } else if pattern.contains('*') || pattern.contains('[') {
            // Treat as regex if it contains other special characters.
            // Built case-insensitively rather than by lowercasing the pattern,
            // which would corrupt character classes such as [A-Z].
            match regex::RegexBuilder::new(&pattern)
                .case_insensitive(true)
                .build()
            {
                Ok(regex) => Self::Regex(regex),
                Err(_) => {
                    // Fall back to exact match if regex compilation fails
                    warn!("Invalid host regex pattern: {}, using exact match", pattern);
                    Self::Exact(pattern)
                }
            }
        } else {
            // Exact match
            Self::Exact(pattern)
        }
    }

    /// Check if this matcher matches the host.
    fn matches(&self, host: &str) -> bool {
        let host = normalize_host(host);
        let host = host.as_str();
        match self {
            Self::Exact(pattern) => host == pattern,
            Self::Wildcard { suffix } => {
                host.ends_with(suffix)
                    && host.len() > suffix.len()
                    && host[..host.len() - suffix.len()].ends_with('.')
            }
            Self::Regex(regex) => regex.is_match(host),
        }
    }
}

impl RouteCache {
    /// Create a new route cache
    fn new(max_size: usize) -> Self {
        Self {
            entries: DashMap::with_capacity(max_size),
            max_size,
            entry_count: AtomicUsize::new(0),
            hits: AtomicU64::new(0),
            misses: AtomicU64::new(0),
        }
    }

    /// Get a route from cache (lock-free)
    fn get(&self, key: &str) -> Option<dashmap::mapref::one::Ref<'_, String, RouteId>> {
        let result = self.entries.get(key);
        if result.is_some() {
            self.hits.fetch_add(1, Ordering::Relaxed);
        }
        result
    }

    /// Record a cache miss
    fn record_miss(&self) {
        self.misses.fetch_add(1, Ordering::Relaxed);
    }

    /// Get the hit rate (0.0 to 1.0)
    fn hit_rate(&self) -> f64 {
        let hits = self.hits.load(Ordering::Relaxed);
        let misses = self.misses.load(Ordering::Relaxed);
        let total = hits + misses;
        if total == 0 {
            0.0
        } else {
            hits as f64 / total as f64
        }
    }

    /// Insert a route into cache (lock-free)
    fn insert(&self, key: String, route_id: RouteId) {
        // Check if we need to evict (approximate check to avoid overhead)
        let current_count = self.entry_count.load(Ordering::Relaxed);
        if current_count >= self.max_size {
            // Evict ~10% of entries randomly for simplicity
            // This is faster than true LRU and good enough for a cache
            self.evict_random();
        }

        if self.entries.insert(key, route_id).is_none() {
            // Only increment if this was a new entry
            self.entry_count.fetch_add(1, Ordering::Relaxed);
        }
    }

    /// Evict random entries when cache is full
    fn evict_random(&self) {
        let to_evict = (self.max_size / 10).max(1); // Evict ~10%
        let mut evicted = 0;

        // Iterate and remove some entries
        self.entries.retain(|_, _| {
            if evicted < to_evict {
                evicted += 1;
                false // Remove this entry
            } else {
                true // Keep this entry
            }
        });

        // Update count (approximate)
        self.entry_count
            .store(self.entries.len(), Ordering::Relaxed);

        debug!(
            evicted = evicted,
            remaining = self.entries.len(),
            max_size = self.max_size,
            "Route cache at capacity; evicted entries"
        );
        if let Some(counter) = ROUTE_CACHE_EVICTIONS.as_ref() {
            counter.inc_by(evicted as u64);
        }
    }

    /// Get current cache size
    fn len(&self) -> usize {
        self.entries.len()
    }

    /// Clear all cache entries
    fn clear(&self) {
        self.entries.clear();
        self.entry_count.store(0, Ordering::Relaxed);
    }
}

/// Request information for route matching (zero-copy where possible)
#[derive(Debug)]
pub struct RequestInfo<'a> {
    /// HTTP method (borrowed from request header)
    pub method: &'a str,
    /// Request path (borrowed from request header)
    pub path: &'a str,
    /// Host header value (borrowed from request header)
    pub host: &'a str,
    /// Headers for matching (lazy-initialized, only if needed)
    headers: Option<HashMap<String, String>>,
    /// Query parameters (lazy-initialized, only if needed)
    query_params: Option<HashMap<String, String>>,
}

impl<'a> RequestInfo<'a> {
    /// Create a new RequestInfo with borrowed references (zero-copy for common case)
    #[inline]
    pub fn new(method: &'a str, path: &'a str, host: &'a str) -> Self {
        Self {
            method,
            path,
            host,
            headers: None,
            query_params: None,
        }
    }

    /// Set headers for header-based matching (only call if RouteMatcher.needs_headers())
    #[inline]
    pub fn with_headers(mut self, headers: HashMap<String, String>) -> Self {
        self.headers = Some(headers);
        self
    }

    /// Set query params for query-based matching (only call if RouteMatcher.needs_query_params())
    #[inline]
    pub fn with_query_params(mut self, params: HashMap<String, String>) -> Self {
        self.query_params = Some(params);
        self
    }

    /// Get headers (returns empty map if not set)
    #[inline]
    pub fn headers(&self) -> &HashMap<String, String> {
        static EMPTY: std::sync::OnceLock<HashMap<String, String>> = std::sync::OnceLock::new();
        self.headers
            .as_ref()
            .unwrap_or_else(|| EMPTY.get_or_init(HashMap::new))
    }

    /// Get query params (returns empty map if not set)
    #[inline]
    pub fn query_params(&self) -> &HashMap<String, String> {
        static EMPTY: std::sync::OnceLock<HashMap<String, String>> = std::sync::OnceLock::new();
        self.query_params
            .as_ref()
            .unwrap_or_else(|| EMPTY.get_or_init(HashMap::new))
    }

    /// Generate a cache key for this request using a thread-local buffer
    /// to avoid per-request heap allocation.
    fn with_cache_key<R>(&self, f: impl FnOnce(&str) -> R) -> R {
        use std::cell::RefCell;
        use std::fmt::Write;

        thread_local! {
            static BUF: RefCell<String> = RefCell::new(String::with_capacity(128));
        }

        BUF.with(|buf| {
            let mut buf = buf.borrow_mut();
            buf.clear();
            let _ = write!(buf, "{}:{}:{}", self.method, self.host, self.path);
            // Include headers in cache key when header-based routing is active,
            // otherwise different header combinations can poison the cache.
            if let Some(ref headers) = self.headers {
                let mut pairs: Vec<_> = headers.iter().collect();
                pairs.sort_by_key(|(k, _)| k.as_str());
                for (k, v) in pairs {
                    let _ = write!(buf, "\n{k}={v}");
                }
            }
            // Query parameters, for the same reason. `path` here comes from
            // `Uri::path()` and carries no query string, so without this two
            // requests differing only in their query share a cache entry: once
            // `/api?version=v2` is cached, `/api?version=v1` is served the v2
            // route and reaches the wrong upstream.
            //
            // The separator differs from the header one so a header and a
            // query parameter with the same name and value cannot produce the
            // same key.
            if let Some(ref params) = self.query_params {
                let mut pairs: Vec<_> = params.iter().collect();
                pairs.sort_by_key(|(k, _)| k.as_str());
                for (k, v) in pairs {
                    let _ = write!(buf, "\t{k}={v}");
                }
            }
            f(&buf)
        })
    }

    /// Parse query parameters from path (only call when needed)
    pub fn parse_query_params(path: &str) -> HashMap<String, String> {
        let mut params = HashMap::new();
        if let Some(query_start) = path.find('?') {
            let query = &path[query_start + 1..];
            for pair in query.split('&') {
                if let Some(eq_pos) = pair.find('=') {
                    let key = &pair[..eq_pos];
                    let value = &pair[eq_pos + 1..];
                    params.insert(
                        urlencoding::decode(key)
                            .unwrap_or_else(|_| key.into())
                            .into_owned(),
                        urlencoding::decode(value)
                            .unwrap_or_else(|_| value.into())
                            .into_owned(),
                    );
                } else {
                    params.insert(
                        urlencoding::decode(pair)
                            .unwrap_or_else(|_| pair.into())
                            .into_owned(),
                        String::new(),
                    );
                }
            }
        }
        params
    }

    /// Build headers map from request header iterator (only call when needed)
    pub fn build_headers<'b, I>(iter: I) -> HashMap<String, String>
    where
        I: Iterator<Item = (&'b http::header::HeaderName, &'b http::header::HeaderValue)>,
    {
        let mut headers = HashMap::new();
        for (name, value) in iter {
            if let Ok(value_str) = value.to_str() {
                headers.insert(name.as_str().to_lowercase(), value_str.to_string());
            }
        }
        headers
    }
}

/// Route match result
#[derive(Debug, Clone)]
pub struct RouteMatch {
    pub route_id: RouteId,
    pub config: Arc<RouteConfig>,
}

impl RouteMatch {
    /// Access route policies (convenience accessor to avoid repeated .config.policies)
    #[inline]
    pub fn policies(&self) -> &RoutePolicies {
        &self.config.policies
    }
}

/// Cache statistics
#[derive(Debug, Clone)]
pub struct CacheStats {
    pub entries: usize,
    pub max_size: usize,
    pub hit_rate: f64,
}

/// Route matching errors
#[derive(Debug, thiserror::Error)]
pub enum RouteError {
    #[error("Invalid regex pattern '{pattern}': {error}")]
    InvalidRegex { pattern: String, error: String },

    #[error("Invalid route configuration: {0}")]
    InvalidConfig(String),

    #[error("Duplicate route ID: {0}")]
    DuplicateRouteId(String),
}

impl std::fmt::Debug for CompiledMatcher {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Path(p) => write!(f, "Path({})", p),
            Self::PathPrefix(p) => write!(f, "PathPrefix({})", p),
            Self::PathRegex(_) => write!(f, "PathRegex(...)"),
            Self::Host(_) => write!(f, "Host(...)"),
            Self::Header { name, .. } => write!(f, "Header({})", name),
            Self::Method(m) => write!(f, "Method({:?})", m),
            Self::QueryParam { name, .. } => write!(f, "QueryParam({})", name),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use zentinel_common::types::Priority;
    use zentinel_config::{MatchCondition, RouteConfig};

    #[test]
    fn route_cache_never_exceeds_max_size() {
        let cache = RouteCache::new(10);
        for i in 0..100 {
            cache.insert(format!("key-{i}"), RouteId::new(format!("route-{i}")));
            assert!(
                cache.len() <= 10,
                "route cache grew past max_size: {}",
                cache.len()
            );
        }
    }

    fn create_test_route(id: &str, matches: Vec<MatchCondition>) -> RouteConfig {
        RouteConfig {
            id: id.to_string(),
            priority: Priority::NORMAL,
            matches,
            upstream: Some("test_upstream".to_string()),
            service_type: zentinel_config::ServiceType::Web,
            policies: Default::default(),
            filters: vec![],
            builtin_handler: None,
            waf_enabled: false,
            retry_policy: None,
            static_files: None,
            api_schema: None,
            error_pages: None,
            websocket: false,
            websocket_inspection: false,
            inference: None,
            mcp: None,
            a2a: None,
            shadow: None,
            fallback: None,
        }
    }

    #[test]
    fn test_path_matching() {
        let routes = vec![
            create_test_route(
                "exact",
                vec![MatchCondition::Path("/api/v1/users".to_string())],
            ),
            create_test_route(
                "prefix",
                vec![MatchCondition::PathPrefix("/api/".to_string())],
            ),
        ];

        let matcher = RouteMatcher::new(routes, None).unwrap();

        let req = RequestInfo {
            method: "GET",
            path: "/api/v1/users",
            host: "example.com",
            headers: None,
            query_params: None,
        };

        let result = matcher.match_request(&req).unwrap();
        assert_eq!(result.route_id.as_str(), "exact");
    }

    #[test]
    fn test_host_wildcard_matching() {
        let routes = vec![create_test_route(
            "wildcard",
            vec![MatchCondition::Host("*.example.com".to_string())],
        )];

        let matcher = RouteMatcher::new(routes, None).unwrap();

        let req = RequestInfo {
            method: "GET",
            path: "/",
            host: "api.example.com",
            headers: None,
            query_params: None,
        };

        let result = matcher.match_request(&req).unwrap();
        assert_eq!(result.route_id.as_str(), "wildcard");
    }

    #[test]
    fn test_priority_ordering() {
        let mut route1 =
            create_test_route("low", vec![MatchCondition::PathPrefix("/".to_string())]);
        route1.priority = Priority::LOW;

        let mut route2 =
            create_test_route("high", vec![MatchCondition::PathPrefix("/".to_string())]);
        route2.priority = Priority::HIGH;

        let routes = vec![route1, route2];
        let matcher = RouteMatcher::new(routes, None).unwrap();

        let req = RequestInfo {
            method: "GET",
            path: "/test",
            host: "example.com",
            headers: None,
            query_params: None,
        };

        let result = matcher.match_request(&req).unwrap();
        assert_eq!(result.route_id.as_str(), "high");
    }

    #[test]
    fn test_query_param_parsing() {
        let params = RequestInfo::parse_query_params("/path?foo=bar&baz=qux&empty=");
        assert_eq!(params.get("foo"), Some(&"bar".to_string()));
        assert_eq!(params.get("baz"), Some(&"qux".to_string()));
        assert_eq!(params.get("empty"), Some(&"".to_string()));
    }

    #[test]
    fn test_path_prefix_segment_boundary() {
        let routes = vec![
            create_test_route("v2", vec![MatchCondition::PathPrefix("/v2".to_string())]),
            create_test_route(
                "catch-all",
                vec![MatchCondition::PathPrefix("/".to_string())],
            ),
        ];

        let matcher = RouteMatcher::new(routes, None).unwrap();

        // /v2 exact → v2
        let req = RequestInfo::new("GET", "/v2", "example.com");
        assert_eq!(matcher.match_request(&req).unwrap().route_id.as_str(), "v2");

        // /v2/ with trailing slash → v2
        let req = RequestInfo::new("GET", "/v2/", "example.com");
        assert_eq!(matcher.match_request(&req).unwrap().route_id.as_str(), "v2");

        // /v2/anything → v2
        let req = RequestInfo::new("GET", "/v2/anything", "example.com");
        assert_eq!(matcher.match_request(&req).unwrap().route_id.as_str(), "v2");

        // /v2example must NOT match /v2 prefix — falls to catch-all
        let req = RequestInfo::new("GET", "/v2example", "example.com");
        assert_eq!(
            matcher.match_request(&req).unwrap().route_id.as_str(),
            "catch-all"
        );

        // /v2?query → v2
        let req = RequestInfo::new("GET", "/v2?foo=bar", "example.com");
        assert_eq!(matcher.match_request(&req).unwrap().route_id.as_str(), "v2");
    }

    #[test]
    fn test_header_matching_with_specificity() {
        let routes = vec![
            create_test_route(
                "catch-all",
                vec![MatchCondition::PathPrefix("/".to_string())],
            ),
            create_test_route(
                "header-v2",
                vec![
                    MatchCondition::Header {
                        name: "version".to_string(),
                        value: Some("two".to_string()),
                    },
                    MatchCondition::PathPrefix("/".to_string()),
                ],
            ),
        ];

        let matcher = RouteMatcher::new(routes, None).unwrap();

        // Without headers → catch-all
        let req = RequestInfo::new("GET", "/", "example.com");
        assert_eq!(
            matcher.match_request(&req).unwrap().route_id.as_str(),
            "catch-all"
        );

        // With version:two header → header-v2 (more specific)
        let mut headers = HashMap::new();
        headers.insert("version".to_string(), "two".to_string());
        let req = RequestInfo::new("GET", "/", "example.com").with_headers(headers);
        assert_eq!(
            matcher.match_request(&req).unwrap().route_id.as_str(),
            "header-v2"
        );
    }
}

#[cfg(test)]
mod probe_113 {
    use super::*;
    use zentinel_common::types::Priority;
    use zentinel_config::{MatchCondition, RouteConfig};

    fn route(id: &str, matches: Vec<MatchCondition>) -> RouteConfig {
        RouteConfig {
            id: id.to_string(),
            priority: Priority::NORMAL,
            matches,
            upstream: Some("u".to_string()),
            service_type: zentinel_config::ServiceType::Web,
            policies: Default::default(),
            filters: vec![],
            builtin_handler: None,
            waf_enabled: false,
            retry_policy: None,
            static_files: None,
            api_schema: None,
            error_pages: None,
            websocket: false,
            websocket_inspection: false,
            inference: None,
            mcp: None,
            a2a: None,
            shadow: None,
            fallback: None,
        }
    }

    #[test]
    fn probe_query_param_matching() {
        let build = || {
            RouteMatcher::new(
                vec![
                    route(
                        "v2",
                        vec![
                            MatchCondition::PathPrefix("/api".into()),
                            MatchCondition::QueryParam {
                                name: "version".into(),
                                value: Some("v2".into()),
                            },
                        ],
                    ),
                    route("fallback", vec![MatchCondition::PathPrefix("/api".into())]),
                ],
                None,
            )
            .unwrap()
        };

        // A: correct param, fresh matcher.
        let m = build();
        let mut p1 = std::collections::HashMap::new();
        p1.insert("version".to_string(), "v2".to_string());
        let r = m.match_request(&RequestInfo::new("GET", "/api/x", "h").with_query_params(p1));
        println!(
            "  A version=v2, fresh   -> {:?}",
            r.map(|r| r.route_id.to_string())
        );

        // B: WRONG param, fresh matcher. Should fall through to "fallback".
        let m = build();
        let mut p2 = std::collections::HashMap::new();
        p2.insert("version".to_string(), "v1".to_string());
        let r = m.match_request(&RequestInfo::new("GET", "/api/x", "h").with_query_params(p2));
        println!(
            "  B version=v1, fresh   -> {:?}",
            r.map(|r| r.route_id.to_string())
        );

        // C: NO params, fresh matcher. Should fall through to "fallback".
        let m = build();
        let r = m.match_request(&RequestInfo::new("GET", "/api/x", "h"));
        println!(
            "  C no params, fresh    -> {:?}",
            r.map(|r| r.route_id.to_string())
        );

        // D: cache poisoning -- same matcher, v2 first then v1.
        let m = build();
        let mut pa = std::collections::HashMap::new();
        pa.insert("version".to_string(), "v2".to_string());
        let first = m.match_request(&RequestInfo::new("GET", "/api/x", "h").with_query_params(pa));
        let mut pb = std::collections::HashMap::new();
        pb.insert("version".to_string(), "v1".to_string());
        let second = m.match_request(&RequestInfo::new("GET", "/api/x", "h").with_query_params(pb));
        println!(
            "  D v2 then v1, shared  -> first={:?} second={:?}",
            first.map(|r| r.route_id.to_string()),
            second.map(|r| r.route_id.to_string())
        );
    }
}

#[cfg(test)]
mod route_cache_correctness {
    use super::*;
    use std::collections::HashMap;
    use zentinel_common::types::Priority;
    use zentinel_config::{MatchCondition, RouteConfig};

    fn route(id: &str, matches: Vec<MatchCondition>) -> RouteConfig {
        RouteConfig {
            id: id.to_string(),
            priority: Priority::NORMAL,
            matches,
            upstream: Some("u".to_string()),
            service_type: zentinel_config::ServiceType::Web,
            policies: Default::default(),
            filters: vec![],
            builtin_handler: None,
            waf_enabled: false,
            retry_policy: None,
            static_files: None,
            api_schema: None,
            error_pages: None,
            websocket: false,
            websocket_inspection: false,
            inference: None,
            mcp: None,
            a2a: None,
            shadow: None,
            fallback: None,
        }
    }

    fn params(pairs: &[(&str, &str)]) -> HashMap<String, String> {
        pairs
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect()
    }

    fn matched(m: &RouteMatcher, req: &RequestInfo<'_>) -> Option<String> {
        m.match_request(req).map(|r| r.route_id.to_string())
    }

    fn query_matcher() -> RouteMatcher {
        RouteMatcher::new(
            vec![
                route(
                    "v2",
                    vec![
                        MatchCondition::PathPrefix("/api".into()),
                        MatchCondition::QueryParam {
                            name: "version".into(),
                            value: Some("v2".into()),
                        },
                    ],
                ),
                route("fallback", vec![MatchCondition::PathPrefix("/api".into())]),
            ],
            None,
        )
        .unwrap()
    }

    /// Two requests differing only in a query parameter must not share a cache
    /// entry.
    ///
    /// `path` comes from `Uri::path()` and carries no query string, so before
    /// query parameters were part of the cache key, the first request through
    /// a path decided the route for every later request to that path. A
    /// `?version=v2` request would pin `/api` to the v2 route and a subsequent
    /// `?version=v1` would be sent to the wrong upstream.
    #[test]
    fn a_query_parameter_change_is_not_served_from_cache() {
        let m = query_matcher();

        let first = matched(
            &m,
            &RequestInfo::new("GET", "/api/x", "h").with_query_params(params(&[("version", "v2")])),
        );
        let second = matched(
            &m,
            &RequestInfo::new("GET", "/api/x", "h").with_query_params(params(&[("version", "v1")])),
        );

        assert_eq!(first.as_deref(), Some("v2"));
        assert_eq!(
            second.as_deref(),
            Some("fallback"),
            "the second request was served the first request's route from cache"
        );
    }

    /// And in the other order, so the test cannot pass merely because the
    /// first result happened to be the fallback.
    #[test]
    fn the_reverse_order_is_also_correct() {
        let m = query_matcher();

        let first = matched(
            &m,
            &RequestInfo::new("GET", "/api/x", "h").with_query_params(params(&[("version", "v1")])),
        );
        let second = matched(
            &m,
            &RequestInfo::new("GET", "/api/x", "h").with_query_params(params(&[("version", "v2")])),
        );

        assert_eq!(first.as_deref(), Some("fallback"));
        assert_eq!(second.as_deref(), Some("v2"));
    }

    /// A request with no query parameters must not pick up a cached entry from
    /// one that had them.
    #[test]
    fn an_absent_query_parameter_is_distinct_from_a_present_one() {
        let m = query_matcher();

        assert_eq!(
            matched(
                &m,
                &RequestInfo::new("GET", "/api/x", "h")
                    .with_query_params(params(&[("version", "v2")]))
            )
            .as_deref(),
            Some("v2")
        );
        assert_eq!(
            matched(&m, &RequestInfo::new("GET", "/api/x", "h")).as_deref(),
            Some("fallback"),
            "a request without the parameter must not inherit the parameterised route"
        );
    }

    /// Parameter order in the map must not change the key, or the cache would
    /// miss on every request and quietly stop being a cache.
    #[test]
    fn parameter_order_does_not_affect_the_cache_key() {
        let m = query_matcher();

        let a = RequestInfo::new("GET", "/api/x", "h")
            .with_query_params(params(&[("version", "v2"), ("page", "1")]));
        let b = RequestInfo::new("GET", "/api/x", "h")
            .with_query_params(params(&[("page", "1"), ("version", "v2")]));

        assert_eq!(matched(&m, &a).as_deref(), Some("v2"));
        assert_eq!(matched(&m, &b).as_deref(), Some("v2"));

        // Both requests are the same request, so they must share one cache
        // entry. Two entries would mean the key depends on map iteration
        // order, and the cache would miss on almost every request.
        assert_eq!(
            m.cache_stats().entries,
            1,
            "parameter order changed the cache key"
        );
    }

    /// A header and a query parameter with the same name and value must not
    /// collide, or one could stand in for the other.
    #[test]
    fn a_header_and_a_query_parameter_do_not_collide() {
        let m = RouteMatcher::new(
            vec![
                route(
                    "by-header",
                    vec![
                        MatchCondition::PathPrefix("/x".into()),
                        MatchCondition::Header {
                            name: "tenant".into(),
                            value: Some("acme".into()),
                        },
                    ],
                ),
                route(
                    "by-query",
                    vec![
                        MatchCondition::PathPrefix("/x".into()),
                        MatchCondition::QueryParam {
                            name: "tenant".into(),
                            value: Some("acme".into()),
                        },
                    ],
                ),
                route("fallback", vec![MatchCondition::PathPrefix("/x".into())]),
            ],
            None,
        )
        .unwrap();

        let with_header =
            RequestInfo::new("GET", "/x", "h").with_headers(params(&[("tenant", "acme")]));
        let with_query =
            RequestInfo::new("GET", "/x", "h").with_query_params(params(&[("tenant", "acme")]));

        assert_eq!(matched(&m, &with_header).as_deref(), Some("by-header"));
        assert_eq!(
            matched(&m, &with_query).as_deref(),
            Some("by-query"),
            "a query parameter was served the header route's cache entry"
        );
    }
}

/// Route matching edge cases (#113).
///
/// The matcher decides which upstream every request reaches, so a wrong
/// answer is a wrong backend rather than an error anyone sees. These pin the
/// behaviour that is easy to get wrong and impossible to notice.
#[cfg(test)]
mod route_matching_edges {
    use super::*;
    use std::collections::HashMap;
    use zentinel_common::types::Priority;
    use zentinel_config::{MatchCondition, RouteConfig};

    fn route(id: &str, matches: Vec<MatchCondition>) -> RouteConfig {
        route_with_priority(id, matches, Priority::NORMAL)
    }

    fn route_with_priority(
        id: &str,
        matches: Vec<MatchCondition>,
        priority: Priority,
    ) -> RouteConfig {
        RouteConfig {
            id: id.to_string(),
            priority,
            matches,
            upstream: Some("u".to_string()),
            service_type: zentinel_config::ServiceType::Web,
            policies: Default::default(),
            filters: vec![],
            builtin_handler: None,
            waf_enabled: false,
            retry_policy: None,
            static_files: None,
            api_schema: None,
            error_pages: None,
            websocket: false,
            websocket_inspection: false,
            inference: None,
            mcp: None,
            a2a: None,
            shadow: None,
            fallback: None,
        }
    }

    fn matcher(routes: Vec<RouteConfig>) -> RouteMatcher {
        RouteMatcher::new(routes, None).expect("routes should compile")
    }

    fn hit(m: &RouteMatcher, method: &str, path: &str, host: &str) -> Option<String> {
        m.match_request(&RequestInfo::new(method, path, host))
            .map(|r| r.route_id.to_string())
    }

    fn pairs(kv: &[(&str, &str)]) -> HashMap<String, String> {
        kv.iter()
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect()
    }

    // -- Host -------------------------------------------------------------

    /// Host comparison is case-insensitive (RFC 3986 §3.2.2). Both sides are
    /// normalized: a mixed-case request must match, and just as importantly a
    /// route written `host "Example.com"` must not silently match nothing.
    #[test]
    fn host_matching_ignores_case_on_both_sides() {
        let m = matcher(vec![route(
            "h",
            vec![MatchCondition::Host("example.com".into())],
        )]);
        assert_eq!(hit(&m, "GET", "/", "example.com").as_deref(), Some("h"));
        assert_eq!(hit(&m, "GET", "/", "EXAMPLE.COM").as_deref(), Some("h"));
        assert_eq!(hit(&m, "GET", "/", "ExAmPlE.cOm").as_deref(), Some("h"));

        let m = matcher(vec![route(
            "h",
            vec![MatchCondition::Host("Example.COM".into())],
        )]);
        assert_eq!(
            hit(&m, "GET", "/", "example.com").as_deref(),
            Some("h"),
            "a mixed-case config host must still match"
        );
    }

    /// A trailing dot names the same host. Without normalizing it, a
    /// restrictive host route can be skipped by appending a dot and falling
    /// through to whatever permissive route follows.
    #[test]
    fn a_trailing_dot_does_not_bypass_a_host_route() {
        // Priority is explicit so the outcome turns on the host match alone,
        // not on how a host-only route ranks against a path catch-all.
        let m = matcher(vec![
            route_with_priority(
                "restricted",
                vec![
                    MatchCondition::Host("admin.example.com".into()),
                    MatchCondition::PathPrefix("/".into()),
                ],
                Priority::HIGH,
            ),
            route_with_priority(
                "catchall",
                vec![MatchCondition::PathPrefix("/".into())],
                Priority::LOW,
            ),
        ]);

        assert_eq!(
            hit(&m, "GET", "/", "admin.example.com").as_deref(),
            Some("restricted")
        );
        assert_eq!(
            hit(&m, "GET", "/", "admin.example.com.").as_deref(),
            Some("restricted"),
            "a trailing dot must not route around the host match"
        );
        // A genuinely different host still falls through.
        assert_eq!(
            hit(&m, "GET", "/", "other.com").as_deref(),
            Some("catchall")
        );
    }

    /// Path specificity outranks host specificity, so a route matching only a
    /// host loses to a catch-all that matches a path.
    ///
    /// This is the documented ordering rather than a defect, but it is
    /// surprising enough to pin: an operator adding a host-only route
    /// alongside a `path-prefix "/"` catch-all gets the catch-all, and the
    /// host route never fires. Give the host route a path condition, or a
    /// higher priority.
    #[test]
    fn a_host_only_route_loses_to_a_path_catchall() {
        let m = matcher(vec![
            route(
                "host_only",
                vec![MatchCondition::Host("admin.example.com".into())],
            ),
            route("catchall", vec![MatchCondition::PathPrefix("/".into())]),
        ]);
        assert_eq!(
            hit(&m, "GET", "/", "admin.example.com").as_deref(),
            Some("catchall")
        );

        // Adding the path condition is what makes it win.
        let m = matcher(vec![
            route(
                "host_and_path",
                vec![
                    MatchCondition::Host("admin.example.com".into()),
                    MatchCondition::PathPrefix("/".into()),
                ],
            ),
            route("catchall", vec![MatchCondition::PathPrefix("/".into())]),
        ]);
        assert_eq!(
            hit(&m, "GET", "/", "admin.example.com").as_deref(),
            Some("host_and_path")
        );
    }

    #[test]
    fn a_port_is_ignored_when_matching_a_host() {
        let m = matcher(vec![route(
            "h",
            vec![MatchCondition::Host("example.com".into())],
        )]);
        assert_eq!(
            hit(&m, "GET", "/", "example.com:8443").as_deref(),
            Some("h")
        );
        assert_eq!(hit(&m, "GET", "/", "example.com:80").as_deref(), Some("h"));
    }

    /// An IPv6 literal is bracketed and full of colons, so splitting on the
    /// first one to strip a port reduces `[::1]:8080` to `[`.
    #[test]
    fn an_ipv6_host_is_not_mangled_by_port_stripping() {
        assert_eq!(normalize_host("[::1]:8080"), "[::1]");
        assert_eq!(normalize_host("[2001:DB8::1]"), "[2001:db8::1]");
        assert_eq!(normalize_host("[::1]"), "[::1]");
    }

    #[test]
    fn wildcard_hosts_also_ignore_case_port_and_trailing_dot() {
        let m = matcher(vec![route(
            "w",
            vec![MatchCondition::Host("*.example.com".into())],
        )]);
        for host in [
            "api.example.com",
            "API.EXAMPLE.COM",
            "api.example.com:8443",
            "api.example.com.",
        ] {
            assert_eq!(
                hit(&m, "GET", "/", host).as_deref(),
                Some("w"),
                "host {host}"
            );
        }
    }

    /// A wildcard covers one or more labels beneath the suffix, never the bare
    /// suffix itself. `*.example.com` must not match `example.com`.
    #[test]
    fn a_wildcard_does_not_match_its_own_suffix() {
        let m = matcher(vec![route(
            "w",
            vec![MatchCondition::Host("*.example.com".into())],
        )]);
        assert_eq!(hit(&m, "GET", "/", "example.com").as_deref(), None);
        assert_eq!(hit(&m, "GET", "/", "notexample.com").as_deref(), None);
    }

    /// Several host conditions on one route are alternatives, while a host and
    /// a path condition must both hold. Getting this backwards would either
    /// make multi-host routes unreachable or make every route far too broad.
    #[test]
    fn hosts_are_alternatives_but_other_conditions_are_required() {
        let m = matcher(vec![route(
            "multi",
            vec![
                MatchCondition::Host("a.com".into()),
                MatchCondition::Host("b.com".into()),
                MatchCondition::PathPrefix("/x".into()),
            ],
        )]);

        assert_eq!(hit(&m, "GET", "/x", "a.com").as_deref(), Some("multi"));
        assert_eq!(hit(&m, "GET", "/x", "b.com").as_deref(), Some("multi"));
        assert_eq!(
            hit(&m, "GET", "/x", "c.com").as_deref(),
            None,
            "an unlisted host must not match"
        );
        assert_eq!(
            hit(&m, "GET", "/y", "a.com").as_deref(),
            None,
            "the path is still required"
        );
    }

    // -- Path -------------------------------------------------------------

    #[test]
    fn an_exact_path_does_not_match_children_or_a_trailing_slash() {
        let m = matcher(vec![route(
            "exact",
            vec![MatchCondition::Path("/api".into())],
        )]);
        assert_eq!(hit(&m, "GET", "/api", "h").as_deref(), Some("exact"));
        assert_eq!(hit(&m, "GET", "/api/", "h").as_deref(), None);
        assert_eq!(hit(&m, "GET", "/api/users", "h").as_deref(), None);
        assert_eq!(hit(&m, "GET", "/apiv2", "h").as_deref(), None);
    }

    /// A prefix must stop at a path segment boundary, or `/api` would claim
    /// `/apikeys` and route it to the wrong backend.
    #[test]
    fn a_prefix_stops_at_a_segment_boundary() {
        let m = matcher(vec![route(
            "p",
            vec![MatchCondition::PathPrefix("/api".into())],
        )]);
        assert_eq!(hit(&m, "GET", "/api", "h").as_deref(), Some("p"));
        assert_eq!(hit(&m, "GET", "/api/", "h").as_deref(), Some("p"));
        assert_eq!(hit(&m, "GET", "/api/users", "h").as_deref(), Some("p"));
        assert_eq!(hit(&m, "GET", "/apikeys", "h").as_deref(), None);
        assert_eq!(hit(&m, "GET", "/apiv2/users", "h").as_deref(), None);
    }

    /// An unusable regex must fail the load rather than becoming a route that
    /// silently never matches.
    #[test]
    fn an_invalid_path_regex_is_rejected_at_load() {
        for pattern in ["(unclosed", "a{2,1}", "[z-a]"] {
            assert!(
                RouteMatcher::new(
                    vec![route("r", vec![MatchCondition::PathRegex(pattern.into())])],
                    None,
                )
                .is_err(),
                "{pattern:?} should be rejected"
            );
        }
    }

    #[test]
    fn a_path_regex_is_anchored_as_written() {
        let m = matcher(vec![route(
            "r",
            vec![MatchCondition::PathRegex("^/v[0-9]+/users$".into())],
        )]);
        assert_eq!(hit(&m, "GET", "/v1/users", "h").as_deref(), Some("r"));
        assert_eq!(hit(&m, "GET", "/v42/users", "h").as_deref(), Some("r"));
        assert_eq!(hit(&m, "GET", "/v1/users/1", "h").as_deref(), None);
        assert_eq!(hit(&m, "GET", "/x/v1/users", "h").as_deref(), None);
    }

    // -- Method, header, query --------------------------------------------

    #[test]
    fn a_method_condition_accepts_any_of_its_methods() {
        let m = matcher(vec![route(
            "rw",
            vec![MatchCondition::Method(vec!["POST".into(), "PUT".into()])],
        )]);
        assert_eq!(hit(&m, "POST", "/x", "h").as_deref(), Some("rw"));
        assert_eq!(hit(&m, "PUT", "/x", "h").as_deref(), Some("rw"));
        assert_eq!(hit(&m, "GET", "/x", "h").as_deref(), None);
        assert_eq!(hit(&m, "DELETE", "/x", "h").as_deref(), None);
    }

    /// A header condition with no value matches on presence alone; with a
    /// value it must match exactly. Conflating the two would make a
    /// presence check accept any value, or a value check accept none.
    #[test]
    fn a_header_condition_distinguishes_presence_from_value() {
        let present = matcher(vec![route(
            "any",
            vec![MatchCondition::Header {
                name: "x-key".into(),
                value: None,
            }],
        )]);
        let exact = matcher(vec![route(
            "exact",
            vec![MatchCondition::Header {
                name: "x-key".into(),
                value: Some("secret".into()),
            }],
        )]);

        let with_other =
            RequestInfo::new("GET", "/x", "h").with_headers(pairs(&[("x-key", "other")]));
        let with_secret =
            RequestInfo::new("GET", "/x", "h").with_headers(pairs(&[("x-key", "secret")]));
        let without = RequestInfo::new("GET", "/x", "h").with_headers(pairs(&[("y", "1")]));

        assert!(present.match_request(&with_other).is_some());
        assert!(present.match_request(&without).is_none());
        assert!(exact.match_request(&with_secret).is_some());
        assert!(exact.match_request(&with_other).is_none());
    }

    #[test]
    fn a_query_condition_distinguishes_presence_from_value() {
        let present = matcher(vec![route(
            "any",
            vec![MatchCondition::QueryParam {
                name: "debug".into(),
                value: None,
            }],
        )]);
        let exact = matcher(vec![route(
            "exact",
            vec![MatchCondition::QueryParam {
                name: "v".into(),
                value: Some("2".into()),
            }],
        )]);

        assert!(present
            .match_request(
                &RequestInfo::new("GET", "/x", "h").with_query_params(pairs(&[("debug", "0")]))
            )
            .is_some());
        assert!(present
            .match_request(&RequestInfo::new("GET", "/x", "h"))
            .is_none());
        assert!(exact
            .match_request(
                &RequestInfo::new("GET", "/x", "h").with_query_params(pairs(&[("v", "2")]))
            )
            .is_some());
        assert!(exact
            .match_request(
                &RequestInfo::new("GET", "/x", "h").with_query_params(pairs(&[("v", "1")]))
            )
            .is_none());
    }

    // -- Selection --------------------------------------------------------

    #[test]
    fn higher_priority_wins_regardless_of_declaration_order() {
        let m = matcher(vec![
            route_with_priority(
                "low",
                vec![MatchCondition::PathPrefix("/a".into())],
                Priority::LOW,
            ),
            route_with_priority(
                "high",
                vec![MatchCondition::PathPrefix("/a".into())],
                Priority::HIGH,
            ),
        ]);
        assert_eq!(hit(&m, "GET", "/a", "h").as_deref(), Some("high"));
    }

    /// Equal priorities must resolve the same way every time. If this depended
    /// on hash or iteration order, two identically configured proxies would
    /// route the same request differently.
    #[test]
    fn equal_priorities_resolve_deterministically() {
        for _ in 0..10 {
            let m = matcher(vec![
                route("first", vec![MatchCondition::PathPrefix("/a".into())]),
                route("second", vec![MatchCondition::PathPrefix("/a".into())]),
            ]);
            assert_eq!(hit(&m, "GET", "/a", "h").as_deref(), Some("first"));
        }
    }

    /// A route with no conditions matches everything, so it is only ever
    /// correct as a catch-all and must not shadow a more specific route.
    #[test]
    fn a_route_with_no_conditions_matches_anything() {
        let m = matcher(vec![route("catchall", vec![])]);
        assert_eq!(
            hit(&m, "GET", "/anything", "h").as_deref(),
            Some("catchall")
        );
        assert_eq!(
            hit(&m, "POST", "/", "other.host").as_deref(),
            Some("catchall")
        );
    }

    #[test]
    fn no_match_returns_none_rather_than_an_arbitrary_route() {
        let m = matcher(vec![route(
            "api",
            vec![MatchCondition::PathPrefix("/api".into())],
        )]);
        assert_eq!(hit(&m, "GET", "/other", "h").as_deref(), None);
    }
}