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.

main.rs 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 webserv::{ThreadPool, Response, Route};
  9. extern crate regex;
  10. use regex::Regex;
  11. fn main() {
  12. let listener = TcpListener::bind("127.0.0.1:26382").unwrap();
  13. let pool = ThreadPool::new(4);
  14. for stream in listener.incoming() {
  15. let stream = stream.unwrap();
  16. pool.execute(|| {
  17. handle_connection(stream);
  18. });
  19. }
  20. println!("Shutting down.");
  21. }
  22. fn handle_connection(mut stream: TcpStream) {
  23. let mut buffer = [0; 512];
  24. stream.read(&mut buffer).unwrap();
  25. let mut r = Response::new("","");
  26. let bf = &String::from_utf8_lossy(&buffer[..]);
  27. // let get = b"GET / HTTP/1.1\r\n";
  28. // let sleep = b"GET /sleep HTTP/1.1\r\n";
  29. if buffer.starts_with(b"GET") {
  30. let hdr = Regex::new(r"GET /([^ ]*) HTTP/1.1").unwrap();
  31. let caps = hdr.captures(&bf);
  32. match caps {
  33. Some(cap) => {
  34. let c = cap.get(1).unwrap().as_str();
  35. route(&c, &mut r);
  36. },
  37. None => {
  38. println!("This wasn't even a well-formed header");
  39. r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
  40. r.add_file_contents("404.html");
  41. }
  42. }
  43. } else {
  44. println!("It didn't start with GET!");
  45. r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
  46. r.add_file_contents("404.html");
  47. }
  48. // println!("Sending {}: {}", r.header, r.path);
  49. let response = format!("{}{}", r.get_header(), r.get_content());
  50. stream.write(response.as_bytes()).unwrap();
  51. stream.flush().unwrap();
  52. // println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
  53. }
  54. fn route(c: &str, mut r: &mut Response) {
  55. let mut routes = vec![];
  56. routes.push(Route::new("api/hi", hi));
  57. routes.push(Route::new("api/bye", bye));
  58. // println!("Asked to fetch {}", c);
  59. //assert!(Path::new(c).exists());
  60. if c == "" {
  61. println!("Asked to send root!");
  62. r.set_header("HTTP/1.1 200 OK\r\n\r\n");
  63. r.add_file_contents("index.html");
  64. } else if c.starts_with("api/") {
  65. let mut iter_routes = routes.into_iter();
  66. let valid_route = iter_routes.find(|x| x.get_path() == c);
  67. match valid_route {
  68. Some(rt) => {
  69. (rt.get_action())(&c, &mut r);
  70. },
  71. None => {
  72. api_none(&c, &mut r);
  73. }
  74. }
  75. } else if Path::new(c).exists() {
  76. route_basic(&c, &mut r);
  77. } else {
  78. println!("Asked to send {} and I could not find it", &c);
  79. r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
  80. r.add_file_contents("404.html");
  81. }
  82. }
  83. fn route_basic(c: &&str, r: &mut Response) {
  84. if Path::new(c).is_dir() {
  85. let mut cs = c.to_string();
  86. if cs.chars().last().unwrap().to_string() != "/" {
  87. cs.push_str("/");
  88. }
  89. cs.push_str("index.html");
  90. if Path::new(&cs).exists() {
  91. println!("Asked to send {}", &cs);
  92. r.set_header("HTTP/1.1 200 OK\r\n\r\n");
  93. r.add_file_contents(&cs);
  94. } else {
  95. println!("Asked to send {} and I couldn't find it", &cs);
  96. r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
  97. r.add_file_contents("404.html");
  98. }
  99. } else {
  100. println!("Asked to send {}", &c);
  101. r.set_header("HTTP/1.1 200 OK\r\n\r\n");
  102. r.add_file_contents(&c);
  103. }
  104. }
  105. fn api_none(_c: &&str, r: &mut Response) {
  106. r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
  107. r.set_content("There is currently no API endpoint at that address.");
  108. }
  109. fn hi(_c: &&str, r: &mut Response) {
  110. r.set_header("HTTP/1.1 200 OK\r\n\r\n");
  111. r.set_content("Hello there!");
  112. }
  113. fn bye(_c: &&str, r: &mut Response) {
  114. r.set_header("HTTP/1.1 200 OK\r\n\r\n");
  115. r.set_content("Leaving so soon?");
  116. }