diff --git a/README.md b/README.md index e22004d..2f33a8c 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,4 @@ -### TODO: -6. limit storage, maybe with pseudorandomness, find a way to clean old threads -7. better html and css -8. optimize/clean code, improve memory usage +# kchan ### Rules + single page diff --git a/src/errors.rs b/src/errors.rs index 4278819..ce99b57 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -76,6 +76,6 @@ impl From for Response { HandlingError::ServerError(e) => Status::from(e), HandlingError::Io(e) => Status::InternalServerError, }; - Response::new(status, vec![], status.message().to_string()) + Response::new(status, vec![], status.message().into()) } } diff --git a/src/http.rs b/src/http.rs index 6fc526c..4579642 100644 --- a/src/http.rs +++ b/src/http.rs @@ -18,7 +18,7 @@ pub struct Request { pub struct Response { pub status: Status, pub headers: Vec<(String, String)>, - pub body: String, // make into Option> + pub body: Vec, // make into Option> } #[derive(Debug, Clone, Copy)] @@ -31,7 +31,7 @@ pub enum Status { InternalServerError = 500, } -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum Method { Get, Post, @@ -114,7 +114,7 @@ impl TryFrom> for Request { } impl Response { - pub fn new(status: Status, headers: Vec<(&str, &str)>, body: String) -> Self { + pub fn new(status: Status, headers: Vec<(&str, &str)>, body: Vec) -> Self { Self { status, headers: headers @@ -124,24 +124,24 @@ impl Response { body, } } -} -impl fmt::Display for Response { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let headers: String = self - .headers - .iter() - .map(|x| format!("{}:{}\n", x.0, x.1)) - .fold(String::from(""), |a, b| format!("{}{}", a, b)); // possibly slow - - write!( - f, - "{} {} {}\n{}\n\n{}", - HTTP_VERSION, - self.status as i32, - self.status.message(), - headers, - self.body, - ) + pub fn respond(&self) -> Vec { + let mut res = vec![]; + res.extend_from_slice(HTTP_VERSION.as_bytes()); + res.extend_from_slice(b" "); + res.extend_from_slice(&(self.status as i32).to_string().as_bytes()); // network endianness + res.extend_from_slice(b" "); + res.extend_from_slice(self.status.message().as_bytes()); + res.extend_from_slice(b"\r\n"); + for (i, j) in self.headers.iter() { + res.extend_from_slice(i.as_bytes()); + res.extend_from_slice(b":"); + res.extend_from_slice(j.as_bytes()); + res.extend_from_slice(b"\r\n"); + } + res.extend_from_slice(b"\r\n"); + res.extend_from_slice(&self.body); + res } } + diff --git a/src/main.rs b/src/main.rs index 3d75b69..0852bb6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -186,15 +186,16 @@ fn handle(mut reader: BufReader<&mut TcpStream>) -> Result = reader - .bytes() - .take(s.1.parse::().unwrap()) - .collect::, io::Error>>() - .unwrap(); - // TODO test if it works - // FIXME deunwrap - // TODO handle body too large - request.add_body(body); + if request.method == Method::Post { + let body: Vec = reader + .bytes() + .take(s.1.parse::().unwrap()) + .collect::, io::Error>>() + .unwrap(); + // FIXME deunwrap + // TODO handle body too large + request.add_body(body); + } } dbg!(&request); @@ -220,14 +221,12 @@ fn get(path: &str) -> Result { .fold(String::from(""), |a, b| format!("{a}\n{b}")); let c = content("index", &ops); - Ok(Response::new(Status::Ok, vec![], c)) + Ok(Response::new(Status::Ok, vec![], c.into())) } // TODO favicon.ico - "/css" => Ok(Response::new(Status::Ok, vec![], String::from(STYLE))), - - "/faq" => Ok(Response::new(Status::Ok, vec![], String::from(FAQ))), - //"/favicon.ico" => Ok(Response::new(Status::Ok, vec![("content-type", "image/x-icon")], FAVICON)), - // TODO favicon + "/css" => Ok(Response::new(Status::Ok, vec![], String::from(STYLE).into())), + "/faq" => Ok(Response::new(Status::Ok, vec![], String::from(FAQ).into())), + "/favicon.ico" => Ok(Response::new(Status::Ok, vec![("content-type", "image/x-icon")], FAVICON.to_vec())), // list specific thread here // FIXME unwrap hell @@ -244,7 +243,7 @@ fn get(path: &str) -> Result { .fold(String::from(""), |a, b| format!("{a}\n{b}")); let c = content(&id.to_string(), &c); - Ok(Response::new(Status::Ok, vec![], c)) + Ok(Response::new(Status::Ok, vec![], c.into())) } } } @@ -266,7 +265,7 @@ fn post(request: Request) -> Result { Ok(Response::new( Status::SeeOther, vec![("location", "/")], - String::from(""), + vec![], )) } @@ -287,7 +286,7 @@ fn post(request: Request) -> Result { Ok(Response::new( Status::SeeOther, vec![("location", s)], - String::from(""), + vec![], )) } } @@ -335,7 +334,7 @@ fn main() { }; // handle request - stream.write(response.to_string().as_bytes()).unwrap(); + stream.write(&response.respond()).unwrap(); }); } } diff --git a/src/post.rs b/src/post.rs index 304f88a..bb7f30a 100644 --- a/src/post.rs +++ b/src/post.rs @@ -36,17 +36,6 @@ impl Thread { } } -//impl Thread { -// pub fn head(&self) -> String { -// let first = &self.0[0]; -// format!( -// "
{}
{}
", -// first.id, -// first.to_string(), -// ) -// } -//} - impl Post { pub fn new(id: u32, body: String) -> Self { Self { @@ -57,6 +46,7 @@ impl Post { } } +// TODO move this to `www/` impl fmt::Display for Post { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( diff --git a/src/www/favicon.ico b/src/www/favicon.ico index f1d213d..6bce58d 100644 Binary files a/src/www/favicon.ico and b/src/www/favicon.ico differ