haki-dl 0.2.0

A Rust downloader library and CLI foundation.
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Source and segment loading utilities.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use crate::config::DownloadOptions;
use crate::error::{Error, Result};
use crate::http::{DefaultHttpClient, HttpRequest, HttpResponse};
use crate::manifest::ExtractorType;
use crate::processor::{
    ContentProcessor, DefaultDashContentProcessor, DefaultHlsContentProcessor, ParserConfig,
};

pub(crate) const HTTP_LIVE_TS_MARKER: &str = "<HAKI_LIVE_TS>";
pub(crate) const BINARY_DATA_MARKER: &str = "<HAKI_BINARY_DATA>";

/// Loaded source classification.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LoadedSourceKind {
    /// HLS text manifest.
    Hls,
    /// MPEG-DASH text manifest.
    Dash,
    /// Smooth Streaming manifest.
    Mss,
    /// Direct MPEG-TS live stream.
    HttpLiveTs,
    /// Direct binary input that is not a supported streaming manifest.
    BinaryData,
}

impl LoadedSourceKind {
    /// Returns the extractor family for this source.
    pub fn extractor_type(self) -> ExtractorType {
        match self {
            Self::Hls => ExtractorType::Hls,
            Self::Dash => ExtractorType::MpegDash,
            Self::Mss => ExtractorType::Mss,
            Self::HttpLiveTs => ExtractorType::HttpLive,
            Self::BinaryData => ExtractorType::HttpLive,
        }
    }

    fn raw_file_name(self) -> &'static str {
        match self {
            Self::Hls => "raw.m3u8",
            Self::Dash => "raw.mpd",
            Self::Mss => "raw.ism",
            Self::HttpLiveTs => "raw.txt",
            Self::BinaryData => "raw.bin",
        }
    }
}

/// Loaded source text and retained raw file map.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LoadedSource {
    /// Source kind.
    pub kind: LoadedSourceKind,
    /// Source text or live-TS marker.
    pub text: String,
    /// Original input URL/path.
    pub original_url: String,
    /// Final URL/path after redirects.
    pub final_url: String,
    /// Raw files that should be written to the temp directory later.
    pub raw_files: BTreeMap<String, String>,
    /// Debug lines captured while loading the source.
    pub debug_logs: Vec<String>,
}

/// Compatibility source loader.
pub struct SourceLoader {
    http: DefaultHttpClient,
    content_processors: Vec<Box<dyn ContentProcessor>>,
}

impl Default for SourceLoader {
    fn default() -> Self {
        Self {
            http: DefaultHttpClient::new(),
            content_processors: vec![
                Box::<DefaultHlsContentProcessor>::default(),
                Box::<DefaultDashContentProcessor>::default(),
            ],
        }
    }
}

impl SourceLoader {
    /// Creates a loader with default processors.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a loader from public download options.
    pub fn from_options(options: &DownloadOptions) -> Self {
        Self::new().with_http(DefaultHttpClient::from_options(options))
    }

    /// Creates a loader with a custom HTTP client.
    pub fn with_http(mut self, http: DefaultHttpClient) -> Self {
        self.http = http;
        self
    }

    /// Loads a manifest, local source, or direct live-TS source.
    pub async fn load_source(
        &self,
        input: &str,
        config: &mut ParserConfig,
    ) -> Result<LoadedSource> {
        let (mut text, original_url, final_url, mut debug_logs) = if input.starts_with("file:") {
            let path = file_uri_to_path(input);
            (
                read_source_text_file(&path).await?,
                input.to_string(),
                input.to_string(),
                Vec::new(),
            )
        } else if input.starts_with("http://") || input.starts_with("https://") {
            let response = self.get_web_source(input, &config.headers).await?;
            (response.0, input.to_string(), response.1, response.2)
        } else if tokio::fs::metadata(input).await.is_ok() {
            let path = tokio::fs::canonicalize(input).await?;
            let file_uri =
                path_to_file_uri(&path).unwrap_or_else(|| path.to_string_lossy().to_string());
            (
                read_source_text_file(&path).await?,
                file_uri.clone(),
                file_uri,
                Vec::new(),
            )
        } else {
            return Err(Error::http("source input could not be loaded"));
        };

        if text.trim().is_empty() {
            return Err(Error::http("source input was empty"));
        }
        text = text.trim().trim_start_matches('\u{feff}').to_string();
        config.original_url = original_url.clone();
        config.url = final_url.clone();
        let kind = detect_source_kind(&text)?;
        let mut raw_files = BTreeMap::new();
        raw_files.insert(kind.raw_file_name().to_string(), text.clone());
        for processor in &self.content_processors {
            if processor.can_process(kind.extractor_type(), &text, config) {
                text = processor.process(&text, config)?;
            }
        }
        debug_logs.retain(|line| !line.trim().is_empty());
        Ok(LoadedSource {
            kind,
            text,
            original_url,
            final_url,
            raw_files,
            debug_logs,
        })
    }

    /// Loads one segment-like byte source.
    pub async fn load_segment_bytes(&self, url: &str, config: &ParserConfig) -> Result<Vec<u8>> {
        if url.starts_with("file:") {
            return Ok(tokio::fs::read(file_uri_to_path(url)).await?);
        }
        if let Some(value) = url.strip_prefix("base64://") {
            return base64_decode(value);
        }
        if let Some(value) = url.strip_prefix("hex://") {
            return hex_to_bytes(value);
        }
        if tokio::fs::metadata(url).await.is_ok() {
            return Ok(tokio::fs::read(url).await?);
        }
        let mut request = HttpRequest::new(url);
        request.headers = config.headers.clone();
        Ok(self.http.send(request).await?.body)
    }

    async fn get_web_source(
        &self,
        url: &str,
        headers: &BTreeMap<String, String>,
    ) -> Result<(String, String, Vec<String>)> {
        let mut request = HttpRequest::new(url);
        request.headers = headers.clone();
        let response = self.http.send_source(request).await?;
        let mut debug_logs = response.debug_logs.clone();
        if is_mpeg2_ts_buffer(&response.body) {
            debug_logs.push("Detected MPEG-TS stream".to_string());
            return Ok((HTTP_LIVE_TS_MARKER.to_string(), url.to_string(), debug_logs));
        }
        if let Some(encoding) = response.headers.get("content-encoding")
            && !encoding.trim().is_empty()
        {
            debug_logs.push(format!("Detected compression: {encoding}"));
        }
        let charset = response_charset(&response);
        if charset.is_some() || has_text_bom(&response.body) {
            let text = decode_text_bytes(&response.body, charset.as_deref())?;
            return Ok((text, response.final_url, debug_logs));
        }
        if looks_like_binary(&sample(&response.body)) {
            debug_logs.push("Heuristic detection: binary data".to_string());
            return Ok((
                BINARY_DATA_MARKER.to_string(),
                response.final_url,
                debug_logs,
            ));
        }
        let text = decode_text_bytes(&response.body, charset.as_deref())?;
        Ok((text, response.final_url, debug_logs))
    }
}

async fn read_source_text_file(path: &std::path::Path) -> Result<String> {
    decode_text_bytes(&tokio::fs::read(path).await?, None)
}

/// Writes retained raw files under a target directory.
pub async fn write_raw_files(
    raw_files: &BTreeMap<String, String>,
    directory: &std::path::Path,
) -> Result<Vec<PathBuf>> {
    tokio::fs::create_dir_all(directory).await?;
    let mut written = Vec::with_capacity(raw_files.len());
    for (name, text) in raw_files {
        let path = directory.join(name);
        if tokio::fs::metadata(&path).await.is_err() {
            tokio::fs::write(&path, n_utf8_text_file_bytes(text)).await?;
        }
        written.push(path);
    }
    Ok(written)
}

fn n_utf8_text_file_bytes(text: &str) -> Vec<u8> {
    let mut bytes = Vec::with_capacity(3 + text.len());
    bytes.extend_from_slice(&[0xef, 0xbb, 0xbf]);
    bytes.extend_from_slice(text.as_bytes());
    bytes
}

fn detect_source_kind(text: &str) -> Result<LoadedSourceKind> {
    let trimmed = text.trim();
    if trimmed.starts_with("#EXTM3U") {
        Ok(LoadedSourceKind::Hls)
    } else if trimmed.contains("</MPD>") && trimmed.contains("<MPD") {
        Ok(LoadedSourceKind::Dash)
    } else if trimmed.contains("</SmoothStreamingMedia>")
        && trimmed.contains("<SmoothStreamingMedia")
    {
        Ok(LoadedSourceKind::Mss)
    } else if trimmed == HTTP_LIVE_TS_MARKER {
        Ok(LoadedSourceKind::HttpLiveTs)
    } else if trimmed == BINARY_DATA_MARKER {
        Ok(LoadedSourceKind::BinaryData)
    } else {
        Err(Error::compatibility("source input type is not supported"))
    }
}

fn response_charset(response: &HttpResponse) -> Option<String> {
    response
        .headers
        .get("content-type")
        .and_then(|value| content_type_charset(value))
}

fn decode_text_bytes(bytes: &[u8], charset: Option<&str>) -> Result<String> {
    if bytes.starts_with(&[0xef, 0xbb, 0xbf]) {
        return Ok(String::from_utf8_lossy(&bytes[3..]).to_string());
    }
    if bytes.starts_with(&[0xff, 0xfe]) {
        return decode_utf16(&bytes[2..], false);
    }
    if bytes.starts_with(&[0xfe, 0xff]) {
        return decode_utf16(&bytes[2..], true);
    }
    match charset {
        Some("utf-16") => decode_utf16_auto(bytes),
        Some("utf-16le") => decode_utf16(bytes, false),
        Some("utf-16be") => decode_utf16(bytes, true),
        Some(label) => Ok(decode_with_label(label, bytes)),
        None => Ok(String::from_utf8_lossy(bytes).to_string()),
    }
}

fn has_text_bom(bytes: &[u8]) -> bool {
    bytes.starts_with(&[0xef, 0xbb, 0xbf])
        || bytes.starts_with(&[0xff, 0xfe])
        || bytes.starts_with(&[0xfe, 0xff])
}

fn decode_with_label(label: &str, bytes: &[u8]) -> String {
    match encoding_rs::Encoding::for_label(label.as_bytes()) {
        Some(encoding) => {
            let (text, _, _) = encoding.decode(bytes);
            text.into_owned()
        }
        None => String::from_utf8_lossy(bytes).to_string(),
    }
}

fn content_type_charset(content_type: &str) -> Option<String> {
    content_type.split(';').find_map(|part| {
        let (key, value) = part.trim().split_once('=')?;
        if key.eq_ignore_ascii_case("charset") {
            Some(value.trim_matches('"').to_ascii_lowercase())
        } else {
            None
        }
    })
}

fn decode_utf16_auto(bytes: &[u8]) -> Result<String> {
    if bytes.starts_with(&[0xff, 0xfe]) {
        return decode_utf16(&bytes[2..], false);
    }
    if bytes.starts_with(&[0xfe, 0xff]) {
        return decode_utf16(&bytes[2..], true);
    }
    decode_utf16(bytes, false)
}

fn decode_utf16(bytes: &[u8], big_endian: bool) -> Result<String> {
    let mut units = Vec::with_capacity(bytes.len() / 2);
    for pair in bytes.chunks_exact(2) {
        let unit = if big_endian {
            u16::from_be_bytes([pair[0], pair[1]])
        } else {
            u16::from_le_bytes([pair[0], pair[1]])
        };
        units.push(unit);
    }
    let mut text = String::from_utf16_lossy(&units);
    if bytes.len() & 1 != 0 {
        text.push(char::REPLACEMENT_CHARACTER);
    }
    Ok(text)
}

fn sample(bytes: &[u8]) -> Vec<u8> {
    bytes.iter().take(4096).copied().collect()
}

fn looks_like_binary(data: &[u8]) -> bool {
    if data.is_empty() {
        return false;
    }
    let mut non_text = 0_usize;
    let mut total = 0_usize;
    let mut index = 0_usize;
    while index < data.len() {
        let byte = data[index];
        total += 1;
        if byte == 0 {
            return true;
        }
        if (0x20..=0x7e).contains(&byte) || matches!(byte, 0x09 | 0x0a | 0x0d) {
            index += 1;
            continue;
        }
        let seq_len = utf8_sequence_length(byte);
        if seq_len > 1
            && index + seq_len <= data.len()
            && valid_utf8_sequence(&data[index..index + seq_len])
        {
            index += seq_len;
            continue;
        }
        non_text += 1;
        index += 1;
    }
    (non_text as f64 / total as f64) > 0.3
}

fn utf8_sequence_length(byte: u8) -> usize {
    if byte & 0x80 == 0x00 {
        1
    } else if byte & 0xe0 == 0xc0 {
        2
    } else if byte & 0xf0 == 0xe0 {
        3
    } else if byte & 0xf8 == 0xf0 {
        4
    } else {
        1
    }
}

fn valid_utf8_sequence(seq: &[u8]) -> bool {
    if seq.len() <= 1 {
        return false;
    }
    seq.iter().skip(1).all(|byte| byte & 0xc0 == 0x80)
}

fn is_mpeg2_ts_buffer(buffer: &[u8]) -> bool {
    const PACKET_SIZE: usize = 188;
    if buffer.len() < PACKET_SIZE {
        return false;
    }
    let packet_count = std::cmp::min(buffer.len() / PACKET_SIZE, 5);
    let sync_count = (0..packet_count)
        .filter(|index| buffer[index * PACKET_SIZE] == 0x47)
        .count();
    sync_count >= 3
}

fn file_uri_to_path(value: &str) -> PathBuf {
    if let Ok(url) = reqwest::Url::parse(value)
        && url.scheme() == "file"
        && let Ok(path) = url.to_file_path()
    {
        return path;
    }
    let stripped = value
        .trim_start_matches("file://")
        .trim_start_matches("file:");
    #[cfg(windows)]
    {
        let mut normalized = stripped.trim_start_matches('/');
        if let Some(rest) = normalized.strip_prefix("?/") {
            normalized = rest;
        }
        PathBuf::from(normalized)
    }
    #[cfg(not(windows))]
    {
        PathBuf::from(stripped)
    }
}

fn path_to_file_uri(path: &Path) -> Option<String> {
    let text = path.to_str()?;
    #[cfg(windows)]
    {
        let text = text.replace('\\', "/");
        let normalized = text.strip_prefix("//?/").unwrap_or(&text);
        Some(format!("file:///{normalized}"))
    }
    #[cfg(not(windows))]
    {
        Some(format!("file://{text}"))
    }
}

fn hex_to_bytes(value: &str) -> Result<Vec<u8>> {
    let value = value.trim();
    let value = value
        .strip_prefix("0x")
        .or_else(|| value.strip_prefix("0X"))
        .unwrap_or(value);
    if value.len() & 1 != 0 {
        return Err(Error::config("hex length must be even"));
    }
    let mut bytes = Vec::with_capacity(value.len() / 2);
    let mut chars = value.chars();
    while let Some(high) = chars.next() {
        let low = chars
            .next()
            .ok_or_else(|| Error::config("hex length must be even"))?;
        let high = hex_value(high).ok_or_else(|| Error::config("hex is invalid"))?;
        let low = hex_value(low).ok_or_else(|| Error::config("hex is invalid"))?;
        bytes.push((high << 4) | low);
    }
    Ok(bytes)
}

fn hex_value(ch: char) -> Option<u8> {
    match ch {
        '0'..='9' => Some(ch as u8 - b'0'),
        'a'..='f' => Some(ch as u8 - b'a' + 10),
        'A'..='F' => Some(ch as u8 - b'A' + 10),
        _ => None,
    }
}

fn base64_decode(value: &str) -> Result<Vec<u8>> {
    crate::base64::decode_base64(value).map_err(Error::config)
}