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};
static METADATA: StrategyMetadata = StrategyMetadata {
strategy_id: "cp-range",
strategy_name: "Cache Probe: Range (satisfiable)",
risk: RiskLevel::Safe,
};
static TECHNIQUE: Technique = Technique {
id: "cp-range",
name: "Satisfiable byte-range request",
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 CpRangeSizeProducer;
impl Producer for CpRangeSizeProducer {
fn admits(&self, class: ResponseClass) -> bool {
matches!(
class,
ResponseClass::PartialContent | ResponseClass::RangeNotSatisfiable
)
}
fn extract(&self, _class: ResponseClass, headers: &HeaderMap) -> Option<ProducerOutput> {
if let Some(ar) = headers.get(http::header::ACCEPT_RANGES) {
if ar.as_bytes().eq_ignore_ascii_case(b"none") {
return None;
}
}
let raw = headers.get(http::header::CONTENT_RANGE)?.to_str().ok()?;
let size_str = raw.rsplit('/').next()?;
let size: u64 = size_str.trim().parse().ok()?;
if size == 0 {
return None;
}
Some(ProducerOutput::ContentRangeSize(size))
}
}
pub(super) struct CpRangeSatisfiableConsumer;
impl Consumer for CpRangeSatisfiableConsumer {
fn needs(&self) -> ProducerOutputKind {
ProducerOutputKind::ContentRangeSize
}
fn generate(&self, ctx: &ScanContext, output: &ProducerOutput) -> Vec<ProbeSpec> {
let ProducerOutput::ContentRangeSize(size) = output else {
return vec![];
};
let full = format!("bytes=0-{}", size - 1);
let last = format!("bytes={0}-{0}", size - 1);
build_range_specs(ctx, [full, last])
}
}
fn build_range_specs(ctx: &ScanContext, range_vals: [String; 2]) -> Vec<ProbeSpec> {
let mut specs = Vec::with_capacity(2);
for range_val in range_vals {
let Some(hdrs) = try_clone_headers_with(&ctx.headers, "range", &range_val) else {
continue;
};
let pair = build_pair(
ctx,
Method::GET,
hdrs.clone(),
hdrs,
None,
METADATA.clone(),
TECHNIQUE,
);
specs.push(ProbeSpec::Pair(pair));
}
specs
}
pub struct CpRangeSatisfiable;
impl Strategy for CpRangeSatisfiable {
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, "range", "bytes=0-0");
let pair = build_pair(
ctx,
Method::GET,
hdrs.clone(),
hdrs,
None,
METADATA.clone(),
TECHNIQUE,
);
vec![ProbeSpec::Pair(pair)]
}
}
#[cfg(test)]
#[path = "range_satisfiable_tests.rs"]
mod tests;