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.
This commit is contained in:
Afloat authored and GitHub committed 2026-11-09 20:16:11 +08:00
1 parent 78b5f86f1b
commit 9a1dfd0d65
4 files changed
+43 -3

No files matched your search

+18 -3
View File
@@ -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<i64>) -> (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<Value, String> {
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<Value, String> {
let mut statuses: BTreeMap<String, usize> = BTreeMap::new();
let mut hosts: BTreeMap<String, usize> = BTreeMap::new();
let mut mimes: BTreeMap<String, usize> = 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<String> = None;
let mut last: Option<String> = None;
let mut slowest: Vec<(f64, usize)> = Vec::new();
@@ -436,7 +450,6 @@ pub fn inspect_har(bytes: &[u8], opts: &HarOptions) -> Result<Value, String> {
*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<Value, String> {
"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,
@@ -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);
}
+2
View File
@@ -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
+1
View File
@@ -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 |