strata-web-api 0.1.1

W3C Web Platform APIs in pure idiomatic Rust for Strata
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
//! Pure-Rust W3C fetch() Engine with Headers, AbortController, and HTTP Framing.

use std::collections::HashMap;
use std::fmt;
use std::io::{Cursor, Read, Write};
use std::net::{TcpStream, ToSocketAddrs};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;

use crate::tls::HttpTransportStream;

/// Errors occurring during HTTP fetch requests.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FetchError {
    InvalidUrl(String),
    NetworkError(String),
    Timeout(String),
    Aborted,
    IoError(String),
    SerializationError(String),
    CorsError(String),
}

impl fmt::Display for FetchError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidUrl(s) => write!(f, "Invalid URL: {s}"),
            Self::NetworkError(s) => write!(f, "Network error: {s}"),
            Self::Timeout(s) => write!(f, "Request timed out: {s}"),
            Self::Aborted => write!(f, "Request was aborted by AbortController"),
            Self::IoError(s) => write!(f, "I/O error: {s}"),
            Self::SerializationError(s) => write!(f, "Serialization error: {s}"),
            Self::CorsError(s) => write!(f, "CORS error: {s}"),
        }
    }
}

impl std::error::Error for FetchError {}

/// W3C AbortSignal signaling cancellation of asynchronous requests.
#[derive(Clone, Default)]
pub struct AbortSignal {
    aborted: Arc<AtomicBool>,
}

impl AbortSignal {
    pub fn is_aborted(&self) -> bool {
        self.aborted.load(Ordering::SeqCst)
    }
}

/// W3C AbortController used to cancel in-flight network requests.
#[derive(Default)]
pub struct AbortController {
    signal: AbortSignal,
}

impl AbortController {
    pub fn new() -> Self {
        Self {
            signal: AbortSignal::default(),
        }
    }

    pub fn signal(&self) -> AbortSignal {
        self.signal.clone()
    }

    pub fn abort(&self) {
        self.signal.aborted.store(true, Ordering::SeqCst);
    }
}

/// Case-insensitive HTTP Header map conforming to W3C Fetch spec.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Headers {
    map: HashMap<String, String>,
    all: HashMap<String, Vec<String>>,
}

impl Headers {
    pub fn new() -> Self {
        Self {
            map: HashMap::new(),
            all: HashMap::new(),
        }
    }

    pub fn set(&mut self, key: &str, value: &str) {
        let lower = key.to_ascii_lowercase();
        self.map.insert(lower.clone(), value.to_string());
        self.all.insert(lower, vec![value.to_string()]);
    }

    pub fn append(&mut self, key: &str, value: &str) {
        let lower = key.to_ascii_lowercase();
        if lower == "set-cookie" {
            // Per RFC 6265 §3, Set-Cookie headers must NOT be joined with comma
            self.map.insert(lower.clone(), value.to_string());
        } else if let Some(existing) = self.map.get_mut(&lower) {
            existing.push_str(", ");
            existing.push_str(value);
        } else {
            self.map.insert(lower.clone(), value.to_string());
        }
        self.all.entry(lower).or_default().push(value.to_string());
    }

    pub fn get(&self, key: &str) -> Option<&str> {
        self.map.get(&key.to_ascii_lowercase()).map(|s| s.as_str())
    }

    pub fn get_all(&self, key: &str) -> Vec<&str> {
        self.all
            .get(&key.to_ascii_lowercase())
            .map(|vec| vec.iter().map(|s| s.as_str()).collect())
            .unwrap_or_default()
    }

    pub fn has(&self, key: &str) -> bool {
        self.map.contains_key(&key.to_ascii_lowercase())
    }

    pub fn delete(&mut self, key: &str) {
        let lower = key.to_ascii_lowercase();
        self.map.remove(&lower);
        self.all.remove(&lower);
    }

    pub fn entries(&self) -> Vec<(&str, &str)> {
        self.map
            .iter()
            .map(|(k, v)| (k.as_str(), v.as_str()))
            .collect()
    }

    pub fn all_entries(&self) -> Vec<(&str, &str)> {
        let mut out = Vec::new();
        for (k, vals) in &self.all {
            for v in vals {
                out.push((k.as_str(), v.as_str()));
            }
        }
        out
    }
}

/// W3C Fetch request mode governing Cross-Origin Resource Sharing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RequestMode {
    #[default]
    Cors,
    NoCors,
    SameOrigin,
}

/// W3C Fetch credentials mode governing cookie inclusion in cross-origin requests.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RequestCredentials {
    #[default]
    SameOrigin,
    Include,
    Omit,
}

/// Configuration options for an HTTP request.
#[derive(Clone)]
pub struct RequestInit {
    pub method: String,
    pub headers: Headers,
    pub body: Option<Vec<u8>>,
    pub timeout_ms: Option<u64>,
    pub signal: Option<AbortSignal>,
    pub cookie_jar: Option<Arc<std::sync::Mutex<crate::cookie::CookieJar>>>,
    pub mode: RequestMode,
    pub credentials: RequestCredentials,
    pub origin: Option<String>,
}

impl Default for RequestInit {
    fn default() -> Self {
        Self {
            method: "GET".to_string(),
            headers: Headers::new(),
            body: None,
            timeout_ms: Some(15000),
            signal: None,
            cookie_jar: None,
            mode: RequestMode::Cors,
            credentials: RequestCredentials::SameOrigin,
            origin: None,
        }
    }
}

impl RequestInit {
    pub fn get() -> Self {
        Self {
            method: "GET".to_string(),
            ..Default::default()
        }
    }

    pub fn post() -> Self {
        Self {
            method: "POST".to_string(),
            ..Default::default()
        }
    }

    pub fn put() -> Self {
        Self {
            method: "PUT".to_string(),
            ..Default::default()
        }
    }

    pub fn delete() -> Self {
        Self {
            method: "DELETE".to_string(),
            ..Default::default()
        }
    }

    pub fn header(mut self, key: &str, val: &str) -> Self {
        self.headers.set(key, val);
        self
    }

    pub fn body(mut self, body: impl Into<Vec<u8>>) -> Self {
        self.body = Some(body.into());
        self
    }

    pub fn body_json_str(mut self, json_str: &str) -> Self {
        self.headers.set("Content-Type", "application/json");
        self.body = Some(json_str.as_bytes().to_vec());
        self
    }

    pub fn signal(mut self, signal: AbortSignal) -> Self {
        self.signal = Some(signal);
        self
    }

    pub fn cookie_jar(mut self, jar: Arc<std::sync::Mutex<crate::cookie::CookieJar>>) -> Self {
        self.cookie_jar = Some(jar);
        self
    }

    pub fn mode(mut self, mode: RequestMode) -> Self {
        self.mode = mode;
        self
    }

    pub fn credentials(mut self, creds: RequestCredentials) -> Self {
        self.credentials = creds;
        self
    }

    pub fn origin(mut self, origin: impl Into<String>) -> Self {
        self.origin = Some(origin.into());
        self
    }
}

/// HTTP Response representation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Response {
    pub status: u16,
    pub status_text: String,
    pub headers: Headers,
    body: Vec<u8>,
}

impl Response {
    pub fn new(status: u16, status_text: &str, headers: Headers, body: Vec<u8>) -> Self {
        Self {
            status,
            status_text: status_text.to_string(),
            headers,
            body,
        }
    }

    /// Whether HTTP status code is in the range 200..=299.
    pub fn ok(&self) -> bool {
        (200..=299).contains(&self.status)
    }

    /// Returns response body as UTF-8 string.
    pub fn text(&self) -> Result<String, FetchError> {
        String::from_utf8(self.body.clone()).map_err(|e| {
            FetchError::SerializationError(format!("Response body is not valid UTF-8: {e}"))
        })
    }

    /// Raw body bytes.
    pub fn bytes(&self) -> &[u8] {
        &self.body
    }
}

/// Multipart form data builder.
#[derive(Debug, Clone, Default)]
pub struct FormData {
    fields: Vec<(String, String)>,
}

impl FormData {
    pub fn new() -> Self {
        Self { fields: Vec::new() }
    }

    pub fn append(&mut self, name: &str, value: &str) {
        self.fields.push((name.to_string(), value.to_string()));
    }

    pub fn get(&self, name: &str) -> Option<&str> {
        self.fields
            .iter()
            .find(|(n, _)| n == name)
            .map(|(_, v)| v.as_str())
    }

    /// Formats multipart form payload with boundary.
    pub fn to_multipart_payload(&self, boundary: &str) -> Vec<u8> {
        let mut out = Vec::new();
        for (name, val) in &self.fields {
            out.extend_from_slice(format!("--{boundary}\r\n").as_bytes());
            out.extend_from_slice(
                format!("Content-Disposition: form-data; name=\"{name}\"\r\n\r\n").as_bytes(),
            );
            out.extend_from_slice(val.as_bytes());
            out.extend_from_slice(b"\r\n");
        }
        out.extend_from_slice(format!("--{boundary}--\r\n").as_bytes());
        out
    }
}

/// Parsed URL components.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedUrl {
    pub scheme: String,
    pub host: String,
    pub port: u16,
    pub path_and_query: String,
}

impl ParsedUrl {
    pub fn parse(url: &str) -> Result<Self, FetchError> {
        let (scheme, rest) = if let Some(stripped) = url.strip_prefix("http://") {
            ("http", stripped)
        } else if let Some(stripped) = url.strip_prefix("https://") {
            ("https", stripped)
        } else {
            return Err(FetchError::InvalidUrl(format!("Unsupported scheme: {url}")));
        };

        // Under RFC 3986, the authority segment terminates at the first '/', '?', or '#' (or end of string).
        let authority_end = rest.find(['/', '?', '#']).unwrap_or(rest.len());
        let (host_port, path_query_frag) = rest.split_at(authority_end);

        // Strip fragment (#...) from HTTP request path_and_query
        let path_and_query_raw = match path_query_frag.split_once('#') {
            Some((pq, _frag)) => pq,
            None => path_query_frag,
        };

        let path = if path_and_query_raw.is_empty() {
            "/".to_string()
        } else if path_and_query_raw.starts_with('?') {
            format!("/{path_and_query_raw}")
        } else {
            path_and_query_raw.to_string()
        };

        let (host, port) = if let Some((h, p)) = host_port.split_once(':') {
            let parsed_port = p
                .parse::<u16>()
                .map_err(|_| FetchError::InvalidUrl(format!("Invalid port: {p}")))?;
            (h.to_string(), parsed_port)
        } else {
            let default_port = if scheme == "https" { 443 } else { 80 };
            (host_port.to_string(), default_port)
        };

        if host.is_empty() {
            return Err(FetchError::InvalidUrl(format!(
                "Missing host in URL: {url}"
            )));
        }

        // CWE-319 / Goal 27: Enforce strict TLS transport on HTTPS standard port 443.
        // Cleartext transmission on port 443 is forbidden.
        if scheme == "http" && port == 443 {
            return Err(FetchError::InvalidUrl(format!(
                "Cleartext transmission on HTTPS standard port 443 is forbidden (CWE-319): {url}"
            )));
        }

        Ok(Self {
            scheme: scheme.to_string(),
            host,
            port,
            path_and_query: path,
        })
    }
}

/// Resolves a redirect location header against the base URL.
pub fn resolve_redirect_url(base: &str, location: &str) -> Result<String, FetchError> {
    let loc_trimmed = location.trim();
    if loc_trimmed.starts_with("http://") || loc_trimmed.starts_with("https://") {
        return Ok(loc_trimmed.to_string());
    }

    let parsed_base = ParsedUrl::parse(base)?;
    let host_part = if (parsed_base.scheme == "http" && parsed_base.port == 80)
        || (parsed_base.scheme == "https" && parsed_base.port == 443)
    {
        parsed_base.host
    } else {
        format!("{}:{}", parsed_base.host, parsed_base.port)
    };

    if let Some(stripped) = loc_trimmed.strip_prefix("//") {
        return Ok(format!("{}://{stripped}", parsed_base.scheme));
    }

    if loc_trimmed.starts_with('/') {
        return Ok(format!(
            "{}://{}{}",
            parsed_base.scheme, host_part, loc_trimmed
        ));
    }

    let base_dir = match parsed_base.path_and_query.rfind('/') {
        Some(idx) => &parsed_base.path_and_query[..=idx],
        None => "/",
    };
    Ok(format!(
        "{}://{}{}{}",
        parsed_base.scheme, host_part, base_dir, loc_trimmed
    ))
}

/// Computes the serialized origin (scheme + host + port) for an HTTP/HTTPS URL per RFC 6454 / W3C Fetch.
pub fn compute_origin(url: &str) -> Result<String, FetchError> {
    let parsed = ParsedUrl::parse(url)?;
    let is_default_port = (parsed.scheme == "http" && parsed.port == 80)
        || (parsed.scheme == "https" && parsed.port == 443);
    if is_default_port {
        Ok(format!("{}://{}", parsed.scheme, parsed.host))
    } else {
        Ok(format!(
            "{}://{}:{}",
            parsed.scheme, parsed.host, parsed.port
        ))
    }
}

/// Checks whether an HTTP request qualifies as a CORS-simple request under W3C Fetch spec.
/// Returns (is_simple, list_of_custom_headers).
pub fn is_cors_simple_request(req: &RequestInit) -> (bool, Vec<String>) {
    let method = req.method.to_uppercase();
    let method_simple = matches!(method.as_str(), "GET" | "HEAD" | "POST");
    let mut custom_headers = Vec::new();

    for (k, v) in req.headers.entries() {
        let key_lower = k.to_ascii_lowercase();
        match key_lower.as_str() {
            "accept" | "accept-language" | "content-language" | "user-agent" | "host"
            | "connection" | "accept-encoding" | "origin" | "content-length" => {}
            "content-type" => {
                let mime = v
                    .split(';')
                    .next()
                    .unwrap_or("")
                    .trim()
                    .to_ascii_lowercase();
                if !matches!(
                    mime.as_str(),
                    "application/x-www-form-urlencoded" | "multipart/form-data" | "text/plain"
                ) {
                    custom_headers.push(key_lower);
                }
            }
            _ => {
                custom_headers.push(key_lower);
            }
        }
    }

    let is_simple = method_simple && custom_headers.is_empty();
    (is_simple, custom_headers)
}

/// Executes an HTTP or HTTPS fetch request, following standard HTTP redirects and enforcing W3C CORS rules.
pub fn fetch(url: &str, init: Option<RequestInit>) -> Result<Response, FetchError> {
    let req = init.unwrap_or_default();
    let max_redirects = 5;
    let mut current_url = url.to_string();
    let mut current_req = req;

    let client_origin = current_req
        .origin
        .clone()
        .or_else(|| current_req.headers.get("origin").map(|s| s.to_string()));

    let client_origin_norm = client_origin
        .as_ref()
        .map(|raw| compute_origin(raw).unwrap_or_else(|_| raw.trim().to_string()));

    for _ in 0..max_redirects {
        let target_origin = compute_origin(&current_url)?;
        let is_same_origin = match &client_origin_norm {
            Some(orig) => orig.eq_ignore_ascii_case(&target_origin),
            None => true,
        };

        if !is_same_origin {
            let origin_str = client_origin_norm.as_deref().unwrap_or("");

            if current_req.mode == RequestMode::SameOrigin {
                return Err(FetchError::CorsError(format!(
                    "Cross-origin request to '{target_origin}' blocked by SameOrigin policy (origin: '{origin_str}')"
                )));
            }

            // In NoCors mode, only simple methods and headers are permitted
            let (is_simple, custom_headers) = is_cors_simple_request(&current_req);
            if current_req.mode == RequestMode::NoCors && !is_simple {
                return Err(FetchError::CorsError(
                    "Invalid request in 'no-cors' mode: method or headers require CORS preflight"
                        .into(),
                ));
            }

            // Omit or SameOrigin credentials mode: strip cookie jar on cross-origin requests
            if current_req.credentials != RequestCredentials::Include {
                current_req.cookie_jar = None;
                current_req.headers.delete("cookie");
            }

            // Under Cors mode, execute preflight OPTIONS check if request is not simple
            if current_req.mode == RequestMode::Cors && !is_simple {
                let mut preflight_headers = Headers::new();
                preflight_headers.set("Origin", origin_str);
                preflight_headers.set(
                    "Access-Control-Request-Method",
                    &current_req.method.to_uppercase(),
                );
                if !custom_headers.is_empty() {
                    preflight_headers
                        .set("Access-Control-Request-Headers", &custom_headers.join(", "));
                }

                let preflight_req = RequestInit {
                    method: "OPTIONS".to_string(),
                    headers: preflight_headers,
                    body: None,
                    timeout_ms: current_req.timeout_ms,
                    signal: current_req.signal.clone(),
                    cookie_jar: None,
                    mode: RequestMode::Cors,
                    credentials: RequestCredentials::Omit,
                    origin: Some(origin_str.to_string()),
                };

                let preflight_resp = fetch_single(&current_url, &preflight_req)?;

                if !(200..=299).contains(&preflight_resp.status) && preflight_resp.status != 204 {
                    return Err(FetchError::CorsError(format!(
                        "CORS preflight failed with HTTP status {}",
                        preflight_resp.status
                    )));
                }

                let allow_origin = preflight_resp
                    .headers
                    .get("access-control-allow-origin")
                    .map(|s| s.trim())
                    .ok_or_else(|| {
                        FetchError::CorsError(
                            "CORS preflight failed: missing 'Access-Control-Allow-Origin' header"
                                .into(),
                        )
                    })?;

                if allow_origin == "*" {
                    if current_req.credentials == RequestCredentials::Include {
                        return Err(FetchError::CorsError(
                            "CORS preflight failed: 'Access-Control-Allow-Origin' cannot be wildcard '*' when credentials mode is 'Include'".into(),
                        ));
                    }
                } else if !allow_origin.eq_ignore_ascii_case(origin_str) {
                    return Err(FetchError::CorsError(format!(
                        "CORS preflight failed: 'Access-Control-Allow-Origin' ('{allow_origin}') does not match origin '{origin_str}'"
                    )));
                }

                if current_req.credentials == RequestCredentials::Include {
                    let allow_creds = preflight_resp
                        .headers
                        .get("access-control-allow-credentials")
                        .map(|s| s.trim());
                    if allow_creds != Some("true") {
                        return Err(FetchError::CorsError(
                            "CORS preflight failed: 'Access-Control-Allow-Credentials' must be 'true'".into(),
                        ));
                    }
                }

                let allow_methods = preflight_resp
                    .headers
                    .get("access-control-allow-methods")
                    .unwrap_or("");
                let methods_list: Vec<&str> = allow_methods.split(',').map(|s| s.trim()).collect();
                let req_method = current_req.method.to_uppercase();
                if !methods_list
                    .iter()
                    .any(|&m| m == "*" || m.eq_ignore_ascii_case(&req_method))
                {
                    return Err(FetchError::CorsError(format!(
                        "CORS preflight failed: method '{req_method}' not permitted by Access-Control-Allow-Methods ('{allow_methods}')"
                    )));
                }

                if !custom_headers.is_empty() {
                    let allow_headers = preflight_resp
                        .headers
                        .get("access-control-allow-headers")
                        .unwrap_or("");
                    let headers_list: Vec<&str> =
                        allow_headers.split(',').map(|s| s.trim()).collect();
                    let wildcard = headers_list.contains(&"*");
                    if !wildcard {
                        for header in &custom_headers {
                            if !headers_list.iter().any(|&h| h.eq_ignore_ascii_case(header)) {
                                return Err(FetchError::CorsError(format!(
                                    "CORS preflight failed: header '{header}' not permitted by Access-Control-Allow-Headers ('{allow_headers}')"
                                )));
                            }
                        }
                    }
                }
            }

            // Set Origin header on actual request
            current_req.headers.set("Origin", origin_str);
        }

        let resp = fetch_single(&current_url, &current_req)?;

        // If cross-origin in Cors mode, validate response CORS headers
        if !is_same_origin && current_req.mode == RequestMode::Cors {
            let origin_str = client_origin_norm.as_deref().unwrap_or("");
            let allow_origin = resp
                .headers
                .get("access-control-allow-origin")
                .map(|s| s.trim())
                .ok_or_else(|| {
                    FetchError::CorsError(format!(
                        "CORS policy violation: missing 'Access-Control-Allow-Origin' header on response from {current_url}"
                    ))
                })?;

            if allow_origin == "*" {
                if current_req.credentials == RequestCredentials::Include {
                    return Err(FetchError::CorsError(
                        "CORS policy violation: 'Access-Control-Allow-Origin' cannot be wildcard '*' when credentials mode is 'Include'".into(),
                    ));
                }
            } else if !allow_origin.eq_ignore_ascii_case(origin_str) {
                return Err(FetchError::CorsError(format!(
                    "CORS policy violation: Access-Control-Allow-Origin '{allow_origin}' does not match request origin '{origin_str}'"
                )));
            }

            if current_req.credentials == RequestCredentials::Include {
                let allow_creds = resp
                    .headers
                    .get("access-control-allow-credentials")
                    .map(|s| s.trim());
                if allow_creds != Some("true") {
                    return Err(FetchError::CorsError(
                        "CORS policy violation: 'Access-Control-Allow-Credentials' must be 'true' for credentialed requests".into(),
                    ));
                }
            }
        }

        if (301..=303).contains(&resp.status) || resp.status == 307 || resp.status == 308 {
            if let Some(loc) = resp.headers.get("location") {
                let next_url = resolve_redirect_url(&current_url, loc)?;
                let current_parsed = ParsedUrl::parse(&current_url)?;
                let next_parsed = ParsedUrl::parse(&next_url)?;

                // Disallow redirect downgrade from HTTPS to HTTP (CWE-319 / Goal 31)
                if current_parsed.scheme == "https" && next_parsed.scheme == "http" {
                    return Err(FetchError::NetworkError(format!(
                        "Insecure redirect downgrade from HTTPS to HTTP rejected (CWE-319): {next_url}"
                    )));
                }

                // Strip sensitive credentials on cross-origin redirects (RFC 9110 §15.4 / CWE-200)
                let is_cross_origin = current_parsed.scheme != next_parsed.scheme
                    || !current_parsed.host.eq_ignore_ascii_case(&next_parsed.host)
                    || current_parsed.port != next_parsed.port;

                if is_cross_origin {
                    current_req.headers.delete("authorization");
                    current_req.headers.delete("proxy-authorization");
                    current_req.headers.delete("cookie");
                }

                current_url = next_url;
                if resp.status == 303 || resp.status == 301 || resp.status == 302 {
                    current_req.method = "GET".to_string();
                    current_req.body = None;
                }
                continue;
            }
        }

        return Ok(resp);
    }

    Err(FetchError::NetworkError(
        "Too many redirects (exceeded 5 hops)".into(),
    ))
}

/// Executes a single HTTP or HTTPS fetch request without following redirects.
pub fn fetch_single(url: &str, req: &RequestInit) -> Result<Response, FetchError> {
    // Check cancellation
    if let Some(ref sig) = req.signal {
        if sig.is_aborted() {
            return Err(FetchError::Aborted);
        }
    }

    let parsed = ParsedUrl::parse(url)?;
    let timeout = Duration::from_millis(req.timeout_ms.unwrap_or(15000));

    // Resolve DNS host and establish TCP connection
    let addrs: Vec<std::net::SocketAddr> =
        match (parsed.host.as_str(), parsed.port).to_socket_addrs() {
            Ok(iter) => iter.collect(),
            Err(e) => {
                return Err(FetchError::NetworkError(format!(
                    "DNS resolution failed for {}: {e}",
                    parsed.host
                )));
            }
        };

    if addrs.is_empty() {
        return Err(FetchError::NetworkError(format!(
            "No IP addresses resolved for host {}",
            parsed.host
        )));
    }

    let mut tcp_stream_opt = None;
    let mut last_err = None;

    for addr in &addrs {
        match TcpStream::connect_timeout(addr, timeout) {
            Ok(s) => {
                tcp_stream_opt = Some(s);
                break;
            }
            Err(e) => {
                last_err = Some(e);
            }
        }
    }

    let tcp_stream = tcp_stream_opt.ok_or_else(|| {
        FetchError::NetworkError(format!(
            "Connection to {}:{} failed: {}",
            parsed.host,
            parsed.port,
            last_err
                .map(|e| e.to_string())
                .unwrap_or_else(|| "Network unreachable".into())
        ))
    })?;

    tcp_stream
        .set_read_timeout(Some(timeout))
        .map_err(|e| FetchError::IoError(e.to_string()))?;
    tcp_stream
        .set_write_timeout(Some(timeout))
        .map_err(|e| FetchError::IoError(e.to_string()))?;

    // Transparently multiplex between Plain TCP and TLS 1.2/1.3
    let mut transport: HttpTransportStream = if parsed.scheme == "https" {
        crate::tls::create_tls_stream(tcp_stream, &parsed.host).map_err(FetchError::NetworkError)?
    } else {
        HttpTransportStream::Plain(tcp_stream)
    };

    let mut req_effective = req.clone();
    if let Some(ref jar) = req_effective.cookie_jar {
        if !req_effective.headers.has("cookie") {
            if let Ok(mut jar_guard) = jar.lock() {
                if let Some(cookie_val) = jar_guard.get_cookie_header_value(url) {
                    req_effective.headers.set("Cookie", &cookie_val);
                }
            }
        }
    }

    // If ALPN negotiated HTTP/2 ("h2"), dispatch to pure-Rust HTTP/2 client engine
    if transport.alpn_protocol() == Some(b"h2") {
        let resp = crate::http2::execute_http2_request(&mut transport, &req_effective, &parsed)?;
        if let Some(ref jar) = req.cookie_jar {
            if let Ok(mut jar_guard) = jar.lock() {
                jar_guard.process_response_headers(&resp.headers, url);
            }
        }
        return Ok(resp);
    }

    // Build HTTP/1.1 request
    let mut request_bytes = Vec::new();
    let method = req_effective.method.to_uppercase();
    request_bytes
        .extend_from_slice(format!("{method} {} HTTP/1.1\r\n", parsed.path_and_query).as_bytes());
    request_bytes.extend_from_slice(format!("Host: {}\r\n", parsed.host).as_bytes());
    request_bytes.extend_from_slice(b"Connection: close\r\n");

    let body_len = req_effective.body.as_ref().map(|b| b.len()).unwrap_or(0);
    if body_len > 0 {
        request_bytes.extend_from_slice(format!("Content-Length: {body_len}\r\n").as_bytes());
    }

    if !req_effective.headers.has("Accept-Encoding") {
        request_bytes.extend_from_slice(b"Accept-Encoding: gzip, deflate, br\r\n");
    }

    if !req_effective.headers.has("User-Agent") && !req_effective.headers.has("user-agent") {
        request_bytes.extend_from_slice(
            b"User-Agent: Mozilla/5.0 (compatible; Onyx/1.0; +https://strataui.org)\r\n",
        );
    }

    for (k, v) in req_effective.headers.entries() {
        request_bytes.extend_from_slice(format!("{k}: {v}\r\n").as_bytes());
    }
    request_bytes.extend_from_slice(b"\r\n");

    if let Some(ref body) = req_effective.body {
        request_bytes.extend_from_slice(body);
    }

    transport
        .write_all(&request_bytes)
        .map_err(|e| FetchError::NetworkError(e.to_string()))?;
    transport
        .flush()
        .map_err(|e| FetchError::NetworkError(e.to_string()))?;

    // Read response with RFC 7230 framing (terminates on Content-Length or chunked without waiting for EOF)
    let resp = read_http_response(&mut transport)?;
    if let Some(ref jar) = req.cookie_jar {
        if let Ok(mut jar_guard) = jar.lock() {
            jar_guard.process_response_headers(&resp.headers, url);
        }
    }
    Ok(resp)
}

/// Decodes HTTP/1.1 chunked transfer encoded payloads.
pub fn decode_chunked_body(raw_body: &[u8]) -> Result<Vec<u8>, FetchError> {
    let mut decoded = Vec::new();
    let mut cursor = 0;

    while cursor < raw_body.len() {
        let slice = &raw_body[cursor..];
        let Some(newline_pos) = slice.windows(2).position(|w| w == b"\r\n") else {
            break;
        };

        let size_str = std::str::from_utf8(&slice[..newline_pos])
            .map_err(|e| FetchError::NetworkError(format!("Invalid chunk size encoding: {e}")))?
            .trim();

        let size_hex = size_str.split(';').next().unwrap_or(size_str).trim();
        let chunk_size = usize::from_str_radix(size_hex, 16).map_err(|e| {
            FetchError::NetworkError(format!("Invalid hex chunk size '{size_hex}': {e}"))
        })?;

        cursor += newline_pos + 2;

        if chunk_size == 0 {
            break;
        }

        if cursor + chunk_size > raw_body.len() {
            return Err(FetchError::NetworkError(
                "Truncated chunked HTTP payload".into(),
            ));
        }

        decoded.extend_from_slice(&raw_body[cursor..cursor + chunk_size]);
        cursor += chunk_size;

        if cursor + 2 <= raw_body.len() && &raw_body[cursor..cursor + 2] == b"\r\n" {
            cursor += 2;
        }
    }

    Ok(decoded)
}

/// Maximum allowable HTTP header size in bytes (RFC 7230 / Slowloris DoS prevention).
pub const MAX_HEADER_SIZE: usize = 64 * 1024; // 64 KB

/// Maximum allowable HTTP response body size in bytes (128 MB).
pub const MAX_RESPONSE_BODY_SIZE: usize = 128 * 1024 * 1024; // 128 MB

/// Maximum allowable decompressed payload size in bytes (Decompression bomb protection).
pub const MAX_DECOMPRESSED_SIZE: usize = 128 * 1024 * 1024; // 128 MB

/// Decompresses body bytes based on HTTP Content-Encoding (gzip, deflate, br) with
/// strict decompression bomb expansion limits.
pub fn decompress_body(raw_body: &[u8], encoding: &str) -> Result<Vec<u8>, FetchError> {
    let lower = encoding.trim().to_ascii_lowercase();
    match lower.as_str() {
        "gzip" => {
            let mut decoder = flate2::read::GzDecoder::new(raw_body);
            let mut decompressed = Vec::new();
            let mut limited = (&mut decoder).take((MAX_DECOMPRESSED_SIZE + 1) as u64);
            limited
                .read_to_end(&mut decompressed)
                .map_err(|e| FetchError::NetworkError(format!("Gzip decompression failed: {e}")))?;
            if decompressed.len() > MAX_DECOMPRESSED_SIZE {
                return Err(FetchError::NetworkError(format!(
                    "Decompressed payload exceeded maximum limit of {MAX_DECOMPRESSED_SIZE} bytes (decompression bomb protection)"
                )));
            }
            Ok(decompressed)
        }
        "deflate" => {
            let mut decoder = flate2::read::ZlibDecoder::new(raw_body);
            let mut decompressed = Vec::new();
            let mut limited = (&mut decoder).take((MAX_DECOMPRESSED_SIZE + 1) as u64);
            if limited.read_to_end(&mut decompressed).is_ok() {
                if decompressed.len() > MAX_DECOMPRESSED_SIZE {
                    return Err(FetchError::NetworkError(format!(
                        "Decompressed payload exceeded maximum limit of {MAX_DECOMPRESSED_SIZE} bytes (decompression bomb protection)"
                    )));
                }
                Ok(decompressed)
            } else {
                let mut raw_decoder = flate2::read::DeflateDecoder::new(raw_body);
                let mut raw_decompressed = Vec::new();
                let mut raw_limited = (&mut raw_decoder).take((MAX_DECOMPRESSED_SIZE + 1) as u64);
                raw_limited
                    .read_to_end(&mut raw_decompressed)
                    .map_err(|e| {
                        FetchError::NetworkError(format!("Deflate decompression failed: {e}"))
                    })?;
                if raw_decompressed.len() > MAX_DECOMPRESSED_SIZE {
                    return Err(FetchError::NetworkError(format!(
                        "Decompressed payload exceeded maximum limit of {MAX_DECOMPRESSED_SIZE} bytes (decompression bomb protection)"
                    )));
                }
                Ok(raw_decompressed)
            }
        }
        "br" | "brotli" => {
            let mut decompressor = brotli::Decompressor::new(raw_body, 4096);
            let mut decompressed = Vec::new();
            let mut limited = (&mut decompressor).take((MAX_DECOMPRESSED_SIZE + 1) as u64);
            limited.read_to_end(&mut decompressed).map_err(|e| {
                FetchError::NetworkError(format!("Brotli decompression failed: {e:?}"))
            })?;
            if decompressed.len() > MAX_DECOMPRESSED_SIZE {
                return Err(FetchError::NetworkError(format!(
                    "Decompressed payload exceeded maximum limit of {MAX_DECOMPRESSED_SIZE} bytes (decompression bomb protection)"
                )));
            }
            Ok(decompressed)
        }
        "identity" | "" => Ok(raw_body.to_vec()),
        _ => Ok(raw_body.to_vec()),
    }
}

/// Reads an HTTP/1.1 response from a synchronous byte stream, enforcing RFC 7230 framing.
/// Terminates immediately upon reading `Content-Length` bytes or the terminal chunk `0\r\n\r\n`,
/// preventing hangs on persistent keep-alive connections.
pub fn read_http_response<R: Read>(reader: &mut R) -> Result<Response, FetchError> {
    let mut buffer = Vec::with_capacity(4096);
    let mut header_end = None;

    // 1. Read headers incrementally until \r\n\r\n with strict 64 KB limit
    let mut temp = [0u8; 1024];
    while header_end.is_none() {
        let n = reader
            .read(&mut temp)
            .map_err(|e| FetchError::NetworkError(e.to_string()))?;
        if n == 0 {
            break;
        }
        buffer.extend_from_slice(&temp[..n]);
        if buffer.len() > MAX_HEADER_SIZE {
            return Err(FetchError::NetworkError(format!(
                "HTTP header section exceeded {MAX_HEADER_SIZE} bytes limit (RFC 7230 DoS protection)"
            )));
        }
        header_end = buffer.windows(4).position(|w| w == b"\r\n\r\n");
    }

    let Some(header_end_pos) = header_end else {
        return Err(FetchError::NetworkError(
            "Malformed HTTP response: no header delimiter".into(),
        ));
    };

    let header_str = String::from_utf8_lossy(&buffer[..header_end_pos]);
    let mut lines = header_str.split("\r\n");

    let status_line = lines
        .next()
        .ok_or_else(|| FetchError::NetworkError("Missing HTTP status line".into()))?;

    let mut status_parts = status_line.splitn(3, ' ');
    let _version = status_parts.next();
    let status_code_str = status_parts.next().unwrap_or("0");
    let status_text = status_parts.next().unwrap_or("");
    let status_code = status_code_str.parse::<u16>().unwrap_or(0);

    let mut headers = Headers::new();
    for line in lines {
        if let Some((k, v)) = line.split_once(':') {
            headers.append(k.trim(), v.trim());
        }
    }

    // Residual bytes already read into buffer after the \r\n\r\n delimiter
    let initial_body = buffer[header_end_pos + 4..].to_vec();

    // Responses to 1xx, 204 No Content, 304 Not Modified have no body per RFC 7230 §3.3.3
    if (100..200).contains(&status_code) || status_code == 204 || status_code == 304 {
        return Ok(Response::new(status_code, status_text, headers, Vec::new()));
    }

    let is_chunked = headers
        .get("transfer-encoding")
        .map(|v| v.eq_ignore_ascii_case("chunked"))
        .unwrap_or(false);

    let raw_body = if is_chunked {
        read_chunked_stream(reader, initial_body)?
    } else if let Some(cl_str) = headers.get("content-length") {
        let content_length = cl_str.trim().parse::<usize>().map_err(|e| {
            FetchError::NetworkError(format!("Invalid Content-Length '{cl_str}': {e}"))
        })?;
        read_fixed_length_body(reader, initial_body, content_length)?
    } else {
        // Delimited by connection closure (RFC 7230 Section 3.3.3 #7)
        let mut full_body = initial_body;
        let mut eof_buf = [0u8; 4096];
        loop {
            match reader.read(&mut eof_buf) {
                Ok(0) => break,
                Ok(n) => {
                    if full_body.len() + n > MAX_RESPONSE_BODY_SIZE {
                        return Err(FetchError::NetworkError(format!(
                            "Response body exceeded maximum allowable size ({MAX_RESPONSE_BODY_SIZE} bytes)"
                        )));
                    }
                    full_body.extend_from_slice(&eof_buf[..n]);
                }
                Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => break,
                Err(e) => return Err(FetchError::NetworkError(e.to_string())),
            }
        }
        full_body
    };

    let body = if let Some(encoding) = headers.get("content-encoding") {
        decompress_body(&raw_body, encoding)?
    } else {
        raw_body
    };

    Ok(Response::new(status_code, status_text, headers, body))
}

fn read_fixed_length_body<R: Read>(
    reader: &mut R,
    mut initial: Vec<u8>,
    content_length: usize,
) -> Result<Vec<u8>, FetchError> {
    if content_length > MAX_RESPONSE_BODY_SIZE {
        return Err(FetchError::NetworkError(format!(
            "Content-Length {content_length} exceeds maximum allowable body size ({MAX_RESPONSE_BODY_SIZE} bytes)"
        )));
    }
    if initial.len() >= content_length {
        initial.truncate(content_length);
        return Ok(initial);
    }

    let remaining = content_length - initial.len();
    let mut temp = [0u8; 16384];
    let mut bytes_read = 0;
    while bytes_read < remaining {
        let to_read = (remaining - bytes_read).min(temp.len());
        let n = reader
            .read(&mut temp[..to_read])
            .map_err(|e| FetchError::NetworkError(format!("Error reading body: {e}")))?;
        if n == 0 {
            return Err(FetchError::NetworkError(format!(
                "Truncated HTTP body: expected {content_length} bytes, received {}",
                initial.len() + bytes_read
            )));
        }
        initial.extend_from_slice(&temp[..n]);
        bytes_read += n;
    }
    Ok(initial)
}

fn read_chunked_stream<R: Read>(reader: &mut R, initial: Vec<u8>) -> Result<Vec<u8>, FetchError> {
    let mut decoded = Vec::new();
    let mut stream_buf = initial;
    let mut temp = [0u8; 4096];

    loop {
        let newline_pos = loop {
            if let Some(pos) = stream_buf.windows(2).position(|w| w == b"\r\n") {
                break pos;
            }
            let n = reader
                .read(&mut temp)
                .map_err(|e| FetchError::NetworkError(e.to_string()))?;
            if n == 0 {
                return Err(FetchError::NetworkError(
                    "Unexpected EOF while reading chunk size".into(),
                ));
            }
            stream_buf.extend_from_slice(&temp[..n]);
        };

        let size_str = std::str::from_utf8(&stream_buf[..newline_pos])
            .map_err(|e| FetchError::NetworkError(format!("Invalid chunk size UTF-8: {e}")))?
            .trim();
        let size_hex = size_str.split(';').next().unwrap_or(size_str).trim();
        let chunk_size = usize::from_str_radix(size_hex, 16).map_err(|e| {
            FetchError::NetworkError(format!("Invalid hex chunk size '{size_hex}': {e}"))
        })?;

        stream_buf.drain(..newline_pos + 2);

        if chunk_size == 0 {
            // Drain trailing CRLF / trailers
            while !stream_buf.starts_with(b"\r\n") {
                if stream_buf.is_empty() {
                    let n = reader
                        .read(&mut temp)
                        .map_err(|e| FetchError::NetworkError(e.to_string()))?;
                    if n == 0 {
                        break;
                    }
                    stream_buf.extend_from_slice(&temp[..n]);
                } else if let Some(end) = stream_buf.windows(2).position(|w| w == b"\r\n") {
                    stream_buf.drain(..end + 2);
                    break;
                } else {
                    break;
                }
            }
            if stream_buf.starts_with(b"\r\n") {
                stream_buf.drain(..2);
            }
            break;
        }

        if chunk_size > MAX_RESPONSE_BODY_SIZE
            || decoded.len() + chunk_size > MAX_RESPONSE_BODY_SIZE
        {
            return Err(FetchError::NetworkError(format!(
                "Chunked response body exceeded maximum allowable size ({MAX_RESPONSE_BODY_SIZE} bytes)"
            )));
        }

        let total_needed = chunk_size + 2;
        while stream_buf.len() < total_needed {
            let n = reader
                .read(&mut temp)
                .map_err(|e| FetchError::NetworkError(e.to_string()))?;
            if n == 0 {
                return Err(FetchError::NetworkError(
                    "Unexpected EOF while reading chunk payload".into(),
                ));
            }
            stream_buf.extend_from_slice(&temp[..n]);
        }

        decoded.extend_from_slice(&stream_buf[..chunk_size]);
        stream_buf.drain(..total_needed);
    }

    Ok(decoded)
}

/// Parses raw HTTP/1.1 response bytes into a structured `Response`.
pub fn parse_http_response(raw: &[u8]) -> Result<Response, FetchError> {
    let mut cursor = Cursor::new(raw);
    read_http_response(&mut cursor)
}