aria2-core 0.3.14

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
//! Protocol-specific command construction behind one registry boundary.
//!
//! The engine loop owns task admission and lifecycle. This registry keeps
//! scheme and metadata dispatch out of that lifecycle path; protocol adapters
//! own construction of their command implementations.

use std::sync::Arc;

use async_trait::async_trait;

use crate::dns::dns_cache::DnsCache;
use crate::engine::command::Command;
use crate::engine::engine_loop::EngineLoopContext;
use crate::error::{Aria2Error, FatalError, Result};
use crate::network::OutboundNetworkPolicy;
use crate::rate_limiter::RateLimiter;
use crate::request::request_group::{DownloadOptions, RequestGroup};

/// Per-task protocol input selected by the engine.
pub(crate) struct ProtocolCommandRequest {
    pub(crate) group: Arc<std::sync::RwLock<RequestGroup>>,
    pub(crate) first_uri: String,
    pub(crate) options: Arc<DownloadOptions>,
}

/// Shared engine services available to command adapters.
#[derive(Clone)]
pub(crate) struct ProtocolServices {
    pub(crate) dns_cache: Arc<tokio::sync::Mutex<DnsCache>>,
    pub(crate) outbound_network_policy: Arc<OutboundNetworkPolicy>,
    pub(crate) global_limiter: Option<RateLimiter>,
}

#[allow(clippy::double_must_use)]
#[async_trait]
pub(crate) trait ProtocolCommandAdapter: Send + Sync {
    /// Whether this adapter owns this task input. Earlier adapters have
    /// priority, so metadata-only formats can claim tasks before URI schemes.
    fn supports(&self, request: &ProtocolCommandRequest) -> bool;

    async fn create(
        &self,
        request: ProtocolCommandRequest,
        services: &ProtocolServices,
    ) -> Result<Box<dyn Command>>;
}

pub(crate) fn is_sftp_uri(uri: &str) -> bool {
    uri.split_once(':')
        .is_some_and(|(scheme, _)| scheme.eq_ignore_ascii_case("sftp"))
}

#[cfg(not(feature = "sftp"))]
struct UnsupportedSftpCommandAdapter;

#[cfg(not(feature = "sftp"))]
#[async_trait]
impl ProtocolCommandAdapter for UnsupportedSftpCommandAdapter {
    fn supports(&self, request: &ProtocolCommandRequest) -> bool {
        is_sftp_uri(&request.first_uri)
    }

    async fn create(
        &self,
        _request: ProtocolCommandRequest,
        _services: &ProtocolServices,
    ) -> Result<Box<dyn Command>> {
        Err(Aria2Error::Fatal(FatalError::UnsupportedProtocol {
            protocol: "sftp".into(),
        }))
    }
}

fn sftp_command_adapter() -> Box<dyn ProtocolCommandAdapter> {
    #[cfg(feature = "sftp")]
    {
        Box::new(crate::engine::sftp::command_adapter::SftpCommandAdapter)
    }
    #[cfg(not(feature = "sftp"))]
    {
        Box::new(UnsupportedSftpCommandAdapter)
    }
}

pub(crate) struct ProtocolAdapterRegistry {
    adapters: Vec<Box<dyn ProtocolCommandAdapter>>,
}

impl ProtocolAdapterRegistry {
    fn new(adapters: Vec<Box<dyn ProtocolCommandAdapter>>) -> Self {
        Self { adapters }
    }

    /// Assemble built-in adapters at the engine boundary. Protocol-specific
    /// shared state is injected into its owning adapter rather than carried by
    /// the generic task spawner.
    pub(crate) fn for_engine(_context: &EngineLoopContext) -> Self {
        let ftp_connection_pool = Arc::clone(&_context.ftp_connection_pool);
        #[cfg(feature = "bittorrent")]
        let bittorrent = crate::engine::bittorrent::command_adapter::BtCommandServices {
            public_tracker_catalog: Arc::clone(&_context.public_tracker_catalog),
            bt_registry: Arc::clone(&_context.bt_registry),
            bt_listener: Arc::clone(&_context.bt_listener),
            lpd_manager: Arc::clone(&_context.lpd_manager),
            open_file_budget: Arc::clone(&_context.open_file_budget),
        };

        Self::builtins(
            ftp_connection_pool,
            #[cfg(feature = "bittorrent")]
            bittorrent,
        )
    }

    pub(crate) fn builtins(
        ftp_connection_pool: Arc<crate::ftp::connection_pool::FtpConnectionPool>,
        #[cfg(feature = "bittorrent")]
        bittorrent: crate::engine::bittorrent::command_adapter::BtCommandServices,
    ) -> Self {
        // Keep the always-available adapters in priority order; optional
        // formats are inserted ahead of their URI-scheme fallbacks below.
        #[allow(unused_mut)]
        let mut adapters: Vec<Box<dyn ProtocolCommandAdapter>> = vec![
            Box::new(crate::engine::ftp::command_adapter::FtpCommandAdapter::new(
                Arc::clone(&ftp_connection_pool),
            )),
            Box::new(crate::engine::http::command_adapter::HttpCommandAdapter),
        ];

        #[cfg(feature = "metalink")]
        adapters.insert(
            0,
            Box::new(
                crate::engine::metalink::command_adapter::MetalinkCommandAdapter::new(
                    Arc::clone(&ftp_connection_pool),
                    #[cfg(feature = "bittorrent")]
                    bittorrent.clone(),
                ),
            ),
        );

        adapters.insert(
            usize::from(cfg!(feature = "metalink")),
            sftp_command_adapter(),
        );

        #[cfg(feature = "bittorrent")]
        adapters.insert(
            usize::from(cfg!(feature = "metalink")) + usize::from(cfg!(feature = "sftp")),
            Box::new(crate::engine::bittorrent::command_adapter::BtCommandAdapter::new(bittorrent)),
        );

        Self::new(adapters)
    }

    /// Build only adapters that can fetch a single Metalink mirror or
    /// metadata URL. Metadata dispatch is owned by Metalink, so this registry
    /// deliberately excludes Metalink itself and BitTorrent task commands.
    #[cfg(feature = "metalink")]
    pub(crate) fn source_protocols(
        ftp_connection_pool: Arc<crate::ftp::connection_pool::FtpConnectionPool>,
    ) -> Self {
        #[allow(unused_mut)]
        let mut adapters: Vec<Box<dyn ProtocolCommandAdapter>> = vec![
            Box::new(crate::engine::ftp::command_adapter::FtpCommandAdapter::new(
                ftp_connection_pool,
            )),
            Box::new(crate::engine::http::command_adapter::HttpCommandAdapter),
        ];

        adapters.insert(1, sftp_command_adapter());

        Self::new(adapters)
    }

    pub(crate) async fn create(
        &self,
        request: ProtocolCommandRequest,
        services: &ProtocolServices,
    ) -> Result<Box<dyn Command>> {
        let adapter = self
            .adapters
            .iter()
            .find(|adapter| adapter.supports(&request))
            .ok_or_else(|| {
                Aria2Error::Fatal(FatalError::Config(format!(
                    "No protocol adapter supports {}",
                    request.first_uri
                )))
            })?;

        adapter.create(request, services).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::engine::command::CommandStatus;
    use crate::request::request_group::GroupId;
    use crate::util::rwlock_ext::RwLockRecover;

    #[test]
    fn sftp_scheme_match_is_case_insensitive_and_requires_a_scheme_boundary() {
        assert!(is_sftp_uri("sftp://host/file"));
        assert!(is_sftp_uri("SFTP://host/file"));
        assert!(is_sftp_uri("sftp:opaque"));
        assert!(!is_sftp_uri("sftpish://host/file"));
        assert!(!is_sftp_uri("https://host/sftp://file"));
    }

    struct FakeAdapter;

    #[async_trait]
    impl ProtocolCommandAdapter for FakeAdapter {
        fn supports(&self, request: &ProtocolCommandRequest) -> bool {
            request.first_uri.starts_with("fake:")
        }

        async fn create(
            &self,
            request: ProtocolCommandRequest,
            _services: &ProtocolServices,
        ) -> Result<Box<dyn Command>> {
            Ok(Box::new(FakeCommand {
                group: request.group,
            }))
        }
    }

    struct FakeCommand {
        group: Arc<std::sync::RwLock<RequestGroup>>,
    }

    #[async_trait]
    impl Command for FakeCommand {
        async fn execute(&mut self) -> Result<()> {
            Ok(())
        }

        fn status(&self) -> CommandStatus {
            CommandStatus::Pending
        }

        fn gid(&self) -> crate::request::request_group::GroupId {
            self.group.recover().gid()
        }
    }

    #[tokio::test]
    async fn newly_registered_adapter_is_selected_without_dispatcher_changes() {
        let registry = ProtocolAdapterRegistry::new(vec![Box::new(FakeAdapter)]);
        let options = Arc::new(DownloadOptions::default());
        let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
            GroupId::new(730),
            vec!["fake://host/object".to_string()],
            (*options).clone(),
        )));
        let request = ProtocolCommandRequest {
            group,
            first_uri: "fake://host/object".to_string(),
            options,
        };
        let services = ProtocolServices {
            dns_cache: Arc::new(tokio::sync::Mutex::new(DnsCache::new())),
            outbound_network_policy: Arc::new(OutboundNetworkPolicy::direct()),
            global_limiter: None,
        };

        let command = registry
            .create(request, &services)
            .await
            .expect("the fake adapter should be selected");

        assert_eq!(command.status(), CommandStatus::Pending);
        assert_eq!(command.gid(), GroupId::new(730));
    }

    #[cfg(all(feature = "metalink", not(feature = "bittorrent")))]
    #[tokio::test]
    async fn metalink_adapter_uses_parsed_file_before_session_source_fallback() {
        let options = Arc::new(DownloadOptions::default());
        let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
            GroupId::new(731),
            vec!["https://mirror.test/payload.bin".to_string()],
            (*options).clone(),
        )));
        group
            .recover()
            .set_metalink_source(b"not valid XML".to_vec(), 0);
        let document = aria2_protocol::metalink::parser::MetalinkDocument::parse(
            br#"<metalink xmlns="urn:ietf:params:xml:ns:metalink"><file name="payload.bin"><url>https://mirror.test/payload.bin</url></file></metalink>"#,
            None,
        )
        .expect("fixture should parse");
        group
            .recover()
            .set_parsed_metalink_file(Arc::new(document.files.into_iter().next().unwrap()));

        let registry = ProtocolAdapterRegistry::new(vec![Box::new(
            crate::engine::metalink::command_adapter::MetalinkCommandAdapter::new(Arc::new(
                crate::ftp::connection_pool::FtpConnectionPool::new(4),
            )),
        )]);
        let request = ProtocolCommandRequest {
            group,
            first_uri: "https://mirror.test/payload.bin".to_string(),
            options,
        };
        let services = ProtocolServices {
            dns_cache: Arc::new(tokio::sync::Mutex::new(DnsCache::new())),
            outbound_network_policy: Arc::new(OutboundNetworkPolicy::direct()),
            global_limiter: None,
        };

        let command = registry
            .create(request, &services)
            .await
            .expect("cached protocol metadata should avoid reparsing the invalid source bytes");

        assert_eq!(command.gid(), GroupId::new(731));
    }

    #[cfg(all(feature = "metalink", not(feature = "bittorrent")))]
    #[tokio::test]
    async fn metalink_adapter_reconstructs_parsed_file_from_restored_source() {
        let options = Arc::new(DownloadOptions::default());
        let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
            GroupId::new(732),
            vec!["https://mirror.test/restored.bin".to_string()],
            (*options).clone(),
        )));
        group.recover().set_metalink_source(
            br#"<metalink xmlns="urn:ietf:params:xml:ns:metalink"><file name="restored.bin"><url>https://mirror.test/restored.bin</url></file></metalink>"#.to_vec(),
            0,
        );
        let request = ProtocolCommandRequest {
            group,
            first_uri: "https://mirror.test/restored.bin".to_string(),
            options,
        };
        let registry = ProtocolAdapterRegistry::new(vec![Box::new(
            crate::engine::metalink::command_adapter::MetalinkCommandAdapter::new(Arc::new(
                crate::ftp::connection_pool::FtpConnectionPool::new(4),
            )),
        )]);
        let services = ProtocolServices {
            dns_cache: Arc::new(tokio::sync::Mutex::new(DnsCache::new())),
            outbound_network_policy: Arc::new(OutboundNetworkPolicy::direct()),
            global_limiter: None,
        };

        let command = registry
            .create(request, &services)
            .await
            .expect("restored source should be parsed into command input");

        assert_eq!(command.gid(), GroupId::new(732));
    }

    #[cfg(feature = "metalink")]
    #[tokio::test]
    async fn metalink_payload_uses_injected_http_proxy_policy() {
        use tokio::io::{AsyncReadExt, AsyncWriteExt};
        use tokio::net::TcpListener;

        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("HTTP proxy fixture should bind");
        let proxy_address = listener.local_addr().expect("HTTP proxy address");
        let server = tokio::spawn(async move {
            let (mut stream, _) = listener.accept().await.expect("HTTP proxy request");
            let mut request = [0u8; 4096];
            let length = stream.read(&mut request).await.expect("read proxy request");
            let request = String::from_utf8_lossy(&request[..length]).into_owned();
            stream
                .write_all(
                    b"HTTP/1.1 200 OK\r\nContent-Length: 7\r\nConnection: close\r\n\r\npayload",
                )
                .await
                .expect("write HTTP payload");
            request
        });

        let output_dir = tempfile::tempdir_in("../target").expect("output directory");
        let xml = "<metalink xmlns=\"urn:ietf:params:xml:ns:metalink\"><file name=\"payload.bin\"><size>7</size><url>http://mirror.test/payload.bin</url></file></metalink>".to_string();
        let options = Arc::new(DownloadOptions {
            dir: Some(output_dir.path().to_string_lossy().into_owned()),
            http_proxy: Some(format!("http://{proxy_address}")),
            max_retries: 1,
            ..DownloadOptions::default()
        });
        let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
            GroupId::new(733),
            vec!["http://mirror.test/payload.bin".to_string()],
            (*options).clone(),
        )));
        group
            .recover()
            .set_metalink_source(xml.as_bytes().to_vec(), 0);
        let document =
            aria2_protocol::metalink::parser::MetalinkDocument::parse(xml.as_bytes(), None)
                .expect("fixture should parse");
        group
            .recover()
            .set_parsed_metalink_file(Arc::new(document.files.into_iter().next().unwrap()));

        #[cfg(feature = "bittorrent")]
        let adapter = crate::engine::metalink::command_adapter::MetalinkCommandAdapter::new(
            Arc::new(crate::ftp::connection_pool::FtpConnectionPool::new(4)),
            crate::engine::bittorrent::command_adapter::BtCommandServices {
                public_tracker_catalog: Arc::new(
                    aria2_protocol::bittorrent::tracker::public_list::PublicTrackerList::new(),
                ),
                bt_registry: Arc::new(std::sync::RwLock::new(
                    crate::engine::bittorrent::registry::BtRegistry::new(),
                )),
                bt_listener: Arc::new(
                    crate::engine::bittorrent::peer::listener::BtPeerListenerManager::new(),
                ),
                lpd_manager: Arc::new(crate::engine::bittorrent::discovery::lpd::LpdManager::new()),
                open_file_budget: crate::filesystem::open_file_budget::OpenFileBudget::new(20),
            },
        );
        #[cfg(not(feature = "bittorrent"))]
        let adapter = crate::engine::metalink::command_adapter::MetalinkCommandAdapter::new(
            Arc::new(crate::ftp::connection_pool::FtpConnectionPool::new(4)),
        );
        let adapter = ProtocolAdapterRegistry::new(vec![Box::new(adapter)]);
        let services = ProtocolServices {
            dns_cache: Arc::new(tokio::sync::Mutex::new(DnsCache::new())),
            outbound_network_policy: Arc::new(OutboundNetworkPolicy::direct()),
            global_limiter: None,
        };
        let mut command = adapter
            .create(
                ProtocolCommandRequest {
                    group,
                    first_uri: "http://mirror.test/payload.bin".to_string(),
                    options,
                },
                &services,
            )
            .await
            .expect("Metalink command should be created");

        command.execute().await.expect("Metalink should complete");
        assert_eq!(
            tokio::fs::read(output_dir.path().join("payload.bin"))
                .await
                .unwrap(),
            b"payload"
        );
        let request = server.await.expect("HTTP proxy fixture should finish");
        assert!(request.starts_with("GET http://mirror.test/payload.bin HTTP/1.1"));
    }
}