diff --git a/src/main.rs b/src/main.rs index bc3534b..36ee637 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,23 +4,25 @@ use std::fs; use std::string::{String}; use std::str; -fn get_page() -> String { +fn get_page(request: String) -> String { // To-do: Make this able to load other pages // // The loaded page should be left immutable as it does // not need to be modified by the server. - let filename = "index.html"; // BAD!!! - let page = fs::read_to_string(filename) - .expect("Page read failed!"); + let mut filename = "index.html"; - return page; + let page = fs::read_to_string(filename); + match page { + Ok(i) => i.to_string(), + Err(e) => "

404 Not Found

The resource you are trying to locate cannot be accessed!

".to_string(), + } } -fn read_stream(mut stream: TcpStream) -> Result { - let mut buffer = BufReader::new(stream.try_clone()?); - - Ok(stream) +fn read_stream(mut stream: TcpStream) { + // To-do: move streaming reading here + + return; } fn serve(mut stream: TcpStream) { @@ -30,11 +32,11 @@ fn serve(mut stream: TcpStream) { let mut reader = BufReader::new(&mut stream); reader .read_until(b'\n', &mut request) - .expect("Failed to read from stream"); + .expect("Failed to read from stream!"); - println!("{}", String::from_utf8_lossy(&request)); + // println!("{}", String::from_utf8_lossy(&request)); - let contents = get_page(); + let contents = get_page(String::from_utf8_lossy(&request).to_string()); let header = "HTTP/1.1 200 OK\r\n\r\n"; let response = format!("{}{}", header, contents);