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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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, 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. response_add_file("404.html", &mut r);
  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. response_add_file("404.html", &mut r);
  47. }
  48. // println!("Sending {}: {}", r.header, r.path);
  49. let response = format!("{}{}", r.header, r.content);
  50. stream.write(response.as_bytes()).unwrap();
  51. stream.flush().unwrap();
  52. // println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
  53. }
  54. fn response_add_file(p: &str, mut r: &mut Response) {
  55. r.set_content(fs::read_to_string(p).unwrap());
  56. }
  57. fn route(c: &str, mut r: &mut Response) {
  58. let routes = vec![
  59. Route {
  60. path: "api/hi",
  61. action: hi,
  62. },
  63. Route {
  64. path: "api/bye",
  65. action: bye,
  66. },
  67. ];
  68. // println!("Asked to fetch {}", c);
  69. //assert!(Path::new(c).exists());
  70. if c == "" {
  71. println!("Asked to send root!");
  72. r.set_header("HTTP/1.1 200 OK\r\n\r\n");
  73. response_add_file("index.html", &mut r);
  74. } else if c.starts_with("api/") {
  75. let valid_route = routes.iter().find(|&x| x.path == c);
  76. match valid_route {
  77. Some(rt) => {
  78. (rt.action)(&c, &mut r);
  79. },
  80. None => {
  81. none_api(&c, &mut r);
  82. }
  83. }
  84. } else if Path::new(c).exists() {
  85. route_basic(&c, &mut r);
  86. } else {
  87. println!("Asked to send {} and I could not find it", &c);
  88. r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
  89. response_add_file("404.html", &mut r);
  90. }
  91. }
  92. fn route_basic(c: &&str, mut r: &mut Response) {
  93. if Path::new(c).is_dir() {
  94. let mut cs = c.to_string();
  95. if cs.chars().last().unwrap().to_string() != "/" {
  96. cs.push_str("/");
  97. }
  98. cs.push_str("index.html");
  99. if Path::new(&cs).exists() {
  100. println!("Asked to send {}", &cs);
  101. r.set_header("HTTP/1.1 200 OK\r\n\r\n");
  102. response_add_file(&cs, &mut r);
  103. } else {
  104. println!("Asked to send {} and I couldn't find it", &cs);
  105. r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
  106. response_add_file("404.html", &mut r);
  107. }
  108. } else {
  109. println!("Asked to send {}", &c);
  110. r.set_header("HTTP/1.1 200 OK\r\n\r\n");
  111. response_add_file(&c, &mut r);
  112. }
  113. }
  114. fn none_api(_c: &&str, r: &mut Response) {
  115. r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
  116. r.set_content("There is currently no API endpoint at that address.");
  117. }
  118. fn hi(_c: &&str, r: &mut Response) {
  119. r.set_header("HTTP/1.1 200 OK\r\n\r\n");
  120. r.set_content("Hello there!");
  121. }
  122. fn bye(_c: &&str, r: &mut Response) {
  123. r.set_header("HTTP/1.1 200 OK\r\n\r\n");
  124. r.set_content("Leaving so soon?");
  125. }