aria2-core 0.3.14

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use bytes::BytesMut;
use futures::StreamExt;
use tokio::sync::mpsc;
use tracing::debug;

use aria2_protocol::http::parse_unsatisfied_content_range_length;

use crate::constants;
use crate::engine::command::ProgressUpdate;
use crate::error::{Aria2Error, RecoverableError, Result};
use crate::http::auth_challenge_handler::{self, AuthChallengeResult};
use crate::http::skip_response::MAX_REDIRECT_COUNT;
use crate::http::{AuthScheme, HttpAuthChallenge};

use super::{
    HttpSegmentDownloader, RangeProbeResult, classify_range_status, validate_content_range,
};

fn response_with_origin_scope(
    response: reqwest::Response,
    url: reqwest::Url,
    scope: &crate::http::request_policy::RedirectOriginScope,
) -> (reqwest::Response, reqwest::Url, bool) {
    let allow_sensitive_headers = scope.allows_sensitive_headers(&url);
    (response, url, allow_sensitive_headers)
}

impl HttpSegmentDownloader {
    /// Send a Range request through the same manual redirect seam as the
    /// sequential downloader. reqwest's client is configured with automatic
    /// redirects disabled so the final URI remains explicit and bounded.
    pub(super) async fn send_range_request(
        &self,
        url: &str,
        range_header: &str,
        cookie_header: Option<&str>,
        headers: &[(String, String)],
    ) -> Result<(reqwest::Response, reqwest::Url, bool)> {
        let mut current_url = reqwest::Url::parse(url).map_err(|error| {
            Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
                message: format!("invalid HTTP Range URL {url}: {error}"),
            })
        })?;
        let mut origin_scope =
            crate::http::request_policy::RedirectOriginScope::new(current_url.clone())
                .with_sensitive_headers_allowed(self.origin_sensitive_headers_allowed);
        let mut redirect_count = 0u32;

        loop {
            self.request_policy.wait_before_request().await;
            let dynamic_cookie_header = self
                .cookie_helper
                .as_ref()
                .map(|helper| helper.build_cookie_header_from_url(&current_url));
            let request_cookie_header = dynamic_cookie_header
                .as_deref()
                .filter(|value| !value.is_empty())
                .or_else(|| (redirect_count == 0).then_some(cookie_header).flatten());
            let allow_origin_auth = origin_scope.allows_sensitive_headers(&current_url);
            let authorization = allow_origin_auth
                .then(|| {
                    self.auth_options.as_ref().and_then(|auth_options| {
                        self.auth_factory
                            .lock()
                            .unwrap_or_else(std::sync::PoisonError::into_inner)
                            .resolve_basic_authorization(&current_url, auth_options)
                    })
                })
                .flatten();
            let request = self.request_policy.apply_with_basic_auth_for_origin(
                self.client
                    .get(current_url.as_str())
                    .header("Range", range_header),
                request_cookie_header,
                headers,
                authorization.as_deref(),
                allow_origin_auth,
            );
            let mut response = request.send().await.map_err(|error| {
                Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
                    message: format!("HTTP Range request failed: {error}"),
                })
            })?;

            let mut status_code = response.status().as_u16();
            let authentication_used = authorization.is_some()
                || (allow_origin_auth && self.request_policy.has_header("Authorization"))
                || (allow_origin_auth
                    && headers
                        .iter()
                        .any(|(name, _)| name.eq_ignore_ascii_case("Authorization")));
            if status_code == 401 || status_code == 407 {
                if status_code == 401 && !allow_origin_auth {
                    return Ok(response_with_origin_scope(
                        response,
                        current_url,
                        &origin_scope,
                    ));
                }
                let Some(auth_options) = &self.auth_options else {
                    return Ok(response_with_origin_scope(
                        response,
                        current_url,
                        &origin_scope,
                    ));
                };
                let is_proxy = status_code == 407;
                if authentication_used
                    || (!is_proxy && !auth_options.http_auth_challenge)
                    || (is_proxy && auth_options.proxy_user.is_none())
                {
                    return Ok(response_with_origin_scope(
                        response,
                        current_url,
                        &origin_scope,
                    ));
                }

                let header_name = if is_proxy {
                    reqwest::header::PROXY_AUTHENTICATE
                } else {
                    reqwest::header::WWW_AUTHENTICATE
                };
                let auth_header = response
                    .headers()
                    .get(header_name)
                    .and_then(|value| value.to_str().ok())
                    .map(str::to_string);
                let scheme = auth_header
                    .as_deref()
                    .and_then(AuthScheme::from_header)
                    .or_else(|| (!is_proxy).then_some(AuthScheme::Basic));
                let Some(scheme) = scheme else {
                    return Ok(response_with_origin_scope(
                        response,
                        current_url,
                        &origin_scope,
                    ));
                };
                let challenge = HttpAuthChallenge {
                    scheme: scheme.clone(),
                    realm: auth_header
                        .as_deref()
                        .map(crate::http::skip_response::HttpSkipResponseHandler::extract_realm)
                        .unwrap_or_default(),
                    is_proxy,
                    digest_challenge: if scheme == AuthScheme::Digest {
                        auth_header.as_deref().and_then(|header| {
                            crate::http::digest_auth::DigestAuthChallenge::parse(header).ok()
                        })
                    } else {
                        None
                    },
                };
                let result = {
                    let mut auth_factory = self
                        .auth_factory
                        .lock()
                        .unwrap_or_else(std::sync::PoisonError::into_inner);
                    auth_challenge_handler::handle_auth_challenge(
                        &challenge,
                        &mut auth_factory,
                        &current_url,
                        auth_options,
                        crate::http::request::HttpMethod::Get,
                        authentication_used,
                        1,
                    )
                };
                let AuthChallengeResult::RetryWithAuth {
                    authorization_header,
                    is_proxy,
                } = result
                else {
                    return Ok(response_with_origin_scope(
                        response,
                        current_url,
                        &origin_scope,
                    ));
                };

                let header_name = if is_proxy {
                    "Proxy-Authorization"
                } else {
                    "Authorization"
                };
                let dynamic_cookie_header = self
                    .cookie_helper
                    .as_ref()
                    .map(|helper| helper.build_cookie_header_from_url(&current_url));
                let retry_cookie_header = dynamic_cookie_header
                    .as_deref()
                    .filter(|value| !value.is_empty())
                    .or_else(|| cookie_header.filter(|_| redirect_count == 0));
                let retry_request = if is_proxy {
                    self.request_policy.apply_for_origin_with_proxy_auth(
                        self.client
                            .get(current_url.as_str())
                            .header("Range", range_header),
                        retry_cookie_header,
                        &[],
                        allow_origin_auth,
                        Some(&authorization_header),
                    )
                } else {
                    self.request_policy.apply_for_origin(
                        self.client
                            .get(current_url.as_str())
                            .header("Range", range_header),
                        retry_cookie_header,
                        &[(header_name.to_string(), authorization_header)],
                        allow_origin_auth,
                    )
                };
                self.request_policy.wait_before_request().await;
                let retry_response = retry_request.send().await.map_err(|error| {
                    Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
                        message: format!("HTTP Range auth retry failed: {error}"),
                    })
                })?;
                if let Some(helper) = &self.cookie_helper {
                    helper.extract_and_store_cookies(current_url.as_str(), &retry_response);
                }
                status_code = retry_response.status().as_u16();
                response = retry_response;
            }
            if !matches!(status_code, 300..=303 | 307 | 308) {
                if let Some(helper) = &self.cookie_helper {
                    helper.extract_and_store_cookies(current_url.as_str(), &response);
                }
                return Ok(response_with_origin_scope(
                    response,
                    current_url,
                    &origin_scope,
                ));
            }

            if let Some(helper) = &self.cookie_helper {
                helper.extract_and_store_cookies(current_url.as_str(), &response);
            }

            if redirect_count >= MAX_REDIRECT_COUNT {
                return Err(Aria2Error::Recoverable(
                    RecoverableError::HttpTooManyRedirects {
                        count: redirect_count,
                    },
                ));
            }

            let location = response
                .headers()
                .get(reqwest::header::LOCATION)
                .and_then(|value| value.to_str().ok())
                .ok_or_else(|| {
                    Aria2Error::Recoverable(RecoverableError::HttpProtocolError {
                        message: format!("HTTP {status_code} redirect without Location header"),
                    })
                })?;
            let target_url = current_url.join(location).map_err(|error| {
                Aria2Error::Recoverable(RecoverableError::HttpProtocolError {
                    message: format!("failed to resolve redirect URL '{location}': {error}"),
                })
            })?;
            origin_scope.follow_redirect(&current_url, &target_url);
            current_url = target_url;
            redirect_count += 1;
        }
    }

    /// Probe byte-range support with the same redirect, cookie, and auth path
    /// used by actual range downloads. `Accept-Ranges` from HEAD is only a
    /// hint; a valid 206 and exact one-byte Content-Range is the evidence that
    /// this resource can be split.
    pub(crate) async fn probe_range_metadata(
        &self,
        url: &str,
        cookie_header: Option<&str>,
    ) -> Result<RangeProbeResult> {
        let (response, effective_url, origin_sensitive_headers_allowed) = self
            .send_range_request(url, "bytes=0-0", cookie_header, &[])
            .await?;

        let version = response.version();
        let status = response.status();
        if !matches!(status.as_u16(), 200 | 206 | 416)
            && let Some(error) = classify_range_status(status, "bytes=0-0")
        {
            return Err(error);
        }
        let content_length = response.content_length().unwrap_or(0);
        let content_range = response
            .headers()
            .get(reqwest::header::CONTENT_RANGE)
            .and_then(|value| value.to_str().ok());
        let parsed_range = content_range
            .and_then(crate::http::response_processor::range::parse_content_range_value);
        let supports_range = status.as_u16() == 206
            && parsed_range.is_some_and(|(start, end, total)| start == 0 && end == 0 && total > 0);
        let total_length = if supports_range {
            parsed_range.map(|(_, _, total)| total).unwrap_or(0)
        } else if status.as_u16() == 200 && content_length > 0 {
            content_length
        } else if status.as_u16() == 416 {
            content_range
                .and_then(parse_unsatisfied_content_range_length)
                .unwrap_or(0)
        } else {
            0
        };

        Ok(RangeProbeResult {
            supports_range,
            effective_url: effective_url.to_string(),
            origin_sensitive_headers_allowed,
            total_length,
            version: Some(version),
        })
    }

    #[allow(clippy::too_many_arguments)]
    pub async fn download_range(
        &self,
        url: &str,
        offset: u64,
        length: u64,
        cookie_header: Option<&str>,
        headers: &[(String, String)],
        progress_tx: Option<&mpsc::Sender<ProgressUpdate>>,
        expected_entity_length: u64,
    ) -> Result<bytes::Bytes> {
        if length == 0 {
            return Ok(bytes::Bytes::new());
        }

        let range_header = format!("bytes={}-{}", offset, offset + length.saturating_sub(1));
        debug!("HTTP Range request: {} ({})", range_header, url);

        let (response, effective_url, _) = self
            .send_range_request(url, &range_header, cookie_header, headers)
            .await?;

        self.remember_peer(response.remote_addr());
        let status = response.status();
        if let Some(error) = classify_range_status(status, &range_header) {
            return Err(error);
        }
        if status.as_u16() == 206 {
            validate_content_range(&response, offset, length, expected_entity_length)?;
        }

        // Don't pre-allocate the full segment length — it can be very large
        // (16 MB+) and wastes memory if the download fails early.  Start with
        // a reasonable chunk size and let BytesMut grow organically.
        let initial_cap = (length as usize).min(256 * 1024);
        let mut data = BytesMut::with_capacity(initial_cap);
        let mut stream = response.bytes_stream();
        let mut last_reported_progress = 0u64;

        while let Some(chunk_result) = stream.next().await {
            match chunk_result {
                Ok(bytes) => {
                    if data.len() as u64 + bytes.len() as u64 > length {
                        return Err(Aria2Error::Recoverable(
                            RecoverableError::TemporaryNetworkFailure {
                                message: format!(
                                    "Response exceeded requested range length: expected {}, received more",
                                    length
                                ),
                            },
                        ));
                    }
                    data.extend_from_slice(&bytes);
                    // Report per-chunk progress if a progress channel is provided
                    let downloaded = data.len() as u64;
                    if let Some(tx) = progress_tx
                        && downloaded - last_reported_progress
                            >= constants::PROGRESS_UPDATE_BYTES as u64
                    {
                        let update = ProgressUpdate {
                            completed_bytes: offset + downloaded,
                            download_speed: 0,
                            upload_speed: 0,
                        };
                        // Progress is advisory; avoid stalling the data path
                        // when the bounded snapshot queue is temporarily full.
                        let _ = tx.try_send(update);
                        last_reported_progress = downloaded;
                    }
                }
                Err(e) => {
                    return Err(Aria2Error::Recoverable(
                        RecoverableError::TemporaryNetworkFailure {
                            message: format!("Stream read error: {}", e),
                        },
                    ));
                }
            }
        }

        if data.len() as u64 != length {
            return Err(Aria2Error::Recoverable(
                RecoverableError::TemporaryNetworkFailure {
                    message: format!(
                        "Incomplete response for range {}-{} from {}: expected {} bytes, received {}",
                        offset,
                        offset + length.saturating_sub(1),
                        effective_url,
                        length,
                        data.len()
                    ),
                },
            ));
        }

        // Freeze BytesMut to immutable Bytes (zero-cost conversion)
        Ok(data.freeze())
    }
}