use std::io::Read as _;
use std::net::TcpStream;
#[derive(Debug, Clone)]
pub struct Recorded {
pub method: String,
pub path: String,
pub headers: Vec<(String, String)>,
pub body: String,
}
pub fn read_request(connection: &mut TcpStream) -> Option<Recorded> {
let mut raw = Vec::new();
let mut byte = [0_u8; 1];
while !raw.ends_with(b"\r\n\r\n") {
match connection.read(&mut byte) {
Ok(0) | Err(_) => return None,
Ok(_) => raw.push(byte[0]),
}
}
let head = String::from_utf8_lossy(&raw).into_owned();
let mut lines = head.split("\r\n");
let mut start = lines.next()?.split_whitespace();
let method = start.next()?.to_owned();
let path = start.next()?.to_owned();
let mut headers = Vec::new();
for line in lines {
if let Some((name, value)) = line.split_once(':') {
headers.push((name.trim().to_ascii_lowercase(), value.trim().to_owned()));
}
}
let length: usize = headers
.iter()
.find(|(name, _)| name == "content-length")
.and_then(|(_, value)| value.parse().ok())
.unwrap_or(0);
let mut body = vec![0_u8; length];
if length > 0 && connection.read_exact(&mut body).is_err() {
return None;
}
Some(Recorded {
method,
path,
headers,
body: String::from_utf8_lossy(&body).into_owned(),
})
}