aria2-core 0.3.14

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
Documentation
use crate::engine::http::cookie_helper::CookieHelper;
use crate::engine::http::sequential_download::PreparedHttpResponse;
use crate::http::digest_auth::DigestAuthChallenge;
use crate::http::request::HttpMethod;
use crate::http::{
    AuthChallengeResult, AuthConfigFactory, AuthResolveOptions, AuthScheme, HttpAuthChallenge,
    HttpSkipResponseHandler,
};
use crate::util::rwlock_ext::RwLockRecover;

use super::DownloadCommand;

struct GetAuthRetryContext<'a> {
    current_url: &'a reqwest::Url,
    cookie_helper: &'a CookieHelper,
    auth_factory: &'a mut AuthConfigFactory,
    auth_options: &'a AuthResolveOptions,
    authentication_used: bool,
    allow_origin_auth: bool,
}

fn prepared_response(
    response: reqwest::Response,
    effective_uri: String,
    origin_auth_allowed: bool,
    auth_factory: AuthConfigFactory,
) -> PreparedHttpResponse {
    PreparedHttpResponse {
        response,
        effective_uri,
        origin_auth_allowed,
        auth_factory: std::sync::Arc::new(std::sync::Mutex::new(auth_factory)),
    }
}

impl DownloadCommand {
    async fn retry_get_after_auth_challenge(
        &self,
        response: reqwest::Response,
        context: GetAuthRetryContext<'_>,
    ) -> reqwest::Response {
        let GetAuthRetryContext {
            current_url,
            cookie_helper,
            auth_factory,
            auth_options,
            authentication_used,
            allow_origin_auth,
        } = context;
        let status_code = response.status().as_u16();
        if status_code != 401 && status_code != 407 {
            return response;
        }

        let is_proxy = status_code == 407;
        if !is_proxy && !allow_origin_auth {
            return response;
        }
        let header_name = if is_proxy {
            "proxy-authenticate"
        } else {
            "www-authenticate"
        };
        let auth_header = response
            .headers()
            .get(header_name)
            .and_then(|value| value.to_str().ok())
            .map(str::to_owned);
        let scheme = auth_header
            .as_deref()
            .and_then(AuthScheme::from_header)
            .or((!authentication_used).then_some(AuthScheme::Basic));
        let Some(scheme) = scheme else {
            return response;
        };

        let challenge = HttpAuthChallenge {
            scheme: scheme.clone(),
            realm: auth_header
                .as_deref()
                .map(HttpSkipResponseHandler::extract_realm)
                .unwrap_or_default(),
            is_proxy,
            digest_challenge: (scheme == AuthScheme::Digest)
                .then(|| {
                    auth_header
                        .as_deref()
                        .and_then(|header| DigestAuthChallenge::parse(header).ok())
                })
                .flatten(),
        };

        let AuthChallengeResult::RetryWithAuth {
            authorization_header,
            is_proxy,
        } = crate::http::handle_auth_challenge(
            &challenge,
            auth_factory,
            current_url,
            auth_options,
            HttpMethod::Get,
            authentication_used,
            1,
        )
        else {
            return response;
        };

        let header_name = if is_proxy {
            "Proxy-Authorization"
        } else {
            "Authorization"
        };
        let cookie_header = cookie_helper.build_cookie_header_from_url(current_url);
        let request = if is_proxy {
            self.request_policy.apply_for_origin_with_proxy_auth(
                self.client.get(current_url.as_str()),
                (!cookie_header.is_empty()).then_some(cookie_header.as_str()),
                &[],
                allow_origin_auth,
                Some(&authorization_header),
            )
        } else {
            self.request_policy.apply_for_origin(
                self.client.get(current_url.as_str()),
                (!cookie_header.is_empty()).then_some(cookie_header.as_str()),
                &[(header_name.to_string(), authorization_header)],
                allow_origin_auth,
            )
        };
        let Ok(retry_response) = request.send().await else {
            return response;
        };
        cookie_helper.extract_and_store_cookies(current_url.as_str(), &retry_response);

        // Let the normal response loop handle redirects and HTTP errors. A
        // second auth challenge remains owned by the established downloader.
        if retry_response.status().as_u16() == 401 || retry_response.status().as_u16() == 407 {
            response
        } else {
            retry_response
        }
    }

    pub(super) async fn send_head_with_redirects(&self, uri: &str) -> Option<PreparedHttpResponse> {
        let mut current_url = reqwest::Url::parse(uri).ok()?;
        let mut origin_scope =
            crate::http::request_policy::RedirectOriginScope::new(current_url.clone());
        let cookie_helper = self.create_cookie_helper();
        let initial_scheme = current_url.scheme().to_owned();
        let options = self.group.recover().options_arc();
        let auth_context = crate::engine::http::auth::from_options(&options, &initial_scheme);
        let (mut auth_factory, auth_options) = (auth_context.factory, auth_context.options);

        for _ in 0..=crate::http::skip_response::MAX_REDIRECT_COUNT {
            let cookie_header = cookie_helper.build_cookie_header_from_url(&current_url);
            let allow_origin_auth = origin_scope.allows_sensitive_headers(&current_url);
            let authorization = allow_origin_auth
                .then(|| auth_factory.resolve_basic_authorization(&current_url, &auth_options))
                .flatten();
            let request = self.request_policy.apply_with_basic_auth_for_origin(
                self.client.head(current_url.as_str()),
                (!cookie_header.is_empty()).then_some(cookie_header.as_str()),
                &[],
                authorization.as_deref(),
                allow_origin_auth,
            );
            let response = request.send().await.ok()?;
            cookie_helper.extract_and_store_cookies(current_url.as_str(), &response);

            let status_code = response.status().as_u16();
            if !matches!(status_code, 300..=303 | 307 | 308) {
                return Some(prepared_response(
                    response,
                    current_url.to_string(),
                    allow_origin_auth,
                    auth_factory,
                ));
            }

            let location = response
                .headers()
                .get(reqwest::header::LOCATION)
                .and_then(|value| value.to_str().ok())?;
            let next_url = current_url.join(location).ok()?;
            origin_scope.follow_redirect(&current_url, &next_url);
            current_url = next_url;
        }

        None
    }

    /// Read the first ordinary GET response before finalizing an inferred
    /// output path. The sequential downloader can reuse this response body;
    /// a later concurrent decision may deliberately discard it and issue
    /// range requests instead.
    pub(super) async fn send_get_with_redirects(&self, uri: &str) -> Option<PreparedHttpResponse> {
        let mut current_url = reqwest::Url::parse(uri).ok()?;
        let mut origin_scope =
            crate::http::request_policy::RedirectOriginScope::new(current_url.clone());
        let cookie_helper = self.create_cookie_helper();
        let initial_scheme = current_url.scheme().to_owned();
        let options = self.group.recover().options_arc();
        let auth_context = crate::engine::http::auth::from_options(&options, &initial_scheme);
        let (mut auth_factory, auth_options) = (auth_context.factory, auth_context.options);

        for _ in 0..=crate::http::skip_response::MAX_REDIRECT_COUNT {
            let cookie_header = cookie_helper.build_cookie_header_from_url(&current_url);
            let allow_origin_auth = origin_scope.allows_sensitive_headers(&current_url);
            let authorization = allow_origin_auth
                .then(|| auth_factory.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()),
                (!cookie_header.is_empty()).then_some(cookie_header.as_str()),
                &[],
                authorization.as_deref(),
                allow_origin_auth,
            );
            let mut response = request.send().await.ok()?;
            cookie_helper.extract_and_store_cookies(current_url.as_str(), &response);
            response = self
                .retry_get_after_auth_challenge(
                    response,
                    GetAuthRetryContext {
                        current_url: &current_url,
                        cookie_helper: &cookie_helper,
                        auth_factory: &mut auth_factory,
                        auth_options: &auth_options,
                        authentication_used: authorization.is_some(),
                        allow_origin_auth,
                    },
                )
                .await;

            if !crate::http::response::is_redirect_status(response.status().as_u16()) {
                return Some(prepared_response(
                    response,
                    current_url.to_string(),
                    allow_origin_auth,
                    auth_factory,
                ));
            }

            let Some(location) = response
                .headers()
                .get(reqwest::header::LOCATION)
                .and_then(|value| value.to_str().ok())
            else {
                return Some(prepared_response(
                    response,
                    current_url.to_string(),
                    allow_origin_auth,
                    auth_factory,
                ));
            };

            let Ok(next_url) = current_url.join(location) else {
                return Some(prepared_response(
                    response,
                    current_url.to_string(),
                    allow_origin_auth,
                    auth_factory,
                ));
            };
            self.group.recover_mut().add_redirect_uri(next_url.as_str());
            origin_scope.follow_redirect(&current_url, &next_url);
            current_url = next_url;
        }

        None
    }
}