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 <noreply@anthropic.com>
This commit is contained in:
1 parent
7cdd33f05e
commit
fe291fab2b
2 files changed
+59
-2
No files matched your search
@@ -16,6 +16,12 @@ pub enum Route {
|
||||
}
|
||||
|
||||
fn client(route: &Route) -> Result<reqwest::Client> {
|
||||
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<Duration>) -> Result<reqwest::Client> {
|
||||
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<reqwest::Client> {
|
||||
.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<Vec<u8>> {
|
||||
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<Vec<u8>> {
|
||||
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<u8> = 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<serde_json::Value> {
|
||||
let mut req = client(route)?.post(url).json(body);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user