// #![feature(if_let_guard)] use std::{ collections::HashMap, io::{ BufRead, BufReader, Write }, net::TcpListener }; use anyhow::Result; mod utils; use utils::*; fn main() -> Result<()> { let listener = TcpListener::bind("127.0.0.1:4221").unwrap(); for stream in listener.incoming() { match stream { Ok(mut stream) => { let buf_reader = BufReader::new(&mut stream); let mut data = buf_reader .lines() .map(|result| result.unwrap()) .take_while(|line| !line.is_empty()); let (_method, path, _ver) = { let start_line = data.next().ok_or(E::InvalidRequest)?; // should be 500; let mut parts = start_line.split_whitespace().map(ToOwned::to_owned); let method = parts.next().ok_or(E::InvalidRequest)?; let path = parts.next().ok_or(E::InvalidRequest)?; let ver = parts.next().ok_or(E::InvalidRequest)?; (method, path, ver) }; let headers = Headers::parse(data); let response = match path.trim_start_matches("/") { "" => Response::Empty, "user-agent" => Response::TextPlain(headers.get("User-Agent")), // p if let Some(echo) = p.strip_prefix("/echo/") => Response::TextPlain(echo), // a nicer way to do that, not available in stable yet p if p.starts_with("echo/") => Response::TextPlain(p.trim_start_matches("echo/")), _ => Response::_404, }; println!("accepted new connection"); let _ = stream.write(response.build().as_bytes()); let _ = stream.flush(); } Err(e) => { println!("error: {}", e); } } } Ok(()) } pub struct Headers (HashMap); impl Headers { pub fn parse (lines: impl Iterator) -> Self { Self(HashMap::from_iter(lines.filter_map(|line| line.split_once(":") .map(|(a, b)| ( a.trim().to_lowercase(), b.trim().to_owned() )) ))) } pub fn get <'a> (&'a self, key: &str) -> &'a str { self.0.get(&key.to_lowercase()).map(|e| e.as_str()).unwrap_or_default() } } #[derive(Debug, Clone)] enum Response <'a> { _404, Empty, TextPlain (&'a str), } #[allow(non_upper_case_globals)] impl Response <'_> { fn build (self) -> String { let (code, body) = match self { Self::_404 => ("404 Not Found", d!()), Self::Empty => ("200 OK", d!()), Self::TextPlain(text) => ("200 OK", text) }; let headers = self.headers().join("\r\n"); f!("HTTP/1.1 {code}\r\n{headers}\r\n\r\n{body}").into() } fn headers (&self) -> Vec { match self { Self::TextPlain(text) => vec![f!("Content-Type: text/plain"), format!("Content-Length: {}", text.len())], _ => d!() } } }