http-server-cc/src/main.rs

90 lines
2.5 KiB
Rust
Raw Normal View History

2024-05-10 18:33:15 +00:00
#![feature(if_let_guard)]
use std::{ fmt::Display, io::{ BufRead, BufReader, Write }, net::TcpListener };
2024-05-10 17:50:36 +00:00
use anyhow::Result;
use thiserror::Error;
2024-05-10 17:06:42 +00:00
2024-05-10 18:33:15 +00:00
macro_rules! d { () => { Default::default() }; }
macro_rules! f { ($s: expr) => { format!($s) }; }
2024-05-10 17:50:36 +00:00
#[derive(Clone, Debug, Error)]
enum E {
#[error("Invalid request data found during parsing")]
InvalidRequest,
}
fn main() -> Result<()> {
2024-05-10 17:23:14 +00:00
let listener = TcpListener::bind("127.0.0.1:4221").unwrap();
for stream in listener.incoming() {
match stream {
2024-05-10 17:26:28 +00:00
Ok(mut stream) => {
2024-05-10 18:07:14 +00:00
let buf_reader = BufReader::new(&mut stream);
2024-05-10 17:50:36 +00:00
2024-05-10 18:07:14 +00:00
let mut data = buf_reader
.lines()
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty());
2024-05-10 17:50:36 +00:00
2024-05-10 18:07:14 +00:00
let (_method, path, _ver) = {
2024-05-10 17:50:36 +00:00
let start_line = data.next().ok_or(E::InvalidRequest)?; // should be 500;
2024-05-10 18:07:14 +00:00
let mut parts = start_line.split_whitespace().map(ToOwned::to_owned);
2024-05-10 17:50:36 +00:00
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)
};
2024-05-10 18:33:15 +00:00
let response = match path.as_str() {
"/" => Response::Empty,
p if let Some(echo) = p.strip_prefix("/echo/") => Response::TextPlain(echo),
_ => Response::_404,
2024-05-10 17:50:36 +00:00
};
2024-05-10 17:23:14 +00:00
println!("accepted new connection");
2024-05-10 17:50:36 +00:00
2024-05-10 18:33:15 +00:00
let _ = stream.write(response.build().as_bytes());
2024-05-10 17:50:36 +00:00
let _ = stream.flush();
2024-05-10 17:23:14 +00:00
}
Err(e) => {
println!("error: {}", e);
}
}
}
2024-05-10 17:50:36 +00:00
Ok(())
}
2024-05-10 18:33:15 +00:00
#[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)
};
2024-05-10 17:50:36 +00:00
2024-05-10 18:33:15 +00:00
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<String> {
match self {
Self::TextPlain(text) => vec![f!("Content-Type: text/plain"), format!("Content-Length: {}", text.len())],
_ => d!()
}
}
2024-05-10 17:06:42 +00:00
}