aria2-core 0.3.14

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
Documentation
mod execution;
#[cfg(test)]
mod tests;
mod transport;
mod types;

pub use types::{select_mirrors_by_priority, try_mirrors_with_failover};

use crate::error::Result;
use crate::rate_limiter::RateLimiter;
use crate::request::request_group::{DownloadOptions, RequestGroup};
use crate::util::rwlock_ext::RwLockRecover;
use std::sync::Arc;

use types::FileDownloadInfo;

/// Information about a single file download created from a multi-file Metalink.
///
/// Returned by [`MetalinkDownloadCommand::create_multi_file`] so the caller
/// can track each per-file command independently.
pub struct MetalinkFileInfo {
    /// The download command for this file.
    pub command: MetalinkDownloadCommand,
    /// The original file index in the Metalink document (0-based).
    pub file_index: usize,
}

pub struct MetalinkDownloadCommand {
    pub(crate) group: Arc<std::sync::RwLock<RequestGroup>>,
    pub(crate) output_path: std::path::PathBuf,
    pub(crate) started: bool,
    pub(crate) completed: bool,
    pub(crate) completed_bytes: u64,
    pub(crate) progress_offset: u64,
    /// Parsed file info for single-file and per-file execution.
    /// Grouped commands keep one entry per file in `grouped_file_infos`.
    pub(crate) file_info: Option<FileDownloadInfo>,
    /// Parsed files for a grouped Metalink payload. A single command owns
    /// the group so the request context can schedule all selected files.
    pub(crate) grouped_file_infos: Vec<(std::path::PathBuf, FileDownloadInfo)>,
    pub(crate) protocol_runtime: Option<transport::ProtocolRuntime>,
    /// Process-wide rate limiter from `DownloadEngine::global_limiter`.
    /// When `Some`, passed down to `ThrottledWriter` for mirror downloads.
    pub(crate) global_limiter: Option<RateLimiter>,
    pub(crate) outbound_network_policy: Arc<crate::network::OutboundNetworkPolicy>,
    #[cfg(feature = "bittorrent")]
    pub(crate) public_tracker_catalog:
        Option<Arc<aria2_protocol::bittorrent::tracker::public_list::PublicTrackerList>>,
    #[cfg(feature = "bittorrent")]
    pub(crate) bt_registry:
        Option<Arc<std::sync::RwLock<crate::engine::bittorrent::registry::BtRegistry>>>,
    #[cfg(feature = "bittorrent")]
    pub(crate) bt_listener:
        Option<Arc<crate::engine::bittorrent::peer::listener::BtPeerListenerManager>>,
    #[cfg(feature = "bittorrent")]
    pub(crate) lpd_manager: Option<Arc<crate::engine::bittorrent::discovery::lpd::LpdManager>>,
    #[cfg(feature = "bittorrent")]
    pub(crate) open_file_budget: Option<Arc<crate::filesystem::open_file_budget::OpenFileBudget>>,
}

mod constructors;

impl MetalinkDownloadCommand {
    /// Get the output path for this download.
    pub fn output_path(&self) -> &std::path::Path {
        &self.output_path
    }

    /// Set the process-wide rate limiter (from `DownloadEngine::global_limiter`).
    ///
    /// When set, mirror downloads acquire tokens from this limiter in addition
    /// to the per-download limiter.
    pub fn set_global_limiter(&mut self, limiter: RateLimiter) {
        self.global_limiter = Some(limiter);
    }

    pub(crate) fn set_protocol_runtime(
        &mut self,
        adapters: crate::engine::protocol_adapter::ProtocolAdapterRegistry,
        services: crate::engine::protocol_adapter::ProtocolServices,
    ) {
        self.protocol_runtime = Some(transport::ProtocolRuntime {
            adapters: Arc::new(adapters),
            services,
        });
    }

    #[cfg(feature = "bittorrent")]
    pub fn set_public_tracker_catalog(
        &mut self,
        catalog: Arc<aria2_protocol::bittorrent::tracker::public_list::PublicTrackerList>,
    ) {
        self.public_tracker_catalog = Some(catalog);
    }

    #[cfg(feature = "bittorrent")]
    pub fn set_bt_registry(
        &mut self,
        registry: Arc<std::sync::RwLock<crate::engine::bittorrent::registry::BtRegistry>>,
    ) {
        self.bt_registry = Some(registry);
    }

    #[cfg(feature = "bittorrent")]
    pub fn set_bt_listener(
        &mut self,
        listener: Arc<crate::engine::bittorrent::peer::listener::BtPeerListenerManager>,
    ) {
        self.bt_listener = Some(listener);
    }

    #[cfg(feature = "bittorrent")]
    pub fn set_lpd_manager(
        &mut self,
        manager: Arc<crate::engine::bittorrent::discovery::lpd::LpdManager>,
    ) {
        self.lpd_manager = Some(manager);
    }

    #[cfg(feature = "bittorrent")]
    pub(crate) fn set_open_file_budget(
        &mut self,
        budget: Arc<crate::filesystem::open_file_budget::OpenFileBudget>,
    ) {
        self.open_file_budget = Some(budget);
    }

    pub fn group(&self) -> std::sync::RwLockReadGuard<'_, RequestGroup> {
        self.group.recover()
    }

    /// Consume this command and return the inner `RequestGroup` Arc.
    ///
    /// Used by post-download handlers that need to extract the group
    /// for insertion into the reserved queue without cloning.
    pub fn into_group(self) -> Arc<std::sync::RwLock<RequestGroup>> {
        self.group
    }
}

pub(crate) fn validate_http_client_options(options: &DownloadOptions) -> Result<()> {
    let tls = crate::http::client_identity::ClientTlsConfig::from_download_options(options);
    let _ = crate::http::client_identity::apply(reqwest::Client::builder(), &tls)?;
    Ok(())
}