aria2-core 0.3.14

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
Documentation
use super::{DownloadOptions, FollowMode};
use url::Url;

impl DownloadOptions {
    /// Effective fixed per-session HTTP/2 Range stream limit.
    pub fn http2_streams_per_session(&self) -> usize {
        self.max_http2_streams_per_session
            .unwrap_or(crate::constants::DEFAULT_HTTP2_STREAMS_PER_SESSION as u16)
            .clamp(1, 256) as usize
    }

    /// Effective lower bound for adaptive HTTP Range requests.
    pub fn min_http_range_size_bytes(&self) -> u64 {
        self.min_http_range_size
            .unwrap_or(crate::constants::DEFAULT_HTTP_RANGE_SIZE_FLOOR_BYTES)
            .clamp(
                crate::constants::HTTP_RANGE_SIZE_FLOOR_MIN_BYTES,
                crate::constants::HTTP_RANGE_SIZE_FLOOR_MAX_BYTES,
            )
    }

    /// Whether a memory follow mode is configured.
    ///
    /// This is an option-level predicate only. Callers that are deciding how
    /// to download a source must also check its URI or response content type
    /// with [`Self::uses_memory_download_for_uri`] or
    /// [`Self::uses_memory_download_for_content_type`].
    pub fn uses_memory_download(&self) -> bool {
        self.follow_torrent.is_some_and(FollowMode::is_memory)
            || self.follow_metalink.is_some_and(FollowMode::is_memory)
    }

    /// Whether a source URI should use memory-backed metadata handling.
    ///
    /// The `follow-*=mem` options apply to recognized metadata sources, not
    /// to every HTTP/FTP/SFTP payload in the request group. URI matching
    /// mirrors aria2's suffix criteria and ignores URL query/fragment parts.
    pub fn uses_memory_download_for_uri(&self, uri: &str) -> bool {
        let path = Url::parse(uri)
            .map(|url| url.path().to_owned())
            .unwrap_or_else(|_| uri.to_owned());
        let path = path.to_ascii_lowercase();

        (self.follow_torrent.is_some_and(FollowMode::is_memory) && path.ends_with(".torrent"))
            || (self.follow_metalink.is_some_and(FollowMode::is_memory)
                && [".meta4", ".metalink", ".metalink3"]
                    .iter()
                    .any(|extension| path.ends_with(extension)))
    }

    /// Whether a response content type identifies a memory-backed metadata
    /// source for the configured follow mode.
    pub fn uses_memory_download_for_content_type(&self, content_type: &str) -> bool {
        let content_type = content_type
            .split(';')
            .next()
            .unwrap_or_default()
            .trim()
            .to_ascii_lowercase();

        (self.follow_torrent.is_some_and(FollowMode::is_memory)
            && content_type == "application/x-bittorrent")
            || (self.follow_metalink.is_some_and(FollowMode::is_memory)
                && matches!(
                    content_type.as_str(),
                    "application/metalink4+xml"
                        | "application/metalink+xml"
                        | "application/x-metalink"
                ))
    }

    /// Return the configured write-back capacity, or `None` when disabled.
    pub fn disk_cache_size_bytes(&self) -> Option<u64> {
        self.disk_cache.filter(|size| *size > 0)
    }

    /// Parse the raw `header` list into `(name, value)` pairs, splitting each
    /// `"Name: Value"` entry on the first `:`. When `user_agent` or `referer`
    /// are set, they are appended as `User-Agent` / `Referer` pairs (unless an
    /// entry with the same name already exists), so callers only need to handle
    /// a single header list.
    pub fn parsed_headers(&self) -> Vec<(String, String)> {
        let mut result: Vec<(String, String)> = Vec::new();
        for raw in &self.header {
            if let Some((name, value)) = raw.split_once(':') {
                let name = name.trim().to_string();
                let value = value.trim().to_string();
                if !name.is_empty() {
                    result.push((name, value));
                }
            }
        }
        // Overlay user_agent / referer if not already present (case-insensitive).
        if let Some(ref ua) = self.user_agent
            && !has_header(&result, "User-Agent")
        {
            result.push(("User-Agent".to_string(), ua.clone()));
        }
        if let Some(ref ref_) = self.referer
            && !has_header(&result, "Referer")
        {
            result.push(("Referer".to_string(), ref_.clone()));
        }
        result
    }

    /// Return the effective User-Agent for HTTP protocol clients that do not
    /// use [`crate::http::HttpRequestPolicy`]. An explicit `header` entry takes precedence
    /// over the dedicated `user-agent` option, matching [`Self::parsed_headers`].
    pub fn http_user_agent(&self) -> Option<&str> {
        self.header
            .iter()
            .filter_map(|raw| raw.split_once(':'))
            .find_map(|(name, value)| {
                name.trim()
                    .eq_ignore_ascii_case("User-Agent")
                    .then(|| value.trim())
            })
            .or(self.user_agent.as_deref())
    }

    /// Build the internal HTTP request policy shared by every HTTP request
    /// path. The option names and defaults remain exposed through the aria2
    /// compatible configuration/RPC surfaces.
    pub fn http_request_policy(&self) -> crate::http::HttpRequestPolicy {
        crate::http::HttpRequestPolicy::new(
            self.parsed_headers(),
            self.http_accept_gzip,
            self.http_no_cache,
            !self.no_want_digest_header,
            self.enable_http_keep_alive,
            self.enable_http_pipelining,
        )
        .with_browser_context(crate::http::global_browser_context())
    }

    /// Resolve credentials from the proxy selected for this protocol.
    ///
    /// Credentials stay scoped to their proxy URL: protocol-specific options
    /// apply only when that protocol's proxy URL is selected; otherwise the
    /// `all-proxy-*` options apply to the selected fallback proxy.
    pub(crate) fn proxy_credentials_for_scheme(
        &self,
        scheme: &str,
    ) -> (Option<String>, Option<String>) {
        self.http_proxy_options().credentials_for_scheme(scheme)
    }

    pub(crate) fn http_proxy_options(&self) -> super::HttpProxyOptions {
        super::HttpProxyOptions::from_download_options(self)
    }
}

/// Case-insensitive check whether a `(name, value)` header list already contains
/// an entry with the given name.
fn has_header(headers: &[(String, String)], name: &str) -> bool {
    headers.iter().any(|(n, _)| n.eq_ignore_ascii_case(name))
}