Kget 1.6.3

A powerful and versatile download manager and library
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! Simple HTTP/HTTPS download functionality.
//!
//! This module provides basic download capabilities with automatic retry,
//! progress tracking, and ISO integrity verification.
//!
//! For advanced features like parallel connections and resume support,
//! see [`AdvancedDownloader`](crate::AdvancedDownloader).
//!
//! # Example
//!
//! ```rust,no_run
//! use kget::{download, DownloadOptions, ProxyConfig, Optimizer};
//!
//! let options = DownloadOptions {
//!     quiet_mode: false,
//!     output_path: Some("./file.zip".to_string()),
//!     verify_iso: false,
//!     expected_sha256: None,
//! };
//!
//! download(
//!     "https://example.com/file.zip",
//!     ProxyConfig::default(),
//!     Optimizer::new(),
//!     options,
//!     None,
//! ).unwrap();
//! ```

use crate::DownloadOptions;
use crate::config::ProxyConfig;
use crate::optimization::Optimizer;
use crate::progress::create_progress_bar;
use crate::utils::{self, print};
use humansize::{DECIMAL, format_size};
use mime::Mime;
use reqwest::blocking::Client;
use reqwest::header::{CONTENT_DISPOSITION, CONTENT_LENGTH, CONTENT_TYPE};
use sha2::Digest;
use std::error::Error;
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

const MAX_RETRIES: u32 = 3;
const RETRY_DELAY: Duration = Duration::from_secs(2);

/// Check if there's enough disk space for the download.
///
/// # Arguments
///
/// * `path` - Target file path
/// * `required_size` - Required space in bytes
///
/// # Errors
///
/// Returns an error if available space is less than required.
pub fn check_disk_space(
    path: &Path,
    required_size: u64,
) -> Result<(), Box<dyn Error + Send + Sync>> {
    let dir = path.parent().unwrap_or(Path::new("."));
    let available_space = fs2::available_space(dir)?;

    if available_space < required_size {
        return Err(format!(
            "Insufficient disk space. Required: {}, Available: {}",
            format_size(required_size, DECIMAL),
            format_size(available_space, DECIMAL)
        )
        .into());
    }
    Ok(())
}

/// Validate that a filename is safe and valid.
///
/// # Errors
///
/// Returns an error if the filename:
/// - Contains directory separators
/// - Is empty
pub fn validate_filename(filename: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
    if filename.contains('/') || filename.contains('\\') {
        return Err("Filename cannot contain directory separators".into());
    }
    if filename.is_empty() {
        return Err("Filename cannot be empty".into());
    }
    Ok(())
}

/// Download a file from a URL with automatic retry and progress tracking.
///
/// This is the simple download function for basic use cases. For parallel
/// connections and resume support, use [`AdvancedDownloader`](crate::AdvancedDownloader).
///
/// # Arguments
///
/// * `target` - URL to download
/// * `proxy` - Proxy configuration (use `ProxyConfig::default()` for no proxy)
/// * `_optimizer` - Optimizer instance (reserved for future use)
/// * `options` - Download options (quiet mode, output path, ISO verification)
/// * `status_callback` - Optional callback for status messages
///
/// # Example
///
/// ```rust,no_run
/// use kget::{download, DownloadOptions, ProxyConfig, Optimizer};
///
/// download(
///     "https://releases.ubuntu.com/22.04/ubuntu-22.04-desktop-amd64.iso",
///     ProxyConfig::default(),
///     Optimizer::new(),
///     DownloadOptions {
///         quiet_mode: false,
///         output_path: None, // Uses filename from URL
///         verify_iso: true,  // Verify SHA256 after download
///         expected_sha256: None,
///     },
///     None,
/// ).unwrap();
/// ```
///
/// # Errors
///
/// Returns an error if:
/// - Network connection fails after MAX_RETRIES attempts
/// - HTTP response indicates an error
/// - Insufficient disk space
/// - File cannot be created
pub fn download(
    target: &str,
    proxy: ProxyConfig,
    optimizer: Optimizer,
    options: DownloadOptions,
    status_callback: Option<&(dyn Fn(String) + Send + Sync)>,
) -> Result<(), Box<dyn Error + Send + Sync>> {
    let quiet_mode = options.quiet_mode;

    let mut client_builder = Client::builder()
        .timeout(Duration::from_secs(30))
        .user_agent(concat!("KGet/", env!("CARGO_PKG_VERSION")))
        .no_gzip()
        .no_deflate();

    if proxy.enabled {
        if let Some(proxy_url) = &proxy.url {
            let proxy_client = match proxy.proxy_type {
                crate::config::ProxyType::Http => reqwest::Proxy::http(proxy_url),
                crate::config::ProxyType::Https => reqwest::Proxy::https(proxy_url),
                crate::config::ProxyType::Socks5 => reqwest::Proxy::all(proxy_url),
            };
            if let Ok(mut proxy_client) = proxy_client {
                if let (Some(username), Some(password)) = (&proxy.username, &proxy.password) {
                    proxy_client = proxy_client.basic_auth(username, password);
                }
                client_builder = client_builder.proxy(proxy_client);
            }
        }
    }

    let client = client_builder.build()?;

    let mut retries = 0;
    let response = loop {
        match client.get(target).send() {
            Ok(resp) => break resp,
            Err(e) => {
                retries += 1;
                if retries >= MAX_RETRIES {
                    return Err(format!("Failed after {} attempts: {}", MAX_RETRIES, e).into());
                }
                print(
                    &format!(
                        "Attempt {} failed, retrying in {} seconds...",
                        retries,
                        RETRY_DELAY.as_secs()
                    ),
                    quiet_mode,
                );
                std::thread::sleep(RETRY_DELAY);
            }
        }
    };

    print(
        &format!("HTTP request sent... {}", response.status()),
        quiet_mode,
    );

    if !response.status().is_success() {
        return Err(format!("HTTP error: {}", response.status()).into());
    }

    let content_length = response
        .headers()
        .get(CONTENT_LENGTH)
        .and_then(|ct_len| ct_len.to_str().ok())
        .and_then(|s| s.parse::<u64>().ok());

    let content_type = response
        .headers()
        .get(CONTENT_TYPE)
        .and_then(|ct| ct.to_str().ok())
        .and_then(|s| s.parse::<Mime>().ok());

    let server_filename = response
        .headers()
        .get(CONTENT_DISPOSITION)
        .and_then(|v| v.to_str().ok())
        .and_then(parse_content_disposition_filename);

    if let Some(len) = content_length {
        print(
            &format!("Length: {} ({})", len, format_size(len, DECIMAL)),
            quiet_mode,
        );
    } else {
        print("Length: unknown", quiet_mode);
    }

    if let Some(ref ct) = content_type {
        print(&format!("Type: {}", ct), quiet_mode);
    }

    let is_iso = target.to_lowercase().ends_with(".iso")
        || content_type.as_ref().map_or(false, |ct| {
            ct.essence_str() == "application/x-iso9001"
                || ct.essence_str() == "application/x-cd-image"
        });

    if is_iso {
        print(
            "ISO file detected. Ensuring raw download to prevent corruption...",
            quiet_mode,
        );
    }

    let tentative_path: PathBuf;

    if let Some(output_arg_str) = options.output_path {
        let user_path = PathBuf::from(output_arg_str.clone());

        let is_target_dir =
            user_path.is_dir() || output_arg_str.ends_with(std::path::MAIN_SEPARATOR);

        if is_target_dir {
            let base_filename = utils::get_filename_from_url_or_default(target, "downloaded_file");
            validate_filename(&base_filename)?;
            tentative_path = user_path.join(base_filename);
        } else {
            if let Some(file_name_osstr) = user_path.file_name() {
                if let Some(file_name_str) = file_name_osstr.to_str() {
                    if file_name_str.is_empty() {
                        return Err(format!(
                            "Invalid output path, does not specify a file name: {}",
                            user_path.display()
                        )
                        .into());
                    }
                    validate_filename(file_name_str)?;
                } else {
                    return Err("Output filename contains invalid characters (non-UTF-8)".into());
                }
            } else {
                return Err(format!(
                    "Invalid output path, does not specify a file name: {}",
                    user_path.display()
                )
                .into());
            }
            tentative_path = user_path;
        }
    } else {
        let base_filename = if let Some(ref name) = server_filename {
            name.clone()
        } else {
            utils::get_filename_from_url_or_default(target, "downloaded_file")
        };
        validate_filename(&base_filename)?;
        tentative_path = PathBuf::from(base_filename);
    }

    let final_path: PathBuf = if tentative_path.is_absolute() {
        tentative_path
    } else {
        let current_dir = std::env::current_dir()
            .map_err(|e| format!("Failed to get current directory: {}", e))?;
        current_dir.join(tentative_path)
    };

    if let Some(parent_dir) = final_path.parent() {
        if !parent_dir.as_os_str().is_empty()
            && parent_dir != Path::new("/")
            && !parent_dir.exists()
        {
            std::fs::create_dir_all(parent_dir).map_err(|e| {
                format!("Failed to create directory {}: {}", parent_dir.display(), e)
            })?;
            if !quiet_mode {
                print(
                    &format!("Created directory: {}", parent_dir.display()),
                    quiet_mode,
                );
            }
        }
    }

    if !quiet_mode {
        print(&format!("Saving to: {}", final_path.display()), quiet_mode);
    }

    if let Some(len) = content_length {
        check_disk_space(&final_path, len)?;
    }

    let mut dest = File::create(&final_path)
        .map_err(|e| format!("Failed to create file {}: {}", final_path.display(), e))?;

    let response_content_length = response.content_length();
    let progress_bar_filename = final_path
        .file_name()
        .unwrap_or_default()
        .to_string_lossy()
        .into_owned();
    let progress = create_progress_bar(
        quiet_mode,
        progress_bar_filename,
        response_content_length,
        false,
    );

    let mut source = response.take(response_content_length.unwrap_or(u64::MAX));
    let mut buffered_reader = progress.wrap_read(&mut source);

    // Stream data instead of reading all into memory
    let mut buffer = [0u8; 8192];
    let mut downloaded: u64 = 0;
    let started_at = Instant::now();
    loop {
        let n = buffered_reader.read(&mut buffer)?;
        if n == 0 {
            break;
        }
        dest.write_all(&buffer[..n])?;
        downloaded += n as u64;

        if let Some(total) = response_content_length {
            if let Some(cb) = status_callback {
                let percent = downloaded as f64 / total.max(1) as f64 * 100.0;
                cb(format!("PROGRESS: {:.1}% ({}/{})", percent, downloaded, total));
            }
        }

        throttle_download(downloaded, started_at, optimizer.speed_limit);
    }

    progress.finish_with_message("Download completed\n");

    if is_iso && options.verify_iso {
        verify_file_sha256(
            &final_path,
            options.expected_sha256.as_deref(),
            status_callback,
        )?;
    } else if let Some(expected) = options.expected_sha256.as_deref() {
        verify_file_sha256(&final_path, Some(expected), status_callback)?;
    }

    Ok(())
}

/// Parse the `filename` or `filename*` from a `Content-Disposition` header value.
///
/// Prefers the RFC 5987 `filename*` form (percent-encoded, with charset) over
/// the plain `filename` form.  Returns `None` if neither is present or valid.
pub fn parse_content_disposition_filename(header_value: &str) -> Option<String> {
    let mut plain: Option<String> = None;

    for part in header_value.split(';') {
        let part = part.trim();

        // filename*= (RFC 5987) takes priority
        if let Some(val) = part.strip_prefix("filename*=") {
            let val = val.trim().trim_matches('"');
            // Format: charset'language'encoded-value
            let mut parts = val.splitn(3, '\'');
            let _charset = parts.next();
            let _language = parts.next();
            if let Some(encoded) = parts.next() {
                if let Ok(decoded) = urlencoding::decode(encoded) {
                    let name = decoded.into_owned();
                    if !name.is_empty() {
                        return Some(name);
                    }
                }
            }
        }

        // Plain filename= (fallback)
        if plain.is_none() {
            if let Some(val) = part.strip_prefix("filename=") {
                let name = val.trim().trim_matches('"').to_string();
                if !name.is_empty() {
                    plain = Some(name);
                }
            }
        }
    }

    plain
}

fn throttle_download(downloaded: u64, started_at: Instant, speed_limit: Option<u64>) {
    let Some(limit) = speed_limit else { return };
    if limit == 0 {
        return;
    }

    let expected_elapsed = Duration::from_secs_f64(downloaded as f64 / limit as f64);
    let actual_elapsed = started_at.elapsed();
    if expected_elapsed > actual_elapsed {
        std::thread::sleep(expected_elapsed - actual_elapsed);
    }
}

/// Verify the integrity of an ISO file by calculating its SHA-256 hash.
///
/// After download, this function calculates the SHA-256 checksum of the file
/// and displays it for manual comparison with the source.
///
/// # Arguments
///
/// * `path` - Path to the ISO file
/// * `callback` - Optional callback for status messages
///
/// # Example
///
/// ```rust,no_run
/// use kget::verify_iso_integrity;
/// use std::path::Path;
///
/// verify_iso_integrity(
///     Path::new("ubuntu-22.04-desktop-amd64.iso"),
///     Some(&|msg| println!("Status: {}", msg)),
/// ).unwrap();
/// ```
///
/// # Output
///
/// Prints the SHA256 hash to stdout and sends it via callback if provided.
pub fn verify_iso_integrity(
    path: &Path,
    callback: Option<&(dyn Fn(String) + Send + Sync)>,
) -> Result<(), Box<dyn Error + Send + Sync>> {
    verify_file_sha256(path, None, callback).map(|_| ())
}

/// Calculate a file SHA-256 hash and optionally compare it with an expected value.
pub fn verify_file_sha256(
    path: &Path,
    expected_hash: Option<&str>,
    callback: Option<&(dyn Fn(String) + Send + Sync)>,
) -> Result<String, Box<dyn Error + Send + Sync>> {
    let msg = "Calculating SHA256 hash... (this may take a while for large ISOs)";
    if let Some(cb) = callback {
        cb(msg.to_string());
    }
    println!("{}", msg);

    let mut file = File::open(path)?;
    let mut hasher = sha2::Sha256::new();
    let mut buffer = [0; 8192];
    loop {
        let n = file.read(&mut buffer)?;
        if n == 0 {
            break;
        }
        hasher.update(&buffer[..n]);
    }
    let hash = hex::encode(hasher.finalize());

    let msg_done = "Integrity check finished.";
    if let Some(cb) = callback {
        cb(msg_done.to_string());
    }
    println!("{}", msg_done);

    let msg_hash = format!("SHA256: {}", hash);
    if let Some(cb) = callback {
        cb(msg_hash.clone());
    }
    println!("SHA256: {}", hash);

    if let Some(expected_hash) = expected_hash {
        let expected_hash = expected_hash.trim().to_ascii_lowercase();
        if hash != expected_hash {
            return Err(
                format!("SHA256 mismatch: expected {}, got {}", expected_hash, hash).into(),
            );
        }

        let msg_match = "SHA256 matches expected hash.";
        if let Some(cb) = callback {
            cb(msg_match.to_string());
        }
        println!("{}", msg_match);
    } else {
        println!("You can compare this hash with the one provided by the source.");
    }

    Ok(hash)
}