aria2-core 0.3.13

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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
use std::sync::Arc;
use tracing::{debug, info, warn};

use crate::constants;
use crate::engine::active_output_registry::{OutputPathPolicy, global_registry};
use crate::engine::http::concurrent_download::{ConcurrentDownloadResult, ConcurrentDownloader};
use crate::engine::http::segment_downloader::HttpSegmentDownloader;
use crate::engine::http::sequential_download::SequentialDownloader;
use crate::engine::retry_policy::RetryPolicy;
use crate::engine::work_runner::run_single_work_item;
use crate::error::{Aria2Error, Result};
use crate::filesystem::file_allocation;
use crate::filesystem::file_allocation_man;
use crate::filesystem::resume_helper::ResumeHelper;
use crate::http::response_processor::determine_filename_from_response;
use crate::util::rwlock_ext::RwLockRecover;

use super::DownloadCommand;
use super::range_probe_work::HttpRangeProbeWork;

impl DownloadCommand {
    pub(super) async fn execute_attempt(&mut self, uri: &str) -> Result<()> {
        debug!(
            "Starting download: {} -> {}",
            uri,
            self.output_path.display()
        );

        // Re-check cancellation before the metadata probe and filesystem work
        // so a remove issued before execution is honoured immediately.
        self.check_cancelled()?;

        let options = self.group.recover().options_arc();
        self.publish_output_path();
        let known_total_length = self.group.recover().total_length();
        let needs_metadata_probe =
            options.uses_memory_download() && !options.uses_memory_download_for_uri(uri);
        let should_head = options.dry_run
            || (options.use_head && known_total_length == 0)
            || needs_metadata_probe;
        let head_resp = if should_head {
            self.send_head_with_redirects(uri).await
        } else {
            None
        };
        let mut prepared_get = if !should_head
            && !options.uses_memory_download()
            && !self.output_name_explicit
            && !self.output_path_resolved
        {
            self.send_get_with_redirects(uri).await
        } else {
            None
        };
        let response_for_metadata = prepared_get
            .as_ref()
            .or(head_resp.as_ref())
            .map(|prepared| &prepared.response);
        if let Some(response) = response_for_metadata {
            self.group
                .recover()
                .observe_http_version(response.version() == reqwest::Version::HTTP_2);
        }
        let mut effective_uri = prepared_get
            .as_ref()
            .or(head_resp.as_ref())
            .map(|prepared| prepared.effective_uri.clone())
            .unwrap_or_else(|| uri.to_owned());
        let mut origin_sensitive_headers_allowed = prepared_get
            .as_ref()
            .or(head_resp.as_ref())
            .map(|prepared| prepared.origin_auth_allowed)
            .unwrap_or(true);

        // An explicit output name is authoritative. For an inferred HTTP
        // name, the first successful response is the metadata seam at which
        // Content-Disposition can replace the URL-derived basename, before
        // collision resolution, resume inspection, or allocation.
        if !self.output_name_explicit
            && !self.output_path_resolved
            && let Some(response) = response_for_metadata
            && response.status().is_success()
        {
            let filename = determine_filename_from_response(
                response,
                options.content_disposition_default_utf8,
            );
            if let Some(parent) = self.output_path.parent() {
                self.output_path = parent.join(filename);
                self.publish_output_path();
            }
        }

        let head_content_type = response_for_metadata.and_then(|response| {
            response
                .headers()
                .get(reqwest::header::CONTENT_TYPE)
                .and_then(|value| value.to_str().ok())
        });
        if let Some(content_type) = head_content_type {
            self.group.recover().set_content_type(content_type);
            if !options.dry_run && options.uses_memory_download_for_content_type(content_type) {
                self.group.recover().mark_in_memory_download();
                return self.execute_in_memory(uri).await;
            }
        }

        // An error response's Content-Length is its error page's, not the
        // file's (a 429 page would otherwise size allocation and resume).
        let mut total_length = if let Some(resp) =
            response_for_metadata.filter(|response| response.status().is_success())
        {
            resp.headers()
                .get(reqwest::header::CONTENT_LENGTH)
                .and_then(|v| v.to_str().ok())
                .and_then(|v| v.parse::<u64>().ok())
                .unwrap_or(0)
        } else {
            0
        };
        if total_length == 0 {
            total_length = known_total_length;
        }

        // `dry_run` is a metadata-only HTTP operation. The HEAD above checks
        // availability and discovers the size, but no range probe, output
        // directory, collision resolution, resume inspection, allocation, or
        // GET request may follow it.
        if options.dry_run {
            self.completed_bytes = total_length;
            {
                let group = self.group.recover();
                group.set_total_length(total_length);
                group.update_progress(total_length);
                group.set_checksum_verified(true);
            }
            self.group.recover_mut().complete()?;
            self.completed = true;
            return Ok(());
        }

        if let Some(parent) = self.output_path.parent()
            && !parent.exists()
        {
            std::fs::create_dir_all(parent).map_err(|e| {
                Aria2Error::Fatal(crate::error::FatalError::Config(format!(
                    "Failed to create directory: {}",
                    e
                )))
            })?;
        }

        let mut supports_range = false;
        let mut probed_http_version = None;
        let mut range_auth_factory = prepared_get
            .as_ref()
            .or(head_resp.as_ref())
            .map(|prepared| Arc::clone(&prepared.auth_factory));
        let metadata_allows_probe = response_for_metadata
            .map(|response| response.status().is_success())
            .unwrap_or(known_total_length > 0);
        let should_probe_range = metadata_allows_probe
            && (total_length == 0 || total_length > constants::CONCURRENT_MIN_FILE_SIZE as u64)
            && (total_length > 0 || should_head || known_total_length > 0);
        if should_probe_range {
            let cookie_helper = self.create_cookie_helper();
            let cookie_header = reqwest::Url::parse(&effective_uri)
                .map(|url| cookie_helper.build_cookie_header_from_url(&url))
                .ok()
                .filter(|header| !header.is_empty());
            let target_scheme = reqwest::Url::parse(&effective_uri)
                .ok()
                .map(|url| url.scheme().to_owned())
                .unwrap_or_else(|| "http".to_owned());
            let (proxy_user, proxy_passwd) = options.proxy_credentials_for_scheme(&target_scheme);
            let auth_options = crate::http::AuthResolveOptions {
                http_auth_challenge: options.http_auth_challenge,
                no_netrc: options.no_netrc,
                http_user: options.http_user.clone(),
                http_passwd: options.http_passwd.clone(),
                ftp_user: options.ftp_user.clone(),
                ftp_passwd: options.ftp_passwd.clone(),
                proxy_user,
                proxy_passwd,
            };
            let prober =
                HttpSegmentDownloader::new_with_policy(&self.client, self.request_policy.clone())
                    .with_cookie_helper(cookie_helper);
            let prober = if let Some(auth_factory) = &range_auth_factory {
                prober.with_shared_auth_options(auth_options, Arc::clone(auth_factory))
            } else {
                prober.with_auth_options(auth_options, options.netrc_path.clone())
            }
            .with_origin_sensitive_headers_allowed(origin_sensitive_headers_allowed);
            let probe_retry_policy =
                RetryPolicy::new(options.max_retries, options.retry_wait.saturating_mul(1000));
            let probe = {
                let mut work = HttpRangeProbeWork {
                    command: self,
                    prober: &prober,
                    uri: &effective_uri,
                    cookie_header: cookie_header.as_deref(),
                    retry_policy: probe_retry_policy,
                    adaptive_options: options.http_adaptive,
                };
                run_single_work_item(&mut work).await?
            };
            range_auth_factory = Some(prober.shared_auth_factory());
            supports_range = probe.supports_range;
            origin_sensitive_headers_allowed &= probe.origin_sensitive_headers_allowed;
            if probe.total_length > 0 {
                total_length = probe.total_length;
            }
            effective_uri = probe.effective_url;
            if effective_uri != uri {
                self.group
                    .recover_mut()
                    .add_redirect_uri(effective_uri.as_str());
            }
            info!(
                supports_range,
                total_length,
                final_url = %effective_uri,
                http_version = ?probe.version,
                "HTTP range capability probe completed"
            );
            probed_http_version = probe.version;
            if let Some(version) = probe.version {
                self.group
                    .recover()
                    .observe_http_version(version == reqwest::Version::HTTP_2);
            }
        }

        let original_path = self.output_path.clone();
        if options.remove_control_file {
            let control_path =
                crate::filesystem::control_file::ControlFile::control_path_for(&original_path);
            match tokio::fs::remove_file(&control_path).await {
                Ok(()) => info!(path = %control_path.display(), "Removed requested control file"),
                Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
                Err(error) => {
                    return Err(Aria2Error::FileIo(format!(
                        "Failed to remove control file {}: {}",
                        control_path.display(),
                        error
                    )));
                }
            }
        }
        if !self.output_path_resolved {
            self.output_path = global_registry()
                .resolve_with_policy(
                    &original_path,
                    OutputPathPolicy {
                        allow_overwrite: options.allow_overwrite,
                        auto_file_renaming: options.auto_file_renaming,
                        continue_download: options.continue_download,
                        check_integrity: self.check_integrity,
                        total_length: (total_length > 0).then_some(total_length),
                    },
                )
                .await?;
            self.output_path_resolved = true;
        } else {
            // A mirror failover re-enters this attempt with the same command.
            // Reclaim the already-resolved path instead of applying the
            // filesystem collision policy a second time and renaming the
            // partial/preallocated output.
            self.output_path = global_registry().resolve(&self.output_path).await;
        }
        if self.output_path != original_path {
            info!(
                "Filename collision resolved: '{}' -> '{}'",
                original_path.display(),
                self.output_path.display()
            );
        }
        self.publish_output_path();

        let continue_download = options.continue_download;
        let resume_helper = ResumeHelper::new(&self.output_path, continue_download);
        let mut resume_state = match resume_helper.detect(total_length).await {
            Ok(state) => state,
            Err(error) => {
                self.release_output_path().await;
                return Err(error);
            }
        };

        // When resuming from a paused state, never short-circuit as "complete" --
        // the file on disk may be a preallocated sparse file that matches
        // total_length but hasn't actually been fully written. A download that
        // was explicitly paused by the user must always continue from where it
        // left off, relying on the control file's bitfield to determine which
        // ranges still need fetching.
        let was_paused = self.group.recover().is_paused_flag();

        if resume_state.is_complete
            && !was_paused
            && !self.check_integrity
            && !options.allow_overwrite
        {
            info!(
                "File already exists completely, skipping download: {} ({} bytes)",
                self.output_path.display(),
                resume_state.existing_length
            );
            self.completed_bytes = resume_state.existing_length;
            {
                let g = self.group.recover();
                g.set_total_length(self.completed_bytes);
                g.update_progress(self.completed_bytes);
                g.set_completed_length(self.completed_bytes);
            }
            {
                let mut g = self.group.recover_mut();
                g.complete()?;
            }
            self.completed = true;
            self.release_output_path().await;
            return Ok(());
        }

        // Final cancellation check before kicking off the (potentially long)
        // network transfer. If the task was removed during the HEAD probe or
        // resume detection, abort now rather than downloading data that will
        // just be discarded. This is placed before spawn_progress_aggregator
        // so a cancelled task does not spawn an unnecessary aggregator task.
        // Release the registered output path so future downloads can reuse
        // the filename.
        if let Err(e) = self.check_cancelled() {
            self.release_output_path().await;
            return Err(e);
        }

        // Initialize tail reclaim progress tracking before the download loop.
        // Mirrors C++ DownloadCommand constructor which initializes
        // lastTailReclaimSessionDownloadLength_ to 0.
        self.update_tail_reclaim_progress();

        let download_result: Result<()> = async {
            if self
                .verify_existing_integrity(total_length, &mut resume_state)
                .await?
            {
                return Ok(());
            }

            let requested_split = options.split.unwrap_or(constants::DEFAULT_SPLIT);
            let min_split_size = self.group.recover().effective_min_split_size();
            let split = crate::engine::http::concurrent_download::effective_segment_count(
                total_length,
                requested_split,
                min_split_size,
            );

            // File allocation can make a new output file look complete even
            // though no response bytes have been written. Persist an empty
            // sequential checkpoint before allocation so a pause during the
            // first request cannot later mistake the preallocated file for a
            // completed download.
            if total_length > 0
                && !resume_state.should_resume
                && !self.should_use_concurrent(total_length, supports_range, split)
            {
                let control_path = crate::filesystem::control_file::ControlFile::
                    control_path_for(&self.output_path);
                if let Err(error) = crate::filesystem::durability::remove_file(&control_path).await
                {
                    warn!(
                        path = %control_path.display(),
                        %error,
                        "Failed to remove stale HTTP control file before allocation"
                    );
                }
                match crate::filesystem::control_file::ControlFile::open_or_create(
                    &control_path,
                    total_length,
                    1,
                )
                .await
                {
                    Ok(mut control_file) => {
                        control_file.record_completed_prefix(0);
                        if let Err(error) = control_file.save().await {
                            warn!(
                                path = %control_path.display(),
                                %error,
                                "Failed to save initial HTTP control file before allocation"
                            );
                        }
                    }
                    Err(error) => warn!(
                        path = %control_path.display(),
                        %error,
                        "Failed to create initial HTTP control file before allocation"
                    ),
                }
            }

            if total_length > 0 {
                // Queue the allocation through the shared FileAllocationMan
                // (mirrors C++ FileAllocationMan + FileAllocationCommand):
                // the background worker drives chunked allocation sequentially
                // across downloads and yields between chunks, so a huge
                // zero-fill never blocks a worker thread or starves other
                // downloads. This task resumes once the file is ready.
                let strategy = file_allocation::AllocationStrategy::from_str(&self.file_allocation);
                if strategy != file_allocation::AllocationStrategy::None {
                    let gid = self.group.recover().gid().value();
                    file_allocation_man::enqueue_path(
                        &file_allocation_man::shared(),
                        &self.output_path,
                        total_length,
                        strategy,
                        self.secure_falloc,
                        gid,
                    )
                    .await?;
                }
            }

            let cookie_helper = self.create_cookie_helper();
            let progress_updater = self.create_progress_updater();

            if self.should_use_concurrent(total_length, supports_range, split)
                && !options.http_accept_gzip
            {
                // A full prepared response cannot satisfy segmented range
                // requests. Drop it before entering the concurrent adapter.
                let _ = prepared_get.take();
                if resume_state.should_resume {
                    info!(
                        "Concurrent mode + resume: existing {} bytes, continuing from offset {}",
                        resume_state.existing_length, resume_state.start_offset
                    );
                }
                let max_retries = options.max_retries;
                let target_scheme = reqwest::Url::parse(&effective_uri)
                    .ok()
                    .map(|url| url.scheme().to_owned())
                    .unwrap_or_else(|| "http".to_string());
                let (proxy_user, proxy_passwd) = options.proxy_credentials_for_scheme(
                    &target_scheme,
                );
                let auth_options = crate::http::AuthResolveOptions {
                    http_auth_challenge: options.http_auth_challenge,
                    no_netrc: options.no_netrc,
                    http_user: options.http_user.clone(),
                    http_passwd: options.http_passwd.clone(),
                    ftp_user: options.ftp_user.clone(),
                    ftp_passwd: options.ftp_passwd.clone(),
                    proxy_user,
                    proxy_passwd,
                };
                let fallback_auth_factory = range_auth_factory.clone();
                let fallback_auth_options = auth_options.clone();
                let progress_arc = Arc::clone(&self.progress);
                let mut concurrent_downloader = ConcurrentDownloader::new(
                    Arc::clone(&self.client),
                    self.output_path.clone(),
                    self.request_policy.clone(),
                    auth_options,
                    options.netrc_path.clone(),
                    cookie_helper.clone(),
                    progress_updater.clone(),
                    Arc::clone(&self.group),
                    progress_arc,
                    self.mmap_threshold,
                    self.file_allocation.clone(),
                    self.global_limiter.clone(),
                )
                .with_range_clients(Arc::clone(&self.range_clients))
                .with_origin_sensitive_headers_allowed(origin_sensitive_headers_allowed)
                .with_initial_http_version(&effective_uri, probed_http_version);
                if let Some(auth_factory) = range_auth_factory.as_ref() {
                    concurrent_downloader =
                        concurrent_downloader.with_shared_auth_factory(Arc::clone(auth_factory));
                }
                match concurrent_downloader.execute_with_retry(
                    uri,
                    &effective_uri,
                    total_length,
                    &resume_state,
                    max_retries,
                ).await {
                    Ok(ConcurrentDownloadResult::Complete) => return Ok(()),
                    Ok(ConcurrentDownloadResult::Fallback { completed_ranges }) => {
                        warn!(
                            "Concurrent download falling back to sequential mode, preserving {} completed ranges",
                            completed_ranges.len()
                        );
                        let retry_policy = RetryPolicy::new(options.max_retries, options.retry_wait * 1000);
                        let mut sequential_downloader = SequentialDownloader::new(
                            Arc::clone(&self.client),
                            self.output_path.clone(),
                            self.request_policy.clone(),
                            cookie_helper,
                            progress_updater,
                            Arc::clone(&self.group),
                            Arc::clone(&self.progress),
                            self.global_limiter.clone(),
                        )
                        .with_outbound_network_policy(Arc::clone(
                            &self.outbound_network_policy,
                        ))
                        .with_origin_sensitive_headers_allowed(origin_sensitive_headers_allowed);
                        if let Some(auth_factory) = fallback_auth_factory {
                            sequential_downloader = sequential_downloader.with_shared_auth_options(
                                auth_factory,
                                fallback_auth_options,
                            );
                        }
                        let result = sequential_downloader.execute_with_gaps_with_retry(
                            uri,
                            total_length,
                            &completed_ranges,
                            &retry_policy,
                        ).await;
                        drop(sequential_downloader);
                        return result;
                    }
                    Err(e) => return Err(e),
                }
            }

            let retry_policy = RetryPolicy::new(options.max_retries, options.retry_wait * 1000);
            let mut sequential_downloader = SequentialDownloader::new(
                Arc::clone(&self.client),
                self.output_path.clone(),
                self.request_policy.clone(),
                cookie_helper,
                progress_updater,
                Arc::clone(&self.group),
                Arc::clone(&self.progress),
                self.global_limiter.clone(),
            )
            .with_outbound_network_policy(Arc::clone(&self.outbound_network_policy))
            .with_origin_sensitive_headers_allowed(origin_sensitive_headers_allowed);
            if let Some(prepared) = prepared_get.take() {
                sequential_downloader = sequential_downloader.with_prepared_response(prepared);
            }
            let result = sequential_downloader.execute_with_retry(
                uri,
                &resume_state,
                total_length,
                &retry_policy,
            ).await;
            drop(sequential_downloader);
            result
        }
        .await;

        self.finalize_attempt(download_result).await
    }

    fn publish_output_path(&self) {
        self.group
            .recover()
            .set_resolved_output_path(self.output_path.to_string_lossy());
    }
}