From 9a1dfd0d65ed8f2d9749581bc0f33bb459d97113 Mon Sep 17 00:00:00 2001 From: Afloat Date: Mon, 9 Nov 2026 20:16:11 +0800 Subject: [PATCH] Count received HAR response bytes separately from decoded content (R3.13) (#1327) A HAR summary added the larger of each response's received and decoded sizes into one total. It now reports the received bytes and the decoded content bytes separately, with how many responses didn't record each. --- crates/analysis/src/web_har.rs | 21 +++++++++++++++++--- crates/analysis/tests/har_transfer_sizes.rs | 22 +++++++++++++++++++++ docs/ROADMAP.md | 2 ++ docs/STATUS.md | 1 + 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 crates/analysis/tests/har_transfer_sizes.rs diff --git a/crates/analysis/src/web_har.rs b/crates/analysis/src/web_har.rs index 231b588c5..084a33b70 100644 --- a/crates/analysis/src/web_har.rs +++ b/crates/analysis/src/web_har.rs @@ -414,6 +414,19 @@ fn entry_view(e: &HarEntry, red: &mut Redactor) -> Value { }) } +/// HAR uses -1 for an unknown size. A known zero (for example on a 304) still counts. +fn response_sizes(entries: &[HarEntry], field: impl Fn(&har::HarResponse) -> Option) -> (u64, usize) { + let mut bytes = 0u64; + let mut missing = 0; + for entry in entries { + match field(&entry.response).and_then(|n| u64::try_from(n).ok()) { + Some(n) => bytes = bytes.saturating_add(n), + None => missing += 1, + } + } + (bytes, missing) +} + /// Inspect a HAR capture. pub fn inspect_har(bytes: &[u8], opts: &HarOptions) -> Result { let har = har::parse(bytes, opts.max_entries.max(1)).map_err(|e| e.to_string())?; @@ -422,7 +435,8 @@ pub fn inspect_har(bytes: &[u8], opts: &HarOptions) -> Result { let mut statuses: BTreeMap = BTreeMap::new(); let mut hosts: BTreeMap = BTreeMap::new(); let mut mimes: BTreeMap = BTreeMap::new(); - let mut total_bytes: i64 = 0; + let (body_bytes, missing_body) = response_sizes(&har.entries, |r| r.body_size); + let (content_bytes, missing_content) = response_sizes(&har.entries, |r| r.content_size); let mut first: Option = None; let mut last: Option = None; let mut slowest: Vec<(f64, usize)> = Vec::new(); @@ -436,7 +450,6 @@ pub fn inspect_har(bytes: &[u8], opts: &HarOptions) -> Result { *hosts.entry(red.text("/hosts", &host)).or_default() += 1; let mime = view["response_mime"].as_str().unwrap_or("").split(';').next().unwrap_or("").trim().to_string(); *mimes.entry(mime.clone()).or_default() += 1; - total_bytes = total_bytes.saturating_add(e.response.body_size.unwrap_or(0).max(e.response.content_size.unwrap_or(0)).max(0)); if let Some(s) = &e.started { if first.as_ref().is_none_or(|f| s < f) { first = Some(s.clone()); @@ -505,7 +518,9 @@ pub fn inspect_har(bytes: &[u8], opts: &HarOptions) -> Result { "entries": har.entries.len(), "declared_entries": har.declared_entries, "methods": methods, "statuses": statuses, "hosts": hosts, "response_mimes": mimes, - "response_body_bytes": total_bytes, "first_request": first, "last_request": last, "slowest": slowest, + "response_body_bytes": body_bytes, "responses_without_body_size": missing_body, + "response_content_bytes": content_bytes, "responses_without_content_size": missing_content, + "first_request": first, "last_request": last, "slowest": slowest, }, "api_surface": ops, "entries": selected, diff --git a/crates/analysis/tests/har_transfer_sizes.rs b/crates/analysis/tests/har_transfer_sizes.rs new file mode 100644 index 000000000..dc6ced5e6 --- /dev/null +++ b/crates/analysis/tests/har_transfer_sizes.rs @@ -0,0 +1,22 @@ +use craft_anything_analysis::web_har::{HarOptions, inspect_har}; +use serde_json::json; + +#[test] +fn received_and_decoded_sizes_stay_separate() { + let responses = [ + json!({"status": 200, "bodySize": 25, "content": {"size": 100}}), + json!({"status": 304, "bodySize": 0, "content": {"size": 120}}), + json!({"status": 200, "bodySize": -1, "content": {"size": 50}}), + json!({"status": 200, "bodySize": 8, "content": {"size": -1}}), + json!({"status": 200, "content": {}}), + ]; + let entries: Vec<_> = + responses.into_iter().map(|response| json!({"request": {"method": "GET", "url": "https://example.test/"}, "response": response})).collect(); + let bytes = serde_json::to_vec(&json!({"log": {"entries": entries}})).unwrap(); + let report = inspect_har(&bytes, &HarOptions { max_entries: 10, limit: 10, ..Default::default() }).unwrap(); + let summary = &report["summary"]; + assert_eq!(summary["response_body_bytes"], 33); + assert_eq!(summary["responses_without_body_size"], 2); + assert_eq!(summary["response_content_bytes"], 270); + assert_eq!(summary["responses_without_content_size"], 2); +} diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 445f0d7bf..f05beb157 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -1647,6 +1647,8 @@ Owner: open · Status: in progress ### R3.13 Web inspection correctness Owner: open · Status: in progress +- [x] Report received HAR response body sizes separately from decoded content sizes and identify entries whose sizes are unknown. + — claimed: Afloat16, branch bugfix/r3.13-har-transfer-size - [x] Read HAR captures with the permitted UTF-8 byte order mark. — claimed: Afloat16, branch bugfix/r3.13-har-bom-bounds-20261107 - [x] The HTML scanner finds script preloads in `rel` token lists, decodes asset URLs once (numeric diff --git a/docs/STATUS.md b/docs/STATUS.md index 663091d4f..9d510e991 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -12,6 +12,7 @@ workspace clippy, layers, fmt and the secret scan of all history were clean. Chi | Binary property-list object types | Accepts compatible version-zero minor signatures and rejects unsupported major versions, invalid simple/date markers and unrepresentable non-string dictionary keys. | Typed binary fixtures with valid null, string-key and date controls. | | ASAR listings | Rejected unsafe names count toward the listing limit alongside retained entries. | Synthetic regression `invalid_names_consume_the_listing_budget` in the formats crate. | | HAR input | Captures with a leading UTF-8 byte order mark parse like the same capture without it. | Synthetic regression `reads_a_har_with_a_utf8_bom` in the formats crate. | +| HAR response byte totals | The network summary totals received body bytes and decoded content bytes separately. Each total reports how many entries lack its measurement. A known zero-byte transfer stays zero. | Compressed, cached and unknown-size response fixtures. | | Tar numeric fields | Reject negative GNU base-256 encodings when reading unsigned header values. | The octal_fields regression covers both a negative eight-byte value and a positive binary value. | | .NET single-file bundles | Entries crossing from the payload into the manifest are reported as unreadable. | Synthetic regression `a_member_cannot_borrow_bytes_from_the_manifest` in the formats crate. | | Watched descendant regression | A guarded Rust parent starts the heartbeat child in its inherited process group/job. No shell quoting is involved. The watched launch is stopped before missing startup markers can fail an assertion | macOS regressions pass for descendant shutdown, startup-timeout cleanup and direct ignored-fixture runs without guards. Windows execution of this replacement remains pending; the prior shell fixture failed there. The platform suite still has the separately tracked preview-connection fixture failure |