diff --git a/Cargo.lock b/Cargo.lock index 7cb9856..17de6cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "anyhow" +version = "1.0.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62e1f47f7dc0422027a4e370dd4548d4d66b26782e513e98dca1e689e058a80e" + [[package]] name = "atty" version = "0.2.14" @@ -315,6 +321,12 @@ dependencies = [ "itoa", ] +[[package]] +name = "httparse" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acd94fdbe1d4ff688b67b04eee2e17bd50995534a61539e45adfefb45e5e5503" + [[package]] name = "indexmap" version = "1.7.0" @@ -344,8 +356,10 @@ checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" name = "kchan" version = "0.1.0" dependencies = [ + "anyhow", "comrak", "http", + "httparse", "num_cpus", "sled", "structopt", diff --git a/Cargo.toml b/Cargo.toml index f5f98db..dc1c2b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,8 @@ http = "0.2.5" threadpool = "1.8.1" num_cpus = "1.13.0" sled = "0.34.7" +httparse = "1.5.1" +anyhow = "1.0.48" [build-dependencies] comrak = "0.12.1" diff --git a/src/main.rs b/src/main.rs index 96d48a5..014fc91 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,16 @@ +mod routes; + use std::path::PathBuf; use std::net::{TcpListener, TcpStream}; use std::sync::mpsc::channel; +use std::io::Read; +use std::str; use structopt::StructOpt; use threadpool::ThreadPool; +use http::request::Request; +use anyhow::Result; +use anyhow::Context; #[derive(StructOpt, Debug)] struct Opt { @@ -17,8 +24,53 @@ struct Opt { max_storage: String, } -fn handle(stream: TcpStream) { - todo!(); +// very specific +// TODO less retarded implementation +fn parse_request(text: &str) -> Result> { + 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).context("lmao") +} + +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(); + + println!("{:?}", parse_request(text).unwrap()); + } fn main() { @@ -31,5 +83,4 @@ fn main() { let stream = stream.unwrap(); pool.execute(|| handle(stream)) } - } diff --git a/src/routes.rs b/src/routes.rs new file mode 100644 index 0000000..e69de29