|
|
@@ -47,7 +47,7 @@ fn handle_connection(mut stream: TcpStream) { |
|
|
|
None => { |
|
|
|
println!("This wasn't even a well-formed header"); |
|
|
|
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n"); |
|
|
|
response_add_file("404.html", &mut r); |
|
|
|
r.add_file_content("404.html"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@@ -55,7 +55,7 @@ fn handle_connection(mut stream: TcpStream) { |
|
|
|
} else { |
|
|
|
println!("It didn't start with GET!"); |
|
|
|
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n"); |
|
|
|
response_add_file("404.html", &mut r); |
|
|
|
r.add_file_content("404.html"); |
|
|
|
} |
|
|
|
// println!("Sending {}: {}", r.header, r.path); |
|
|
|
|
|
|
@@ -69,10 +69,6 @@ fn handle_connection(mut stream: TcpStream) { |
|
|
|
// println!("Request: {}", String::from_utf8_lossy(&buffer[..])); |
|
|
|
} |
|
|
|
|
|
|
|
fn response_add_file(p: &str, r: &mut Response) { |
|
|
|
r.set_content(fs::read_to_string(p).unwrap().as_str()); |
|
|
|
} |
|
|
|
|
|
|
|
fn route(c: &str, mut r: &mut Response) { |
|
|
|
|
|
|
|
let mut routes = vec![]; |
|
|
@@ -86,10 +82,10 @@ fn route(c: &str, mut r: &mut Response) { |
|
|
|
if c == "" { |
|
|
|
println!("Asked to send root!"); |
|
|
|
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
|
response_add_file("index.html", &mut r); |
|
|
|
r.add_file_content("index.html"); |
|
|
|
} else if c.starts_with("api/") { |
|
|
|
let mut iter_routes = routes.into_iter(); |
|
|
|
let valid_route = iter_routes.find(|&&x| x.get_path() == c); |
|
|
|
let valid_route = iter_routes.find(|x| x.get_path() == c); |
|
|
|
match valid_route { |
|
|
|
Some(rt) => { |
|
|
|
(rt.get_action())(&c, &mut r); |
|
|
@@ -103,7 +99,7 @@ fn route(c: &str, mut r: &mut Response) { |
|
|
|
} else { |
|
|
|
println!("Asked to send {} and I could not find it", &c); |
|
|
|
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n"); |
|
|
|
response_add_file("404.html", &mut r); |
|
|
|
r.add_file_contents("404.html"); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
@@ -118,16 +114,16 @@ fn route_basic(c: &&str, mut r: &mut Response) { |
|
|
|
if Path::new(&cs).exists() { |
|
|
|
println!("Asked to send {}", &cs); |
|
|
|
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
|
response_add_file(&cs, &mut r); |
|
|
|
r.add_file_contents(&cs); |
|
|
|
} else { |
|
|
|
println!("Asked to send {} and I couldn't find it", &cs); |
|
|
|
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n"); |
|
|
|
response_add_file("404.html", &mut r); |
|
|
|
r.add_file_contents("404.html"); |
|
|
|
} |
|
|
|
} else { |
|
|
|
println!("Asked to send {}", &c); |
|
|
|
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
|
response_add_file(&c, &mut r); |
|
|
|
r.add_file_contents(&c); |
|
|
|
} |
|
|
|
} |
|
|
|
|