|
|
|
|
|
|
|
|
extern crate regex; |
|
|
extern crate regex; |
|
|
use regex::Regex; |
|
|
use regex::Regex; |
|
|
|
|
|
|
|
|
|
|
|
struct Response { |
|
|
|
|
|
header: String, |
|
|
|
|
|
path: String, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
fn main() { |
|
|
fn main() { |
|
|
let listener = TcpListener::bind("127.0.0.1:26382").unwrap(); |
|
|
let listener = TcpListener::bind("127.0.0.1:26382").unwrap(); |
|
|
let pool = ThreadPool::new(4); |
|
|
let pool = ThreadPool::new(4); |
|
|
|
|
|
|
|
|
// let get = b"GET / HTTP/1.1\r\n"; |
|
|
// let get = b"GET / HTTP/1.1\r\n"; |
|
|
// let sleep = b"GET /sleep HTTP/1.1\r\n"; |
|
|
// let sleep = b"GET /sleep HTTP/1.1\r\n"; |
|
|
|
|
|
|
|
|
let (status_line, filename) = if buffer.starts_with(b"GET") { |
|
|
|
|
|
|
|
|
let mut r = Response{ |
|
|
|
|
|
header: String::from(""), |
|
|
|
|
|
path: String::from(""), |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
if buffer.starts_with(b"GET") { |
|
|
|
|
|
|
|
|
let caps = hdr.captures(bf); |
|
|
let caps = hdr.captures(bf); |
|
|
match caps { |
|
|
match caps { |
|
|
Some(cap) => { |
|
|
Some(cap) => { |
|
|
let c = cap.get(1).unwrap().as_str(); |
|
|
let c = cap.get(1).unwrap().as_str(); |
|
|
// println!("Asked to fetch {}", c); |
|
|
|
|
|
|
|
|
println!("Asked to fetch {}", c); |
|
|
//assert!(Path::new(c).exists()); |
|
|
//assert!(Path::new(c).exists()); |
|
|
if c == "" { |
|
|
if c == "" { |
|
|
("HTTP/1.1 200 OK\r\n\r\n", "index.html") |
|
|
|
|
|
|
|
|
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() { |
|
|
} else if Path::new(c).exists() { |
|
|
("HTTP/1.1 200 OK\r\n\r\n", c) |
|
|
|
|
|
|
|
|
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 { |
|
|
} else { |
|
|
("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") |
|
|
|
|
|
|
|
|
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"); |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
None => { |
|
|
None => { |
|
|
("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") |
|
|
|
|
|
|
|
|
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"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") |
|
|
|
|
|
}; |
|
|
|
|
|
let contents = fs::read_to_string(filename).unwrap(); |
|
|
|
|
|
let response = format!("{}{}", status_line, contents); |
|
|
|
|
|
|
|
|
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"); |
|
|
|
|
|
} |
|
|
|
|
|
// println!("Sending {}: {}", r.header, r.path); |
|
|
|
|
|
let contents = fs::read_to_string(r.path).unwrap(); |
|
|
|
|
|
let response = format!("{}{}", r.header, contents); |
|
|
|
|
|
|
|
|
stream.write(response.as_bytes()).unwrap(); |
|
|
stream.write(response.as_bytes()).unwrap(); |
|
|
stream.flush().unwrap(); |
|
|
stream.flush().unwrap(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// println!("Request: {}", String::from_utf8_lossy(&buffer[..])); |
|
|
// println!("Request: {}", String::from_utf8_lossy(&buffer[..])); |
|
|
} |
|
|
|
|
|
|
|
|
} |