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