From fe291fab2b75aaf2e235ff094d2d206a692691e8 Mon Sep 17 00:00:00 2001 From: faumaray Date: Tue, 27 Oct 2026 19:13:34 +0300 Subject: [PATCH] windows: component archives download on a slow line A component's archive (sing-box's is 15 MB) had a minute for the whole transfer, which a slow line misses (CI run #9 timed out on it). Now a minute without a byte fails it, the whole has no limit, and a transfer cut short is taken up where it stopped (an HTTP range), eight times at most; the pinned hash checks the result as before. Co-Authored-By: Claude Opus 5.5 --- windows/crates/ps-service/src/fetch.rs | 59 +++++++++++++++++++++++- windows/crates/ps-service/src/install.rs | 2 +- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/windows/crates/ps-service/src/fetch.rs b/windows/crates/ps-service/src/fetch.rs index cfa622c..e29e843 100644 --- a/windows/crates/ps-service/src/fetch.rs +++ b/windows/crates/ps-service/src/fetch.rs @@ -16,6 +16,12 @@ pub enum Route { } fn client(route: &Route) -> Result { + client_with(route, Some(Duration::from_secs(60))) +} + +/// `total`: the whole transfer's limit; none for an archive, which a stall (a minute without a +/// byte) ends instead. +fn client_with(route: &Route, total: Option) -> Result { let policy = reqwest::redirect::Policy::custom(|attempt| { let from = attempt.previous().last().map(|u| u.as_str().to_owned()).unwrap_or_default(); let to = attempt.url().as_str().to_owned(); @@ -34,7 +40,10 @@ fn client(route: &Route) -> Result { .user_agent(USER_AGENT) .redirect(policy) .connect_timeout(Duration::from_secs(15)) - .timeout(Duration::from_secs(60)); + .read_timeout(Duration::from_secs(60)); + if let Some(total) = total { + b = b.timeout(total); + } b = match route { Route::Direct => b.no_proxy(), Route::Proxy { port, login } => { @@ -69,6 +78,54 @@ pub async fn get(url: &str, route: &Route, limit: usize) -> Result> { Ok(body) } +/// A large download (a component's archive), at most `limit` bytes: no limit on the whole +/// transfer, which a slow line needs, but a minute without a byte fails it, and a transfer cut +/// short is taken up where it stopped (an HTTP range), eight times at most. +pub async fn get_large(url: &str, route: &Route, limit: usize) -> Result> { + let scheme = url_parts::split(url).map(|u| u.scheme).unwrap_or_default(); + if scheme != "http" && scheme != "https" { + bail!("the URL must be http:// or https://"); + } + let http = client_with(route, None)?; + let mut body: Vec = Vec::new(); + let mut last = String::new(); + for _ in 0..8 { + let mut req = http.get(url); + if !body.is_empty() { + req = req.header(reqwest::header::RANGE, format!("bytes={}-", body.len())); + } + let mut resp = match req.send().await { + Ok(r) => r, + Err(e) => { + last = describe(&e); + continue; + } + }; + match resp.status().as_u16() { + 206 if !body.is_empty() => {} + // The whole of it again: the server takes no range. + 200 => body.clear(), + s => bail!("the server answered {s}"), + } + loop { + match resp.chunk().await { + Ok(Some(chunk)) => { + body.extend_from_slice(&chunk); + if body.len() > limit { + bail!("the answer is larger than {limit} bytes"); + } + } + Ok(None) => return Ok(body), + Err(e) => { + last = describe(&e); + break; + } + } + } + } + bail!("{last} (after eight tries)") +} + /// `body` posted as JSON to `url` with `headers`, and the JSON answer. pub async fn post_json(url: &str, body: &serde_json::Value, headers: &[(&str, &str)], route: &Route) -> Result { let mut req = client(route)?.post(url).json(body); diff --git a/windows/crates/ps-service/src/install.rs b/windows/crates/ps-service/src/install.rs index 749ad76..ca6e7fc 100644 --- a/windows/crates/ps-service/src/install.rs +++ b/windows/crates/ps-service/src/install.rs @@ -27,7 +27,7 @@ pub async fn archive(src: &Source, routes: &[fetch::Route], cache: &Path) -> Res } let mut last = None; for route in routes { - match fetch::get(&src.url, route, 512 * 1024 * 1024).await { + match fetch::get_large(&src.url, route, 512 * 1024 * 1024).await { Ok(data) => { if let Some(want) = &src.sha256 { let got = hex(&data);