use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant, SystemTime};
use http::uri::Authority;
use http::{HeaderMap, StatusCode, Uri};
const DEFAULT_MAX_AGE: u64 = 86_400;
const MAX_HEADER_BYTES: usize = 16 * 1024;
const MAX_ALTERNATIVES: usize = 8;
#[cfg(test)]
const BROKEN_BACKOFF: Duration = Duration::from_secs(300);
#[derive(Clone, Debug)]
pub struct AltSvcCache {
inner: Arc<Mutex<State>>,
capacity: usize,
}
#[derive(Debug, Default)]
struct State {
origins: HashMap<String, Entry>,
sequence: u64,
generation: u64,
}
#[derive(Debug)]
struct Entry {
alternatives: Vec<Alternative>,
sequence: u64,
}
#[derive(Clone, Debug)]
pub(crate) struct Alternative {
pub(crate) authority: Authority,
pub(crate) from_dns: bool,
pub(crate) requires_ech: bool,
expires: Instant,
persist: bool,
broken_until: Option<Instant>,
}
#[derive(Clone, Debug)]
pub struct Record {
pub origin: String,
pub authority: String,
pub expires_at: SystemTime,
pub persist: bool,
}
impl Alternative {
pub(crate) fn service(authority: Authority, ttl: Duration, requires_ech: bool) -> Self {
Self {
authority,
from_dns: true,
requires_ech,
expires: Instant::now() + ttl.min(Duration::from_secs(86_400)),
persist: false,
broken_until: None,
}
}
}
impl Default for AltSvcCache {
fn default() -> Self {
Self::with_capacity(256)
}
}
impl AltSvcCache {
pub fn new() -> Self {
Self::default()
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
inner: Arc::new(Mutex::new(State::default())),
capacity,
}
}
pub fn clear(&self) {
self.inner.lock().unwrap().origins.clear();
}
pub fn network_changed(&self) {
let now = Instant::now();
let mut state = self.inner.lock().unwrap();
state.generation = state.generation.wrapping_add(1);
state.origins.retain(|_, entry| {
entry
.alternatives
.retain(|alt| alt.persist && alt.expires > now);
!entry.alternatives.is_empty()
});
}
pub fn len(&self) -> usize {
let mut state = self.inner.lock().unwrap();
prune(&mut state, Instant::now());
state.origins.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn snapshot(&self) -> Vec<Record> {
let now = Instant::now();
let wall = SystemTime::now();
let mut state = self.inner.lock().unwrap();
prune(&mut state, now);
state
.origins
.iter()
.flat_map(|(origin, entry)| {
entry.alternatives.iter().map(|alt| Record {
origin: origin.clone(),
authority: alt.authority.to_string(),
expires_at: wall + alt.expires.saturating_duration_since(now),
persist: alt.persist,
})
})
.collect()
}
pub fn restore(&self, records: impl IntoIterator<Item = Record>) {
let mut grouped: HashMap<String, Vec<Alternative>> = HashMap::new();
let now = Instant::now();
for record in records
.into_iter()
.take(self.capacity.saturating_mul(MAX_ALTERNATIVES))
{
let Ok(origin) = record.origin.parse::<Uri>() else {
continue;
};
let Some(key) = origin_key(&origin) else {
continue;
};
let Ok(age) = record.expires_at.duration_since(SystemTime::now()) else {
continue;
};
let age = age.min(Duration::from_secs(31_536_000));
let item = format!(
"h3=\"{}\";ma={};persist={}",
record.authority,
age.as_secs(),
u8::from(record.persist)
);
let Some(alt) = parse_alternative(&origin, &item, Duration::ZERO, now) else {
continue;
};
if !grouped.contains_key(&key) && grouped.len() >= self.capacity {
continue;
}
let alternatives = grouped.entry(key).or_default();
if alternatives.len() < MAX_ALTERNATIVES
&& !alternatives
.iter()
.any(|existing| existing.authority == alt.authority)
{
alternatives.push(alt);
}
}
let mut state = self.inner.lock().unwrap();
prune(&mut state, now);
for (origin, alternatives) in grouped {
if state.origins.len() >= self.capacity && !state.origins.contains_key(&origin) {
if let Some(oldest) = state
.origins
.iter()
.min_by_key(|(_, entry)| entry.sequence)
.map(|(key, _)| key.clone())
{
state.origins.remove(&oldest);
}
}
state.sequence = state.sequence.wrapping_add(1);
let sequence = state.sequence;
state.origins.insert(
origin,
Entry {
alternatives,
sequence,
},
);
}
}
pub(crate) fn generation(&self) -> u64 {
self.inner.lock().unwrap().generation
}
pub(crate) fn candidates(&self, origin: &Uri) -> Vec<Alternative> {
let Some(key) = origin_key(origin) else {
return Vec::new();
};
let mut state = self.inner.lock().unwrap();
let Some(entry) = state.origins.get_mut(&key) else {
return Vec::new();
};
let now = Instant::now();
entry.alternatives.retain(|alt| alt.expires > now);
if entry.alternatives.is_empty() {
state.origins.remove(&key);
return Vec::new();
}
entry.alternatives.clone()
}
#[cfg(test)]
pub(crate) fn get(&self, origin: &Uri) -> Option<Alternative> {
let key = origin_key(origin)?;
let now = Instant::now();
let mut state = self.inner.lock().unwrap();
prune(&mut state, now);
state
.origins
.get(&key)?
.alternatives
.iter()
.find_map(|alt| {
if alt.broken_until.is_some_and(|until| until > now) {
None
} else {
Some(alt.clone())
}
})
}
#[cfg(test)]
pub(crate) fn mark_broken(&self, origin: &Uri, alternative: &Alternative) {
let Some(key) = origin_key(origin) else {
return;
};
if let Some(entry) = self.inner.lock().unwrap().origins.get_mut(&key) {
for alt in &mut entry.alternatives {
if alt.authority == alternative.authority {
alt.broken_until = Some(Instant::now() + BROKEN_BACKOFF);
}
}
}
}
pub(crate) fn remove(&self, origin: &Uri, alternative: &Alternative) {
let Some(key) = origin_key(origin) else {
return;
};
let mut state = self.inner.lock().unwrap();
if let Some(entry) = state.origins.get_mut(&key) {
entry
.alternatives
.retain(|alt| alt.authority != alternative.authority);
if entry.alternatives.is_empty() {
state.origins.remove(&key);
}
}
}
pub(crate) fn observe(
&self,
origin: &Uri,
status: StatusCode,
headers: &HeaderMap,
response_delay: Duration,
) {
if self.capacity == 0 || status == StatusCode::MISDIRECTED_REQUEST {
return;
}
let Some(key) = origin_key(origin) else {
return;
};
if !headers.contains_key("alt-svc") {
return;
}
let now = Instant::now();
let age = response_age(headers, response_delay);
let mut alternatives = Vec::new();
let mut bytes = 0_usize;
let values = headers.get_all("alt-svc");
let mut clear = false;
for value in &values {
bytes = bytes.saturating_add(value.as_bytes().len());
if bytes > MAX_HEADER_BYTES {
return;
}
let Ok(value) = value.to_str() else {
return;
};
if value.trim() == "clear" {
clear = true;
continue;
}
let Some(items) = split_quoted(value, b',') else {
return;
};
for item in items {
if item.trim().is_empty() {
continue;
}
let Some(alt) = parse_alternative(origin, item, age, now) else {
continue;
};
if alternatives.len() < MAX_ALTERNATIVES
&& !alternatives
.iter()
.any(|old: &Alternative| old.authority == alt.authority)
{
alternatives.push(alt);
}
}
}
let mut state = self.inner.lock().unwrap();
prune(&mut state, now);
if let Some(old) = state.origins.remove(&key) {
for alt in &mut alternatives {
if let Some(previous) = old
.alternatives
.iter()
.find(|previous| previous.authority == alt.authority)
{
alt.broken_until = previous.broken_until;
}
}
}
if clear || alternatives.is_empty() {
return;
}
if state.origins.len() >= self.capacity {
if let Some(oldest) = state
.origins
.iter()
.min_by_key(|(_, entry)| entry.sequence)
.map(|(key, _)| key.clone())
{
state.origins.remove(&oldest);
}
}
state.sequence = state.sequence.wrapping_add(1);
let sequence = state.sequence;
state.origins.insert(
key,
Entry {
alternatives,
sequence,
},
);
}
}
fn origin_key(uri: &Uri) -> Option<String> {
if uri.scheme_str()? != "https" {
return None;
}
Some(format!(
"https://{}:{}",
uri.host()?.to_ascii_lowercase(),
uri.port_u16().unwrap_or(443)
))
}
fn prune(state: &mut State, now: Instant) {
state.origins.retain(|_, entry| {
entry.alternatives.retain(|alt| alt.expires > now);
!entry.alternatives.is_empty()
});
}
fn response_age(headers: &HeaderMap, response_delay: Duration) -> Duration {
let age = headers
.get(http::header::AGE)
.and_then(|age| age.to_str().ok())
.and_then(|age| age.parse::<u64>().ok())
.unwrap_or(0);
let apparent_age = headers
.get(http::header::DATE)
.and_then(|date| date.to_str().ok())
.and_then(|date| httpdate::parse_http_date(date).ok())
.and_then(|date| SystemTime::now().duration_since(date).ok())
.unwrap_or_default();
apparent_age.max(Duration::from_secs(age).saturating_add(response_delay))
}
fn parse_alternative(origin: &Uri, item: &str, age: Duration, now: Instant) -> Option<Alternative> {
let fields = split_quoted(item, b';')?;
let (protocol, authority) = fields.first()?.trim().split_once('=')?;
let protocol = protocol.trim();
if !protocol.bytes().all(is_token) {
return None;
}
if percent_encoding::percent_decode_str(protocol).collect::<Vec<_>>() != b"h3" {
return None;
}
let authority = quoted(authority.trim())?;
let authority = if authority.starts_with(':') {
format!("{}{}", origin.host()?, authority)
} else {
authority
};
if authority.contains('@') {
return None;
}
let authority: Authority = authority.parse().ok()?;
if authority.host().is_empty() || authority.port_u16()? == 0 {
return None;
}
let mut max_age = None;
let mut persist = None;
for field in fields.iter().skip(1) {
let (name, value) = field.trim().split_once('=')?;
let name = name.trim();
if name.is_empty() || !name.bytes().all(is_token) {
return None;
}
let value = value.trim();
let value = if value.starts_with('"') {
quoted(value)?
} else if !value.is_empty() && value.bytes().all(is_token) {
value.to_owned()
} else {
return None;
};
if name.eq_ignore_ascii_case("ma") {
if max_age.is_some() || !value.bytes().all(|b| b.is_ascii_digit()) {
return None;
}
max_age = Some(value.parse::<u64>().ok()?);
} else if name.eq_ignore_ascii_case("persist") {
if persist.is_some() {
return None;
}
persist = Some(value == "1");
}
}
let lifetime = Duration::from_secs(max_age.unwrap_or(DEFAULT_MAX_AGE).min(31_536_000));
let remaining = lifetime.checked_sub(age)?;
if remaining.is_zero() {
return None;
}
Some(Alternative {
authority,
from_dns: false,
requires_ech: false,
expires: now.checked_add(remaining)?,
persist: persist.unwrap_or(false),
broken_until: None,
})
}
fn split_quoted(value: &str, delimiter: u8) -> Option<Vec<&str>> {
let mut fields = Vec::new();
let mut start = 0;
let mut in_quotes = false;
let mut escaped = false;
for (index, byte) in value.bytes().enumerate() {
if escaped {
escaped = false;
} else if in_quotes && byte == b'\\' {
escaped = true;
} else if byte == b'"' {
in_quotes = !in_quotes;
} else if !in_quotes && byte == delimiter {
fields.push(&value[start..index]);
start = index + 1;
}
}
if in_quotes || escaped {
return None;
}
fields.push(&value[start..]);
Some(fields)
}
fn quoted(value: &str) -> Option<String> {
let inner = value.strip_prefix('"')?.strip_suffix('"')?;
let mut result = String::new();
let mut escaped = false;
for byte in inner.bytes() {
if !(b' '..=b'~').contains(&byte) {
return None;
}
if escaped {
result.push(byte as char);
escaped = false;
} else if byte == b'\\' {
escaped = true;
} else if byte == b'"' {
return None;
} else {
result.push(byte as char);
}
}
(!escaped).then_some(result)
}
fn is_token(byte: u8) -> bool {
byte.is_ascii_alphanumeric() || b"!#$%&'*+-.^_`|~".contains(&byte)
}
#[cfg(test)]
mod tests {
use super::*;
fn learn(cache: &AltSvcCache, origin: &str, value: &str) {
let mut headers = HeaderMap::new();
headers.insert("alt-svc", value.parse().unwrap());
cache.observe(
&origin.parse().unwrap(),
StatusCode::OK,
&headers,
Duration::ZERO,
);
}
fn expire(cache: &AltSvcCache, origin: &Uri, index: usize) {
cache
.inner
.lock()
.unwrap()
.origins
.get_mut(&origin_key(origin).unwrap())
.unwrap()
.alternatives[index]
.expires = Instant::now();
}
#[test]
fn candidate_lookup_expires_alternatives_and_preserves_backoff() {
let cache = AltSvcCache::new();
let origin: Uri = "https://example.test/".parse().unwrap();
learn(
&cache,
&origin.to_string(),
"h3=\":443\";ma=60, h3=\":8443\";ma=60;persist=1",
);
let alternatives = cache.candidates(&origin);
cache.mark_broken(&origin, &alternatives[1]);
expire(&cache, &origin, 0);
let live = cache.candidates(&"https://EXAMPLE.test:443/path".parse().unwrap());
assert_eq!(live.len(), 1);
assert_eq!(live[0].authority, "example.test:8443");
assert!(live[0].persist);
assert!(live[0].broken_until.unwrap() > Instant::now());
expire(&cache, &origin, 0);
assert!(cache.candidates(&origin).is_empty());
assert!(cache.is_empty());
}
#[test]
fn insertion_reclaims_unrelated_expired_origins_before_eviction() {
let cache = AltSvcCache::with_capacity(2);
let fresh: Uri = "https://fresh.test/".parse().unwrap();
let expired: Uri = "https://expired.test/".parse().unwrap();
learn(&cache, &fresh.to_string(), "h3=\":443\"");
learn(&cache, &expired.to_string(), "h3=\":443\"");
expire(&cache, &expired, 0);
assert_eq!(cache.candidates(&fresh).len(), 1);
let added: Uri = "https://added.test/".parse().unwrap();
learn(&cache, &added.to_string(), "h3=\":443\"");
assert_eq!(cache.len(), 2);
assert_eq!(cache.candidates(&fresh).len(), 1);
assert_eq!(cache.candidates(&added).len(), 1);
assert!(cache.candidates(&expired).is_empty());
}
#[test]
fn observers_prune_expired_origins_after_lookup_misses() {
for snapshot_first in [false, true] {
let cache = AltSvcCache::with_capacity(2);
learn(&cache, "https://fresh.test/", "h3=\":443\"");
let expired: Uri = "https://expired.test/".parse().unwrap();
learn(&cache, &expired.to_string(), "h3=\":443\"");
expire(&cache, &expired, 0);
assert!(cache
.candidates(&"https://missing.test/".parse().unwrap())
.is_empty());
if snapshot_first {
assert_eq!(cache.snapshot().len(), 1);
}
assert_eq!(cache.len(), 1);
let records = cache.snapshot();
assert_eq!(records.len(), 1);
assert_eq!(records[0].origin, "https://fresh.test:443");
}
}
#[test]
fn origin_scope_and_default_port_normalization() {
let cache = AltSvcCache::new();
learn(&cache, "https://example.com/", "h3=\":8443\"");
assert_eq!(
cache
.get(&"https://EXAMPLE.com:443/path".parse().unwrap())
.unwrap()
.authority,
"example.com:8443"
);
for uri in [
"http://example.com/",
"https://other.com/",
"https://example.com:444/",
] {
assert!(cache.get(&uri.parse().unwrap()).is_none());
}
}
#[test]
fn parses_lists_escapes_parameters_and_ipv6() {
let cache = AltSvcCache::new();
learn(
&cache,
"https://[::1]/",
"h2=\":443\", h%33=\":8443\"; ma=\"60\"; persist=1; ext=\"a,b;\\\"c\"",
);
let alt = cache.get(&"https://[::1]/".parse().unwrap()).unwrap();
assert_eq!(alt.authority, "[::1]:8443");
assert!(alt.persist);
}
#[test]
fn expiration_age_replacement_and_clear() {
let cache = AltSvcCache::new();
let origin: Uri = "https://example.com/".parse().unwrap();
learn(&cache, &origin.to_string(), "h3=\":443\"");
let mut headers = HeaderMap::new();
headers.insert("alt-svc", "h3=\":8443\"; ma=30".parse().unwrap());
headers.insert("age", "30".parse().unwrap());
cache.observe(&origin, StatusCode::OK, &headers, Duration::ZERO);
assert!(cache.is_empty());
learn(&cache, &origin.to_string(), "h3=\":443\"");
learn(&cache, &origin.to_string(), "h2=\":443\"");
assert!(cache.is_empty());
learn(&cache, &origin.to_string(), "h3=\":443\"");
learn(&cache, &origin.to_string(), "clear");
assert!(cache.is_empty());
}
#[test]
fn date_and_response_delay_reduce_freshness() {
let cache = AltSvcCache::new();
let origin = "https://example.com/".parse().unwrap();
let mut headers = HeaderMap::new();
headers.insert("alt-svc", "h3=\":443\"; ma=1".parse().unwrap());
headers.insert("date", "Sun, 06 Nov 1994 08:49:37 GMT".parse().unwrap());
cache.observe(&origin, StatusCode::OK, &headers, Duration::ZERO);
assert!(cache.is_empty());
headers.remove("date");
cache.observe(&origin, StatusCode::OK, &headers, Duration::from_secs(2));
assert!(cache.is_empty());
}
#[test]
fn rejects_invalid_authorities_and_lifetimes() {
let cache = AltSvcCache::new();
for value in [
"h3=\"evil.example\"",
"h3=\"user@evil.example:443\"",
"h3=\":0\"",
"h3=\":65536\"",
"h3=\":443\"; ma=-1",
"h3=\":443\"; ma=0",
"h3=\":443\"; ma=1; ma=2",
"h3=\":443\"; ma=999999999999999999999999",
"h3=\"evil.example:443/path\"",
"h3=\":443",
"h3=:443",
] {
learn(&cache, "https://example.com/", value);
assert!(cache.is_empty(), "accepted {value}");
}
}
#[test]
fn bounded_storage_network_changes_and_backoff() {
let cache = AltSvcCache::with_capacity(2);
learn(&cache, "https://one.test/", "h3=\":443\"");
learn(&cache, "https://two.test/", "h3=\":443\"; persist=1");
learn(&cache, "https://three.test/", "h3=\":443\"");
assert_eq!(cache.len(), 2);
assert!(cache.get(&"https://one.test/".parse().unwrap()).is_none());
cache.network_changed();
assert_eq!(cache.len(), 1);
let origin: Uri = "https://two.test/".parse().unwrap();
let alternative = cache.get(&origin).unwrap();
cache.mark_broken(&origin, &alternative);
learn(&cache, "https://two.test/", "h3=\":443\"");
assert!(cache.get(&origin).is_none());
cache.clear();
assert!(cache.is_empty());
let disabled = AltSvcCache::with_capacity(0);
learn(&disabled, "https://example.com/", "h3=\":443\"");
assert!(disabled.is_empty());
}
#[test]
fn misdirected_responses_cannot_repopulate_cache() {
let cache = AltSvcCache::new();
let mut headers = HeaderMap::new();
headers.insert("alt-svc", "h3=\":443\"".parse().unwrap());
cache.observe(
&"https://example.com/".parse().unwrap(),
StatusCode::MISDIRECTED_REQUEST,
&headers,
Duration::ZERO,
);
assert!(cache.is_empty());
}
#[test]
fn snapshots_preserve_freshness_and_validate_imported_records() {
let source = AltSvcCache::new();
learn(&source, "https://one.test/", "h3=\":8443\";ma=60;persist=1");
let records = source.snapshot();
let restored = AltSvcCache::new();
restored.restore(records.clone());
let snapshot = restored.snapshot();
assert_eq!(snapshot.len(), 1);
assert_eq!(snapshot[0].origin, records[0].origin);
assert_eq!(snapshot[0].authority, records[0].authority);
assert!(snapshot[0].persist);
assert!(snapshot[0].expires_at <= records[0].expires_at);
restored.network_changed();
assert_eq!(restored.len(), 1);
for (origin, authority, expires_at) in [
(
"http://one.test",
"one.test:443",
SystemTime::now() + Duration::from_secs(60),
),
(
"https://one.test",
"user@one.test:443",
SystemTime::now() + Duration::from_secs(60),
),
("https://one.test", "one.test:443", SystemTime::UNIX_EPOCH),
] {
let cache = AltSvcCache::new();
cache.restore([Record {
origin: origin.into(),
authority: authority.into(),
expires_at,
persist: false,
}]);
assert!(cache.is_empty());
}
let bounded = AltSvcCache::with_capacity(1);
bounded.restore((0..100).map(|index| Record {
origin: format!("https://origin{index}.test"),
authority: "alternative.test:443".into(),
expires_at: SystemTime::now() + Duration::from_secs(60),
persist: false,
}));
assert_eq!(bounded.len(), 1);
}
}