use std::io::Read;
use std::net::TcpStream;
pub(crate) fn read_full_request(stream: &mut TcpStream) -> Vec<u8> {
let mut buf = [0u8; 8192];
let mut total = Vec::new();
loop {
let n = stream.read(&mut buf).unwrap_or(0);
if n == 0 {
break;
}
total.extend_from_slice(&buf[..n]);
let Some(pos) = total.windows(4).position(|w| w == b"\r\n\r\n") else {
continue;
};
let content_length: usize = String::from_utf8_lossy(&total[..pos])
.lines()
.find(|l| l.to_lowercase().starts_with("content-length:"))
.and_then(|l| l.split(':').nth(1))
.and_then(|v| v.trim().parse().ok())
.unwrap_or(0);
if total.len() >= pos + 4 + content_length {
break;
}
}
total
}