From bf35ed468097039ad294bfc3ef51677581b2ce48 Mon Sep 17 00:00:00 2001 From: silverwizard Date: Thu, 2 Feb 2023 00:18:21 -0500 Subject: [PATCH] Fixed main - next step to make it properly iterate --- src/main.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1f67214..f333a33 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,12 +12,6 @@ struct Asset; fn main() { let listener = TcpListener::bind("0.0.0.0:7878").unwrap(); - for file in Asset::iter() { - let page = Asset::get(&file.to_string()).unwrap(); - println!("{:?}", std::str::from_utf8(page.data.as_ref())); - } - - for stream in listener.incoming() { let stream = stream.unwrap(); @@ -31,14 +25,23 @@ fn handle_connection(mut stream: TcpStream) { let mut content = "Error 404

Not Found

"; let mut page; let mut status_line = "HTTP/1.1 404 NOT FOUND"; - if request_line == "GET / HTTP/1.1" { - status_line = "HTTP/1.1 200 OK"; - page = Asset::get("index.html").unwrap(); - content = std::str::from_utf8(page.data.as_ref()).unwrap(); - }else if request_line == "GET /style.css HTTP/1.1" { - status_line = "HTTP/1.1 200 OK"; - page = Asset::get("style.css").unwrap(); - content = std::str::from_utf8(page.data.as_ref()).unwrap(); + let end = request_line.len()-9; + let keyword = &request_line[0..3]; + let path = &request_line[5..end]; + let version = &request_line[(end+1)..request_line.len()]; + println!("Line length: {}{}{}", keyword, path, version); + if keyword == "GET" { + if version == "HTTP/1.1" { + if path == "index.html" || path == "" { + status_line = "HTTP/1.1 200 OK"; + page = Asset::get("index.html").unwrap(); + content = std::str::from_utf8(page.data.as_ref()).unwrap(); + }else if path == "style.css" { + status_line = "HTTP/1.1 200 OK"; + page = Asset::get("style.css").unwrap(); + content = std::str::from_utf8(page.data.as_ref()).unwrap(); + } + } } let status_line = "HTTP/1.1 200 OK"; let length = content.len();