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()
);
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);
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;
}
}
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;
}
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 {
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);
}
};
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(());
}
if let Err(e) = self.check_cancelled() {
self.release_output_path().await;
return Err(e);
}
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,
);
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 {
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
{
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_dns_resolver(self.dns_resolver.clone())
.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_dns_resolver(self.dns_resolver.clone())
.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());
}
}