aria2-core 0.2.2

High-performance download engine core: multi-protocol segmented downloads, rate limiting, config management, 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
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
use std::fs;
use std::path::Path;
use std::sync::RwLock;
use std::time::{Duration, SystemTime};

use crate::error::{Aria2Error, Result};
use crate::http::cookie::Cookie;
use serde::{Deserialize, Serialize};

// ==================== Existing CookieStorage ====================

/// Thread-safe cookie storage using RwLock for concurrent access.
///
/// This is the original cookie storage that works with the `Cookie` struct
/// from `cookie.rs`, providing add/find/expire operations with host+path matching.
pub struct CookieStorage {
    cookies: RwLock<Vec<Cookie>>,
}

impl CookieStorage {
    pub fn new() -> Self {
        Self {
            cookies: RwLock::new(Vec::new()),
        }
    }

    pub fn add(&self, cookie: Cookie) {
        let mut cookies = self.cookies.write().unwrap_or_else(|e| e.into_inner());
        if let Some(pos) = cookies.iter().position(|c| c == &cookie) {
            cookies[pos] = cookie;
        } else {
            cookies.push(cookie);
        }
    }

    pub fn find_cookies(&self, host: &str, path: &str, secure: bool) -> Vec<Cookie> {
        let cookies = self.cookies.read().unwrap_or_else(|e| e.into_inner());
        let date = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs() as i64;
        cookies
            .iter()
            .filter(|c| c.match_request(host, path, date, secure))
            .cloned()
            .collect()
    }

    pub fn find_cookies_for_url(&self, url: &reqwest::Url) -> Vec<Cookie> {
        let host = url.host_str().unwrap_or("");
        let path = url.path();
        let scheme = url.scheme();
        let secure = scheme == "https";
        self.find_cookies(host, path, secure)
    }

    pub fn expire_cookies(&self, base_time: i64) -> usize {
        let mut cookies = self.cookies.write().unwrap_or_else(|e| e.into_inner());
        let before = cookies.len();
        cookies.retain(|c| !c.is_expired(base_time));
        before - cookies.len()
    }

    pub fn count(&self) -> usize {
        self.cookies.read().unwrap_or_else(|e| e.into_inner()).len()
    }

    pub fn load_file(&self, path: &Path) -> Result<usize> {
        let data = fs::read_to_string(path).map_err(|e| Aria2Error::Io(e.to_string()))?;
        let parsed = crate::http::ns_cookie_parser::NsCookieParser::parse_str(&data)?;
        let n = parsed.len();
        let mut cookies = self.cookies.write().unwrap_or_else(|e| e.into_inner());
        for cookie in parsed {
            if let Some(pos) = cookies.iter().position(|c| *c == cookie) {
                cookies[pos] = cookie;
            } else {
                cookies.push(cookie);
            }
        }
        Ok(n)
    }

    pub fn save_file(&self, path: &Path) -> Result<()> {
        let cookies = self.cookies.read().unwrap_or_else(|e| e.into_inner());
        let mut lines = Vec::with_capacity(cookies.len() + 3);
        lines.push("# Netscape HTTP Cookie File".to_string());
        lines.push("# This file is generated by aria2-rust".to_string());
        for c in cookies.iter() {
            lines.push(c.to_netscape_line());
        }
        fs::write(path, lines.join("\n")).map_err(|e| Aria2Error::Io(e.to_string()))?;
        Ok(())
    }

    pub fn clear(&self) {
        self.cookies
            .write()
            .unwrap_or_else(|e| e.into_inner())
            .clear();
    }

    pub fn to_header_string(&self, host: &str, path: &str, secure: bool) -> String {
        let cookies = self.find_cookies(host, path, secure);
        if cookies.is_empty() {
            return String::new();
        }
        cookies
            .iter()
            .map(|c| format!("{}={}", c.name, c.value))
            .collect::<Vec<_>>()
            .join("; ")
    }

    pub fn is_empty(&self) -> bool {
        self.cookies
            .read()
            .unwrap_or_else(|e| e.into_inner())
            .is_empty()
    }
}

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

// ==================== Enhanced CookieJar (J4) ====================

/// An enhanced cookie representation using SystemTime for expiration tracking.
///
/// This struct provides URL-based matching, Set-Cookie header parsing,
/// and serialization. It is designed to work alongside the existing `Cookie`
/// from `cookie.rs` while adding SystemTime-based expiration and simpler
/// URL-based matching.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JarCookie {
    /// Cookie name
    pub name: String,
    /// Cookie value
    pub value: String,
    /// Domain this cookie belongs to (e.g., "example.com")
    pub domain: String,
    /// Path scope (usually "/")
    pub path: String,
    /// Expiration time; None means session cookie (no persistent expiry)
    pub expires: Option<SystemTime>,
    /// Only send over HTTPS connections
    pub secure: bool,
    /// Not accessible to JavaScript (client-side only)
    pub http_only: bool,
    /// When this cookie was created
    pub creation_time: SystemTime,
}

impl JarCookie {
    /// Create a new basic session cookie.
    ///
    /// # Arguments
    ///
    /// * `name` - Cookie name
    /// * `value` - Cookie value
    /// * `domain` - Domain the cookie belongs to
    pub fn new(name: &str, value: &str, domain: &str) -> Self {
        Self {
            name: name.to_string(),
            value: value.to_string(),
            domain: domain.to_string(),
            path: "/".to_string(),
            expires: None,
            secure: false,
            http_only: false,
            creation_time: SystemTime::now(),
        }
    }

    /// Check whether this cookie should be sent for the given URL.
    ///
    /// Matching rules:
    /// 1. Secure cookies are only sent over HTTPS (`is_secure = true`)
    /// 2. The URL must contain the cookie's domain
    /// 3. The URL must match the cookie's path scope
    /// 4. The cookie must not have expired
    ///
    /// # Arguments
    ///
    /// * `url` - The request URL string to match against
    /// * `is_secure` - Whether the connection uses HTTPS
    ///
    /// # Returns
    ///
    /// `true` if this cookie should be included in requests to the given URL.
    pub fn matches_url(&self, url: &str, is_secure: bool) -> bool {
        // Secure flag check: secure cookies only over HTTPS
        if self.secure && !is_secure {
            return false;
        }

        // Domain matching: URL must contain the cookie's domain
        let url_lower = url.to_lowercase();
        let domain_lower = self.domain.to_lowercase();
        if !url_lower.contains(&domain_lower) && !domain_lower.is_empty() {
            return false;
        }

        // Path matching: if path is "/", it matches everything
        if self.path != "/" {
            // Strip leading slash from path for comparison against URL path portion
            let path_clean = self.path.trim_start_matches('/');
            if !url_lower.contains(&path_clean.to_lowercase()) {
                // For non-root paths, require at least a partial match
                // Allow if the domain already matched and path is a prefix of URL path
                if let Some(after_domain) = url_lower.find(&domain_lower) {
                    let rest = &url_lower[after_domain + domain_lower.len()..];
                    if !rest.starts_with(&format!("/{}", path_clean.to_lowercase()))
                        && !rest.starts_with(&format!("/{}", self.path.to_lowercase()))
                    {
                        return false;
                    }
                }
            }
        }

        // Expiry check: remove expired cookies
        if let Some(expires) = self.expires
            && SystemTime::now() > expires
        {
            return false;
        }

        true
    }

    /// Format this cookie as a Set-Cookie header value string.
    ///
    /// Produces output like: `name=value; Domain=example.com; Path=/`
    pub fn to_header_value(&self) -> String {
        let mut s = format!("{}={}", self.name, self.value);
        if !self.domain.is_empty() {
            s.push_str(&format!("; Domain={}", self.domain));
        }
        s.push_str(&format!("; Path={}", self.path));
        if let Some(_expires) = self.expires {
            // Use simplified date formatting for expires attribute
            if let Ok(_dur) = _expires.duration_since(SystemTime::UNIX_EPOCH) {
                // Approximate HTTP-date format
                s.push_str(&format!(
                    "; Expires={}",
                    format_systemtime_as_http_date(_expires)
                ));
            }
        }
        if self.secure {
            s.push_str("; Secure");
        }
        if self.http_only {
            s.push_str("; HttpOnly");
        }
        s
    }

    /// Parse a cookie from a Set-Cookie response header value.
    ///
    /// Supports standard attributes:
    /// - `Domain=` - cookie domain scope
    /// - `Path=` - cookie path scope
    /// - `Expires=` - expiration timestamp (RFC 7231 / RFC 850 / asctime formats)
    /// - `Secure` - HTTPS-only flag
    /// - `HttpOnly` - JavaScript-inaccessible flag
    /// - `Max-Age=` - relative expiration in seconds
    ///
    /// # Arguments
    ///
    /// * `header_value` - The raw Set-Cookie header value string
    ///
    /// # Returns
    ///
    /// `Some(JarCookie)` on successful parse, `None` if the header is malformed.
    ///
    /// # Example
    ///
    /// ```
    /// use aria2_core::http::cookie_storage::JarCookie;
    ///
    /// let cookie = JarCookie::parse_set_cookie(
    ///     "session=abc123; Domain=example.com; Path=/; Secure; HttpOnly"
    /// ).unwrap();
    /// assert_eq!(cookie.name, "session");
    /// assert_eq!(cookie.domain, "example.com");
    /// assert!(cookie.secure);
    /// assert!(cookie.http_only);
    /// ```
    pub fn parse_set_cookie(header_value: &str) -> Option<Self> {
        let header = header_value.trim();
        if header.is_empty() {
            return None;
        }

        // Split into name=value part and attributes
        let parts: Vec<&str> = header.split(';').collect();
        if parts.is_empty() {
            return None;
        }

        // Parse name=value (first part before any semicolon)
        let nv: Vec<&str> = parts[0].trim().splitn(2, '=').collect();
        if nv.len() != 2 {
            return None;
        }

        let name = nv[0].trim();
        let value = nv[1].trim();
        if name.is_empty() {
            return None;
        }

        let mut cookie = Self::new(name, value, "");

        // Parse remaining attributes
        for attr in &parts[1..] {
            let attr = attr.trim();
            if attr.is_empty() {
                continue;
            }
            let kv: Vec<&str> = attr.splitn(2, '=').collect();
            match kv[0].trim().to_lowercase().as_str() {
                "domain" if kv.len() > 1 => {
                    cookie.domain = kv[1].trim().to_string();
                }
                "path" if kv.len() > 1 => {
                    cookie.path = kv[1].trim().to_string();
                }
                "max-age" if kv.len() > 1 => {
                    // Max-Age takes precedence over Expires per RFC 6265
                    if let Ok(secs) = kv[1].trim().parse::<u64>() {
                        cookie.expires = Some(SystemTime::now() + Duration::from_secs(secs));
                    }
                }
                "expires" if kv.len() > 1 => {
                    // Only set if not already set by Max-Age
                    if cookie.expires.is_none()
                        && let Ok(dt) = parse_http_date(kv[1].trim())
                    {
                        cookie.expires = Some(dt);
                    }
                }
                "secure" => {
                    cookie.secure = true;
                }
                "httponly" => {
                    cookie.http_only = true;
                }
                _ => {} // Ignore unknown attributes
            }
        }

        Some(cookie)
    }
}

impl PartialEq for JarCookie {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name && self.domain == other.domain && self.path == other.path
    }
}

// ==================== CookieJar Storage ====================

/// A cookie jar that stores cookies and provides URL-based matching for HTTP requests.
///
/// `CookieJar` manages a collection of `JarCookie` instances, supporting:
/// - Storing cookies received from Set-Cookie response headers
/// - Retrieving matching cookies for outgoing requests by URL
/// - Generating Cookie header strings from matching cookies
/// - Loading cookies from Netscape/Mozilla cookie file format
/// - Cleaning up expired cookies
///
/// # Example
///
/// ```rust,no_run
/// use aria2_core::http::cookie_storage::{CookieJar, JarCookie};
///
/// let mut jar = CookieJar::new();
///
/// // Store a cookie from a Set-Cookie response header
/// if let Some(cookie) = JarCookie::parse_set_cookie("sid=abc; Domain=example.com") {
///     jar.store(cookie);
/// }
///
/// // Get cookies for an outgoing request
/// if let Some(header) = jar.cookie_header_for_url("https://example.com/api", true) {
///     println!("Cookie: {}", header); // "sid=abc"
/// }
/// ```
pub struct CookieJar {
    /// Internal cookie storage - made pub for session persistence serialization
    pub cookies: Vec<JarCookie>,
}

impl CookieJar {
    /// Create a new empty cookie jar.
    pub fn new() -> Self {
        Self {
            cookies: Vec::new(),
        }
    }

    /// Store a cookie received from a Set-Cookie response header.
    ///
    /// If a cookie with the same name and domain already exists, it is replaced.
    /// This implements the update semantics defined in RFC 6265 Section 5.3.
    pub fn store(&mut self, cookie: JarCookie) {
        // Remove existing cookie with same name+domain (update semantics)
        self.cookies
            .retain(|c| !(c.name == cookie.name && c.domain == cookie.domain));
        self.cookies.push(cookie);
    }

    /// Get all cookies that match the given URL for sending in a Cookie request header.
    ///
    /// Filters stored cookies based on domain, path, security, and expiration status.
    ///
    /// # Arguments
    ///
    /// * `url` - The request URL string
    /// * `is_secure` - Whether the connection uses HTTPS
    ///
    /// # Returns
    ///
    /// A vector of matching `JarCookie` clones.
    pub fn get_cookies_for_url(&self, url: &str, is_secure: bool) -> Vec<JarCookie> {
        self.cookies
            .iter()
            .filter(|c| c.matches_url(url, is_secure))
            .cloned()
            .collect()
    }

    /// Format all matching cookies as a Cookie header value string.
    ///
    /// Produces output like `"name1=val1; name2=val2"` suitable for the
    /// HTTP Cookie request header. Returns `None` if no cookies match.
    ///
    /// # Arguments
    ///
    /// * `url` - The request URL string
    /// * `is_secure` - Whether the connection uses HTTPS
    ///
    /// # Returns
    ///
    /// `Some(header_string)` if matching cookies exist, `None` otherwise.
    pub fn cookie_header_for_url(&self, url: &str, is_secure: bool) -> Option<String> {
        let matching = self.get_cookies_for_url(url, is_secure);
        if matching.is_empty() {
            return None;
        }
        let header: String = matching
            .iter()
            .map(|c| format!("{}={}", c.name, c.value))
            .collect::<Vec<_>>()
            .join("; ");
        Some(header)
    }

    /// Remove all expired cookies from the jar.
    ///
    /// Session cookies (those without an expiration time) are never removed
    /// by this method — they persist until the session ends or explicitly deleted.
    ///
    /// # Returns
    ///
    /// The number of cookies that were removed.
    pub fn cleanup_expired(&mut self) -> usize {
        let before = self.cookies.len();
        self.cookies.retain(|c| {
            if let Some(exp) = c.expires {
                SystemTime::now() <= exp
            } else {
                true // No expiry = session cookie, keep it
            }
        });
        before - self.cookies.len()
    }

    /// Load cookies from a Netscape/Mozilla cookie file.
    ///
    /// The Netscape cookie file format uses tab-separated fields:
    /// ```text
    /// # Netscape HTTP Cookie File
    ///
    /// .example.com    TRUE    /    FALSE    0    session-cookie    value
    /// ```
    ///
    /// Fields: domain, include_subdomains, path, is_secure, expires_timestamp, name, value
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the Netscape-format cookie file
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be read.
    pub fn load_netscape_file(&mut self, path: &Path) -> Result<usize> {
        let content = fs::read_to_string(path).map_err(|e| Aria2Error::Io(e.to_string()))?;
        let mut loaded = 0;

        for line in content.lines() {
            // Skip comments, empty lines, and the header line
            if line.starts_with('#') || line.starts_with('\n') || line.is_empty() {
                continue;
            }

            let fields: Vec<&str> = line.split('\t').collect();
            if fields.len() >= 7 {
                // Parse the 7+ tab-separated fields
                let domain = fields[0].trim();
                // fields[1]: include_subdomains (TRUE/FALSE) — not used for matching here
                let path = fields[2].trim();
                let secure = fields[3] == "TRUE";
                // fields[4]: expires timestamp (Unix epoch), 0 = session cookie
                let expires = fields[4].trim().parse::<i64>().ok().and_then(|ts| {
                    if ts > 0 {
                        Some(SystemTime::UNIX_EPOCH + Duration::from_secs(ts as u64))
                    } else {
                        None // Session cookie
                    }
                });
                let name = fields[5].trim().to_string();
                let value = if fields.len() > 7 {
                    // Value may contain tabs if there are extra fields; join remainder
                    fields[6..].join("\t")
                } else {
                    fields[6].trim().to_string()
                };

                let cookie = JarCookie {
                    name,
                    value,
                    domain: domain.to_string(),
                    path: path.to_string(),
                    expires,
                    secure,
                    http_only: false, // Netscape format doesn't track HttpOnly
                    creation_time: SystemTime::now(),
                };

                self.cookies.push(cookie);
                loaded += 1;
            }
        }

        Ok(loaded)
    }

    /// Return the number of cookies currently stored in the jar.
    pub fn len(&self) -> usize {
        self.cookies.len()
    }

    /// Check if the jar contains no cookies.
    pub fn is_empty(&self) -> bool {
        self.cookies.is_empty()
    }

    /// Remove all cookies from the jar.
    pub fn clear(&mut self) {
        self.cookies.clear();
    }
}

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

// ==================== HTTP Date Helpers ====================

/// Format a SystemTime as an HTTP-date string (RFC 7231 IMF-fixdate).
///
/// Produces output like: `Sun, 06 Nov 1994 08:49:37 GMT`
fn format_systemtime_as_http_date(time: SystemTime) -> String {
    const DAYS: [&str; 7] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    const MONTHS: [&str; 12] = [
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
    ];

    let dur = time
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap_or_default();
    let epoch = dur.as_secs();

    let days_since_epoch = (epoch / 86400) as u32;
    let mut year = 1970u32;
    let mut remaining = days_since_epoch;

    loop {
        let leap =
            (year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400);
        let days_in_year = if leap { 366 } else { 365 };
        if remaining < days_in_year {
            break;
        }
        remaining -= days_in_year;
        year += 1;
    }

    let mdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    let mut month = 0u32;
    while month < 12 {
        let dim = if month == 1
            && ((year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400))
        {
            29
        } else {
            mdays[month as usize]
        };
        if remaining < dim {
            break;
        }
        remaining -= dim;
        month += 1;
    }
    let day = remaining + 1;
    let secs = epoch % 86400;
    let hour = (secs / 3600) as u32;
    let min = ((secs % 3600) / 60) as u32;
    let sec = (secs % 60) as u32;
    // Zeller's congruence to determine day of week (0=Sun..6=Sat)
    let dow: usize =
        ((year + (year / 4) - (year / 100) + (year / 400) + (13 * month + 1) / 5 + day + 308) % 7)
            as usize;

    format!(
        "{}, {:02} {} {:04} {:02}:{:02}:{:02} GMT",
        DAYS[dow], day, MONTHS[month as usize], year, hour, min, sec
    )
}

/// Parse an HTTP-date string into a SystemTime.
///
/// Supports common date formats from RFC 7231 Section 7.1.1.1:
/// - IMF-fixdate: `Sun, 06 Nov 1994 08:49:37 GMT`
/// - RFC 850: `Sunday, 06-Nov-94 08:49:37 GMT`
/// - ANSI C asctime: `Sun Nov  6 08:49:37 1994`
///
/// If parsing fails, returns a far-future timestamp as fallback (1 year from now)
/// to avoid prematurely expiring cookies due to unparseable dates.
fn parse_http_date(s: &str) -> Result<SystemTime> {
    let s = s.trim();
    let parts: Vec<&str> = s.split_whitespace().collect();

    // Handle various formats
    if parts.len() >= 5 {
        // Try to extract day, month, year, time components
        let (day_str, mon_str, year_str, time_str) = if parts[0].ends_with(',') {
            // IMF-fixdate or RFC 850 format: "Sun, 06 Nov 1994 08:49:37 GMT"
            // or "Sunday, 06-Nov-94 08:49:37 GMT"
            if parts.len() >= 6 {
                (
                    parts[1].trim(),
                    parts[2].trim(),
                    parts[3].trim(),
                    parts[4].trim(),
                )
            } else if parts.len() >= 5 {
                (
                    parts[1].split('-').next().unwrap_or("1"),
                    parts[1].split('-').nth(1).unwrap_or("Jan"),
                    parts[2].trim(),
                    parts[3].trim(),
                )
            } else {
                return Err(Aria2Error::Parse("Invalid date format".to_string()));
            }
        } else {
            // asctime format: "Sun Nov  6 08:49:37 1994"
            (
                parts[2].trim(),
                parts[1].trim(),
                parts[4].trim(),
                parts[3].trim(),
            )
        };

        const MONTHS: [&str; 12] = [
            "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
        ];

        let day: u32 = day_str.parse().unwrap_or(1);
        let month_idx = MONTHS
            .iter()
            .position(|&m| m.eq_ignore_ascii_case(mon_str))
            .unwrap_or(0);
        let year: i32 = year_str.parse().unwrap_or({
            // Handle 2-digit years (RFC 850)
            let y: u32 = year_str.parse().unwrap_or(70);
            if y < 100 { (1900 + y) as i32 } else { y as i32 }
        });

        let time_parts: Vec<u32> = time_str.split(':').filter_map(|x| x.parse().ok()).collect();
        let hour = time_parts.first().copied().unwrap_or(0);
        let min = time_parts.get(1).copied().unwrap_or(0);
        let sec = time_parts.get(2).copied().unwrap_or(0);

        // Convert to Unix timestamp (simplified calculation)
        let total_days = calculate_days_since_epoch(year, month_idx as u32, day);
        let timestamp =
            total_days as u64 * 86400 + hour as u64 * 3600 + min as u64 * 60 + sec as u64;

        return Ok(SystemTime::UNIX_EPOCH + Duration::from_secs(timestamp));
    }

    // Fallback: treat as far-future expiry (1 year from now) to avoid
    // incorrectly expiring cookies when we can't parse the date format.
    // This is safer than returning an error which would cause cookie rejection.
    Ok(SystemTime::now() + Duration::from_secs(86400 * 365))
}

/// Calculate the number of days since Unix epoch (1970-01-01) for a given date.
fn calculate_days_since_epoch(year: i32, month: u32, day: u32) -> i64 {
    let mut days: i64 = 0;

    // Full years before current year
    for y in 1970..year {
        days += if (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 {
            366
        } else {
            365
        };
    }

    // Months before current month in current year
    let mdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    let is_leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    for m in 0..month {
        let d = if m == 1 && is_leap {
            29
        } else {
            mdays[m as usize]
        };
        days += d as i64;
    }

    // Days in current month (day is 1-indexed)
    days += day as i64 - 1;

    days
}

// ==================== Tests ====================

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

    // ===== Original CookieStorage tests =====

    #[test]
    fn test_creation_and_count() {
        let store = CookieStorage::new();
        assert_eq!(store.count(), 0);
        assert!(store.is_empty());
    }

    #[test]
    fn test_add_cookie() {
        let store = CookieStorage::new();
        store.add(Cookie::new("sid", "v1", "example.com"));
        assert_eq!(store.count(), 1);
        assert!(!store.is_empty());
    }

    #[test]
    fn test_add_updates_existing() {
        let store = CookieStorage::new();
        store.add(Cookie::new("sid", "old", "example.com"));
        store.add(Cookie::new("sid", "new", "example.com"));
        assert_eq!(store.count(), 1);

        let found = store.find_cookies("example.com", "/", false);
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].value, "new");
    }

    #[test]
    fn test_find_cookies_filters() {
        let store = CookieStorage::new();
        store.add(Cookie::new("a", "1", "example.com"));
        store.add(Cookie::new("b", "2", "other.com"));

        let found = store.find_cookies("example.com", "/", false);
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].name, "a");
    }

    #[test]
    fn test_find_cookies_for_url() {
        let store = CookieStorage::new();
        let mut c = Cookie::new("lang", "en", "example.com");
        c.path = "/api".to_string();
        store.add(c);

        let url = reqwest::Url::parse("http://example.com/api/data").unwrap();
        let found = store.find_cookies_for_url(&url);
        assert_eq!(found.len(), 1);

        let url2 = reqwest::Url::parse("http://example.com/home").unwrap();
        let found2 = store.find_cookies_for_url(&url2);
        assert!(found2.is_empty());
    }

    #[test]
    fn test_expire_cookies() {
        let store = CookieStorage::new();
        let mut expired = Cookie::new("old", "v", "x.com");
        expired.persistent = true;
        expired.expiry_time = 1;
        store.add(expired);

        let mut fresh = Cookie::new("fresh", "v", "x.com");
        fresh.persistent = true;
        fresh.expiry_time = i64::MAX;
        store.add(fresh);

        assert_eq!(store.count(), 2);
        let removed = store.expire_cookies(2);
        assert_eq!(removed, 1);
        assert_eq!(store.count(), 1);
    }

    #[test]
    fn test_clear() {
        let store = CookieStorage::new();
        store.add(Cookie::new("a", "1", "b.com"));
        store.add(Cookie::new("c", "2", "d.com"));
        store.clear();
        assert_eq!(store.count(), 0);
    }

    #[test]
    fn test_to_header_string() {
        let store = CookieStorage::new();
        store.add(Cookie::new("a", "1", "example.com"));
        store.add(Cookie::new("b", "2", "example.com"));

        let hdr = store.to_header_string("example.com", "/", false);
        assert!(hdr.contains("a=1"));
        assert!(hdr.contains("b=2"));
    }

    #[test]
    fn test_to_header_string_empty_for_no_match() {
        let store = CookieStorage::new();
        store.add(Cookie::new("a", "1", "example.com"));
        let hdr = store.to_header_string("other.com", "/", false);
        assert!(hdr.is_empty());
    }

    #[test]
    #[ignore]
    fn test_load_save_roundtrip() {
        let dir = std::env::temp_dir().join("aria2_test_cookie");
        fs::create_dir_all(&dir).ok();
        let path = dir.join("cookies.txt");

        let store = CookieStorage::new();
        store.add(Cookie::new("sid", "abc", "example.com"));
        store.save_file(&path).expect("save should succeed");

        let store2 = CookieStorage::new();
        let n = store2.load_file(&path).expect("load should succeed");
        assert_eq!(n, 1);

        let found = store2.find_cookies("example.com", "/", false);
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].value, "abc");

        fs::remove_dir_all(dir).ok();
    }

    #[test]
    fn test_multiple_domains_independent() {
        let store = CookieStorage::new();
        store.add(Cookie::new("a", "1", "alpha.com"));
        store.add(Cookie::new("b", "2", "beta.com"));
        store.add(Cookie::new("c", "3", "alpha.com"));

        assert_eq!(store.count(), 3);
        assert_eq!(store.find_cookies("alpha.com", "/", false).len(), 2);
        assert_eq!(store.find_cookies("beta.com", "/", false).len(), 1);
    }

    // ===== J4: New JarCookie Tests =====

    /// Test J4.4 #1: Parse Set-Cookie header with various attributes.
    ///
    /// Verifies that name/value extraction and common attribute parsing work correctly,
    /// including Domain, Path, Secure, HttpOnly, and Expires.
    #[test]
    fn test_cookie_parse_set_cookie() {
        // Basic parsing with all common attributes
        let cookie = JarCookie::parse_set_cookie(
            "session_id=abc123; Domain=example.com; Path=/login; Secure; HttpOnly",
        )
        .expect("Should parse valid Set-Cookie header");

        assert_eq!(cookie.name, "session_id");
        assert_eq!(cookie.value, "abc123");
        assert_eq!(cookie.domain, "example.com");
        assert_eq!(cookie.path, "/login");
        assert!(cookie.secure, "Secure flag should be set");
        assert!(cookie.http_only, "HttpOnly flag should be set");
    }

    /// Test J4.4 #1 continued: Parse minimal Set-Cookie (name=value only).
    #[test]
    fn test_cookie_parse_minimal() {
        let cookie = JarCookie::parse_set_cookie("SID=31d4d96e407aad42")
            .expect("Should parse minimal cookie");

        assert_eq!(cookie.name, "SID");
        assert_eq!(cookie.value, "31d4d96e407aad42");
        assert_eq!(cookie.domain, ""); // No domain specified
        assert_eq!(cookie.path, "/");
        assert!(!cookie.secure);
        assert!(!cookie.http_only);
    }

    /// Test J4.4 #1 continued: Parse with Max-Age attribute.
    #[test]
    fn test_cookie_parse_max_age() {
        let cookie =
            JarCookie::parse_set_cookie("token=xyz; Max-Age=3600").expect("Should parse Max-Age");

        assert_eq!(cookie.name, "token");
        assert!(cookie.expires.is_some(), "Max-Age should set expiration");
    }

    /// Test J4.4 #1 continued: Invalid headers return None.
    #[test]
    fn test_cookie_parse_invalid_returns_none() {
        assert!(JarCookie::parse_set_cookie("").is_none());
        assert!(JarCookie::parse_set_cookie("noequal").is_none());
        assert!(JarCookie::parse_set_cookie("=").is_none());
        assert!(JarCookie::parse_set_cookie(";").is_none());
    }

    /// Test J4.4 #2: Secure cookie must not be sent over plain HTTP.
    ///
    /// A cookie with the Secure flag should only match URLs accessed via HTTPS.
    /// When `is_secure=false`, the cookie should not match even if domain/path align.
    #[test]
    fn test_cookie_matches_url_secure_flag() {
        let mut cookie = JarCookie::new("auth_token", "secret123", "secure.example.com");
        cookie.secure = true; // Mark as secure-only

        // Should match HTTPS URL
        assert!(
            cookie.matches_url("https://secure.example.com/api", true),
            "Secure cookie should match HTTPS URL"
        );

        // Should NOT match HTTP URL
        assert!(
            !cookie.matches_url("http://secure.example.com/api", false),
            "Secure cookie must NOT match HTTP URL"
        );
    }

    /// Test J4.4 #2 continued: Non-secure cookies work on both HTTP and HTTPS.
    #[test]
    fn test_cookie_matches_url_non_secure_both_schemes() {
        let cookie = JarCookie::new("lang", "en", "example.com");

        assert!(
            cookie.matches_url("http://example.com/", false),
            "Non-secure cookie should match HTTP"
        );
        assert!(
            cookie.matches_url("https://example.com/", true),
            "Non-secure cookie should also match HTTPS"
        );
    }

    /// Test J4.4 #2 continued: Expired cookies don't match.
    #[test]
    fn test_cookie_matches_url_expired() {
        let mut cookie = JarCookie::new("old_session", "val", "example.com");
        // Set expiration to 1 second in the past
        cookie.expires = Some(SystemTime::now() - Duration::from_secs(1));

        assert!(
            !cookie.matches_url("https://example.com/", true),
            "Expired cookie should not match any URL"
        );
    }

    /// Test J4.4 #3: CookieJar returns correct subset of cookies for a given URL.
    ///
    /// Stores multiple cookies for different domains/paths and verifies that
    /// querying for a specific URL returns only the matching subset.
    #[test]
    fn test_cookie_jar_get_for_url() {
        let mut jar = CookieJar::new();

        // Add cookies for different domains and paths
        jar.store(JarCookie::new("session", "abc123", "example.com"));
        jar.store(JarCookie::new("theme", "dark", "example.com"));
        jar.store(JarCookie::new("tracker", "xyz999", "other.com"));

        // Query for example.com — should get 2 cookies
        let example_cookies = jar.get_cookies_for_url("http://example.com/page", false);
        assert_eq!(
            example_cookies.len(),
            2,
            "Should get exactly 2 cookies for example.com"
        );

        let names: Vec<&str> = example_cookies.iter().map(|c| c.name.as_str()).collect();
        assert!(names.contains(&"session"));
        assert!(names.contains(&"theme"));

        // Query for other.com — should get 1 cookie
        let other_cookies = jar.get_cookies_for_url("http://other.com/", false);
        assert_eq!(other_cookies.len(), 1);
        assert_eq!(other_cookies[0].name, "tracker");
    }

    /// Test J4.4 #3 continued: cookie_header_for_url produces correct header format.
    #[test]
    fn test_cookie_jar_header_format() {
        let mut jar = CookieJar::new();
        jar.store(JarCookie::new("a", "1", "example.com"));
        jar.store(JarCookie::new("b", "2", "example.com"));

        let header = jar.cookie_header_for_url("http://example.com/", false);
        assert!(header.is_some());
        let hdr = header.unwrap();
        assert!(hdr.contains("a=1"), "Header should contain a=1");
        assert!(hdr.contains("b=2"), "Header should contain b=2");
        // Verify format: "name=val; name=val"
        assert!(
            hdr == "a=1; b=2" || hdr == "b=2; a=1",
            "Unexpected header format: {}",
            hdr
        );
    }

    /// Test J4.4 #3 continued: No matching cookies returns None.
    #[test]
    fn test_cookie_jar_no_match_returns_none() {
        let mut jar = CookieJar::new();
        jar.store(JarCookie::new("x", "y", "example.com"));

        let header = jar.cookie_header_for_url("http://other.com/", false);
        assert!(header.is_none(), "No match should return None");
    }

    /// Test J4.4 #4: Load cookies from Netscape/Mozilla cookie file format.
    ///
    /// Creates a temporary file in the Netscape cookie format and verifies
    /// that loading it produces the expected cookies with correct field values.
    #[test]
    fn test_netscape_cookie_file_load() {
        let dir = std::env::temp_dir().join("aria2_netscape_test");
        fs::create_dir_all(&dir).ok();
        let path = dir.join("cookies.txt");

        // Write a Netscape-format cookie file
        let content = "# Netscape HTTP Cookie File\n\
                       \n\
                       .example.com\tTRUE\t/\tFALSE\t0\tsession_id\tabc123\n\
                       .api.example.com\tTRUE\t/api\tTRUE\t1700000000\ttoken\tsecret\n\
                       localhost\tFALSE\t/\tFALSE\t0\tlocal_key\tlocal_val\n";
        fs::write(&path, content).expect("Failed to write test cookie file");

        // Load into CookieJar
        let mut jar = CookieJar::new();
        let result = jar.load_netscape_file(&path);
        assert!(
            result.is_ok(),
            "Loading netscape file should succeed: {:?}",
            result.err()
        );

        let count = result.unwrap();
        assert_eq!(count, 3, "Should have loaded 3 cookies");
        assert_eq!(jar.len(), 3);

        // Verify specific cookie properties
        let session_cookie = jar
            .cookies
            .iter()
            .find(|c| c.name == "session_id")
            .expect("Should find session_id cookie");
        assert_eq!(session_cookie.domain, ".example.com");
        assert_eq!(session_cookie.value, "abc123");
        assert!(!session_cookie.secure);
        assert!(
            session_cookie.expires.is_none(),
            "Timestamp 0 means session cookie (no expiry)"
        );

        let token_cookie = jar
            .cookies
            .iter()
            .find(|c| c.name == "token")
            .expect("Should find token cookie");
        assert_eq!(token_cookie.domain, ".api.example.com");
        assert_eq!(token_cookie.path, "/api");
        assert!(token_cookie.secure, "Token cookie should be secure");
        assert!(
            token_cookie.expires.is_some(),
            "Token cookie should have expiry time"
        );

        // Cleanup
        fs::remove_dir_all(dir).ok();
    }

    /// Test J4.4 #4 continued: Loading nonexistent file returns error.
    #[test]
    fn test_netscape_load_nonexistent_file() {
        let mut jar = CookieJar::new();
        let result = jar.load_netscape_file(Path::new("/nonexistent/path/cookies.txt"));
        assert!(result.is_err(), "Loading nonexistent file should fail");
    }

    /// Test J4.4 #4 continued: Empty file loads zero cookies.
    #[test]
    fn test_netscape_load_empty_file() {
        let dir = std::env::temp_dir().join("aria2_netscape_empty");
        fs::create_dir_all(&dir).ok();
        let path = dir.join("empty.txt");
        fs::write(&path, "").expect("Failed to write empty file");

        let mut jar = CookieJar::new();
        let result = jar.load_netscape_file(&path).expect("Load should succeed");
        assert_eq!(result, 0, "Empty file should yield 0 cookies");
        assert!(jar.is_empty());

        fs::remove_dir_all(dir).ok();
    }

    // ===== Additional JarCookie/CookieJar tests =====

    #[test]
    fn test_jar_cookie_creation() {
        let c = JarCookie::new("test", "value", "example.com");
        assert_eq!(c.name, "test");
        assert_eq!(c.value, "value");
        assert_eq!(c.domain, "example.com");
        assert_eq!(c.path, "/");
        assert!(!c.secure);
        assert!(!c.http_only);
        assert!(c.expires.is_none()); // Session cookie by default
    }

    #[test]
    fn test_jar_cookie_to_header_value() {
        let mut c = JarCookie::new("sid", "abc", "example.com");
        c.secure = true;
        c.http_only = true;
        let hdr = c.to_header_value();
        assert!(hdr.starts_with("sid=abc"));
        assert!(hdr.contains("Domain=example.com"));
        assert!(hdr.contains("Secure"));
        assert!(hdr.contains("HttpOnly"));
    }

    #[test]
    fn test_jar_cookie_equality() {
        let a = JarCookie::new("x", "1", "a.com");
        let b = JarCookie::new("x", "2", "a.com"); // Same name+domain+path
        assert_eq!(a, b, "Cookies with same name/domain/path should be equal");

        let c = JarCookie::new("y", "1", "a.com");
        assert_ne!(a, c, "Different names should not be equal");
    }

    #[test]
    fn test_cookie_jar_store_updates_existing() {
        let mut jar = CookieJar::new();
        jar.store(JarCookie::new("sid", "old", "example.com"));
        jar.store(JarCookie::new("sid", "new", "example.com"));

        assert_eq!(jar.len(), 1, "Store should update, not duplicate");
        let cookies = jar.get_cookies_for_url("http://example.com/", false);
        assert_eq!(cookies[0].value, "new");
    }

    #[test]
    fn test_cookie_jar_cleanup_expired() {
        let mut jar = CookieJar::new();

        // Add an expired cookie
        let mut expired = JarCookie::new("old", "val", "x.com");
        expired.expires = Some(SystemTime::now() - Duration::from_secs(60));
        jar.store(expired);

        // Add a fresh cookie (far future expiry)
        let mut fresh = JarCookie::new("fresh", "val", "x.com");
        fresh.expires = Some(SystemTime::now() + Duration::from_secs(86400 * 365));
        jar.store(fresh);

        // Add a session cookie (no expiry)
        jar.store(JarCookie::new("session", "val", "x.com"));

        assert_eq!(jar.len(), 3);
        let removed = jar.cleanup_expired();
        assert_eq!(removed, 1, "Should remove exactly 1 expired cookie");
        assert_eq!(jar.len(), 2, "Fresh + session cookies remain");
    }

    #[test]
    fn test_cookie_jar_clear() {
        let mut jar = CookieJar::new();
        jar.store(JarCookie::new("a", "1", "x.com"));
        jar.store(JarCookie::new("b", "2", "y.com"));
        jar.clear();
        assert!(jar.is_empty());
    }

    #[test]
    fn test_format_systemtime_as_http_date() {
        let time = SystemTime::UNIX_EPOCH + Duration::from_secs(784111777); // Known timestamp
        let formatted = format_systemtime_as_http_date(time);
        // Should produce something like "Thu, 29 Nov 1984 20:22:57 GMT"
        assert!(formatted.contains("GMT"), "HTTP date should end with GMT");
        assert!(
            formatted.contains(','),
            "IMF-fixdate should have comma after weekday"
        );
    }

    #[test]
    fn test_parse_http_date_imf_fixdate() {
        let result = parse_http_date("Sun, 06 Nov 1994 08:49:37 GMT");
        assert!(result.is_ok(), "Should parse IMF-fixdate format");
        let time = result.unwrap();
        let dur = time
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or_default();
        // Nov 6, 1994 08:49:37 GMT ≈ 784629777 seconds since epoch
        // Allow generous range for different parser implementations
        assert!(
            dur.as_secs() > 784000000,
            "Timestamp should be roughly correct, got {}",
            dur.as_secs()
        );
    }

    #[test]
    fn test_parse_http_date_fallback() {
        // Unparseable input should return far-future (not error)
        let result = parse_http_date("totally-invalid-date-string");
        assert!(result.is_ok(), "Should return fallback, not error");
        let time = result.unwrap();
        assert!(time > SystemTime::now(), "Fallback should be in the future");
    }
}