From a8b61ee96c19003658751b66e7b8b527b05fafaf Mon Sep 17 00:00:00 2001 From: "Dawid J. Kubis" Date: Mon, 6 Dec 2021 14:37:46 +0100 Subject: [PATCH] got rid of that fucking http crate for fags --- Cargo.lock | 18 ----------- Cargo.toml | 2 -- README.md | 4 +-- src/main.rs | 83 ++++++++++++++++----------------------------------- src/models.rs | 36 ++++++++++++++++++++++ src/server.rs | 6 ---- 6 files changed, 63 insertions(+), 86 deletions(-) create mode 100644 src/models.rs delete mode 100644 src/server.rs diff --git a/Cargo.lock b/Cargo.lock index ebd86bf..3d8a302 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -97,12 +97,6 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" -[[package]] -name = "bytes" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" - [[package]] name = "bytesize" version = "1.1.0" @@ -310,17 +304,6 @@ dependencies = [ "libc", ] -[[package]] -name = "http" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1323096b05d41827dadeaee54c9981958c0f94e670bc94ed80037d1a7b8b186b" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - [[package]] name = "indexmap" version = "1.7.0" @@ -352,7 +335,6 @@ version = "0.1.0" dependencies = [ "bytesize", "comrak", - "http", "num_cpus", "sled", "structopt", diff --git a/Cargo.toml b/Cargo.toml index 488d7fb..50e833d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,8 @@ edition = "2021" [dependencies] structopt = "0.3.25" comrak = "0.12.1" -http = "0.2.5" threadpool = "1.8.1" num_cpus = "1.13.0" sled = "0.34.7" thiserror = "1.0.30" bytesize = "1.1.0" - diff --git a/README.md b/README.md index 13779d8..d4fbe70 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -### Server Settings -+ no bans +### Rules ++ single page + threads might be deleted if they reach maximum storage capacity - the least active thread will then be removed. + id's will be attached to every thread and every response diff --git a/src/main.rs b/src/main.rs index 6388af5..06d4458 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,7 @@ mod routes; +mod models; + +use crate::models::Request; use std::io::Read; use std::net::{TcpListener, TcpStream}; @@ -6,11 +9,12 @@ use std::path::PathBuf; use std::str; use std::sync::mpsc::channel; -use http::request::Request; -use http::method::Method; use structopt::StructOpt; use threadpool::ThreadPool; +static INDEX: &str = include_str!("www/index.html"); +static FAVICON: &'static [u8] = include_bytes!("www/favicon.ico"); + #[derive(StructOpt, Debug)] struct Opt { #[structopt(short, long, default_value = "8000")] @@ -26,60 +30,13 @@ struct Opt { database: PathBuf, } -struct KchanError { -} - -// very specific -// TODO less retarded implementation -// and get rid of unwraps -fn parse_request(text: &str) -> Request { - let request = Request::builder(); - - let mut iter = text.lines(); - let mut first = iter.nth(0).unwrap().split_whitespace(); - - let method = first.next().unwrap(); - let uri = first.next().unwrap(); - - // now headers - let mut headers: Vec<(&str, &str)> = vec![]; - - for line in &mut iter { - println!("{}", line); - if line.is_empty() { - break; - }; - let line: Vec<&str> = line.split(":").take(2).collect(); - headers.push((line[0], line[1])); - } - - let body: String = iter.fold(String::from(""), |a, b| format!("{}{}", a, b)); - - let mut request = request.method(method).uri(uri); - for header in headers { - request = request.header(header.0, header.1); - } - - request.body(body).unwrap() -} - -fn handle(mut stream: TcpStream) { - let ip = stream.peer_addr(); - let mut buffer = [0; 1 << 10]; // 2 to the power of 10 - stream.read(&mut buffer).unwrap(); - - let text = str::from_utf8(&buffer) - .unwrap() - .trim_matches(0 as char) // gets rid of null at the end of buffer - .trim(); - - let request = parse_request(text); - println!("{:?}", request); - match *request.method() { - Method::GET => "", - Method::POST => "", - Method::DELETE => "", +fn handle(request: Request) { + // check admin priviledges + match request.method.as_str() { + "GET" => "", + "POST" => "", + "DELETE" => "", _ => "", }; } @@ -93,10 +50,20 @@ fn main() { // maybe send back here? for stream in listener.incoming() { - let stream = stream.unwrap(); - pool.execute(|| { + let mut stream = stream.unwrap(); + pool.execute(move || { + + let ip = stream.peer_addr(); + let mut buffer = [0; 1 << 10]; // 2 to the power of 10 + stream.read(&mut buffer).unwrap(); + + let text = str::from_utf8(&buffer) + .unwrap() + .trim_matches(0 as char); // gets rid of null at the end of buffer + + let request: Request = text.parse().unwrap(); - handle(stream) + handle(request) }); } } diff --git a/src/models.rs b/src/models.rs new file mode 100644 index 0000000..277ade3 --- /dev/null +++ b/src/models.rs @@ -0,0 +1,36 @@ +use std::str::FromStr; + +pub struct Request { + pub method: String, + pub uri: String, + pub headers: Vec<(String, String)>, + pub body: String, +} + +// TODO implement this with display and enums +impl FromStr for Request { + type Err = String; + fn from_str(s: &str) -> Result { + let mut iter = s.lines(); + let mut first = iter.nth(0).unwrap().split_whitespace(); + let method = first.next().unwrap().to_string(); + let uri = first.next().unwrap().to_string(); + let mut headers: Vec<(String, String)> = vec![]; + + for line in &mut iter { + if line.is_empty() { + break; + } + let line: Vec<&str> = line.split(":").take(2).collect(); + headers.push((line[0].into(), line[1].into())); + } + let body: String = iter.fold(String::new(), |a, b| format!("{}{}", a, b)); + + Ok(Self { + method, + uri, + headers, + body, + }) + } +} diff --git a/src/server.rs b/src/server.rs deleted file mode 100644 index b8696d7..0000000 --- a/src/server.rs +++ /dev/null @@ -1,6 +0,0 @@ -use std::net::TcpListener; -use rayon::prelude::*; - - - -