From 4410950c761970d640af00f245fdba859f8795b0 Mon Sep 17 00:00:00 2001 From: gennyble Date: Fri, 4 Apr 2025 01:45:07 -0500 Subject: rip out module system --- Cargo.lock | 33 +++--------- Cargo.toml | 2 +- corgi/Cargo.toml | 2 - corgi/src/caller.rs | 112 ++------------------------------------- corgi/src/main.rs | 43 +-------------- corgi_stats/Cargo.toml | 8 +++ corgi_stats/src/main.rs | 95 +++++++++++++++++++++++++++++++++ smalldog/Cargo.toml | 6 --- smalldog/README.md | 43 --------------- smalldog/src/lib.rs | 138 ------------------------------------------------ stats_module/Cargo.toml | 13 ----- stats_module/src/lib.rs | 85 ----------------------------- 12 files changed, 117 insertions(+), 463 deletions(-) create mode 100644 corgi_stats/Cargo.toml create mode 100644 corgi_stats/src/main.rs delete mode 100644 smalldog/Cargo.toml delete mode 100644 smalldog/README.md delete mode 100644 smalldog/src/lib.rs delete mode 100644 stats_module/Cargo.toml delete mode 100644 stats_module/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 85f7e8b..eefd0c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -89,14 +89,20 @@ dependencies = [ "http-body-util", "hyper", "hyper-util", - "libloading", "regex-lite", "rusqlite", "sha2", - "smalldog", "tokio", ] +[[package]] +name = "corgi-stats" +version = "0.1.0" +dependencies = [ + "rusqlite", + "time", +] + [[package]] name = "cpufeatures" version = "0.2.17" @@ -318,16 +324,6 @@ version = "0.2.171" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" -[[package]] -name = "libloading" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34" -dependencies = [ - "cfg-if", - "windows-targets", -] - [[package]] name = "libsqlite3-sys" version = "0.32.0" @@ -499,10 +495,6 @@ dependencies = [ "libc", ] -[[package]] -name = "smalldog" -version = "0.1.0" - [[package]] name = "smallvec" version = "1.14.0" @@ -519,15 +511,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "stats_module" -version = "0.1.0" -dependencies = [ - "rusqlite", - "smalldog", - "time", -] - [[package]] name = "syn" version = "2.0.100" diff --git a/Cargo.toml b/Cargo.toml index a6381ad..25c0cd5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["corgi", "parrot", "smalldog", "stats_module"] +members = ["corgi", "parrot", "corgi_stats"] resolver = "3" # use this profile like this: diff --git a/corgi/Cargo.toml b/corgi/Cargo.toml index 0a492c6..f3f6bda 100644 --- a/corgi/Cargo.toml +++ b/corgi/Cargo.toml @@ -10,11 +10,9 @@ version = "1.0.0" edition = "2024" [dependencies] -smalldog = { path = "../smalldog" } base64 = "0.22.1" http-body-util = "0.1.3" hyper-util = { version = "0.1.10", features = ["tokio"] } -libloading = "0.8.6" regex-lite = "0.1.6" rusqlite = { version = "0.34.0", features = ["bundled"] } sha2 = "0.10.8" diff --git a/corgi/src/caller.rs b/corgi/src/caller.rs index 7014d22..29be5ca 100644 --- a/corgi/src/caller.rs +++ b/corgi/src/caller.rs @@ -1,20 +1,8 @@ -use std::{ - ffi::{self, CString}, - marker::PhantomData, - net::IpAddr, - process::Stdio, - ptr, - str::FromStr, -}; +use std::{net::IpAddr, process::Stdio}; -use smalldog::ffi::{ModuleRequest, ModuleResponse}; -use tokio::{ - io::AsyncWriteExt, - process::Command, - sync::oneshot::{self, Sender}, -}; +use tokio::{io::AsyncWriteExt, process::Command}; -use crate::{Script, ScriptKind}; +use crate::Script; pub struct HttpRequest { pub content_type: String, @@ -63,12 +51,6 @@ impl HttpRequest { } pub async fn call_and_parse_cgi(script: Script, http: HttpRequest) -> CgiResponse { - if script.kind != ScriptKind::Executable { - eprintln!("Somehow made it to executable path with module script"); - eprintln!("Script: {}", script.name); - panic!("TODO: recover") - } - let mut cmd = Command::new(&script.filename); // Set env specified in the conf. Be sure we do this after we @@ -169,91 +151,3 @@ pub struct CgiResponse { /// CGI response body pub body: Option>, } - -type HandleFn = unsafe extern "C" fn(*const ModuleRequest) -> *const ModuleResponse; -type CleanupFn = unsafe extern "C" fn(*const ModuleResponse); - -pub async fn call_and_parse_module(script: Script, req: HttpRequest) -> CgiResponse { - let (tx, rx) = oneshot::channel(); - std::thread::spawn(move || unsafe { module_thread(script, req, tx) }); - - rx.await.unwrap() -} - -unsafe fn module_thread(script: Script, req: HttpRequest, tx: Sender) { - let env: Vec<(String, String)> = req - .build_kv() - .into_iter() - .chain(req.http_headers.into_iter()) - .chain(script.env.into_iter()) - .collect(); - - let mut headers_owned = vec![]; - for (k, v) in env { - headers_owned.push([ - CString::from_str(k.as_str()).unwrap(), - CString::from_str(v.as_str()).unwrap(), - ]); - } - - let headers: Vec<[*const ffi::c_char; 2]> = - headers_owned.iter().map(|kvarr| [kvarr[0].as_ptr(), kvarr[1].as_ptr()]).collect(); - - let modreq = ModuleRequest { - headers_len: headers.len() as u64, - headers: headers[..].as_ptr(), - body_len: req.body.as_ref().map(|v| v.len()).unwrap_or(0) as u64, - body: req.body.as_ref().map(|v| v.as_ptr()).unwrap_or(ptr::null()), - _phantom: PhantomData::default(), - }; - - let mut cgi = CgiResponse { - status: 200, - headers: vec![], - body: None, - }; - - // Since we can only load a dynamic library once, block other requests so - // they do not return the same handle. per dlopen docs: - // > If the same shared object is opened again with dlopen(), - // > the same object handle is returned. - let lock = match script.module_lock.lock() { - Ok(lock) => lock, - Err(poison) => { - eprintln!("!!! mutex for {} was poisoned!", script.name); - - // get the guard - poison.into_inner() - } - }; - - unsafe { - let lib = libloading::Library::new(script.filename).unwrap(); - let handle: libloading::Symbol = lib.get(b"cgi_handle").unwrap(); - let free: libloading::Symbol = lib.get(b"cgi_cleanup").unwrap(); - - let response = handle((&modreq) as *const ModuleRequest); - let response_ref = response.as_ref().unwrap(); - - let headers_ffi = - std::slice::from_raw_parts(response_ref.headers, response_ref.headers_len as usize); - - for pair in headers_ffi { - let k = ffi::CStr::from_ptr(pair[0]).to_string_lossy(); - let v = ffi::CStr::from_ptr(pair[1]).to_string_lossy(); - - cgi.headers.push((k.as_bytes().to_vec(), v.as_bytes().to_vec())); - } - - let maybe_body: Option> = response_ref - .body - .as_ref() - .map(|b| std::slice::from_raw_parts(b, response_ref.body_len as usize).to_vec()); - cgi.body = maybe_body; - - free(response); - }; - - drop(lock); - tx.send(cgi).unwrap() -} diff --git a/corgi/src/main.rs b/corgi/src/main.rs index fb6b75a..b8b23de 100644 --- a/corgi/src/main.rs +++ b/corgi/src/main.rs @@ -2,9 +2,7 @@ use std::{ net::{IpAddr, SocketAddr}, path::PathBuf, pin::Pin, - sync::{Arc, Mutex}, - thread::JoinHandle, - time::Instant, + sync::Arc, }; use caller::HttpRequest; @@ -31,26 +29,9 @@ pub struct Settings { scripts: Vec