|
|
|
|
|
|
|
|
None => { |
|
|
None => { |
|
|
println!("This wasn't even a well-formed header"); |
|
|
println!("This wasn't even a well-formed header"); |
|
|
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n"); |
|
|
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"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
} else { |
|
|
println!("It didn't start with GET!"); |
|
|
println!("It didn't start with GET!"); |
|
|
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n"); |
|
|
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); |
|
|
// println!("Sending {}: {}", r.header, r.path); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// println!("Request: {}", String::from_utf8_lossy(&buffer[..])); |
|
|
// 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) { |
|
|
fn route(c: &str, mut r: &mut Response) { |
|
|
|
|
|
|
|
|
let mut routes = vec![]; |
|
|
let mut routes = vec![]; |
|
|
|
|
|
|
|
|
if c == "" { |
|
|
if c == "" { |
|
|
println!("Asked to send root!"); |
|
|
println!("Asked to send root!"); |
|
|
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
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/") { |
|
|
} else if c.starts_with("api/") { |
|
|
let mut iter_routes = routes.into_iter(); |
|
|
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 { |
|
|
match valid_route { |
|
|
Some(rt) => { |
|
|
Some(rt) => { |
|
|
(rt.get_action())(&c, &mut r); |
|
|
(rt.get_action())(&c, &mut r); |
|
|
|
|
|
|
|
|
} else { |
|
|
} else { |
|
|
println!("Asked to send {} and I could not find it", &c); |
|
|
println!("Asked to send {} and I could not find it", &c); |
|
|
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n"); |
|
|
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"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if Path::new(&cs).exists() { |
|
|
if Path::new(&cs).exists() { |
|
|
println!("Asked to send {}", &cs); |
|
|
println!("Asked to send {}", &cs); |
|
|
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
response_add_file(&cs, &mut r); |
|
|
|
|
|
|
|
|
r.add_file_contents(&cs); |
|
|
} else { |
|
|
} else { |
|
|
println!("Asked to send {} and I couldn't find it", &cs); |
|
|
println!("Asked to send {} and I couldn't find it", &cs); |
|
|
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n"); |
|
|
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 { |
|
|
} else { |
|
|
println!("Asked to send {}", &c); |
|
|
println!("Asked to send {}", &c); |
|
|
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
response_add_file(&c, &mut r); |
|
|
|
|
|
|
|
|
r.add_file_contents(&c); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|