use base64::{engine::general_purpose::STANDARD as BASE64, Engine as _};
use crate::engines::types::InternalCapturedResponse;
#[derive(Debug, Clone)]
pub struct XhrCaptureConfig {
pub url_patterns: Vec<String>,
pub max_bytes: u64,
pub max_responses: usize,
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct XhrCaptureStats {
pub overflow_skipped: usize,
pub oversized_dropped: usize,
pub decode_failed: usize,
pub fetch_failed: usize,
}
#[derive(Debug, Clone)]
pub struct PendingXhr {
pub request_id: String,
pub url: String,
pub status: u16,
pub mime_type: String,
}
#[derive(Debug)]
pub struct XhrCaptureBuffer {
max_responses: usize,
pending: Vec<PendingXhr>,
captured: Vec<InternalCapturedResponse>,
finished_early: std::collections::HashSet<String>,
overflow_skipped: usize,
oversized_dropped: usize,
decode_failed: usize,
fetch_failed: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RecordOutcome {
Recorded,
AlreadyFinished,
Skipped,
}
impl XhrCaptureBuffer {
pub fn new(max_responses: usize) -> Self {
Self {
max_responses,
pending: Vec::new(),
captured: Vec::new(),
finished_early: std::collections::HashSet::new(),
overflow_skipped: 0,
oversized_dropped: 0,
decode_failed: 0,
fetch_failed: 0,
}
}
pub fn record(&mut self, entry: PendingXhr) -> RecordOutcome {
if self.finished_early.remove(&entry.request_id) {
return RecordOutcome::AlreadyFinished;
}
if self.pending.len() + self.captured.len() >= self.max_responses {
self.overflow_skipped += 1;
return RecordOutcome::Skipped;
}
self.pending.push(entry);
RecordOutcome::Recorded
}
pub fn take_pending(&mut self, request_id: &str) -> Option<PendingXhr> {
let pos = self
.pending
.iter()
.position(|e| e.request_id == request_id)?;
Some(self.pending.remove(pos))
}
pub fn mark_finished_early(&mut self, request_id: &str) {
let cap = self.max_responses.saturating_mul(4).max(16);
if self.finished_early.len() < cap {
self.finished_early.insert(request_id.to_string());
}
}
pub fn push_captured(&mut self, response: InternalCapturedResponse) {
self.captured.push(response);
}
pub fn note_oversize(&mut self) {
self.oversized_dropped += 1;
}
pub fn note_decode_failed(&mut self) {
self.decode_failed += 1;
}
pub fn note_fetch_failed(&mut self) {
self.fetch_failed += 1;
}
pub fn drain_captured(&mut self) -> Vec<InternalCapturedResponse> {
std::mem::take(&mut self.captured)
}
pub fn pending_len(&self) -> usize {
self.pending.len()
}
pub fn stats(&self) -> XhrCaptureStats {
XhrCaptureStats {
overflow_skipped: self.overflow_skipped,
oversized_dropped: self.oversized_dropped,
decode_failed: self.decode_failed,
fetch_failed: self.fetch_failed,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BodyOutcome {
Kept(String),
Oversize,
DecodeFailed,
}
pub fn process_body(raw: String, base64_encoded: bool, max_bytes: u64) -> BodyOutcome {
if base64_encoded {
match BASE64.decode(raw.as_bytes()) {
Ok(bytes) => {
if bytes.len() as u64 > max_bytes {
return BodyOutcome::Oversize;
}
BodyOutcome::Kept(String::from_utf8_lossy(&bytes).into_owned())
}
Err(_) => BodyOutcome::DecodeFailed,
}
} else if raw.len() as u64 > max_bytes {
BodyOutcome::Oversize
} else {
BodyOutcome::Kept(raw)
}
}
pub use crate::utils::glob::glob_match;
pub fn url_matches_capture_patterns(url: &str, patterns: &[String]) -> bool {
if patterns.is_empty() {
return true;
}
patterns.iter().any(|p| glob_match(p, url))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn glob_match_star_spans_path_separators() {
assert!(glob_match("*/api/*", "https://example.com/api/data"));
assert!(glob_match(
"*/api/*",
"https://example.com/api/v2/users?id=1"
));
}
#[test]
fn glob_match_rejects_non_matching() {
assert!(!glob_match("*/api/*", "https://example.com/static/app.js"));
assert!(!glob_match("*/api/*", "https://api.example.com/v2/other"));
}
#[test]
fn glob_match_exact_and_wildcard_forms() {
assert!(glob_match(
"https://api.example.com/*",
"https://api.example.com/v1/users"
));
assert!(glob_match("*", "anything at all"));
assert!(glob_match("exact", "exact"));
assert!(!glob_match("exact", "other"));
assert!(glob_match("*api*", "https://x.com/api"));
}
#[test]
fn glob_match_question_mark_requires_exactly_one_char() {
assert!(glob_match("a?c", "abc"));
assert!(!glob_match("a?c", "ac"));
assert!(!glob_match("a?c", "abbc"));
}
#[test]
fn glob_match_empty_pattern_only_matches_empty() {
assert!(glob_match("", ""));
assert!(!glob_match("", "x"));
}
#[test]
fn glob_match_leading_and_trailing_star() {
assert!(glob_match("*/data", "https://x.com/a/b/data"));
assert!(glob_match("https://x.com/*", "https://x.com/"));
assert!(!glob_match("https://x.com/*", "https://x.com"));
}
#[test]
fn empty_patterns_capture_all() {
assert!(url_matches_capture_patterns(
"https://example.com/anything",
&[]
));
}
#[test]
fn single_pattern_filters() {
let patterns = vec!["*/api/*".to_string()];
assert!(url_matches_capture_patterns(
"https://example.com/api/data",
&patterns
));
assert!(!url_matches_capture_patterns(
"https://example.com/static/app.js",
&patterns
));
}
#[test]
fn multiple_patterns_any_match() {
let patterns = vec!["*/a/*".to_string(), "*/b/*".to_string()];
assert!(url_matches_capture_patterns(
"https://x.com/b/item",
&patterns
));
assert!(!url_matches_capture_patterns(
"https://x.com/c/item",
&patterns
));
}
#[test]
fn regex_like_metachars_are_literal_no_panic() {
let patterns = vec!["*[id=1]*".to_string()];
assert!(!url_matches_capture_patterns(
"https://x.com/item",
&patterns
));
assert!(url_matches_capture_patterns(
"https://x.com/[id=1]",
&patterns
));
}
fn pending(id: &str) -> PendingXhr {
PendingXhr {
request_id: id.to_string(),
url: format!("https://example.com/api/{}", id),
status: 200,
mime_type: "application/json".to_string(),
}
}
fn captured(id: &str) -> InternalCapturedResponse {
InternalCapturedResponse {
url: format!("https://example.com/api/{}", id),
status: 200,
mime_type: "application/json".to_string(),
body: format!("body-{}", id),
}
}
#[test]
fn buffer_records_and_takes_pending() {
let mut buf = XhrCaptureBuffer::new(3);
assert_eq!(buf.record(pending("1")), RecordOutcome::Recorded);
assert_eq!(buf.record(pending("2")), RecordOutcome::Recorded);
assert_eq!(buf.stats().overflow_skipped, 0);
let entry = buf.take_pending("1").expect("pending entry must exist");
assert_eq!(entry.url, "https://example.com/api/1");
assert_eq!(entry.status, 200);
assert_eq!(entry.mime_type, "application/json");
assert!(buf.take_pending("1").is_none());
assert!(buf.take_pending("missing").is_none());
}
#[test]
fn buffer_stops_at_max_responses_and_counts() {
let mut buf = XhrCaptureBuffer::new(2);
assert_eq!(buf.record(pending("1")), RecordOutcome::Recorded);
assert_eq!(buf.record(pending("2")), RecordOutcome::Recorded);
assert_eq!(buf.record(pending("3")), RecordOutcome::Skipped);
assert_eq!(buf.record(pending("4")), RecordOutcome::Skipped);
assert_eq!(buf.stats().overflow_skipped, 2);
assert_eq!(buf.pending_len(), 2);
}
#[test]
fn buffer_counts_captured_toward_cap() {
let mut buf = XhrCaptureBuffer::new(2);
assert_eq!(buf.record(pending("1")), RecordOutcome::Recorded);
let entry = buf.take_pending("1").unwrap();
buf.push_captured(captured(&entry.request_id));
assert_eq!(buf.record(pending("2")), RecordOutcome::Recorded);
let entry = buf.take_pending("2").unwrap();
buf.push_captured(captured(&entry.request_id));
assert_eq!(buf.record(pending("3")), RecordOutcome::Skipped);
assert_eq!(buf.stats().overflow_skipped, 1);
}
#[test]
fn buffer_zero_cap_skips_everything() {
let mut buf = XhrCaptureBuffer::new(0);
assert_eq!(buf.record(pending("1")), RecordOutcome::Skipped);
assert_eq!(buf.stats().overflow_skipped, 1);
assert!(buf.drain_captured().is_empty());
}
#[test]
fn buffer_early_finish_flow_yields_already_finished() {
let mut buf = XhrCaptureBuffer::new(3);
assert!(buf.take_pending("9").is_none());
buf.mark_finished_early("9");
assert_eq!(buf.record(pending("9")), RecordOutcome::AlreadyFinished);
assert_eq!(buf.pending_len(), 0);
buf.mark_finished_early("ghost");
assert_eq!(buf.record(pending("1")), RecordOutcome::Recorded);
}
#[test]
fn buffer_finished_early_set_is_bounded() {
let mut buf = XhrCaptureBuffer::new(4);
let cap = 4 * 4;
for i in 0..(cap + 10) {
buf.mark_finished_early(&format!("id-{i}"));
}
assert_eq!(buf.record(pending("untracked")), RecordOutcome::Recorded);
}
#[test]
fn buffer_drain_captured_round_trip() {
let mut buf = XhrCaptureBuffer::new(4);
buf.push_captured(captured("a"));
buf.push_captured(captured("b"));
let drained = buf.drain_captured();
assert_eq!(drained.len(), 2);
assert_eq!(drained[0].url, "https://example.com/api/a");
assert_eq!(drained[0].body, "body-a");
assert_eq!(drained[1].body, "body-b");
assert!(buf.drain_captured().is_empty());
}
#[test]
fn buffer_stats_reflect_drop_reasons() {
let mut buf = XhrCaptureBuffer::new(4);
buf.note_oversize();
buf.note_decode_failed();
buf.note_fetch_failed();
let stats = buf.stats();
assert_eq!(stats.oversized_dropped, 1);
assert_eq!(stats.decode_failed, 1);
assert_eq!(stats.fetch_failed, 1);
assert_eq!(stats.overflow_skipped, 0);
}
#[test]
fn process_body_plain_text_under_limit_kept() {
let out = process_body("hello".to_string(), false, 1024);
assert_eq!(out, BodyOutcome::Kept("hello".to_string()));
}
#[test]
fn process_body_plain_text_over_limit_dropped() {
let big = "x".repeat(4096);
let out = process_body(big.clone(), false, 256);
assert_eq!(out, BodyOutcome::Oversize);
assert_eq!(
process_body("x".repeat(256), false, 256),
BodyOutcome::Kept("x".repeat(256))
);
let _ = big;
}
#[test]
fn process_body_base64_decoded() {
use base64::Engine as _;
let encoded = BASE64.encode("{\"ok\":true}");
let out = process_body(encoded, true, 1024);
assert_eq!(out, BodyOutcome::Kept("{\"ok\":true}".to_string()));
}
#[test]
fn process_body_base64_oversize_measured_after_decode() {
use base64::Engine as _;
let encoded = BASE64.encode(vec![b'a'; 2048]);
assert_eq!(process_body(encoded, true, 1024), BodyOutcome::Oversize);
let encoded2 = BASE64.encode(vec![b'a'; 2048]);
assert!(matches!(
process_body(encoded2, true, 4096),
BodyOutcome::Kept(_)
));
}
#[test]
fn process_body_base64_invalid_fails_explicitly() {
assert_eq!(
process_body("!!!not base64!!!".to_string(), true, 1024),
BodyOutcome::DecodeFailed
);
}
#[test]
fn process_body_empty_body_kept() {
assert_eq!(
process_body(String::new(), false, 1024),
BodyOutcome::Kept(String::new())
);
}
}