From fdfeae97b25bc5972ffc16fca7b959d2436c2645 Mon Sep 17 00:00:00 2001 From: p11 Date: Fri, 21 Jul 2023 20:06:17 +0200 Subject: [PATCH] Moved the real handling of the request to the site info struct --- src/main.rs | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1c5bc56..d064d5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -202,10 +202,35 @@ impl Context { ), }; + info.handle_connection( + stream, + &request.body, + &relative_path, + &path, + &pk_path, + pki_path.as_ref().map(|path| path.as_ref()), + &data_path, + start_level, + ) + } +} + +impl SiteInfo { + fn handle_connection( + &mut self, + mut stream: TcpStream, + body: &str, + relative_path: &str, + path: &Path, + pk_path: &Path, + pki_path: Option<&Path>, + data_path: &Path, + start_level: usize, + ) { let mut name = None; let mut text = None; - for entry in request.body.split('&') { + for entry in body.split('&') { let Some((key, input)) = entry.split_once('=') else { continue; }; @@ -238,20 +263,20 @@ impl Context { .into(), ); } - "up" => info.up += 1, - "down" => info.down += 1, + "up" => self.up += 1, + "down" => self.down += 1, _ => (), } } if let (Some(name), Some(text)) = (name, text) { - info.comments.push(Comment { name, text }); + self.comments.push(Comment { name, text }); } - info.visits += 1; + self.visits += 1; if let Ok(mut file) = File::create(data_path) { - if to_stream::(info, &mut file).is_err() { + if to_stream::(self, &mut file).is_err() { eprintln!("Error saving data!"); eprintln!(); } @@ -264,22 +289,20 @@ impl Context { let _ = writeln!( stream, "

👁️{} 💖️{} 💔️{}

", - info.visits, info.up, info.down + self.visits, self.up, self.down ); let title = relative_path .rsplit_once('/') .map(|(_, title)| title) - .unwrap_or(&relative_path); + .unwrap_or(relative_path); if !title.is_empty() { let _ = writeln!(stream, "

{title}

"); } - let path_ref = &path; - let handle_entry = |mut entry: &str, output: &mut TcpStream, level: usize| { let level = level + 1; - let mut path = path_ref.clone(); + let mut path = path.to_path_buf(); if let Some((real_entry, _)) = entry.split_once(':') { entry = real_entry } @@ -363,7 +386,7 @@ impl Context { }; let _ = stream.write_all(html.into_string().as_bytes()); - for Comment { name, text } in &info.comments { + for Comment { name, text } in &self.comments { let _ = writeln!(stream, "
{name}{text}
"); }