aria2-core 0.3.13

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
//! Metalink-to-Request-Group converter
//!
//! Converts a parsed Metalink document into download request groups,
//! one per file entry (or per metaurl group) in the Metalink.
//!
//! In C++ aria2, `Metalink2RequestGroup` is responsible for:
//! 1. Parsing a Metalink file (from disk or from a binary stream)
//! 2. Querying entries by version/language/os
//! 3. Applying URL priority ordering and location preferences
//! 4. Filtering unsupported resources
//! 5. Selecting specific files by index
//! 6. Grouping entries by metaurl name (for BT dependency injection)
//! 7. Creating `RequestGroup` objects for each group
//!
//! # C++ Equivalence
//!
//! | Rust | C++ |
//! |---|---|
//! | `MetalinkToRequestGroup` | `Metalink2RequestGroup` |
//! | `generate_from_bytes()` | `generate(groups, binaryStream, option, baseUri)` |
//! | `create_request_groups()` | `createRequestGroup(groups, entries, option)` |

use tracing::{debug, info};

use crate::engine::metalink::download_command::MetalinkDownloadCommand;
use crate::error::{Aria2Error, Result};
use crate::request::request_group::{DownloadOptions, RequestGroup};
#[cfg(feature = "bittorrent")]
use aria2_protocol::metalink::parser::{MediaType, MetaUrlEntry};
use aria2_protocol::metalink::parser::{
    MetalinkDocument, MetalinkFile, group_entry_by_metaurl_name,
};
use aria2_protocol::metalink::resource::{LOWEST_PRIORITY, ResourceType};
use std::sync::{Arc, RwLock};

#[cfg(feature = "bittorrent")]
use crate::engine::metalink::request_graph::MetalinkRequestGraph;

mod groups;
#[cfg(test)]
mod tests;

/// Request groups produced from one parsed Metalink document.
pub struct MetalinkGroupExpansion {
    pub resource_groups: Vec<Arc<RwLock<RequestGroup>>>,
    #[cfg(feature = "bittorrent")]
    pub torrent_graphs: Vec<MetalinkRequestGraph>,
}

pub struct MetalinkToRequestGroup {
    /// Optional base URI for resolving relative URLs in the Metalink.
    base_uri: Option<String>,
    /// Version filter (mirrors C++ `PREF_METALINK_VERSION`).
    version: String,
    /// Language filter (mirrors C++ `PREF_METALINK_LANGUAGE`).
    language: String,
    /// OS filter (mirrors C++ `PREF_METALINK_OS`).
    os: String,
    /// Location preference codes (mirrors C++ `PREF_METALINK_LOCATION`).
    locations: Vec<String>,
    /// Preferred protocol (mirrors C++ `PREF_METALINK_PREFERRED_PROTOCOL`).
    preferred_protocol: Option<String>,
    /// Select-file segments (1-based indices, mirrors C++ `PREF_SELECT_FILE`).
    select_files: Vec<usize>,
    /// Whether to pause newly created groups.
    pause_requested: bool,
}

impl MetalinkToRequestGroup {
    /// Create a new converter with default options.
    pub fn new() -> Self {
        Self {
            base_uri: None,
            version: String::new(),
            language: String::new(),
            os: String::new(),
            locations: Vec::new(),
            preferred_protocol: None,
            select_files: Vec::new(),
            pause_requested: false,
        }
    }

    /// Set the base URI for resolving relative URLs.
    pub fn with_base_uri(mut self, base_uri: impl Into<String>) -> Self {
        self.base_uri = Some(base_uri.into());
        self
    }

    /// Set version filter.
    pub fn with_version(mut self, version: impl Into<String>) -> Self {
        self.version = version.into();
        self
    }

    /// Set language filter.
    pub fn with_language(mut self, language: impl Into<String>) -> Self {
        self.language = language.into();
        self
    }

    /// Set OS filter.
    pub fn with_os(mut self, os: impl Into<String>) -> Self {
        self.os = os.into();
        self
    }

    /// Set location preference codes (comma-separated or pre-split).
    pub fn with_locations(mut self, locations: Vec<String>) -> Self {
        self.locations = locations;
        self
    }

    /// Set preferred protocol (e.g. "http", "https", "ftp").
    pub fn with_preferred_protocol(mut self, protocol: impl Into<String>) -> Self {
        let proto = protocol.into();
        if proto != "none" && !proto.is_empty() {
            self.preferred_protocol = Some(proto);
        }
        self
    }

    /// Set select-file segments (1-based file indices to keep).
    pub fn with_select_files(mut self, segments: Vec<usize>) -> Self {
        self.select_files = segments;
        self
    }

    /// Set whether newly created groups should be paused.
    pub fn with_pause_requested(mut self, pause: bool) -> Self {
        self.pause_requested = pause;
        self
    }

    /// Parse the C++ `PREF_SELECT_FILE` syntax used by Metalink.
    ///
    /// The returned values are 1-based positions in the filtered Metalink
    /// entry list, matching `Metalink2RequestGroup::createRequestGroup()`.
    fn parse_select_files(value: &str) -> Result<Vec<usize>> {
        let mut result = Vec::new();
        for segment in value.split(',').map(str::trim).filter(|s| !s.is_empty()) {
            if let Some((start, end)) = segment.split_once('-') {
                let start = start.trim().parse::<usize>().map_err(|_| {
                    Aria2Error::MetalinkParse(format!("invalid select-file segment `{segment}`"))
                })?;
                let end = end.trim().parse::<usize>().map_err(|_| {
                    Aria2Error::MetalinkParse(format!("invalid select-file segment `{segment}`"))
                })?;
                if start == 0 || end < start {
                    return Err(Aria2Error::MetalinkParse(format!(
                        "invalid select-file segment `{segment}`"
                    )));
                }
                result.extend(start..=end);
            } else {
                let index = segment.parse::<usize>().map_err(|_| {
                    Aria2Error::MetalinkParse(format!("invalid select-file segment `{segment}`"))
                })?;
                if index == 0 {
                    return Err(Aria2Error::MetalinkParse(format!(
                        "invalid select-file segment `{segment}`"
                    )));
                }
                result.push(index);
            }
        }
        result.sort_unstable();
        result.dedup();
        Ok(result)
    }

    /// Return the effective select-file positions, giving explicit builder
    /// configuration precedence over the per-download option.
    fn effective_select_files(&self, options: &DownloadOptions) -> Result<Vec<usize>> {
        if !self.select_files.is_empty() {
            return Ok(self.select_files.clone());
        }
        options
            .select_file
            .as_deref()
            .map(Self::parse_select_files)
            .transpose()
            .map(|segments| segments.unwrap_or_default())
    }

    /// Apply the Metalink resource rules that are needed by every execution
    /// path, including manager-owned groups that keep the raw document for
    /// restart/fallback handling.
    pub(crate) fn normalize_file_for_runtime(
        &self,
        file: &mut MetalinkFile,
        options: &DownloadOptions,
    ) {
        let locations: Vec<String> = if self.locations.is_empty() {
            options
                .metalink_location
                .as_deref()
                .map(|value| {
                    value
                        .split(',')
                        .map(str::trim)
                        .filter(|location| !location.is_empty())
                        .map(str::to_ascii_lowercase)
                        .collect()
                })
                .unwrap_or_default()
        } else {
            self.locations.clone()
        };
        let preferred_protocol = self
            .preferred_protocol
            .as_deref()
            .or(options.metalink_preferred_protocol.as_deref())
            .filter(|protocol| !protocol.eq_ignore_ascii_case("none"));

        file.drop_unsupported_resources();
        #[cfg(feature = "bittorrent")]
        {
            let mut torrent_meta_urls = Vec::new();
            file.urls.retain(|url| {
                if url.resource_type == ResourceType::BitTorrent {
                    torrent_meta_urls.push(
                        MetaUrlEntry::new(&url.url, MediaType::Torrent).with_priority(url.priority),
                    );
                    false
                } else {
                    true
                }
            });
            file.meta_urls.extend(torrent_meta_urls);
        }
        #[cfg(not(feature = "bittorrent"))]
        file.urls
            .retain(|url| url.resource_type != ResourceType::BitTorrent);
        if !locations.is_empty() {
            let location_refs: Vec<&str> = locations.iter().map(String::as_str).collect();
            file.set_location_priority(&location_refs, -LOWEST_PRIORITY);
        }
        if let Some(protocol) = preferred_protocol {
            file.set_protocol_priority(protocol, -LOWEST_PRIORITY);
        }
        file.reorder_metaurls_by_priority();
    }

    /// Parse, query, select, and normalize Metalink entries once for all
    /// manager-owned construction paths.
    ///
    /// Keeping this operation behind one internal seam prevents the resource
    /// and torrent graph paths from silently disagreeing about filters,
    /// priorities, or source indices.
    fn prepare_files(
        &self,
        doc: &MetalinkDocument,
        options: &DownloadOptions,
    ) -> Result<Vec<(usize, MetalinkFile)>> {
        let version = if self.version.is_empty() {
            options.metalink_version.as_deref().unwrap_or("")
        } else {
            self.version.as_str()
        };
        let language = if self.language.is_empty() {
            options.metalink_language.as_deref().unwrap_or("")
        } else {
            self.language.as_str()
        };
        let os = if self.os.is_empty() {
            options.metalink_os.as_deref().unwrap_or("")
        } else {
            self.os.as_str()
        };

        let queried: Vec<(usize, MetalinkFile)> = doc
            .query_entries(version, language, os)
            .into_iter()
            .filter_map(|index| doc.files.get(index).cloned().map(|file| (index, file)))
            .collect();
        let select_files = self.effective_select_files(options)?;
        let mut files: Vec<(usize, MetalinkFile)> = if select_files.is_empty() {
            queried
        } else {
            // C++ applies select-file after queryEntry(), so indices refer to
            // the filtered list rather than the original XML positions.
            select_files
                .into_iter()
                .filter_map(|position| queried.get(position - 1).cloned())
                .collect()
        };

        for (_, file) in &mut files {
            self.normalize_file_for_runtime(file, options);
        }

        files.retain(|(_, file)| !file.urls.is_empty() || !file.meta_urls.is_empty());
        Ok(files)
    }

    pub fn generate_from_bytes(
        &self,
        metalink_data: &[u8],
        options: &DownloadOptions,
    ) -> Result<Vec<MetalinkDownloadCommand>> {
        let base_uri = self
            .base_uri
            .as_deref()
            .or(options.metalink_base_uri.as_deref());
        let doc =
            MetalinkDocument::parse(metalink_data, base_uri).map_err(Aria2Error::MetalinkParse)?;
        self.create_request_groups(doc, options)
    }

    /// Core conversion logic: MetalinkDocument → download commands.
    ///
    /// Mirrors C++ `Metalink2RequestGroup::createRequestGroup()`.
    fn create_request_groups(
        &self,
        doc: MetalinkDocument,
        options: &DownloadOptions,
    ) -> Result<Vec<MetalinkDownloadCommand>> {
        let files: Vec<MetalinkFile> = self
            .prepare_files(&doc, options)?
            .into_iter()
            .map(|(_, file)| file)
            .collect();

        if files.is_empty() {
            info!("No Metalink entries with supported resources remain after filtering");
            return Ok(Vec::new());
        }

        // Step 7: Group entries by metaurl name
        // (mirrors C++ metalink::groupEntryByMetaurlName)
        let groups = group_entry_by_metaurl_name(&files);

        debug!(
            "Metalink: {} files grouped into {} request groups",
            files.len(),
            groups.len()
        );

        // Step 8: Create download commands for each group
        let mut commands = Vec::with_capacity(groups.len());

        for (metaurl_key, file_indices) in groups {
            let grouped_files: Vec<MetalinkFile> = file_indices
                .iter()
                .filter_map(|&idx| files.get(idx))
                .filter(|file| !file.urls.is_empty() || !metaurl_key.is_empty())
                .cloned()
                .map(|mut file| {
                    // Reorder resources by priority once per selected entry.
                    // The grouped command retains the resulting per-file URI
                    // lists in its DownloadContext.
                    file.reorder_resources_by_priority();
                    file
                })
                .collect();

            if grouped_files.is_empty() {
                continue;
            }

            let gid = (commands.len() as u64) + 1;
            if grouped_files.len() == 1 {
                let file_infos = MetalinkDownloadCommand::create_multi_file_for_single(
                    &grouped_files[0],
                    options,
                    options.dir.as_deref(),
                    gid,
                )?;
                if self.pause_requested {
                    for file_info in &file_infos {
                        file_info.command.group().request_pause();
                    }
                }
                commands.extend(file_infos.into_iter().map(|file_info| file_info.command));
            } else {
                let command = MetalinkDownloadCommand::create_multi_file_group(
                    &grouped_files,
                    options,
                    options.dir.as_deref(),
                    gid,
                )?;
                if self.pause_requested {
                    command.group().request_pause();
                }
                commands.push(command);
            }
        }

        info!(
            count = commands.len(),
            "Metalink-to-request-group: generated download commands"
        );

        Ok(commands)
    }
}

impl Default for MetalinkToRequestGroup {
    fn default() -> Self {
        Self::new()
    }
}