S007: typed download addresses, blocked count reset in the hub, small download sizes, live bridge byte count

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
astar76 2026-11-12 10:26:07 -06:00
commit 8257bf90f6
4 changed files with 48 additions and 13 deletions

View file

@ -1132,6 +1132,29 @@ impl App {
}
return;
}
// Typed addresses don't pass through `request_navigation`: catch downloads here as links are.
if matches!(parsed.scheme(), "http" | "https") {
let page = win.tabs[i].url.clone();
if download::looks_like_download(&parsed) {
self.start_download(parsed, page, None);
return;
}
if download::worth_probing(&parsed) {
let headers = self.request_headers(&parsed, &page);
let probe_url = parsed.clone();
self.spawn("probe", move || download::probe(&probe_url, &headers), move |app, result| match result {
Ok(probe) if probe.is_download => app.start_download(parsed, page, Some(probe.file_name)),
_ => app.load_in_active_tab(window, url, parsed),
});
return;
}
}
self.load_in_active_tab(window, url, parsed);
}
fn load_in_active_tab(&mut self, window: WindowId, url: String, parsed: Url) {
let Some(win) = self.windows.get(&window) else { return };
let Some(i) = (!win.tabs.is_empty()).then_some(win.active) else { return };
let has_web = win.tabs[i].web.is_some();
if has_web {
let win = self.windows.get_mut(&window).expect("found");
@ -1766,7 +1789,10 @@ impl App {
d.state = DlState::Done;
d.path = path.clone();
d.finished = util::now();
d.received = d.received.max(d.total);
// A small file finishes before its first progress report: take the size from the file.
let size = std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0);
d.received = d.received.max(d.total).max(size);
d.total = d.total.max(d.received);
d.speed = 0;
}
drop(downloads);
@ -1901,7 +1927,7 @@ impl App {
app.applied.adblock.last_update = app.shared.settings.borrow().adblock.last_update;
app.save_settings();
let failures = app.shared.adblock.failures.read().map(|f| f.len()).unwrap_or(0);
*app.shared.adblock_status.borrow_mut() = if failures > 0 { format!("{} rules · {failures} lists couldn't be downloaded", fmt_count(rules)) } else { format!("{} rules · {refreshed} lists updated", fmt_count(rules)) };
*app.shared.adblock_status.borrow_mut() = if failures > 0 { format!("{} rules · {failures} list{} couldn't be downloaded", fmt_count(rules), if failures == 1 { "" } else { "s" }) } else { format!("{} rules · {refreshed} list{} updated", fmt_count(rules), if refreshed == 1 { "" } else { "s" }) };
app.redraw_all();
});
}

View file

@ -779,7 +779,6 @@ impl App {
let Some((w, i)) = self.find_webview(id) else { return };
match status {
LoadStatus::Started => {
self.hub.blocked.borrow_mut().insert(id, 0);
let win = self.windows.get_mut(&w).expect("found");
let tab = &mut win.tabs[i];
tab.load_status = Some(status);

View file

@ -236,8 +236,10 @@ impl Hub {
return;
}
drop(config);
// The app resets the blocked count when the page starts loading (LoadStatus::Started).
// A new page: count its blocked requests from zero. Here, not at LoadStatus::Started: the app
// sees that event after the page's first sub-resources were already decided and counted.
if let Some(id) = id {
self.blocked.borrow_mut().insert(id, 0);
self.log_request(id, &method, url.as_str(), &kind, false);
}
return;

View file

@ -189,6 +189,7 @@ impl Bridge {
if !pending.is_empty() {
let mut u = &upstream;
u.write_all(&pending)?;
self.bytes.fetch_add(pending.len() as u64, Ordering::Relaxed);
}
pump(client, upstream, &self.bytes);
return Ok(());
@ -228,6 +229,7 @@ impl Bridge {
let pending = reader.buffer().to_vec();
if !pending.is_empty() {
u.write_all(&pending)?;
self.bytes.fetch_add(pending.len() as u64, Ordering::Relaxed);
}
}
pump(client, upstream, &self.bytes);
@ -409,29 +411,27 @@ fn pump(client: TcpStream, upstream: TcpStream, counter: &Arc<AtomicU64>) {
let (Ok(client2), Ok(upstream2)) = (client.try_clone(), upstream.try_clone()) else { return };
let counter2 = counter.clone();
let up = thread::Builder::new().stack_size(64 * 1024).spawn(move || {
let n = copy(&client2, &upstream2);
counter2.fetch_add(n, Ordering::Relaxed);
copy(&client2, &upstream2, &counter2);
let _ = upstream2.shutdown(Shutdown::Write);
});
let n = copy(&upstream, &client);
counter.fetch_add(n, Ordering::Relaxed);
copy(&upstream, &client, counter);
let _ = client.shutdown(Shutdown::Write);
if let Ok(handle) = up {
let _ = handle.join();
}
}
fn copy(mut from: &TcpStream, mut to: &TcpStream) -> u64 {
fn copy(mut from: &TcpStream, mut to: &TcpStream, counter: &AtomicU64) {
// Counted as the bytes flow: kept-alive tunnels (HTTP/2) stay open for minutes.
let mut buf = [0u8; 16 * 1024];
let mut total = 0u64;
loop {
match from.read(&mut buf) {
Ok(0) | Err(_) => return total,
Ok(0) | Err(_) => return,
Ok(n) => {
if to.write_all(&buf[..n]).is_err() {
return total;
return;
}
total += n as u64;
counter.fetch_add(n as u64, Ordering::Relaxed);
},
}
}
@ -469,6 +469,8 @@ mod tests {
let mut buf = [0u8; 5];
s.read_exact(&mut buf).unwrap();
s.write_all(b"pong!").unwrap();
// Keep the tunnel open until the client goes.
let _ = s.read(&mut buf);
});
let bridge = Bridge::start().unwrap();
let mut c = TcpStream::connect(("127.0.0.1", bridge.port)).unwrap();
@ -483,5 +485,11 @@ mod tests {
let mut reply = vec![0u8; "HTTP/1.1 200 Connection established\r\n\r\npong!".len()];
c.read_exact(&mut reply).unwrap();
assert!(String::from_utf8_lossy(&reply).ends_with("pong!"));
// Traffic is counted while the tunnel is still open (it used to be counted when it closed).
let counted = (0..100).any(|_| {
thread::sleep(std::time::Duration::from_millis(10));
bridge.bytes.load(Ordering::Relaxed) >= 10
});
assert!(counted, "bytes counted: {}", bridge.bytes.load(Ordering::Relaxed));
}
}