stage 4 attempt 1

This commit is contained in:
YK 2024-05-10 21:33:15 +03:00
parent f28811942b
commit 5fb0b9478e

View File

@ -1,7 +1,12 @@
use std::{ fmt::Display, io::{BufRead, BufReader, Read, Write}, net::TcpListener }; #![feature(if_let_guard)]
use std::{ fmt::Display, io::{ BufRead, BufReader, Write }, net::TcpListener };
use anyhow::Result; use anyhow::Result;
use thiserror::Error; use thiserror::Error;
macro_rules! d { () => { Default::default() }; }
macro_rules! f { ($s: expr) => { format!($s) }; }
#[derive(Clone, Debug, Error)] #[derive(Clone, Debug, Error)]
enum E { enum E {
#[error("Invalid request data found during parsing")] #[error("Invalid request data found during parsing")]
@ -31,15 +36,15 @@ fn main() -> Result<()> {
(method, path, ver) (method, path, ver)
}; };
let code = match path.as_str() { let response = match path.as_str() {
"/" => "200 OK", "/" => Response::Empty,
_ => "404 Not Found", p if let Some(echo) = p.strip_prefix("/echo/") => Response::TextPlain(echo),
_ => Response::_404,
}; };
println!("accepted new connection"); println!("accepted new connection");
let resp = create_response_simple(code);
let _ = stream.write(&resp); let _ = stream.write(response.build().as_bytes());
let _ = stream.flush(); let _ = stream.flush();
} }
@ -52,7 +57,33 @@ fn main() -> Result<()> {
Ok(()) Ok(())
} }
#[derive(Debug, Clone)]
fn create_response_simple <T> (code: T) -> Vec<u8> where T: Display { enum Response <'a> {
format!("HTTP/1.1 {code}\r\n\r\n").into() _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<String> {
match self {
Self::TextPlain(text) => vec![f!("Content-Type: text/plain"), format!("Content-Length: {}", text.len())],
_ => d!()
}
}
} }