123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- use std::net::{TcpStream, TcpListener};
- use std::io::prelude::*;
- use std::fs;
- // use std::thread;
- // use std::time::Duration;
- // use std::str;
- use std::path::Path;
-
- use hello::{ThreadPool, Response, Route};
-
- extern crate regex;
- use regex::Regex;
-
- fn main() {
- let listener = TcpListener::bind("127.0.0.1:26382").unwrap();
- let pool = ThreadPool::new(4);
-
- for stream in listener.incoming() {
- let stream = stream.unwrap();
- pool.execute(|| {
- handle_connection(stream);
- });
- }
-
- println!("Shutting down.");
- }
-
- fn handle_connection(mut stream: TcpStream) {
- let mut buffer = [0; 512];
- stream.read(&mut buffer).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";
-
-
-
- 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();
- route(&c, &mut r);
- },
- None => {
- println!("This wasn't even a well-formed header");
- 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.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 response = format!("{}{}", r.header, r.content);
-
- stream.write(response.as_bytes()).unwrap();
- stream.flush().unwrap();
-
-
-
- // 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?");
- }
|