You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. use std::net::{TcpStream, TcpListener};
  2. use std::io::prelude::*;
  3. use std::fs;
  4. // use std::thread;
  5. // use std::time::Duration;
  6. // use std::str;
  7. use std::path::Path;
  8. use hello::ThreadPool;
  9. extern crate regex;
  10. use regex::Regex;
  11. struct Response {
  12. header: String,
  13. path: String,
  14. }
  15. fn main() {
  16. let listener = TcpListener::bind("127.0.0.1:26382").unwrap();
  17. let pool = ThreadPool::new(4);
  18. for stream in listener.incoming() {
  19. let stream = stream.unwrap();
  20. pool.execute(|| {
  21. handle_connection(stream);
  22. });
  23. }
  24. println!("Shutting down.");
  25. }
  26. fn handle_connection(mut stream: TcpStream) {
  27. let mut buffer = [0; 512];
  28. stream.read(&mut buffer).unwrap();
  29. let hdr = Regex::new(r"GET /([^ ]*) HTTP/1.1").unwrap();
  30. let bf = &String::from_utf8_lossy(&buffer[..]);
  31. // let get = b"GET / HTTP/1.1\r\n";
  32. // let sleep = b"GET /sleep HTTP/1.1\r\n";
  33. let mut r = Response{
  34. header: String::from(""),
  35. path: String::from(""),
  36. };
  37. if buffer.starts_with(b"GET") {
  38. let caps = hdr.captures(bf);
  39. match caps {
  40. Some(cap) => {
  41. let c = cap.get(1).unwrap().as_str();
  42. println!("Asked to fetch {}", c);
  43. //assert!(Path::new(c).exists());
  44. if c == "" {
  45. println!("Asked to send root!");
  46. r.header.push_str("HTTP/1.1 200 OK\r\n\r\n");
  47. r.path.push_str("index.html");
  48. } else if Path::new(c).exists() {
  49. if Path::new(c).is_dir() {
  50. let mut cs = c.to_string();
  51. if cs.chars().last().unwrap().to_string() != "/" {
  52. cs.push_str("/");
  53. }
  54. cs.push_str("index.html");
  55. if Path::new(&cs).exists() {
  56. println!("Asked to send {}", &cs);
  57. r.header.push_str("HTTP/1.1 200 OK\r\n\r\n");
  58. r.path.push_str(&cs);
  59. } else {
  60. println!("Asked to send {} and I couldn't find it", &cs);
  61. r.header.push_str("HTTP/1.1 404 NOT FOUND\r\n\r\n");
  62. r.path.push_str("404.html");
  63. }
  64. } else {
  65. println!("Asked to send {}", &c);
  66. r.header.push_str("HTTP/1.1 200 OK\r\n\r\n");
  67. r.path.push_str(c);
  68. }
  69. } else {
  70. println!("Asked to send {} and I could not find it", &c);
  71. r.header.push_str("HTTP/1.1 404 NOT FOUND\r\n\r\n");
  72. r.path.push_str("404.html");
  73. }
  74. },
  75. None => {
  76. println!("This wasn't even a well-formed header");
  77. r.header.push_str("HTTP/1.1 404 NOT FOUND\r\n\r\n");
  78. r.path.push_str("404.html");
  79. }
  80. }
  81. } else {
  82. println!("It didn't start with GET!");
  83. r.header.push_str("HTTP/1.1 404 NOT FOUND\r\n\r\n");
  84. r.path.push_str("404.html");
  85. }
  86. // println!("Sending {}: {}", r.header, r.path);
  87. let contents = fs::read_to_string(r.path).unwrap();
  88. let response = format!("{}{}", r.header, contents);
  89. stream.write(response.as_bytes()).unwrap();
  90. stream.flush().unwrap();
  91. // println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
  92. }