|
|
@@ -1,12 +1,12 @@ |
|
|
|
use std::net::{TcpStream, TcpListener}; |
|
|
|
use std::io::prelude::*; |
|
|
|
use std::fs; |
|
|
|
// use std::fs; |
|
|
|
// use std::thread; |
|
|
|
// use std::time::Duration; |
|
|
|
// use std::str; |
|
|
|
use std::path::Path; |
|
|
|
|
|
|
|
use hello::{ThreadPool, Response, Route}; |
|
|
|
use webserv::{ThreadPool, Response, Route}; |
|
|
|
|
|
|
|
extern crate regex; |
|
|
|
use regex::Regex; |
|
|
@@ -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"); |
|
|
|
r.add_file_content("404.html"); |
|
|
|
r.add_file_contents("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"); |
|
|
|
r.add_file_content("404.html"); |
|
|
|
r.add_file_contents("404.html"); |
|
|
|
} |
|
|
|
// println!("Sending {}: {}", r.header, r.path); |
|
|
|
|
|
|
@@ -74,15 +74,13 @@ fn route(c: &str, mut r: &mut Response) { |
|
|
|
let mut routes = vec![]; |
|
|
|
routes.push(Route::new("api/hi", hi)); |
|
|
|
routes.push(Route::new("api/bye", bye)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// println!("Asked to fetch {}", c); |
|
|
|
//assert!(Path::new(c).exists()); |
|
|
|
if c == "" { |
|
|
|
println!("Asked to send root!"); |
|
|
|
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
|
r.add_file_content("index.html"); |
|
|
|
r.add_file_contents("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); |
|
|
@@ -91,7 +89,7 @@ fn route(c: &str, mut r: &mut Response) { |
|
|
|
(rt.get_action())(&c, &mut r); |
|
|
|
}, |
|
|
|
None => { |
|
|
|
none_api(&c, &mut r); |
|
|
|
api_none(&c, &mut r); |
|
|
|
} |
|
|
|
} |
|
|
|
} else if Path::new(c).exists() { |
|
|
@@ -104,7 +102,7 @@ fn route(c: &str, mut r: &mut Response) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
fn route_basic(c: &&str, mut r: &mut Response) { |
|
|
|
fn route_basic(c: &&str, r: &mut Response) { |
|
|
|
if Path::new(c).is_dir() { |
|
|
|
let mut cs = c.to_string(); |
|
|
|
if cs.chars().last().unwrap().to_string() != "/" { |
|
|
@@ -127,7 +125,7 @@ fn route_basic(c: &&str, mut r: &mut Response) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
fn none_api(_c: &&str, r: &mut Response) { |
|
|
|
fn api_none(_c: &&str, r: &mut Response) { |
|
|
|
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n"); |
|
|
|
r.set_content("There is currently no API endpoint at that address."); |
|
|
|
} |