aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml2
-rw-r--r--src/main.rs55
3 files changed, 31 insertions, 28 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 6f153d5..902a068 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2,4 +2,4 @@
# It is not intended for manual editing.
[[package]]
name = "herb"
-version = "0.1.2"
+version = "0.1.4"
diff --git a/Cargo.toml b/Cargo.toml
index 221575f..1487a93 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "herb"
-version = "0.1.3"
+version = "0.2.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
diff --git a/src/main.rs b/src/main.rs
index 5e7442a..1175818 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,4 +1,5 @@
use std::io::{Write, BufReader, BufRead};
+use std::thread;
use std::net::{TcpListener, TcpStream};
use std::fs;
use std::string::{String};
@@ -183,40 +184,42 @@ fn process_request(request: Vec<u8>) -> String {
}
fn serve(mut stream: TcpStream) {
- let mut request = Vec::new();
- let mut reader = BufReader::new(&mut stream);
- reader
- .read_until(b'\n', &mut request)
- .expect("Failed to read from stream!");
+ thread::spawn(move || {
+ println!("Stream thread created.");
+ let mut request = Vec::new();
+ let mut reader = BufReader::new(&mut stream);
+ reader
+ .read_until(b'\n', &mut request)
+ .expect("Failed to read from stream!");
- let resource = process_request(request);
+ let resource = process_request(request);
- // Haha, bodged my way around Rust's ownership paradigm!
- let mime = detect_media_type(resource.to_string());
+ // Haha, bodged my way around Rust's ownership paradigm!
+ let mime = detect_media_type(resource.to_string());
- let contents = get_page(resource);
- let header = "HTTP/1.1 200 OK\r\n";
- let content_type = format!("Content-Type: {}\r\n", mime);
- let server = "Server: Herb/0.1.4\r\n";
- let extra_fields;
+ let contents = get_page(resource);
+ let header = "HTTP/1.1 200 OK\r\n";
+ let content_type = format!("Content-Type: {}\r\n", mime);
+ let server = "Server: Herb/0.2.0\r\n";
+ let extra_fields;
- if cfg!(unix) {
- let mut time = format!("Date: {}\r\n", grab_time());
- time = time.replace("'","");
- extra_fields = format!("{}{}\r\n", server, time);
- } else {
- // I don't have a Windows or macOS box to test anything on
- // which means others are gonna have to deal with it :/
+ if cfg!(unix) {
+ let mut time = format!("Date: {}\r\n", grab_time());
+ time = time.replace("'","");
+ extra_fields = format!("{}{}\r\n", server, time);
+ } else {
+ // I don't have a Windows or macOS box to test anything on
+ // which means others are gonna have to deal with it :/
- extra_fields = format!("{}\r\n", server);
- }
+ extra_fields = format!("{}\r\n", server);
+ }
- let response = format!("{}{}{}{}", header, content_type, extra_fields, contents);
+ let response = format!("{}{}{}{}", header, content_type, extra_fields, contents);
- stream.write(response.as_bytes()).unwrap();
- stream.flush().unwrap();
-
+ stream.write(response.as_bytes()).unwrap();
+ stream.flush().unwrap();
+ });
}