use std::io::{Cursor, Read};
use peekable::PeekExt;
fn main() -> std::io::Result<()> {
let stream = Cursor::new(b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello".to_vec());
let mut peekable = stream.peekable();
let mut magic = [0u8; 4];
peekable.peek_exact(&mut magic)?;
let kind = match &magic {
b"HTTP" => "HTTP",
b"SSH-" => "SSH",
[0x16, 0x03, 0x01..=0x03, _] => "TLS",
_ => "unknown",
};
println!("[sync] detected protocol: {kind}");
let mut full = String::new();
peekable.read_to_string(&mut full)?;
assert!(
full.starts_with("HTTP"),
"peeked bytes should still be readable"
);
println!("[sync] full payload ({} bytes):\n{}", full.len(), full);
Ok(())
}