]> piware.de Git - learn-rust.git/blobdiff - simple-http/src/main.rs
simple-http: Naïve unlimited threads
[learn-rust.git] / simple-http / src / main.rs
index 3316864c12bc5095f0b68c021ae0ea469a21c9da..0993075332c1c2335f135f45f0b1c875d7b5a783 100644 (file)
@@ -2,8 +2,8 @@ use std::io::prelude::*;
 
 use std::net::TcpListener;
 use std::net::TcpStream;
-use std::fs;
-use std::str;
+use std::time::Duration;
+use std::{fs, str, thread};
 
 fn handle_connection(mut stream: TcpStream) {
      let mut buffer = [0; 1024];
@@ -38,6 +38,9 @@ fn handle_connection(mut stream: TcpStream) {
 
     let (code, file) = if path == "/" || path == "/index.html" {
         ("200 OK", "index.html")
+    } else if path == "/slow" {
+        thread::sleep(Duration::from_secs(5));
+        ("200 OK", "index.html")
     } else {
         ("404 NOT FOUND", "404.html")
     };
@@ -61,6 +64,6 @@ fn main() {
 
     for stream in listener.incoming() {
         let stream = stream.unwrap();
-        handle_connection(stream);
+        thread::spawn(|| handle_connection(stream));
     }
 }