]> 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 ca624762c2a1005335669710e0ceb54d254d2e3d..0993075332c1c2335f135f45f0b1c875d7b5a783 100644 (file)
@@ -2,22 +2,58 @@ use std::io::prelude::*;
 
 use std::net::TcpListener;
 use std::net::TcpStream;
-use std::fs;
+use std::time::Duration;
+use std::{fs, str, thread};
 
 fn handle_connection(mut stream: TcpStream) {
      let mut buffer = [0; 1024];
 
     stream.read(&mut buffer).unwrap();
-    println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
 
-    let hello_contents = fs::read_to_string("hello.html").unwrap();
+    let buffer = match str::from_utf8(&buffer[..]) {
+        Ok(s) => s,
+        Err(e) => {
+            eprintln!("Invalid non-UTF8 request: {}\n{}", e, String::from_utf8_lossy(&buffer[..]));
+            return;
+        }
+    };
+    println!("Request: {}", buffer);
+
+    let path;
+
+    // simple web server, just interested in first line
+    if let Some(line) = buffer.lines().next() {
+        let request: Vec<_> = line.split_whitespace().collect();
+        if request.len() != 3 || request[0] != "GET" || request[2] != "HTTP/1.1" {
+            stream.write(b"HTTP/1.1 501 NOT IMPLEMENTED\r\n").unwrap();
+            stream.flush().unwrap();
+            return;
+        }
+
+        path = request[1];
+    } else {
+        eprintln!("Ignoring empty request: {}", buffer);
+        return;
+    }
+
+    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")
+    };
+
+    let text = fs::read_to_string(file).unwrap();
 
     let response = format!(
-        "HTTP/1.1 200 OK\r\n\
+        "HTTP/1.1 {}\r\n\
          Content-Type: text/html\r\n\
          Content-Length: {}\r\n\r\n{}",
-        hello_contents.len(),
-        hello_contents);
+        code,
+        text.len(),
+        text);
 
     stream.write(response.as_bytes()).unwrap();
     stream.flush().unwrap();
@@ -28,6 +64,6 @@ fn main() {
 
     for stream in listener.incoming() {
         let stream = stream.unwrap();
-        handle_connection(stream);
+        thread::spawn(|| handle_connection(stream));
     }
 }