gosh-dl 0.4.0

A fast, embeddable download engine for Rust. HTTP/HTTPS with multi-connection acceleration and full BitTorrent protocol support.
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//! Bencode Parser
//!
//! This module provides a custom bencode parser that preserves raw bytes
//! for info_hash calculation. While serde_bencode exists, we need raw
//! byte access to calculate SHA-1 hashes of the info dictionary.
//!
//! Bencode format:
//! - Integers:   `i<number>e`        Example: `i42e`
//! - Strings:    `<length>:<data>`   Example: `4:spam`
//! - Lists:      `l<items>e`         Example: `l4:spami42ee`
//! - Dicts:      `d<pairs>e`         Example: `d3:cow3:moo4:spam4:eggse`

use std::collections::BTreeMap;

/// Maximum allowed length for a bencode string (100 MiB)
/// This prevents malicious torrents from causing memory exhaustion
const MAX_STRING_LENGTH: usize = 100 * 1024 * 1024;
use std::fmt;

use crate::error::{EngineError, ProtocolErrorKind, Result};

/// A bencode value
#[derive(Clone, PartialEq, Eq)]
pub enum BencodeValue {
    /// Integer value (can be negative)
    Integer(i64),
    /// Byte string (not necessarily valid UTF-8)
    Bytes(Vec<u8>),
    /// List of values
    List(Vec<BencodeValue>),
    /// Dictionary with byte string keys (sorted by key)
    Dict(BTreeMap<Vec<u8>, BencodeValue>),
}

impl fmt::Debug for BencodeValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Integer(n) => write!(f, "Integer({})", n),
            Self::Bytes(b) => {
                // Try to display as UTF-8 if valid, otherwise show hex
                if let Ok(s) = std::str::from_utf8(b) {
                    if s.len() <= 50 {
                        write!(f, "Bytes(\"{}\")", s)
                    } else {
                        write!(f, "Bytes(\"{}...\" [{} bytes])", &s[..50], b.len())
                    }
                } else {
                    write!(f, "Bytes([{} bytes])", b.len())
                }
            }
            Self::List(l) => f.debug_tuple("List").field(l).finish(),
            Self::Dict(d) => {
                let readable: BTreeMap<String, &BencodeValue> = d
                    .iter()
                    .map(|(k, v)| {
                        let key = String::from_utf8_lossy(k).to_string();
                        (key, v)
                    })
                    .collect();
                f.debug_tuple("Dict").field(&readable).finish()
            }
        }
    }
}

/// Result of parsing bencode, includes the remaining unparsed bytes
pub struct ParseResult<'a> {
    /// The parsed value
    pub value: BencodeValue,
    /// The remaining unparsed bytes
    pub remaining: &'a [u8],
}

impl BencodeValue {
    /// Parse bencode from bytes
    ///
    /// Returns the parsed value and remaining unparsed bytes.
    pub fn parse(data: &[u8]) -> Result<ParseResult<'_>> {
        if data.is_empty() {
            return Err(EngineError::protocol(
                ProtocolErrorKind::BencodeParse,
                "Empty input",
            ));
        }

        match data[0] {
            b'i' => Self::parse_integer(data),
            b'l' => Self::parse_list(data),
            b'd' => Self::parse_dict(data),
            b'0'..=b'9' => Self::parse_bytes(data),
            c => Err(EngineError::protocol(
                ProtocolErrorKind::BencodeParse,
                format!("Invalid bencode type marker: {:?}", c as char),
            )),
        }
    }

    /// Parse a complete bencode value (ensuring no trailing data)
    pub fn parse_exact(data: &[u8]) -> Result<Self> {
        let result = Self::parse(data)?;
        if !result.remaining.is_empty() {
            return Err(EngineError::protocol(
                ProtocolErrorKind::BencodeParse,
                format!("Trailing data: {} bytes", result.remaining.len()),
            ));
        }
        Ok(result.value)
    }

    /// Parse an integer: i<number>e
    fn parse_integer(data: &[u8]) -> Result<ParseResult<'_>> {
        if data.is_empty() || data[0] != b'i' {
            return Err(EngineError::protocol(
                ProtocolErrorKind::BencodeParse,
                "Expected integer",
            ));
        }

        let end = data[1..].iter().position(|&c| c == b'e').ok_or_else(|| {
            EngineError::protocol(ProtocolErrorKind::BencodeParse, "Unterminated integer")
        })? + 1; // Add 1 because we started at index 1

        let num_str = std::str::from_utf8(&data[1..end]).map_err(|_| {
            EngineError::protocol(ProtocolErrorKind::BencodeParse, "Invalid integer encoding")
        })?;

        // Check for invalid formats: leading zeros (except for "0"), negative zero
        if num_str.len() > 1 && num_str.starts_with('0') {
            return Err(EngineError::protocol(
                ProtocolErrorKind::BencodeParse,
                "Invalid integer: leading zero",
            ));
        }
        if num_str == "-0" {
            return Err(EngineError::protocol(
                ProtocolErrorKind::BencodeParse,
                "Invalid integer: negative zero",
            ));
        }
        if num_str.starts_with("-0") && num_str.len() > 2 {
            return Err(EngineError::protocol(
                ProtocolErrorKind::BencodeParse,
                "Invalid integer: leading zero after minus",
            ));
        }

        let value = num_str.parse::<i64>().map_err(|_| {
            EngineError::protocol(ProtocolErrorKind::BencodeParse, "Integer parse error")
        })?;

        Ok(ParseResult {
            value: BencodeValue::Integer(value),
            remaining: &data[end + 1..], // Skip the 'e'
        })
    }

    /// Parse a byte string: <length>:<data>
    fn parse_bytes(data: &[u8]) -> Result<ParseResult<'_>> {
        let colon = data.iter().position(|&c| c == b':').ok_or_else(|| {
            EngineError::protocol(ProtocolErrorKind::BencodeParse, "Expected colon in string")
        })?;

        let len_str = std::str::from_utf8(&data[..colon]).map_err(|_| {
            EngineError::protocol(ProtocolErrorKind::BencodeParse, "Invalid string length")
        })?;

        let len = len_str.parse::<usize>().map_err(|_| {
            EngineError::protocol(
                ProtocolErrorKind::BencodeParse,
                "Invalid string length number",
            )
        })?;

        // Prevent memory exhaustion attacks with extremely large strings
        if len > MAX_STRING_LENGTH {
            return Err(EngineError::protocol(
                ProtocolErrorKind::BencodeParse,
                format!(
                    "String length {} exceeds maximum allowed {} bytes",
                    len, MAX_STRING_LENGTH
                ),
            ));
        }

        let start = colon + 1;
        let end = start + len;

        if end > data.len() {
            return Err(EngineError::protocol(
                ProtocolErrorKind::BencodeParse,
                format!(
                    "String length {} exceeds available data {}",
                    len,
                    data.len() - start
                ),
            ));
        }

        Ok(ParseResult {
            value: BencodeValue::Bytes(data[start..end].to_vec()),
            remaining: &data[end..],
        })
    }

    /// Parse a list: l<items>e
    fn parse_list(data: &[u8]) -> Result<ParseResult<'_>> {
        if data.is_empty() || data[0] != b'l' {
            return Err(EngineError::protocol(
                ProtocolErrorKind::BencodeParse,
                "Expected list",
            ));
        }

        let mut items = Vec::new();
        let mut remaining = &data[1..]; // Skip 'l'

        while !remaining.is_empty() && remaining[0] != b'e' {
            let result = Self::parse(remaining)?;
            items.push(result.value);
            remaining = result.remaining;
        }

        if remaining.is_empty() {
            return Err(EngineError::protocol(
                ProtocolErrorKind::BencodeParse,
                "Unterminated list",
            ));
        }

        Ok(ParseResult {
            value: BencodeValue::List(items),
            remaining: &remaining[1..], // Skip 'e'
        })
    }

    /// Parse a dictionary: d<pairs>e
    fn parse_dict(data: &[u8]) -> Result<ParseResult<'_>> {
        if data.is_empty() || data[0] != b'd' {
            return Err(EngineError::protocol(
                ProtocolErrorKind::BencodeParse,
                "Expected dict",
            ));
        }

        let mut items = BTreeMap::new();
        let mut remaining = &data[1..]; // Skip 'd'
        let mut last_key: Option<Vec<u8>> = None;

        while !remaining.is_empty() && remaining[0] != b'e' {
            // Parse key (must be a string)
            let key_result = Self::parse_bytes(remaining)?;
            let key = match key_result.value {
                BencodeValue::Bytes(k) => k,
                _ => {
                    return Err(EngineError::protocol(
                        ProtocolErrorKind::BencodeParse,
                        "Dict key must be a string",
                    ))
                }
            };

            // Keys must be in sorted order
            if let Some(ref lk) = last_key {
                if &key <= lk {
                    return Err(EngineError::protocol(
                        ProtocolErrorKind::BencodeParse,
                        "Dict keys not in sorted order",
                    ));
                }
            }
            last_key = Some(key.clone());

            remaining = key_result.remaining;

            // Parse value
            let value_result = Self::parse(remaining)?;
            items.insert(key, value_result.value);
            remaining = value_result.remaining;
        }

        if remaining.is_empty() {
            return Err(EngineError::protocol(
                ProtocolErrorKind::BencodeParse,
                "Unterminated dict",
            ));
        }

        Ok(ParseResult {
            value: BencodeValue::Dict(items),
            remaining: &remaining[1..], // Skip 'e'
        })
    }

    /// Encode to bencode bytes
    pub fn encode(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        self.encode_to(&mut buf);
        buf
    }

    /// Encode to an existing buffer
    pub fn encode_to(&self, buf: &mut Vec<u8>) {
        match self {
            Self::Integer(n) => {
                buf.push(b'i');
                buf.extend_from_slice(n.to_string().as_bytes());
                buf.push(b'e');
            }
            Self::Bytes(b) => {
                buf.extend_from_slice(b.len().to_string().as_bytes());
                buf.push(b':');
                buf.extend_from_slice(b);
            }
            Self::List(l) => {
                buf.push(b'l');
                for item in l {
                    item.encode_to(buf);
                }
                buf.push(b'e');
            }
            Self::Dict(d) => {
                buf.push(b'd');
                for (k, v) in d {
                    // Encode key as string
                    buf.extend_from_slice(k.len().to_string().as_bytes());
                    buf.push(b':');
                    buf.extend_from_slice(k);
                    // Encode value
                    v.encode_to(buf);
                }
                buf.push(b'e');
            }
        }
    }

    // Accessor methods

    /// Get as string (UTF-8)
    pub fn as_string(&self) -> Option<&str> {
        match self {
            Self::Bytes(b) => std::str::from_utf8(b).ok(),
            _ => None,
        }
    }

    /// Get as integer
    pub fn as_int(&self) -> Option<i64> {
        match self {
            Self::Integer(n) => Some(*n),
            _ => None,
        }
    }

    /// Get as unsigned integer
    pub fn as_uint(&self) -> Option<u64> {
        match self {
            Self::Integer(n) if *n >= 0 => Some(*n as u64),
            _ => None,
        }
    }

    /// Get as bytes
    pub fn as_bytes(&self) -> Option<&[u8]> {
        match self {
            Self::Bytes(b) => Some(b),
            _ => None,
        }
    }

    /// Get as list
    pub fn as_list(&self) -> Option<&[BencodeValue]> {
        match self {
            Self::List(l) => Some(l),
            _ => None,
        }
    }

    /// Get as mutable list
    pub fn as_list_mut(&mut self) -> Option<&mut Vec<BencodeValue>> {
        match self {
            Self::List(l) => Some(l),
            _ => None,
        }
    }

    /// Get as dict
    pub fn as_dict(&self) -> Option<&BTreeMap<Vec<u8>, BencodeValue>> {
        match self {
            Self::Dict(d) => Some(d),
            _ => None,
        }
    }

    /// Get as mutable dict
    pub fn as_dict_mut(&mut self) -> Option<&mut BTreeMap<Vec<u8>, BencodeValue>> {
        match self {
            Self::Dict(d) => Some(d),
            _ => None,
        }
    }

    /// Get dict value by key
    pub fn get(&self, key: &str) -> Option<&BencodeValue> {
        match self {
            Self::Dict(d) => d.get(key.as_bytes()),
            _ => None,
        }
    }

    /// Get dict value by key (byte key)
    pub fn get_bytes(&self, key: &[u8]) -> Option<&BencodeValue> {
        match self {
            Self::Dict(d) => d.get(key),
            _ => None,
        }
    }

    /// Check if this is a dict
    pub fn is_dict(&self) -> bool {
        matches!(self, Self::Dict(_))
    }

    /// Check if this is a list
    pub fn is_list(&self) -> bool {
        matches!(self, Self::List(_))
    }

    /// Check if this is a string/bytes
    pub fn is_bytes(&self) -> bool {
        matches!(self, Self::Bytes(_))
    }

    /// Check if this is an integer
    pub fn is_int(&self) -> bool {
        matches!(self, Self::Integer(_))
    }
}

/// Find the raw bytes of the "info" dictionary in a torrent file
///
/// This is needed to calculate the info_hash (SHA-1 of the raw info dict)
pub fn find_info_dict_bytes(data: &[u8]) -> Result<&[u8]> {
    // First, parse to validate the structure
    let root = BencodeValue::parse_exact(data)?;
    let dict = root.as_dict().ok_or_else(|| {
        EngineError::protocol(ProtocolErrorKind::InvalidTorrent, "Root is not a dict")
    })?;

    if !dict.contains_key(b"info".as_slice()) {
        return Err(EngineError::protocol(
            ProtocolErrorKind::InvalidTorrent,
            "Missing 'info' key",
        ));
    }

    // Now find the raw bytes of the info dict
    // We need to find "4:info" followed by a dict
    let info_key = b"4:info";

    // Find the position of "4:info" in the data
    let mut pos = 0;
    while pos < data.len() {
        if data[pos..].starts_with(info_key) {
            let info_start = pos + info_key.len();
            if info_start < data.len() && data[info_start] == b'd' {
                // Parse from here to find the end
                let result = BencodeValue::parse(&data[info_start..])?;
                let info_len = data.len() - info_start - result.remaining.len();
                return Ok(&data[info_start..info_start + info_len]);
            }
        }
        pos += 1;
    }

    Err(EngineError::protocol(
        ProtocolErrorKind::InvalidTorrent,
        "Could not locate info dict bytes",
    ))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_integer() {
        let result = BencodeValue::parse(b"i42e").unwrap();
        assert_eq!(result.value, BencodeValue::Integer(42));
        assert!(result.remaining.is_empty());

        let result = BencodeValue::parse(b"i-42e").unwrap();
        assert_eq!(result.value, BencodeValue::Integer(-42));

        let result = BencodeValue::parse(b"i0e").unwrap();
        assert_eq!(result.value, BencodeValue::Integer(0));

        // Invalid: leading zero
        assert!(BencodeValue::parse(b"i03e").is_err());

        // Invalid: negative zero
        assert!(BencodeValue::parse(b"i-0e").is_err());
    }

    #[test]
    fn test_parse_bytes() {
        let result = BencodeValue::parse(b"4:spam").unwrap();
        assert_eq!(result.value, BencodeValue::Bytes(b"spam".to_vec()));
        assert!(result.remaining.is_empty());

        let result = BencodeValue::parse(b"0:").unwrap();
        assert_eq!(result.value, BencodeValue::Bytes(vec![]));

        // Binary data
        let data = b"5:\x00\x01\x02\x03\x04";
        let result = BencodeValue::parse(data).unwrap();
        assert_eq!(result.value, BencodeValue::Bytes(vec![0, 1, 2, 3, 4]));
    }

    #[test]
    fn test_parse_list() {
        let result = BencodeValue::parse(b"l4:spami42ee").unwrap();
        if let BencodeValue::List(items) = result.value {
            assert_eq!(items.len(), 2);
            assert_eq!(items[0], BencodeValue::Bytes(b"spam".to_vec()));
            assert_eq!(items[1], BencodeValue::Integer(42));
        } else {
            panic!("Expected list");
        }

        // Empty list
        let result = BencodeValue::parse(b"le").unwrap();
        assert_eq!(result.value, BencodeValue::List(vec![]));

        // Nested list
        let result = BencodeValue::parse(b"ll4:spamee").unwrap();
        if let BencodeValue::List(items) = result.value {
            assert_eq!(items.len(), 1);
            assert!(matches!(&items[0], BencodeValue::List(_)));
        } else {
            panic!("Expected list");
        }
    }

    #[test]
    fn test_parse_dict() {
        let result = BencodeValue::parse(b"d3:cow3:moo4:spam4:eggse").unwrap();
        if let BencodeValue::Dict(d) = &result.value {
            assert_eq!(d.len(), 2);
            assert_eq!(
                d.get(b"cow".as_slice()),
                Some(&BencodeValue::Bytes(b"moo".to_vec()))
            );
            assert_eq!(
                d.get(b"spam".as_slice()),
                Some(&BencodeValue::Bytes(b"eggs".to_vec()))
            );
        } else {
            panic!("Expected dict");
        }

        // Empty dict
        let result = BencodeValue::parse(b"de").unwrap();
        assert_eq!(result.value, BencodeValue::Dict(BTreeMap::new()));
    }

    #[test]
    fn test_encode() {
        // Integer
        let v = BencodeValue::Integer(42);
        assert_eq!(v.encode(), b"i42e");

        // String
        let v = BencodeValue::Bytes(b"spam".to_vec());
        assert_eq!(v.encode(), b"4:spam");

        // List
        let v = BencodeValue::List(vec![
            BencodeValue::Bytes(b"spam".to_vec()),
            BencodeValue::Integer(42),
        ]);
        assert_eq!(v.encode(), b"l4:spami42ee");

        // Dict
        let mut d = BTreeMap::new();
        d.insert(b"cow".to_vec(), BencodeValue::Bytes(b"moo".to_vec()));
        d.insert(b"spam".to_vec(), BencodeValue::Bytes(b"eggs".to_vec()));
        let v = BencodeValue::Dict(d);
        assert_eq!(v.encode(), b"d3:cow3:moo4:spam4:eggse");
    }

    #[test]
    fn test_roundtrip() {
        // Simple roundtrip test with nested structures
        // Dict with: "items" (list), "name" (string), "value" (integer)
        let original = b"d5:itemsli1ei2ei3ee4:name4:test5:valuei42ee";

        let value = BencodeValue::parse_exact(original).unwrap();
        let encoded = value.encode();
        assert_eq!(encoded, original.to_vec());

        // Verify the parsed structure
        assert_eq!(value.get("name").and_then(|v| v.as_string()), Some("test"));
        assert_eq!(value.get("value").and_then(|v| v.as_int()), Some(42));
        assert_eq!(
            value
                .get("items")
                .and_then(|v| v.as_list())
                .map(|l| l.len()),
            Some(3)
        );
    }

    #[test]
    fn test_accessor_methods() {
        // Keys must be in sorted byte order: "list" < "name" < "num"
        let data = b"d4:listli1ei2ei3ee4:name4:test3:numi42ee";
        let value = BencodeValue::parse_exact(data).unwrap();

        assert_eq!(value.get("num").and_then(|v| v.as_int()), Some(42));
        assert_eq!(value.get("name").and_then(|v| v.as_string()), Some("test"));
        assert_eq!(
            value.get("list").and_then(|v| v.as_list()).map(|l| l.len()),
            Some(3)
        );
        assert!(value.get("missing").is_none());
    }
}