use super::{method::Method, uri::URI, header::HeaderMap}; #[derive(Debug, Clone)] pub struct Request { pub method: Method, pub uri: URI, pub headers: HeaderMap, pub body: Option } impl Request { pub fn serialize(req: &str) -> Option { let mut lines = req.split('\n').to_owned(); let Some(head) = lines.next() else { eprintln!("missing head str"); return None }; let mut parts = head.trim().split(" "); let Some(method_str) = parts.next() else { eprintln!("missing method str"); return None }; let Some(method) = Method::serialize(method_str.trim()) else { eprintln!("invalid http method"); return None }; let Some(uri_str) = parts.next() else { eprintln!("missing uri str"); return None }; let Some(uri) = URI::serialize(uri_str.trim()) else { eprintln!("invalid http uri"); return None }; let headers = HeaderMap::serialize(&mut lines); let body: String = lines.collect(); Some(Self { method, uri, headers, body: if body.len() > 0 { Some(body) } else { None }, }) } }