diff --git a/.gitignore b/.gitignore index aaf7dd1..7a1cde5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target -data.sled +data + diff --git a/Cargo.lock b/Cargo.lock index 6dbf748..f3c0a50 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,18 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + +[[package]] +name = "adler32" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" + [[package]] name = "ansi_term" version = "0.11.0" @@ -56,12 +68,30 @@ dependencies = [ "serde", ] +[[package]] +name = "bit_field" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcb6dd1c2376d2e096796e234a70e17e94cc2d5d54ff8ce42b28cef1d0d359a4" + [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bumpalo" +version = "3.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899" + +[[package]] +name = "bytemuck" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e851ca7c24871e7336801608a4797d7376545b6928a10d32d75685687141ead" + [[package]] name = "byteorder" version = "1.4.3" @@ -136,6 +166,12 @@ dependencies = [ "vec_map", ] +[[package]] +name = "color_quant" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" + [[package]] name = "crc32fast" version = "1.3.2" @@ -145,6 +181,27 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e54ea8bc3fb1ee042f5aace6e3c6e025d3874866da222930f70ce62aceba0bfa" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6455c0ca19f0d2fbf751b908d5c55c1f5cbc65e03c4225427254b46890bdde1e" +dependencies = [ + "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + [[package]] name = "crossbeam-epoch" version = "0.9.7" @@ -203,12 +260,68 @@ dependencies = [ "syn", ] +[[package]] +name = "deflate" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c86f7e25f518f4b81808a2cf1c50996a61f5c2eb394b2393bd87f2a4780a432f" +dependencies = [ + "adler32", +] + +[[package]] +name = "either" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" + [[package]] name = "event-listener" version = "2.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77f3309417938f28bf8228fcff79a4a37103981e3e186d2ccd19c74b38f4eb71" +[[package]] +name = "exr" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4badb9489a465cb2c555af1f00f0bfd8cecd6fc12ac11da9d5b40c5dd5f0200" +dependencies = [ + "bit_field", + "deflate", + "flume", + "half", + "inflate", + "lebe", + "smallvec", + "threadpool", +] + +[[package]] +name = "flate2" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e6988e897c1c9c485f43b47a529cef42fde0547f9d8d41a7062518f1d8fc53f" +dependencies = [ + "cfg-if", + "crc32fast", + "libc", + "miniz_oxide 0.4.4", +] + +[[package]] +name = "flume" +version = "0.10.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843c03199d0c0ca54bc1ea90ac0d507274c28abcc4f691ae8b4eaa375087c76a" +dependencies = [ + "futures-core", + "futures-sink", + "nanorand", + "pin-project", + "spin", +] + [[package]] name = "fnv" version = "1.0.7" @@ -225,6 +338,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "futures-core" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3" + +[[package]] +name = "futures-sink" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21163e139fa306126e6eedaf49ecdb4588f939600f0b1e770f4205ee4b7fa868" + [[package]] name = "fxhash" version = "0.2.1" @@ -236,15 +361,33 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.3" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +checksum = "d39cd93900197114fa1fcb7ae84ca742095eed9442088988ae74fa744e930e77" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi", + "wasm-bindgen", ] +[[package]] +name = "gif" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3a7187e78088aead22ceedeee99779455b23fc231fe13ec443f99bb71694e5b" +dependencies = [ + "color_quant", + "weezl", +] + +[[package]] +name = "half" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" + [[package]] name = "hashbrown" version = "0.12.0" @@ -275,6 +418,35 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" +[[package]] +name = "image" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db207d030ae38f1eb6f240d5a1c1c88ff422aa005d10f8c6c6fc5e75286ab30e" +dependencies = [ + "bytemuck", + "byteorder", + "color_quant", + "exr", + "gif", + "jpeg-decoder 0.2.2", + "num-iter", + "num-rational", + "num-traits", + "png", + "scoped_threadpool", + "tiff", +] + +[[package]] +name = "inflate" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cdb29978cc5797bd8dcc8e5bf7de604891df2a8dc576973d71a281e916db2ff" +dependencies = [ + "adler32", +] + [[package]] name = "instant" version = "0.1.12" @@ -284,12 +456,37 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "jpeg-decoder" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "229d53d58899083193af11e15917b5640cd40b29ff475a1fe4ef725deb02d0f2" + +[[package]] +name = "jpeg-decoder" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "105fb082d64e2100074587f59a74231f771750c664af903f1f9f76c9dedfc6f1" +dependencies = [ + "rayon", +] + +[[package]] +name = "js-sys" +version = "0.3.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a38fc24e30fd564ce974c02bf1d337caddff65be6cc4735a1f7eab22a7440f04" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "kchan" version = "0.1.0" dependencies = [ "bincode", "cached", + "image", "lazy_static", "log", "num_cpus", @@ -308,6 +505,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "lebe" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7efd1d698db0759e6ef11a7cd44407407399a910c774dd804c64c032da7826ff" + [[package]] name = "libc" version = "0.2.108" @@ -341,6 +544,34 @@ dependencies = [ "autocfg", ] +[[package]] +name = "miniz_oxide" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" +dependencies = [ + "adler", + "autocfg", +] + +[[package]] +name = "miniz_oxide" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2b29bd4bc3f33391105ebee3589c19197c4271e3e5a9ec9bfe8127eeff8f082" +dependencies = [ + "adler", +] + +[[package]] +name = "nanorand" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a51313c5820b0b02bd422f4b44776fbf47961755c74ce64afc73bfad10226c3" +dependencies = [ + "getrandom", +] + [[package]] name = "num-integer" version = "0.1.44" @@ -351,6 +582,28 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-iter" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2021c8337a54d21aca0d59a92577a029af9431cb59b909b03252b9c164fad59" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d41702bd167c2df5520b384281bc111a4b5efcf7fbc4c9c222c815b07e0a6a6a" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.14" @@ -401,12 +654,44 @@ dependencies = [ "winapi", ] +[[package]] +name = "pin-project" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58ad3879ad3baf4e44784bc6a718a8698867bb991f8ce24d1bcbe2cfb4c3a75e" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "744b6f092ba29c3650faf274db506afd39944f48420f6c86b17cfe0ee1cb36bb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "pin-project-lite" version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e280fbe77cc62c91527259e9442153f4688736748d24660126286329742b4c6c" +[[package]] +name = "png" +version = "0.17.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc38c0ad57efb786dd57b9864e5b18bae478c00c824dc55a38bbc9da95dde3ba" +dependencies = [ + "bitflags", + "crc32fast", + "deflate", + "miniz_oxide 0.5.1", +] + [[package]] name = "ppv-lite86" version = "0.2.15" @@ -495,6 +780,31 @@ dependencies = [ "rand_core", ] +[[package]] +name = "rayon" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06aca804d41dbc8ba42dfd964f0d01334eceb64314b9ecf7c5fad5188a06d90" +dependencies = [ + "autocfg", + "crossbeam-deque", + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d78120e2c850279833f1dd3582f730c4ab53ed95aeaaaa862a2a5c71b1656d8e" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "lazy_static", + "num_cpus", +] + [[package]] name = "redox_syscall" version = "0.2.11" @@ -504,6 +814,12 @@ dependencies = [ "bitflags", ] +[[package]] +name = "scoped_threadpool" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" + [[package]] name = "scopeguard" version = "1.1.0" @@ -563,6 +879,15 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83" +[[package]] +name = "spin" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "511254be0c5bcf062b019a6c89c01a664aa359ded62f78aa72c6fc137c0590e5" +dependencies = [ + "lock_api", +] + [[package]] name = "strsim" version = "0.8.0" @@ -657,6 +982,17 @@ dependencies = [ "num_cpus", ] +[[package]] +name = "tiff" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0247608e998cb6ce39dfc8f4a16c50361ce71e5b52e6d24ea1227ea8ea8ee0b2" +dependencies = [ + "flate2", + "jpeg-decoder 0.1.22", + "weezl", +] + [[package]] name = "time" version = "0.1.43" @@ -725,6 +1061,66 @@ version = "0.10.2+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" +[[package]] +name = "wasm-bindgen" +version = "0.2.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25f1af7423d8588a3d840681122e72e6a24ddbcb3f0ec385cac0d12d24256c06" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b21c0df030f5a177f3cba22e9bc4322695ec43e7257d865302900290bcdedca" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f4203d69e40a52ee523b2529a773d5ffc1dc0071801c87b3d270b471b80ed01" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa8a30d46208db204854cadbb5d4baf5fcf8071ba5bf48190c3e59937962ebc" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d958d035c4438e28c70e4321a2911302f10135ce78a9c7834c0cab4123d06a2" + +[[package]] +name = "weezl" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8b77fdfd5a253be4ab714e4ffa3c49caf146b4de743e97510c0656cf90f1e8e" + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 81ede53..d717c81 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,4 +18,5 @@ serde = { version = "1.0.136", features = ["derive"] } cached = "0.33.0" log = "0.4.14" simplelog = "0.11.2" +image = "0.24.1" diff --git a/README.md b/README.md index cb49a44..fe5f7bf 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ +### TODO: +1. proper logging +2. better html/css, working threads and posts +3. add authorization and ip whitelist +4. optimize/clean code, improve memory usage +5. add images + ### Rules + single page + threads might be deleted if they reach maximum storage diff --git a/src/errors.rs b/src/errors.rs index 544e615..a6b32c7 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -4,8 +4,12 @@ use thiserror::Error; pub enum RequestError { #[error("request unable to be parsed as a form")] NotAForm, + #[error("resource not found")] + NotFound, #[error("request not authorized, incorred hash: {0}")] NotAuthorized(String), + #[error("bad request")] + BadRequest, } #[derive(Debug, Error)] diff --git a/src/http.rs b/src/http.rs index 61d732d..a72e6a8 100644 --- a/src/http.rs +++ b/src/http.rs @@ -45,25 +45,26 @@ impl Status { impl Request { pub fn form(&self) -> Result, RequestError> { let mut hashmap = HashMap::new(); - self.body.split("&").map(|x| x.split("=")).for_each( - |mut x| { - hashmap.insert(x.next().unwrap(), x.next().unwrap()); - }, // fix unwrap hell - ); + for mut x in self.body.split("&").map(|x| x.split("=")) { + hashmap.insert( + x.next().ok_or(RequestError::NotAForm)?, + x.next().ok_or(RequestError::NotAForm)? + ); + } Ok(hashmap) } } -// TODO proper errors impl FromStr for Request { - type Err = String; + type Err = RequestError; fn from_str(s: &str) -> Result { - dbg!(&s); + //dbg!(&s); let mut iter = s.lines(); - let mut first = iter.next().unwrap().split_whitespace(); - let method = first.next().unwrap(); - let uri = first.next().unwrap(); + let mut first = iter.next().ok_or(RequestError::BadRequest)? + .split_whitespace(); + let method = first.next().ok_or(RequestError::BadRequest)?; + let uri = first.next().ok_or(RequestError::BadRequest)?; let mut headers: Vec<(String, String)> = vec![]; for line in &mut iter { @@ -113,3 +114,4 @@ impl fmt::Display for Response { ) } } + diff --git a/src/main.rs b/src/main.rs index bedb90b..d688c5a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ mod errors; mod http; mod post; -use crate::errors::RequestError; +use crate::errors::{RequestError, HandlingError, InternalError}; use crate::http::{Request, Response, Status}; use crate::post::{Post, Thread}; @@ -16,8 +16,10 @@ use std::sync::{Arc, Mutex, MutexGuard}; use bincode::{deserialize, serialize}; use cached::proc_macro::cached; use lazy_static::lazy_static; +use simplelog::*; use structopt::StructOpt; use threadpool::ThreadPool; +use log::*; // statically linked index and favicon const INDEX: &'static str = include_str!("www/index.html"); @@ -61,7 +63,13 @@ impl State { } /// top level handling of requests -fn handle(request: Request, state: Arc>) -> Response { +fn handle(request: &str, state: Arc>) -> Response { + let request = match request.parse::() { + Ok(s) => s, + Err(e) => { + return Response::new(Status::BadRequest, vec![], "".to_string()) + }, + }; let state = state.lock().unwrap(); match request.method.as_str() { @@ -105,7 +113,10 @@ fn get(path: &str, state: MutexGuard) -> Result s => { let content = content( s, - &deserialize::(&state.db.get(&s.as_bytes()).unwrap().unwrap()) + &deserialize::( + &state.db.get(&s.as_bytes()) + .map_err(|_| HandlingError::ServerError(InternalError::DatabaseReadError))? + .ok_or(HandlingError::ClientError(RequestError::NotFound))?) .unwrap() .to_string(), ); @@ -121,11 +132,11 @@ fn post(request: Request, mut state: MutexGuard) -> Response { "/" => { // first we increment id let id = state.next_id(); - let content = dbg!(request.form()); + let content = dbg!(request.form().unwrap()); let thread = Thread(vec![Post::new( id, None, - String::from(*content.get(":D").unwrap()), + String::from(*content.get("content").unwrap()), )]); state @@ -156,7 +167,9 @@ fn main() { // create threadpool for incoming requests let pool = ThreadPool::new(num_cpus::get()); // setup logger - + TermLogger::init( + LevelFilter::Warn, Config::default(), TerminalMode::Mixed, ColorChoice::Auto + ).unwrap(); // open database let state = Arc::new(Mutex::new(State { db: sled::open(&OPT.database).unwrap(), @@ -182,13 +195,12 @@ fn main() { 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(); + let text = str::from_utf8(&buffer).unwrap().trim_matches(0 as char); + // ^-- gets rid of null at the end of buffer // handle request stream - .write(handle(request, state).to_string().as_bytes()) + .write(handle(text, state).to_string().as_bytes()) .unwrap(); }); } diff --git a/src/www/index.html b/src/www/index.html index 4c4b799..4743312 100644 --- a/src/www/index.html +++ b/src/www/index.html @@ -20,7 +20,7 @@

Create new thread

- +