use http::{HeaderMap, Method};
use parlov_core::{
always_applicable, NormativeStrength, OracleClass, ResponseClass, SignalSurface, Technique,
Vector,
};
use crate::chain::{Consumer, Producer, ProducerOutput, ProducerOutputKind};
use crate::context::ScanContext;
use crate::strategy::Strategy;
use crate::types::{ProbeSpec, RiskLevel, StrategyMetadata};
use crate::util::{build_pair, clone_headers_static, try_clone_headers_with};
const DISTANT_PAST: &str = "Mon, 01 Jan 1990 00:00:00 GMT";
static METADATA: StrategyMetadata = StrategyMetadata {
strategy_id: "cp-if-unmodified-since",
strategy_name: "Cache Probe: If-Unmodified-Since",
risk: RiskLevel::Safe,
};
static TECHNIQUE: Technique = Technique {
id: "cp-if-unmodified-since",
name: "If-Unmodified-Since precondition check",
oracle_class: OracleClass::Existence,
vector: Vector::CacheProbing,
strength: NormativeStrength::Should,
normalization_weight: None,
inverted_signal_weight: None,
method_relevant: false,
parser_relevant: false,
applicability: always_applicable,
contradiction_surface: SignalSurface::Status,
};
pub(super) struct CpIfUnmodifiedSinceProducer;
impl Producer for CpIfUnmodifiedSinceProducer {
fn admits(&self, class: ResponseClass) -> bool {
matches!(class, ResponseClass::Success)
}
fn extract(&self, _class: ResponseClass, headers: &HeaderMap) -> Option<ProducerOutput> {
let raw = headers.get(http::header::LAST_MODIFIED)?.to_str().ok()?;
Some(ProducerOutput::LastModified(raw.to_owned()))
}
}
pub(super) struct CpIfUnmodifiedSinceConsumer;
impl Consumer for CpIfUnmodifiedSinceConsumer {
fn needs(&self) -> ProducerOutputKind {
ProducerOutputKind::LastModified
}
fn generate(&self, ctx: &ScanContext, output: &ProducerOutput) -> Vec<ProbeSpec> {
let ProducerOutput::LastModified(date) = output else {
return vec![];
};
let Some(hdrs) = try_clone_headers_with(&ctx.headers, "if-unmodified-since", date) else {
return vec![];
};
let pair = build_pair(
ctx,
Method::GET,
hdrs.clone(),
hdrs,
None,
METADATA.clone(),
TECHNIQUE,
);
vec![ProbeSpec::Pair(pair)]
}
}
pub struct CpIfUnmodifiedSince;
impl Strategy for CpIfUnmodifiedSince {
fn metadata(&self) -> &'static StrategyMetadata {
&METADATA
}
fn technique_def(&self) -> &'static Technique {
&TECHNIQUE
}
fn methods(&self) -> &[Method] {
&[Method::GET]
}
fn is_applicable(&self, _ctx: &ScanContext) -> bool {
true
}
fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
let hdrs = clone_headers_static(&ctx.headers, "if-unmodified-since", DISTANT_PAST);
let pair = build_pair(
ctx,
Method::GET,
hdrs.clone(),
hdrs,
None,
METADATA.clone(),
TECHNIQUE,
);
vec![ProbeSpec::Pair(pair)]
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::minimal_ctx;
#[test]
fn generates_correct_technique_vector() {
let specs = CpIfUnmodifiedSince.generate(&minimal_ctx());
assert_eq!(specs[0].technique().vector, Vector::CacheProbing);
}
#[test]
fn generates_correct_technique_strength() {
let specs = CpIfUnmodifiedSince.generate(&minimal_ctx());
assert_eq!(specs[0].technique().strength, NormativeStrength::Should);
}
#[test]
fn generates_probe_with_correct_header() {
let specs = CpIfUnmodifiedSince.generate(&minimal_ctx());
let ProbeSpec::Pair(pair) = &specs[0] else {
panic!("expected Pair variant")
};
assert_eq!(
pair.probe.headers.get("if-unmodified-since").unwrap(),
DISTANT_PAST
);
}
#[test]
fn is_always_applicable() {
assert!(CpIfUnmodifiedSince.is_applicable(&minimal_ctx()));
}
#[test]
fn risk_is_safe() {
assert_eq!(CpIfUnmodifiedSince.risk(), RiskLevel::Safe);
}
}