use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex, OnceLock, PoisonError};
use ::regex::{Error, Regex, RegexBuilder};
const MAX_COMPILED_BYTES: usize = 1 << 20;
const MAX_KEPT_COMPILED_BYTES: usize = 128 * 1024;
const MAX_KEPT_PATTERNS: usize = 128;
const MAX_KEPT_SOURCE_BYTES: usize = 32 * 1024;
#[derive(Clone)]
enum Compiled {
Kept(Arc<Regex>),
PerCall,
Refused,
}
struct KeptPatterns {
compiled: HashMap<Arc<str>, Compiled>,
order: VecDeque<Arc<str>>,
source_bytes: usize,
}
impl KeptPatterns {
fn new() -> Self {
Self {
compiled: HashMap::new(),
order: VecDeque::new(),
source_bytes: 0,
}
}
fn get(this: &Mutex<Self>, source: &str) -> Option<Arc<Regex>> {
let lock = || this.lock().unwrap_or_else(PoisonError::into_inner);
let found = lock().compiled.get(source).cloned();
let compiled = match found {
Some(compiled) => compiled,
None => {
let fresh = compile_to_keep(source);
let mut kept = lock();
match kept.compiled.get(source) {
Some(raced) => raced.clone(),
None => {
kept.insert(source, fresh.clone());
fresh
}
}
}
};
match compiled {
Compiled::Kept(regex) => Some(regex),
Compiled::PerCall => compile(source, MAX_COMPILED_BYTES).ok().map(Arc::new),
Compiled::Refused => None,
}
}
fn insert(&mut self, source: &str, compiled: Compiled) {
if source.len() > MAX_KEPT_SOURCE_BYTES {
return;
}
while self.compiled.len() >= MAX_KEPT_PATTERNS
|| self.source_bytes + source.len() > MAX_KEPT_SOURCE_BYTES
{
let Some(oldest) = self.order.pop_front() else {
break;
};
self.source_bytes -= oldest.len();
self.compiled.remove(&oldest);
}
let source: Arc<str> = source.into();
self.source_bytes += source.len();
self.order.push_back(Arc::clone(&source));
self.compiled.insert(source, compiled);
}
}
fn compile_to_keep(source: &str) -> Compiled {
match compile(source, MAX_KEPT_COMPILED_BYTES) {
Ok(regex) => Compiled::Kept(Arc::new(regex)),
Err(Error::CompiledTooBig(_)) => match compile(source, MAX_COMPILED_BYTES) {
Ok(_) => Compiled::PerCall,
Err(_) => Compiled::Refused,
},
Err(_) => Compiled::Refused,
}
}
fn compile(source: &str, bytes: usize) -> Result<Regex, Error> {
RegexBuilder::new(source)
.size_limit(bytes)
.dfa_size_limit(MAX_COMPILED_BYTES)
.build()
}
pub(super) fn guest_regex(source: &str) -> Option<Arc<Regex>> {
static KEPT: OnceLock<Mutex<KeptPatterns>> = OnceLock::new();
KeptPatterns::get(KEPT.get_or_init(|| Mutex::new(KeptPatterns::new())), source)
}
#[cfg(test)]
mod tests {
use super::*;
fn kept(set: &Mutex<KeptPatterns>, source: &str) -> Arc<Regex> {
KeptPatterns::get(set, source).expect("compiles")
}
#[test]
fn a_pattern_named_again_is_not_compiled_again() {
let first = guest_regex("nginx/([0-9.]+)").expect("compiles");
let again = guest_regex("nginx/([0-9.]+)").expect("compiles");
assert!(Arc::ptr_eq(&first, &again), "compiled twice");
assert!(guest_regex("(unclosed").is_none());
assert!(guest_regex("(unclosed").is_none(), "a refusal stands");
}
#[test]
fn the_set_holds_no_more_patterns_than_its_bound() {
let set = Mutex::new(KeptPatterns::new());
for n in 0..MAX_KEPT_PATTERNS + 10 {
kept(&set, &format!("^p{n}$"));
}
let set = set.into_inner().expect("not poisoned");
assert_eq!(set.compiled.len(), MAX_KEPT_PATTERNS);
assert!(!set.compiled.contains_key("^p0$"), "the oldest left first");
let latest = format!("^p{}$", MAX_KEPT_PATTERNS + 9);
assert!(set.compiled.contains_key(latest.as_str()));
}
#[test]
fn the_set_holds_no_more_source_than_its_bound() {
let set = Mutex::new(KeptPatterns::new());
let long = |n: usize| format!("(?x){n}{}", " ".repeat(MAX_KEPT_SOURCE_BYTES / 4));
for n in 0..8 {
kept(&set, &long(n));
}
assert!(set.lock().expect("not poisoned").source_bytes <= MAX_KEPT_SOURCE_BYTES);
let past = format!("(?x)b{}", " ".repeat(MAX_KEPT_SOURCE_BYTES));
let first = kept(&set, &past);
let again = kept(&set, &past);
assert!(!Arc::ptr_eq(&first, &again), "kept past the bound");
assert!(again.is_match("b"));
assert!(set.lock().expect("not poisoned").source_bytes <= MAX_KEPT_SOURCE_BYTES);
}
#[test]
fn a_pattern_too_large_to_keep_still_matches() {
let set = Mutex::new(KeptPatterns::new());
let large = r"\w{1,8}";
assert!(
compile(large, MAX_KEPT_COMPILED_BYTES).is_err(),
"fits the kept bound"
);
let first = kept(&set, large);
let again = kept(&set, large);
assert!(!Arc::ptr_eq(&first, &again), "held past the kept bound");
assert!(again.is_match("zond"));
}
}