webio 0.5.0-alpha

A minimalist, high-performance web framework for Rust built with a zero-dependency philosophy.
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
//! # WebIO šŸ¦…
//! 
//! A minimalist, high-performance web framework for Rust built with a **zero-dependency philosophy**.
//! By utilizing only the Rust Standard Library (`std`), WebIO achieves an ultra-light footprint, 
//! lightning-fast compilation, and a transparent security audit trail.
//! 
//! ## šŸ”¬ The Rationale: WebIO vs. External Runtimes (Tokio/Hyper)
//! 
//! Most modern Rust web frameworks (like Axum or Actix) are built on top of **Tokio**, 
//! a high-performance asynchronous event-loop. While excellent for handling millions 
//! of *idle* connections (like a chat app), Tokio faces a "Cooperative Scheduling" 
//! challenge in **Data Science** and **Computational** environments.
//! 
//! ### 🧠 The "Event Loop Freeze" Problem
//! In a traditional async runtime, tasks must "yield" voluntarily to the executor. If a 
//! single request performs a heavy 10-second mathematical calculation—such as a 
//! regression model, matrix inversion, or a large CSV parse—the **entire event loop 
//! thread is blocked**. This creates a "Stop-the-World" scenario where one heavy 
//! CPU task freezes the responses for all other users on that executor.
//! 
//! ### šŸš€ The WebIO Solution: Go-Inspired OS Pre-emption
//! WebIO is engineered as a specialized core engine for projects like **Fluxor**, 
//! where mathematical precision and memory predictability are the highest priority. 
//! 
//! **WebIO mimics the Go concurrency model** by spawning a unique OS thread for every 
//! incoming connection. This ensures non-blocking I/O and high efficiency for both 
//! small JSON/HTML responses and large "Big Data" streaming uploads.
//! 
//! * **The Safety Distinction:** While WebIO adopts the task-per-request strategy 
//!   found in Go, it enforces **Rust’s Ownership Model**. This provides the 
//!   concurrency simplicity of Go without the unpredictable latency of a 
//!   Garbage Collector (GC). Deterministic memory management ensures no 
//!   "GC pauses" occur during heavy mathematical calculations.
//! * **OS-Level Isolation:** Utilizing **OS Threads** managed by the Kernel 
//!   achieves pre-emptive multitasking. If one handler is 100% occupied by 
//!   heavy math on Core 1, the OS automatically ensures other threads remain 
//!   responsive on other cores. This architecture eliminates the risk of 
//!   "blocking the loop."
//! * **Safe-Turbo Bridge:** While WebIO uses OS threads for isolation, a 
//!   specialized **`block_on`** executor allows the use of `async` logic 
//!   (like calling an external API or database) inside threads without 
//!   the bloat of a massive dependency tree.
//! * **Zero-RAM Big Data:** Raw `TcpStream` access enables moving 100GB+ 
//!   datasets directly from the socket to disk in 64KB chunks. This 
//!   bypasses the 10MB RAM safety guard, ensuring the engine remains 
//!   stable under massive data ingestion.
//!
//! ## šŸ› ļø Performance Architecture
//! * **Hybrid Spin-Wait Strategy:** The `block_on` executor uses a 
//!   150k-cycle spin-loop to catch I/O ready states in nanoseconds, 
//!   maintaining sub-millisecond tail latency by bypassing OS scheduler 
//!   jitter for "hot" tasks.
//! * **Smart RAM Cache:** Transparently caches hot assets (<500KB) 
//!   using an `RwLock` to provide **~50µs** RAM-speed delivery for 
//!   CSS/JS/JSON, while large files stream safely from the Disk.
//! * **Zero-Dependency Integrity:** By strictly avoiding external crates, 
//!   WebIO is immune to "supply chain attacks" and remains a pure, 
//!   high-performance core for Computing Science and Mathematical applications.
//! * **Nagle Control:** Granular builder-pattern control over TCP 
//!   throughput vs. latency (set_nagle) optimizes for either 
//!   real-time APIs or massive data syncs.

#![doc = include_str!("../README.md")]

use std::{
    collections::HashMap,
    future::Future,
    hint,
    io::{Read, Write, BufWriter},
    net::{TcpListener, TcpStream, Shutdown},
    pin::{Pin, pin},
    sync::{Arc, Mutex, LazyLock, RwLock},
    task::{Context, Poll, Waker},
    time::{Instant, SystemTime, UNIX_EPOCH},
    thread
};

use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};

// --- Core Traits & Types ---

// --- STREAMING ARCHITECTURE ---

/// A specialized trait for asynchronous data delivery, optimized for zero-copy 
/// and low-memory footprints in High-Integrity Computing environments.
/// 
/// `BodyStream` is the core mechanism that allows WebIO to serve multi-gigabyte 
/// datasets (e.g., large CSVs, models, or video streams) while maintaining a 
/// constant, predictable RAM overhead. By breaking payloads into discrete 
/// chunks, the engine avoids the "RAM Spike" common in traditional frameworks 
/// that buffer entire responses before transmission.
pub trait BodyStream: Send {
    /// Retrieves the next available data fragment for the HTTP/1.1 Chunked 
    /// Transfer Encoding lifecycle. 
    /// 
    /// * **Returns `Some(Vec<u8>)`:** Indicates a valid data chunk is ready to 
    ///   be written to the socket.
    /// * **Returns `None`:** Indicates the stream is exhausted, signaling 
    ///   the engine to send the final 0-length termination chunk.
    fn next_chunk(&mut self) -> Option<Vec<u8>>; 
}

/// A standard implementation for static, in-memory payloads.
/// 
/// This implementation allows standard byte vectors to be treated as streams. 
/// Utilizing `std::mem::take`, it ensures that the data is moved efficiently 
/// to the network writer without redundant heap allocations.
impl BodyStream for Vec<u8> {
    fn next_chunk(&mut self) -> Option<Vec<u8>> {
        if self.is_empty() { 
            None 
        } else {
            // Memory Optimization: Swap the internal vector with an empty one 
            // to transfer ownership to the caller in O(1) time.
            Some(std::mem::take(self)) 
        }
    }
}

// --- DATA TRANSFORMATION TRAITS --

/// A polymorphic conversion trait designed to normalize heterogeneous data 
/// sources into a unified byte representation. 
/// 
/// `IntoBytes` serves as the foundational abstraction layer for the WebIO 
/// response engine. It enables the builder-pattern `.body()` method to 
/// accept a variety of common Rust types—such as heap-allocated strings, 
/// static string slices, or raw byte vectors—without requiring the 
/// developer to perform manual transformations. 
/// 
/// By abstracting these conversions, WebIO maintains a clean, high-level API 
/// while ensuring that the underlying **Computing Science** logic receives 
/// a consistent binary stream for transmission.
pub trait IntoBytes {
    /// Consumes the source value and transforms it into a standard byte vector.
    fn into_bytes(self) -> Vec<u8>; 
}

/// Specialized implementation for heap-allocated strings.
/// This utilizes the standard library's zero-copy conversion where possible.
impl IntoBytes for String { 
    fn into_bytes(self) -> Vec<u8> { self.into_bytes() } 
}

/// Implementation for static or borrowed string slices.
/// Performs a deterministic allocation to create an owned byte representation 
/// suitable for the asynchronous response lifecycle.
impl IntoBytes for &str { 
    fn into_bytes(self) -> Vec<u8> { self.as_bytes().to_vec() } 
}

/// Identity implementation for pre-formatted binary data.
/// This allows direct, zero-overhead passing of binary datasets, 
/// such as serialized models or image fragments.
impl IntoBytes for Vec<u8> { 
    fn into_bytes(self) -> Vec<u8> { self } 
}

/// Represents an incoming HTTP/WebSocket request.
/// 
/// Request metadata (method, path, headers) and a 10MB safety-limited body 
/// are pre-parsed into memory. For large datasets exceeding the RAM limit, 
/// the raw `stream` field provides direct access to the socket.
pub struct Req {
    /// HTTP method (e.g., "GET", "POST").
    pub method: String,
    /// Full request path (e.g., "/user/123?query=true").
    pub path: String,
    /// Parsed request body (limited to 10MB to prevent RAM exhaustion).
    pub body: String,
    /// Metadata headers mapped from the raw HTTP text.
    pub headers: HashMap<String, String>,
    /// Raw socket access for Zero-RAM Big Data streaming.
    /// Enables handling multi-gigabyte uploads without RAM bloat.
    pub stream: TcpStream,
}

// --- REQUEST IMPLEMENTATION ---

impl Req {
    /// Parses the request body as a URL-encoded form (application/x-www-form-urlencoded).
    /// 
    /// This utility provides rapid, deterministic access to key-value pairs submitted via 
    /// HTML POST forms or serialized metadata strings. By utilizing an internal 
    /// parsing logic with `replace` for standard '+' space decoding, it maintains 
    /// high efficiency for typical metadata ingestion tasks.
    pub fn form(&self) -> HashMap<String, String> {
        let mut map = HashMap::new();
        for pair in self.body.split('&') {
            let mut kv = pair.split('=');
            if let (Some(k), Some(v)) = (kv.next(), kv.next()) {
                // Space decoding per HTTP standards
                let decoded_v = v.replace('+', " "); 
                map.insert(k.to_string(), decoded_v);
            }
        }
        map
    }

    /// Performs Zero-RAM Big Data ingestion by streaming the raw socket data directly 
    /// to the filesystem.
    /// 
    /// This method is the primary mechanism for handling massive datasets (e.g., multi-gigabyte 
    /// CSVs, models, or video files) without triggering the 10MB RAM safety guard. 
    /// Utilizing a high-throughput 64KB buffer and a `BufWriter`, it ensures maximum 
    /// I/O performance while maintaining a constant, negligible memory footprint. 
    /// 
    /// ### Reliability:
    /// It returns the total number of bytes successfully written, providing 
    /// a precise integrity check for high-concurrency data science pipelines.
    pub fn save_to_file(&mut self, path: &str) -> std::io::Result<u64> {
        let file = std::fs::File::create(path)?;
        let mut writer = BufWriter::new(file);
        let mut buffer = [0; 65536]; // Optimized 64KB fragment
        let mut total = 0;

        while let Ok(n) = self.stream.read(&mut buffer) {
            if n == 0 { break; }
            writer.write_all(&buffer[..n])?;
            total += n as u64;
        }
        writer.flush()?;
        Ok(total)
    }

    /// Upgrades the active HTTP connection to a persistent WebSocket.
    /// 
    /// This method facilitates the transition from the standard request-response 
    /// lifecycle to a full-duplex, persistent communication state (RFC 6455). 
    /// By hijacking the existing `TcpStream`, it allows handlers to establish 
    /// low-latency, real-time data feeds essential for live computational 
    /// monitoring or interactive collaborative tools.
    /// 
    /// ### Lifecycle:
    /// Upon a successful handshake (101 Switching Protocols), the handler 
    /// gains exclusive ownership of the socket, effectively isolating the 
    /// WebSocket thread from the general HTTP routing engine.
    pub fn upgrade_websocket(&self) -> Option<TcpStream> {
        let key = self.headers.get("sec-websocket-key")?;
        
        // RFC 6455 Cryptographic Handshake Logic
        let accept_key = derive_websocket_accept(key); 

        let response = format!(
            "HTTP/1.1 101 Switching Protocols\r\n\
             Upgrade: websocket\r\n\
             Connection: Upgrade\r\n\
             Sec-WebSocket-Accept: {}\r\n\r\n",
            accept_key
        );

        let mut s = self.stream.try_clone().ok()?;
        let _ = s.write_all(response.as_bytes());
        Some(s)
    }
}

// --- CRYPTOGRAPHIC PRIMITIVES ---

/// A specialized, zero-dependency implementation of the SHA-1 Hashing Algorithm (FIPS 180-1).
/// 
/// In accordance with WebIO's **Zero-Dependency Philosophy**, this internal 
/// implementation facilitates the cryptographic handshake required for RFC 6455 
/// WebSocket upgrades without the binary bloat or supply-chain risks of external 
/// crates. It ensures that the framework's core remains lightweight, portable, 
/// and fully auditable.
struct Sha1 {
    /// Internal 160-bit state represented as five 32-bit words.
    state: [u32; 5],
    /// Processing buffer for 512-bit message blocks.
    buffer: Vec<u8>,
    /// Total message bit length used for final padding.
    count: u64,
}

impl Sha1 {
    /// Initializes the SHA-1 state with the standard cryptographic constants.
    fn new() -> Self {
        Self {
            state: [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0],
            buffer: Vec::new(),
            count: 0,
        }
    }

    /// Ingests a byte slice into the hashing engine, updating the bit count.
    fn update(&mut self, data: &[u8]) {
        self.buffer.extend_from_slice(data);
        self.count += (data.len() * 8) as u64;
    }

    /// Finalizes the hashing process by applying FIPS-standard padding 
    /// and processing remaining chunks.
    /// 
    /// Returns a deterministic 20-byte (160-bit) message digest.
    fn finalize(mut self) -> [u8; 20] {
        // Append bit '1' followed by '0's for block alignment
        self.buffer.push(0x80);
        while (self.buffer.len() % 64) != 56 { self.buffer.push(0); }

        // Append 64-bit big-endian integer representing original bit length
        self.buffer.extend_from_slice(&self.count.to_be_bytes());
        
        // Core transformation loop: process 512-bit (64-byte) blocks
        for chunk in self.buffer.chunks_exact(64) {
            let mut w = [0u32; 80];
            for i in 0..16 {
                w[i] = u32::from_be_bytes([chunk[i*4], chunk[i*4+1], chunk[i*4+2], chunk[i*4+3]]);
            }
            // Expand 16 words into 80 words
            for i in 16..80 {
                w[i] = (w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]).rotate_left(1);
            }
            let [mut a, mut b, mut c, mut d, mut e] = self.state;
            // 80 Rounds of hashing transformation
            for i in 0..80 {
                let (f, k) = match i {
                    0..=19 => ((b & c) | (!b & d), 0x5A827999),
                    20..=39 => (b ^ c ^ d, 0x6ED9EBA1),
                    40..=59 => ((b & c) | (b & d) | (c & d), 0x8F1BBCDC),
                    _ => (b ^ c ^ d, 0xCA62C1D6),
                };
                let temp = a.rotate_left(5).wrapping_add(f).wrapping_add(e).wrapping_add(k).wrapping_add(w[i]);
                e = d; d = c; c = b.rotate_left(30); b = a; a = temp;
            }
            // Accumulate the transformation back into the internal state
            self.state[0] = self.state[0].wrapping_add(a);
            self.state[1] = self.state[1].wrapping_add(b);
            self.state[2] = self.state[2].wrapping_add(c);
            self.state[3] = self.state[3].wrapping_add(d);
            self.state[4] = self.state[4].wrapping_add(e);
        }
        // Flatten state words into final byte array
        let mut out = [0u8; 20];
        for i in 0..5 { out[i*4..(i+1)*4].copy_from_slice(&self.state[i].to_be_bytes()); }
        out
    }
}

// --- DATA ENCODING UTILITIES ---

/// A specialized, zero-dependency implementation of Base64 Encoding (RFC 4648).
/// 
/// In accordance with the **Zero-Dependency Philosophy**, this internal utility 
/// facilitates the binary-to-text transformation required for the WebSocket 
/// cryptographic handshake. By avoiding external encoding crates, WebIO 
/// maintains a deterministic memory footprint and a minimal binary size, 
/// essential for high-integrity **Computational Science** environments.
/// 
/// ### Algorithm Detail:
/// The encoder processes data in 24-bit fragments (3 bytes), mapping them 
/// into four 6-bit indices against the standard 64-character alphabet. 
/// Standard padding characters (`=`) are appended to ensure the output 
/// string length is always a multiple of four.
fn base64_encode(input: &[u8]) -> String {
    const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    // Memory Optimization: Pre-allocate the exact required capacity
    // to prevent redundant heap reallocations during the encoding process.
    let mut result = String::with_capacity((input.len() + 2) / 3 * 4);
    for chunk in input.chunks(3) {
        // Concatenate 8-bit bytes into a 32-bit register for bit-shifting.
        let b = match chunk.len() {
            3 => (chunk[0] as u32) << 16 | (chunk[1] as u32) << 8 | (chunk[2] as u32),
            2 => (chunk[0] as u32) << 16 | (chunk[1] as u32) << 8,
            _ => (chunk[0] as u32) << 16,
        };
        // Extract 6-bit indices and map to the CHARSET
        result.push(CHARSET[(b >> 18 & 0x3F) as usize] as char);
        result.push(CHARSET[(b >> 12 & 0x3F) as usize] as char);
        // Handle deterministic padding for incomplete 24-bit blocks
        if chunk.len() > 1 { result.push(CHARSET[(b >> 6 & 0x3F) as usize] as char); } else { result.push('='); }
        if chunk.len() > 2 { result.push(CHARSET[(b & 0x3F) as usize] as char); } else { result.push('='); }
    }
    result
}

// --- WEBSOCKET PROTOCOL ENGINE ---

/// Represents a discrete data packet within the WebSocket protocol.
/// 
/// In a multi-threaded **Computational Science** environment, `WSFrame` facilitates 
/// the real-time ingestion of binary or text data fragments. By isolating frame 
/// parsing into a dedicated structure, WebIO maintains a clean separation between 
/// standard HTTP request-response cycles and persistent, full-duplex streams.
pub struct WSFrame {
    /// The unmasked binary payload extracted from the WebSocket frame.
    pub payload: Vec<u8>,
}

impl WSFrame {
    /// Ingests and decodes a raw WebSocket frame directly from the network stream.
    /// 
    /// This method implements the core framing logic defined in **RFC 6455**. 
    /// It handles dynamic payload length detection and performs the cryptographic 
    /// XOR unmasking required for client-to-server security. 
    /// 
    /// ### Technical Implementation:
    /// * **Length Handling:** Supports small (7-bit), medium (16-bit), and 
    ///   "Big Data" (64-bit) payload lengths, enabling the transmission of 
    ///   massive datasets over a persistent socket.
    /// * **Bitwise Masking:** Complies with the mandatory masking requirement 
    ///   for client frames, ensuring the data is correctly de-obfuscated before 
    ///   being passed to the application layer.
    /// * **I/O Efficiency:** Utilizes `read_exact` to ensure frame integrity, 
    ///   preventing partial reads that could corrupt high-precision data streams.
    pub fn read(stream: &mut TcpStream) -> std::io::Result<Self> {
        let mut head = [0u8; 2];
        stream.read_exact(&mut head)?;

        // The second byte contains the Mask bit (MSB) and Payload Length (7 bits)
        let is_masked = (head[1] & 0x80) != 0;
        let mut len = (head[1] & 0x7F) as usize;

        // Dynamic Length Decoding: Handles extended 16-bit or 64-bit length fields
        if len == 126 {
            let mut extended_len = [0u8; 2];
            stream.read_exact(&mut extended_len)?;
            len = u16::from_be_bytes(extended_len) as usize;
        } else if len == 127 {
            let mut extended_len = [0u8; 8];
            stream.read_exact(&mut extended_len)?;
            len = u64::from_be_bytes(extended_len) as usize;
        }

        // Memory Allocation: Pre-allocates the payload buffer based on the decoded length
        let mut payload = vec![0u8; len];

        if is_masked {
            let mut mask = [0u8; 4];
            stream.read_exact(&mut mask)?;
            stream.read_exact(&mut payload)?;
            // Cryptographic XOR Unmasking (RFC 6455 Section 5.3)
            // Essential for maintaining protocol safety and data integrity.
            for i in 0..len {
                payload[i] ^= mask[i % 4];
            }
        } else {
            // Unmasked frames from clients are technically a protocol violation,
            // but the logic provides a fallback for non-compliant implementations.
            stream.read_exact(&mut payload)?;
        }

        Ok(WSFrame { payload })
    }
}

//// A collection of dynamic URL segments extracted from the request path.
/// 
/// If a route is defined as `/user/<id>`, and the request is `/user/42`, 
/// the Params will contain `{"id": "42"}`.
pub struct Params(pub HashMap<String, String>);

/// Standard HTTP Status Codes. Using a `u16` representation ensures
/// compatibility with the HTTP protocol while providing type-safe common codes.
#[derive(Copy, Clone)]
#[repr(u16)]
pub enum StatusCode { 
    Ok = 200, 
    Unauthorized = 401, 
    Forbidden = 403, 
    NotFound = 404,
    InternalError = 500,
}

/// The outgoing HTTP response. 
/// Uses a `Box<dyn BodyStream>` to allow for flexible, memory-efficient body types.
pub struct Reply {
    pub status: u16,
    pub headers: HashMap<String, String>,
    pub body: Box<dyn BodyStream>,
}

impl Reply {
    /// Creates a new response with a specific status code.
    pub fn new(status: StatusCode) -> Self {
        Self { status: status as u16, headers: HashMap::new(), body: Box::new(Vec::new()) }
    }

    /// Builder pattern method to add headers to the response.
    pub fn header(mut self, key: &str, value: &str) -> Self {
        self.headers.insert(key.to_string(), value.to_string());
        self
    }

    /// Sets the response body. Accepts any type implementing [`IntoBytes`].
    pub fn body<T: IntoBytes>(mut self, data: T) -> Self {
        self.body = Box::new(data.into_bytes());
        self
    }
}

/// Type alias for a pinned, thread-safe future. 
/// Necessary for handling async route logic without an external runtime.
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

/// Represents a route handler. 
/// Receives the Request and Path Params, returning an async Response.
pub type Handler = Box<dyn Fn(Req, Params) -> BoxFuture<'static, Reply> + Send + Sync>;

/// Middleware signature for early-exit logic (e.g., Auth or Logging).
pub type Middleware = Box<dyn Fn(&str) -> Option<Reply> + Send + Sync>;

/// Supported HTTP Methods.
pub enum Method { GET, POST }
pub use Method::*;

// --- WebIo Engine ---

/// The central Application controller.
/// Manages routing, middleware, and the internal TCP lifecycle.
pub struct WebIo {
    routes: Vec<(String, String, Handler)>,
    mw: Option<Middleware>,
    handlers_404: HashMap<String, Handler>,
    static_dir: String,
    pub log_reply_enabled: bool,
    pub nagle_enabled: bool,
    pub clients: Arc<Mutex<Vec<TcpStream>>>,
    pub cache: Arc<RwLock<HashMap<String, Vec<u8>>>>,
}

impl WebIo {
    /// Initializes a new WebIo instance with an empty routing table.
    pub fn new() -> Self { 
        Self { 
            routes: Vec::new(), 
            mw: None, 
            handlers_404: HashMap::new() ,
            static_dir: "assets".to_string(), // Default name ==> "assets"
            log_reply_enabled: false, // default value
            nagle_enabled: true, // Default to ON (High Throughput)
            clients: Arc::new(Mutex::new(Vec::new())), 
            cache: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    pub fn broadcast(&self, message: &str) {
        // Lock the global client list
        let mut clients = self.clients.lock().expect("Registry Lock Poisoned");
        
        // Wrap the text message in the binary WebSocket protocol
        let frame = encode_ws_frame(message);
        
        // Send to everyone and automatically clean up "ghost" connections
        clients.retain_mut(|client| {
            client.write_all(&frame).is_ok()
        });
    }


    /// Toggles TCP_NODELAY (Nagle's Algorithm)
    pub fn set_nagle(mut self, enabled: bool) -> Self {
        self.nagle_enabled = enabled;
        self
    }

    /// Logs the details of an HTTP reply, including method, path, status code, and processing time.
    /// 
    /// # Parameters
    /// - `method`: The HTTP method of the handled request (e.g., "GET", "POST").
    /// - `path`: The URL path that was processed (e.g., "/api/data").
    /// - `status`: The HTTP status code of the outgoing reply (e.g., 200, 404).
    /// - `start`: The `Instant` timestamp when the request started, used to calculate reply latency.
    /// - `should_log`: A boolean flag indicating whether to perform logging. If `false`, the function returns immediately.
    ///
    /// # Behavior
    /// - If `should_log` is `false`, no log is produced.
    /// - If `true`, logs a timestamped message showing the reply details and the elapsed time since `start`.
    /// - The timestamp is formatted as HH:MM:SS based on the current system time.
    /// - The log includes high-resolution timing (`start.elapsed()`) to measure the full lifecycle of the reply.
    fn log_reply(&self, method: &str, path: &str, status: u16, start: Instant, should_log: bool) {
        if !should_log {
            return;
        }
        
        // --- High-Resolution Performance Logging ---
        // We calculate the precise duration from the moment the TCP stream was accepted
        // until the final byte of the chunked response is flushed.
        let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
        println!(
            "[{:02}:{:02}:{:02}] {} {} -> {} ({:?})", 
            (now/3600)%24, (now/60)%60, now%60, 
            method, path, status, start.elapsed() // Direct high-resolution measurement
        );
    }

    /// Configures the root directory for serving static assets (CSS, JS, Images, etc.).
    /// Example: app.use_static("src/assets");
    pub fn use_static(&mut self, path: &str) {
        self.static_dir = path.to_string();
    }

    /// Internal helper to serve static files from the configured directory.
    async fn serve_static(&self, path: &str) -> Option<Reply> {
        let relative_path = path.trim_start_matches('/');
        let base_path = PathBuf::from(&self.static_dir);
        let target_path = base_path.join(relative_path);

        // 1. Direct Match (e.g., /css/style.css)
        if target_path.exists() && target_path.is_file() {
            return self.create_file_reply(&target_path);
        }

        // 2. Dynamic Discovery for favicon.ico
        // If the browser asks for /favicon.ico but it's not at the root, search all folders.
        if relative_path == "favicon.ico" {
            if let Some(found_path) = find_file_recursive(&base_path, "favicon.ico") {
                return self.create_file_reply(&found_path);
            }
        }

        None
    }

    fn create_file_reply(&self, path: &Path) -> Option<Reply> {
        let path_str = path.to_string_lossy().to_string();

        // --- STEP 1: FAST PATH (Check RAM Cache) ---
        {
            let cache = self.cache.read().unwrap();
            if let Some(content) = cache.get(&path_str) {
                return Some(self.build_reply_with_mime(path, content.clone()));
            }
        }

        // --- STEP 2: SLOW PATH (Read Disk & Save to RAM) ---

        if let Ok(content) = std::fs::read(path) {
            let mut cache = self.cache.write().unwrap();
            
            // ONLY CACHE SMALL FILES (e.g., under 500KB)
            // This keeps your 28ms CSS in RAM, but leaves 100MB CSVs on Disk.
            if content.len() < 500 * 1024 {
                cache.insert(path_str, content.clone());
            }
            
            return Some(self.build_reply_with_mime(path, content));
        }
        
        None
    }

    /// Helper: Logic for MIME types and building the Reply object
    fn build_reply_with_mime(&self, path: &Path, content: Vec<u8>) -> Reply {
        let ext = path.extension().and_then(|s| s.to_str()).unwrap_or("");
        
        let ct = match ext {
            "ico"  => "image/x-icon",
            "css"  => "text/css",
            "js"   => "application/javascript",
            "svg"  => "image/svg+xml",
            "png"  => "image/png",
            "jpg" | "jpeg" => "image/jpeg",
            "gif"  => "image/gif",
            "mp4"  => "video/mp4",
            _      => "application/octet-stream",
        };

        Reply::new(StatusCode::Ok)
            .header("Content-Type", ct)
            .body(content)
    }

    /// Registers a global middleware function. 
    /// If the middleware returns `Some(Reply)`, the request cycle ends early.
    pub fn use_mw<F>(&mut self, f: F) where F: Fn(&str) -> Option<Reply> + Send + Sync + 'static {
        self.mw = Some(Box::new(f));
    }

    /// Configures custom 404 handlers. 
    pub fn on_404<F, Fut>(&mut self, handler: F) 
    where 
        F: Fn(Req, Params) -> Fut + Send + Sync + 'static, 
        Fut: Future<Output = Reply> + Send + 'static,
    {
        let h: Handler = Box::new(move |r, p| Box::pin(handler(r, p)));

        // 1. Create a temporary listener and connect to it to get a valid TcpStream
        let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind dummy listener");
        let addr = listener.local_addr().expect("Failed to get local addr");
        let stream = TcpStream::connect(addr).expect("Failed to connect dummy stream");

        // 2. Build the dummy Req with the new stream field
        let dummy_req = Req { 
            method: "GET".into(), 
            path: "/404_sniff".into(), 
            body: "".into(), 
            headers: HashMap::new(),
            stream, // Added to satisfy the new Req struct
        };

        // 3. Execute the handler to "sniff" the Content-Type
        let sniff = block_on(h(dummy_req, Params(HashMap::new())));
        
        // 4. Categorize based on the headers HashMap
        let ct = sniff.headers.get("Content-Type").cloned().unwrap_or_default().to_lowercase();
        if ct.contains("json") {
            self.handlers_404.insert("json".to_string(), h);
        } else {
            self.handlers_404.insert("html".to_string(), h);
        }
    }

    // Defines a route with a specific method and path.
    /// Supports dynamic segments using `<name>` syntax (e.g., `/user/<id>`).
    pub fn route<F, Fut>(&mut self, method: Method, path: &str, handler: F)
    where F: Fn(Req, Params) -> Fut + Send + Sync + 'static, Fut: Future<Output = Reply> + Send + 'static,
    {
        let m = match method { GET => "GET", POST => "POST" }.to_string();
        self.routes.push((m, path.to_string(), Box::new(move |r, p| Box::pin(handler(r, p)))));
    }

    /// Initializes the Multi-Threaded TCP Listener and enters the primary execution loop.
    /// 
    /// This method serves as the entry point for the **Go-inspired concurrency model**, 
    /// binding the engine to a specific network interface and delegating every incoming 
    /// connection to a dedicated OS thread. This architecture ensures pre-emptive 
    /// multitasking, where computational-heavy tasks (e.g., matrix calculations) 
    /// cannot block the responsiveness of other network participants.
    ///
    /// ### Reliability & Safety:
    /// * **The Safety Valve (Thread Guard):** Implements an `AtomicUsize` concurrency 
    ///   throttle to prevent system resource exhaustion. If the active connection 
    ///   count exceeds `max_threads` (2,000), subsequent connections are gracefully 
    ///   discarded to maintain the stability of the host environment.
    /// * **Atomic Lifecycle Management:** Utilizes `Ordering::SeqCst` for deterministic 
    ///   tracking of thread entry and exit, ensuring the global state is consistent 
    ///   across all CPU cores.
    /// * **Nagle Optimization:** Dynamically applies the `TCP_NODELAY` configuration 
    ///   per-socket, allowing the engine to be tuned for either low-latency 
    ///   interactive APIs or high-throughput **Big Data** ingestion.
    pub fn run(self, host: &str, port: &str) {
        let listener = TcpListener::bind(format!("{}:{}", host, port)).expect("Bind failed");
        let app = Arc::new(self);
        
        // --- SAFETY VALVE: Thread Limit ---
        // Prevents "Thread-Bombing" attacks and protects OS kernel stability.
        let active_threads = Arc::new(AtomicUsize::new(0));
        let max_threads = 2000; // Typical safe limit for OS threads

        println!("šŸ¦… WebIO Multi-Threaded Engine Live: http://{}:{}", host, port);

        for stream in listener.incoming() {
            if let Ok(s) = stream {
                let a = Arc::clone(&app);
                let counter = Arc::clone(&active_threads);

                // Deterministic Concurrency Check
                if counter.load(Ordering::SeqCst) < max_threads {
                    counter.fetch_add(1, Ordering::SeqCst);
                    
                    // Toggle Nagle's Algorithm (TCP_NODELAY) based on engine config
                    let _ = s.set_nodelay(!app.nagle_enabled);

                    // OS-Level Isolation: 
                    // Move the connection to a dedicated thread for pre-emptive multitasking.
                    thread::spawn(move || {
                        a.handle_connection(s);
                        // Atomic Cleanup: 
                        // Decrement the counter regardless of connection outcome.
                        counter.fetch_sub(1, Ordering::SeqCst);
                    });
                } else {
                    // Resource Exhaustion Policy: Drop connection to preserve core stability.
                    println!("āš ļø Server Busy: Max threads reached!");
                }
            }
        }
    }

    // --- WORKER IMPLEMENTATION ---

    /// Internal worker function responsible for the full HTTP request-response lifecycle 
    /// within an isolated OS thread.
    /// 
    /// This function acts as the **Synchronous-to-Asynchronous Bridge**, utilizing 
    /// the `block_on` executor to drive asynchronous handlers while ensuring 
    /// computational isolation. It manages raw buffer parsing, metadata extraction, 
    /// RAM security enforcement, and dynamic route dispatching.
    ///
    /// ### Protocol Execution Phases:
    /// 1. **Initial Ingestion:** Reads the first 4KB of data from the socket to 
    ///    identify the HTTP verb, path, and header block.
    /// 2. **RAM Safety Guard:** Inspects the `Content-Length` header pre-emptively. 
    ///    If the payload exceeds 10MB, the connection is terminated with a 
    ///    `403 Forbidden` to protect the system's heap memory.
    /// 3. **Deterministic Body Extraction:** Uses `read_exact` to guarantee body 
    ///    integrity, ensuring the framework remains resilient against network jitter 
    ///    during high-concurrency **Big Data** ingestion.
    /// 4. **Dynamic Segment Routing:** Performs O(N) path analysis to extract 
    ///    `<name>` parameters, populating the `Params` collection for the handler.
    /// 5. **Async Future Resolution:** Bridges into the async world via `block_on`, 
    ///    driving the route handler or static asset delivery to completion.
    ///
    /// ### Thread Isolation:
    /// By running this worker in a dedicated OS thread, WebIO ensures that if a 
    /// handler enters a long-running mathematical loop, it does not impede 
    /// the I/O processing or `spin_wait` cycles of other active connections.
    fn handle_connection(&self, mut stream: TcpStream) {
        let start_time = Instant::now();
        
        // --- 1. TCP OPTIMIZATION ---
        // Dynamically toggle Nagle's algorithm based on engine settings.
        // set_nagle(false) -> Low Latency (Best for small JSON/HTML, PWA)
        // set_nagle(true)  -> High Throughput (Best for Big Data CSVs)
        let _ = stream.set_nodelay(!self.nagle_enabled); 
        let _ = stream.set_read_timeout(Some(std::time::Duration::from_millis(150)));

        // --- 2. HEADER INGESTION ---
        let mut buffer = [0; 4096];
        let n = match stream.read(&mut buffer) { Ok(n) if n > 0 => n, _ => return };
        
        let header_str = String::from_utf8_lossy(&buffer[..n]);
        let mut lines = header_str.lines();
        let first_line = lines.next().unwrap_or("");
        let parts: Vec<&str> = first_line.split_whitespace().collect();
        if parts.len() < 2 { return; } 
        let (method, full_path) = (parts[0], parts[1]);

        // Parse key-value metadata into a lowercase-indexed HashMap.
        let mut headers = HashMap::new();
        for line in lines {
            if line.is_empty() { break; }
            if let Some((k, v)) = line.split_once(": ") {
                headers.insert(k.to_lowercase(), v.to_string());
            }
        }

         // --- 3. RAM SAFETY GUARD ---
        let max_body_size = 10 * 1024 * 1024; // 10MB Threshold
        let content_length = headers.get("content-length")
            .and_then(|v| v.parse::<usize>().ok())
            .unwrap_or(0);

        // Pre-emptive rejection to prevent heap exhaustion.
        if content_length > max_body_size {
            let too_large = Reply::new(StatusCode::Forbidden).body("Payload Too Large (Max 10MB)");
            block_on(self.finalize(stream, too_large, method, full_path, start_time));
            return;
        }

        // --- 4. SMART PAYLOAD EXTRACTION ---
        // Protocol-accurate read to handle data spanning across multiple TCP packets.
        let body = if let Some(pos) = header_str.find("\r\n\r\n") {
            let initial_body = &header_str[pos + 4..];
            if initial_body.len() >= content_length {
                // Case A: Small body already inside the 4KB buffer
                initial_body[..content_length].to_string()
            } else {
                // Case B: Larger body requires further reading from the socket
                let mut remaining = vec![0u8; content_length - initial_body.len()];
                let _ = stream.read_exact(&mut remaining);
                format!("{}{}", initial_body, String::from_utf8_lossy(&remaining))
            }
        } else {
            String::new()
        };

        // --- 5. MIDDLEWARE EXECUTION ---
        if let Some(ref mw_func) = self.mw {
            if let Some(early_reply) = mw_func(full_path) {
                block_on(self.finalize(stream, early_reply, method, full_path, start_time));
                return;
            }
        }

        // --- 6. ROUTING & DISPATCH ---
        let path_only = full_path.split('?').next().unwrap_or("/");
        let mut final_params = HashMap::new();
        let mut active_handler: Option<&Handler> = None;
        let path_segments: Vec<&str> = path_only.split('/').filter(|s| !s.is_empty()).collect();

        for (r_method, r_path, handler) in &self.routes {
            if r_method != method { continue; }
            let route_segments: Vec<&str> = r_path.split('/').filter(|s| !s.is_empty()).collect();
            if route_segments.len() == path_segments.len() {
                let mut matches = true;
                let mut temp_params = HashMap::new();
                for (r_seg, p_seg) in route_segments.iter().zip(path_segments.iter()) {
                    if r_seg.starts_with('<') && r_seg.ends_with('>') {
                        temp_params.insert(r_seg[1..r_seg.len()-1].to_string(), p_seg.to_string());
                    } else if r_seg != p_seg { matches = false; break; }
                }
                if matches { final_params = temp_params; active_handler = Some(handler); break; }
            }
        }

        // --- 7. REQUEST OBJECTIFICATION ---
        let req = Req { 
            method: method.to_string(), 
            path: full_path.to_string(), 
            body, 
            headers,
            // Allows handlers to stream GBs of data directly to disk/DB
            // Pass cloned TcpStream to allow raw Big Data streaming in the handler.
            stream: stream.try_clone().expect("CRITICAL: Socket clone failed"),
        };
        
        // --- 8. RESPONSE GENERATION ---
        let reply = if let Some(handler) = active_handler {
            block_on(handler(req, Params(final_params)))
        } else if let Some(static_reply) = block_on(self.serve_static(path_only)) {
            static_reply
        } else {
            // Content-Type Aware Smart 404
            let accept = req.headers.get("accept").cloned().unwrap_or_default();
            let h_404 = if accept.contains("text/html") { 
                self.handlers_404.get("html") 
            } else { 
                self.handlers_404.get("json") 
            };

            if let Some(h) = h_404 {
                block_on(h(req, Params(HashMap::new())))
            } else {
                Reply::new(StatusCode::NotFound).body("404 Not Found")
            }
        };

        // --- PROTOCOL FINALIZATION ---
        block_on(self.finalize(stream, reply, method, full_path, start_time));
    }

    // --- PROTOCOL FINALIZATION & SERIALIZATION ---

    /// Finalizes the HTTP response by serializing metadata and streaming body fragments 
    /// directly to the network interface.
    /// 
    /// This method implements the **HTTP/1.1 Chunked Transfer Encoding** protocol, 
    /// providing a highly efficient mechanism for delivering large datasets (Big Data) 
    /// without requiring prior knowledge of the total payload size. 
    ///
    /// ### Performance Architecture:
    /// * **Syscall Minimization:** Utilizes a high-capacity (64KB) `BufWriter` to 
    ///   batch headers and initial data fragments. This minimizes the "Context Switch Tax" 
    ///   between user-space and the OS kernel, frequently enabling **50µs - 150µs** 
    ///   response times in local environments.
    /// * **Memory Safety:** By iterating through the `BodyStream` trait, the engine 
    ///   moves data in discrete chunks, preventing the allocation of massive byte 
    ///   vectors on the heap and ensuring a stable RAM profile regardless of file size.
    /// * **Low-Latency Finalization:** Employs `TCP_NODELAY` (when configured) to 
    ///   bypass Nagle's algorithm, ensuring that small metadata headers are transmitted 
    ///   instantly rather than being buffered by the network stack.
    /// * **Zero-Copy Serialization:** Response headers are pushed directly into the 
    ///   buffered writer, reducing intermediate string allocations during the 
    ///   serialization lifecycle.
    ///
    /// ### Diagnostic & Telemetry:
    /// Upon completion, the method triggers high-resolution telemetry logging, 
    /// capturing the precise duration from the initial `TcpStream` acceptance 
    /// to the final byte flush. Immediate socket shutdown follows to prevent 
    /// "hanging" connections and optimize resource reclamation.
    async fn finalize(&self, stream: TcpStream, reply: Reply, method: &str, path: &str, start: Instant) {
        {
            // Optimization: 64KB Buffer ensures headers and first chunks 
            // occupy a single MTU packet for maximum throughput.
            let mut writer = BufWriter::with_capacity(65536, &stream);
            
            // Construct the Status Line and Protocol Headers.
            // Transfer-Encoding: chunked is the foundation of WebIO's streaming engine.
            let mut head = format!(
                "HTTP/1.1 {} OK\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n", reply.status
            );

            for (k, v) in &reply.headers { 
                head.push_str(&format!("{}: {}\r\n", k, v)); 
            }
            head.push_str("\r\n");

            let _ = writer.write_all(head.as_bytes());

            // --- BIG DATA STREAMING LOOP ---
            // Consumes chunks from the BodyStream until exhaustion.
            let mut b = reply.body;
            while let Some(data) = b.next_chunk() {
                // Protocol Format: [Hex Size]\r\n[Payload]\r\n
                let _ = writer.write_all(format!("{:X}\r\n", data.len()).as_bytes());
                let _ = writer.write_all(&data);
                let _ = writer.write_all(b"\r\n");
            }

            // Termination: RFC-standard zero-length chunk
            let _ = writer.write_all(b"0\r\n\r\n");
            let _ = writer.flush();
        }
        
        // Final Telemetry: High-resolution timestamping
        self.log_reply(method, path, reply.status, start, self.log_reply_enabled);

        // Resource Reclamation: Force OS-level socket closure
        let _ = stream.shutdown(Shutdown::Both);
    }
}

// --- THE SAFE-TURBO EXECUTOR ---

/// Drives a [`Future`] to completion by blocking the current OS thread.
///
/// As a high-performance, zero-dependency blocking executor, `block_on` (formerly `launch`)
/// serves as the primary entry point for bridging the synchronous world of OS threads
/// into the asynchronous logic of WebIO. It is engineered to maintain ultra-low-latency 
/// performance, frequently achieving response times in the **70µs - 400µs** range.
///
/// ### Hybrid Spin-Wait Strategy:
/// To maintain **sub-100µs** response times essential for Data Science and Big Data 
/// throughput, the executor employs a two-phase polling strategy:
/// 
/// 1. **Spin-Phase (150,000 cycles):** Utilizes [`std::hint::spin_loop()`] to stay 
///    "on-core." This bypasses OS scheduler latency by catching I/O ready states in 
///    nanoseconds, ensuring the CPU remains dedicated to the task during brief stalls.
/// 2. **Yield-Phase:** If the future remains `Pending` after the spin budget, the 
///    executor calls [`std::thread::yield_now()`]. This prevents 100% CPU starvation 
///    during long-running stalls, allowing the Kernel to rebalance thread priorities.
///
/// ### Evolution: From Unsafe to Safe-Turbo:
/// Originally utilizing `unsafe` blocks for stack pinning and `RawWakerVTable` 
/// construction, the current implementation leverages modern, zero-cost abstractions 
/// from the Rust Standard Library:
/// - **Modern Wakers:** Employs [`Waker::noop()`] (Rust 1.77+), the most efficient 
///   possible "do-nothing" waker available.
/// - **Pinned Stability:** Uses [`std::pin::pin!`] to satisfy the pinning contract 
///   entirely within the safe-type system, eliminating Undefined Behavior (UB).
///
/// ### Zero-Dependency Philosophy:
/// By strictly utilizing `std` primitives, WebIO avoids the heavy binary footprint 
/// and complex task-stealing overhead of external runtimes like Tokio, making it 
/// ideal for high-integrity **Computational Science** microservices.
pub fn block_on<F: Future>(future: F) -> F::Output {
    // Pin the future to the stack safely using the standard macro.
    let mut future = pin!(future);
    
    // Create a zero-overhead waker for the polling context.
    let waker = Waker::noop(); 
    let mut cx = Context::from_waker(waker);
    
    let mut spins = 0u64;
    loop {
        match future.as_mut().poll(&mut cx) {
            Poll::Ready(v) => return v,
            Poll::Pending => {
                // Processor-level hint to keep the CPU core active.
                if spins < 150_000 { 
                    hint::spin_loop(); 
                    spins += 1;
                } else {
                    // Fallback to the OS scheduler to maintain system fairness.
                    thread::yield_now();
                    spins = 0;
                }
            }
        }
    }
}

// --- SYSTEM UTILITIES & BROADCAST ENGINE ---

/// Performs a depth-first recursive search for a specific filename within a directory tree.
/// 
/// This utility enables dynamic asset discovery, such as locating a `favicon.ico` 
/// hidden within deep subdirectories. By traversing the filesystem recursively, 
/// WebIO provides a flexible "Smart Static" delivery system that reduces 
/// manual route configuration for complex frontend directory structures.
fn find_file_recursive(dir: &std::path::Path, filename: &str) -> Option<std::path::PathBuf> {
    if !dir.is_dir() { return None; }
    
    // Check current scope: Priority 1
    let current_check = dir.join(filename);
    if current_check.exists() { return Some(current_check); }

    // Recursive Descent: Scan subdirectories
    if let Ok(entries) = std::fs::read_dir(dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                if let Some(found) = find_file_recursive(&path, filename) {
                    return Some(found);
                }
            }
        }
    }
    None
}

/// Generates the cryptographic `Sec-WebSocket-Accept` signature (RFC 6455).
/// 
/// This internal helper combines the client-provided key with the WebSocket 
/// "Magic String" (GUID) and performs a SHA-1 hash followed by Base64 encoding. 
/// This deterministic handshake is the security foundation of WebIO's 
/// zero-dependency WebSocket implementation.
fn derive_websocket_accept(key: &str) -> String {
    let mut hasher = Sha1::new();
    hasher.update(key.as_bytes());
    // The "Magic String" defined by the official IETF specification
    hasher.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
    base64_encode(&hasher.finalize())
}

/// Serializes a raw text message into a compliant WebSocket Binary Frame.
/// 
/// Following the **RFC 6455 Section 5.2** protocol, this function constructs 
/// unmasked server-to-client frames. It intelligently scales the frame header 
/// based on payload size, supporting everything from short status updates 
/// (7-bit length) to massive Big Data broadcasts (64-bit length).
fn encode_ws_frame(message: &str) -> Vec<u8> {
    let payload = message.as_bytes();
    let len = payload.len();
    let mut frame = Vec::new();

    // Opcode: 0x81 (Final Fragment + Text Frame)
    frame.push(0x81);

    // Dynamic Payload Length Encoding
    if len <= 125 {
        frame.push(len as u8);
    } else if len <= 65535 {
        frame.push(126);
        frame.extend_from_slice(&(len as u16).to_be_bytes());
    } else {
        // Support for multi-gigabyte real-time data streaming
        frame.push(127);
        frame.extend_from_slice(&(len as u64).to_be_bytes());
    }

    // Payload
    frame.extend_from_slice(payload);
    frame
}

/// A globally accessible, thread-safe registry of active WebSocket participants.
/// 
/// Utilizes [`LazyLock`] (Rust 1.80+) for zero-cost initialization and a 
/// [`Mutex`] to coordinate cross-thread access. This allows any OS thread 
/// within the WebIO engine to interact with the persistent socket pool.
pub static CLIENTS: LazyLock<Mutex<Vec<TcpStream>>> = LazyLock::new(|| {
    Mutex::new(Vec::new())
});

/// Disseminates a message to all active WebSocket clients across the entire engine.
/// 
/// `broadcast` is a high-level orchestration method that facilitates real-time 
/// synchronization between isolated OS threads. It automatically performs 
/// **Ghost Connection Cleanup**: if a write fails (e.g., client disconnected), 
/// the socket is atomically removed from the registry, ensuring RAM stability.
pub fn broadcast(message: &str) {
    if let Ok(mut clients) = CLIENTS.lock() {
        let frame = encode_ws_frame(message);
        clients.retain_mut(|client| {
            client.write_all(&frame).is_ok()
        });
    }
}