use std::collections::HashMap; use std::fmt; use crate::http_method::HttpMethod; pub struct Request { pub method: HttpMethod, path: String, http_version: String, headers: HashMap, // body: &'a [u8] } impl Request { pub fn new( method: HttpMethod , path: String , http_version: String , headers: HashMap) -> Request { return Request { method, path, http_version, headers }; } // TODO(andrew): Add some error handling. pub fn from(data: &str) -> Option { let lines = data.split("\r\n").collect::>(); let mut headers: HashMap = HashMap::new(); for line in &lines[1..] { let line = line.split(": ").collect::>(); headers.insert(String::from(line[0]), line[1..].join(" ")); } let first_line = lines[0].split(" ").collect::>(); let method = String::from(first_line[0]); match HttpMethod::parse(method) { Some(method) => return Some(Request::new( method, String::from(first_line[1]), String::from(first_line[2]), headers )), None => return None }; } } impl fmt::Display for Request { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { return write!(f, "Request({}, {})", self.method, self.path); } }