|
|
@@ -6,16 +6,11 @@ use std::fs; |
|
|
|
// use std::str; |
|
|
|
use std::path::Path; |
|
|
|
|
|
|
|
use hello::ThreadPool; |
|
|
|
use hello::{ThreadPool, Response, Route}; |
|
|
|
|
|
|
|
extern crate regex; |
|
|
|
use regex::Regex; |
|
|
|
|
|
|
|
struct Response { |
|
|
|
header: String, |
|
|
|
path: String, |
|
|
|
} |
|
|
|
|
|
|
|
fn main() { |
|
|
|
let listener = TcpListener::bind("127.0.0.1:26382").unwrap(); |
|
|
|
let pool = ThreadPool::new(4); |
|
|
@@ -33,70 +28,38 @@ fn main() { |
|
|
|
fn handle_connection(mut stream: TcpStream) { |
|
|
|
let mut buffer = [0; 512]; |
|
|
|
stream.read(&mut buffer).unwrap(); |
|
|
|
|
|
|
|
let hdr = Regex::new(r"GET /([^ ]*) HTTP/1.1").unwrap(); |
|
|
|
let mut r = Response::new("",""); |
|
|
|
let bf = &String::from_utf8_lossy(&buffer[..]); |
|
|
|
// let get = b"GET / HTTP/1.1\r\n"; |
|
|
|
// let sleep = b"GET /sleep HTTP/1.1\r\n"; |
|
|
|
|
|
|
|
let mut r = Response{ |
|
|
|
header: String::from(""), |
|
|
|
path: String::from(""), |
|
|
|
}; |
|
|
|
|
|
|
|
if buffer.starts_with(b"GET") { |
|
|
|
|
|
|
|
let caps = hdr.captures(bf); |
|
|
|
if buffer.starts_with(b"GET") { |
|
|
|
let hdr = Regex::new(r"GET /([^ ]*) HTTP/1.1").unwrap(); |
|
|
|
|
|
|
|
let caps = hdr.captures(&bf); |
|
|
|
match caps { |
|
|
|
Some(cap) => { |
|
|
|
let c = cap.get(1).unwrap().as_str(); |
|
|
|
println!("Asked to fetch {}", c); |
|
|
|
//assert!(Path::new(c).exists()); |
|
|
|
if c == "" { |
|
|
|
println!("Asked to send root!"); |
|
|
|
r.header.push_str("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
|
r.path.push_str("index.html"); |
|
|
|
} else if Path::new(c).exists() { |
|
|
|
if Path::new(c).is_dir() { |
|
|
|
let mut cs = c.to_string(); |
|
|
|
if cs.chars().last().unwrap().to_string() != "/" { |
|
|
|
cs.push_str("/"); |
|
|
|
} |
|
|
|
cs.push_str("index.html"); |
|
|
|
if Path::new(&cs).exists() { |
|
|
|
println!("Asked to send {}", &cs); |
|
|
|
r.header.push_str("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
|
r.path.push_str(&cs); |
|
|
|
} else { |
|
|
|
println!("Asked to send {} and I couldn't find it", &cs); |
|
|
|
r.header.push_str("HTTP/1.1 404 NOT FOUND\r\n\r\n"); |
|
|
|
r.path.push_str("404.html"); |
|
|
|
} |
|
|
|
} else { |
|
|
|
println!("Asked to send {}", &c); |
|
|
|
r.header.push_str("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
|
r.path.push_str(c); |
|
|
|
} |
|
|
|
} else { |
|
|
|
println!("Asked to send {} and I could not find it", &c); |
|
|
|
r.header.push_str("HTTP/1.1 404 NOT FOUND\r\n\r\n"); |
|
|
|
r.path.push_str("404.html"); |
|
|
|
} |
|
|
|
route(&c, &mut r); |
|
|
|
}, |
|
|
|
None => { |
|
|
|
println!("This wasn't even a well-formed header"); |
|
|
|
r.header.push_str("HTTP/1.1 404 NOT FOUND\r\n\r\n"); |
|
|
|
r.path.push_str("404.html"); |
|
|
|
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n"); |
|
|
|
response_add_file("404.html", &mut r); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
println!("It didn't start with GET!"); |
|
|
|
r.header.push_str("HTTP/1.1 404 NOT FOUND\r\n\r\n"); |
|
|
|
r.path.push_str("404.html"); |
|
|
|
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n"); |
|
|
|
response_add_file("404.html", &mut r); |
|
|
|
} |
|
|
|
// println!("Sending {}: {}", r.header, r.path); |
|
|
|
let contents = fs::read_to_string(r.path).unwrap(); |
|
|
|
let response = format!("{}{}", r.header, contents); |
|
|
|
|
|
|
|
let response = format!("{}{}", r.header, r.content); |
|
|
|
|
|
|
|
stream.write(response.as_bytes()).unwrap(); |
|
|
|
stream.flush().unwrap(); |
|
|
@@ -104,4 +67,86 @@ fn handle_connection(mut stream: TcpStream) { |
|
|
|
|
|
|
|
|
|
|
|
// println!("Request: {}", String::from_utf8_lossy(&buffer[..])); |
|
|
|
} |
|
|
|
|
|
|
|
fn response_add_file(p: &str, mut r: &mut Response) { |
|
|
|
r.set_content(fs::read_to_string(p).unwrap()); |
|
|
|
} |
|
|
|
|
|
|
|
fn route(c: &str, mut r: &mut Response) { |
|
|
|
|
|
|
|
let routes = vec![ |
|
|
|
Route { |
|
|
|
path: "api/hi", |
|
|
|
action: hi, |
|
|
|
}, |
|
|
|
Route { |
|
|
|
path: "api/bye", |
|
|
|
action: 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"); |
|
|
|
response_add_file("index.html", &mut r); |
|
|
|
} else if c.starts_with("api/") { |
|
|
|
let valid_route = routes.iter().find(|&x| x.path == c); |
|
|
|
match valid_route { |
|
|
|
Some(rt) => { |
|
|
|
(rt.action)(&c, &mut r); |
|
|
|
}, |
|
|
|
None => { |
|
|
|
none_api(&c, &mut r); |
|
|
|
} |
|
|
|
} |
|
|
|
} else if Path::new(c).exists() { |
|
|
|
route_basic(&c, &mut r); |
|
|
|
} 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); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
fn route_basic(c: &&str, mut r: &mut Response) { |
|
|
|
if Path::new(c).is_dir() { |
|
|
|
let mut cs = c.to_string(); |
|
|
|
if cs.chars().last().unwrap().to_string() != "/" { |
|
|
|
cs.push_str("/"); |
|
|
|
} |
|
|
|
cs.push_str("index.html"); |
|
|
|
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); |
|
|
|
} 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); |
|
|
|
} |
|
|
|
} else { |
|
|
|
println!("Asked to send {}", &c); |
|
|
|
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
|
response_add_file(&c, &mut r); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
fn none_api(_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."); |
|
|
|
} |
|
|
|
|
|
|
|
fn hi(_c: &&str, r: &mut Response) { |
|
|
|
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
|
r.set_content("Hello there!"); |
|
|
|
} |
|
|
|
|
|
|
|
fn bye(_c: &&str, r: &mut Response) { |
|
|
|
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
|
r.set_content("Leaving so soon?"); |
|
|
|
} |