about summary refs log tree commit diff
path: root/smalldog
diff options
context:
space:
mode:
authorgennyble <gen@nyble.dev>2025-04-04 00:39:07 -0500
committergennyble <gen@nyble.dev>2025-04-04 00:39:07 -0500
commit31d650e75acfd447cf6c58d29ca4d6ad1010a65c (patch)
treedd1e3d2d2659f4932c9b7f8c6b2af6086a113749 /smalldog
parentff02aba40dd599373380631a1d0e87ecbed3f8b5 (diff)
downloadcorgi-31d650e75acfd447cf6c58d29ca4d6ad1010a65c.tar.gz
corgi-31d650e75acfd447cf6c58d29ca4d6ad1010a65c.zip
halt module system work
Diffstat (limited to 'smalldog')
-rw-r--r--smalldog/src/lib.rs46
1 files changed, 21 insertions, 25 deletions
diff --git a/smalldog/src/lib.rs b/smalldog/src/lib.rs
index 1691588..1b1ddbb 100644
--- a/smalldog/src/lib.rs
+++ b/smalldog/src/lib.rs
@@ -1,4 +1,7 @@
-use std::{borrow::Cow, ffi::CStr, ptr, sync::Mutex};
+use std::{
+	borrow::Cow,
+	ffi::{CStr, c_void},
+};
 
 use ffi::{ModuleRequest, ModuleResponse};
 
@@ -22,6 +25,9 @@ pub mod ffi {
 		pub headers: *const [*const ffi::c_char; 2],
 		pub body_len: u64,
 		pub body: *const u8,
+		/// The etc field is not read by corgi and exists so you may
+		/// associate data with a request
+		pub etc: *const ffi::c_void,
 	}
 }
 
@@ -73,12 +79,9 @@ impl<'req> Request<'req> {
 	}
 }
 
-const HEADERS_LEN: usize = 64;
-static mut HEADERS: [[*const core::ffi::c_char; 2]; HEADERS_LEN] = [[ptr::null(), ptr::null()]; 64];
-static RESPONSE: Mutex<Option<Response>> = Mutex::new(None);
-
 pub struct Response {
 	headers: Vec<(Cow<'static, CStr>, Cow<'static, CStr>)>,
+	ffi_headers: Vec<[*const i8; 2]>,
 	body: Vec<u8>,
 }
 
@@ -86,30 +89,26 @@ impl Response {
 	pub fn new() -> Self {
 		Self {
 			headers: vec![],
+			ffi_headers: vec![],
 			body: vec![],
 		}
 	}
 
 	pub fn into_mod_response(self, status: u16) -> *const ModuleResponse {
-		let mut lock = RESPONSE.lock().unwrap();
-		*lock = Some(self);
-
-		let this = lock.as_mut().unwrap();
+		let mut boxed_self = Box::new(self);
 
-		for (idx, (key, value)) in this.headers.iter().enumerate().take(HEADERS_LEN) {
-			unsafe {
-				HEADERS[idx][0] = key.as_ptr();
-				HEADERS[idx][1] = value.as_ptr();
-			}
+		for (key, value) in boxed_self.headers.iter() {
+			let ffi_pair = [key.as_ptr(), value.as_ptr()];
+			boxed_self.ffi_headers.push(ffi_pair);
 		}
 
-		let headers_len = this.headers.len().min(HEADERS_LEN) as u64;
 		let boxed = Box::new(ModuleResponse {
 			status,
-			headers_len,
-			headers: unsafe { HEADERS[..headers_len as usize].as_ptr() },
-			body_len: this.body.len() as u64,
-			body: this.body.as_ptr(),
+			headers_len: boxed_self.ffi_headers.len() as u64,
+			headers: boxed_self.ffi_headers.as_ptr(),
+			body_len: boxed_self.body.len() as u64,
+			body: boxed_self.body.as_ptr(),
+			etc: Box::<Response>::into_raw(boxed_self) as *const c_void,
 		});
 
 		Box::<ModuleResponse>::into_raw(boxed)
@@ -130,13 +129,10 @@ impl Response {
 	}
 
 	pub fn cleanup(response: *const ModuleResponse) {
-		let mut lock = RESPONSE.lock().unwrap();
-		match lock.take() {
-			Some(response) => drop(response),
-			None => (),
-		}
-
 		let boxed = unsafe { Box::from_raw(response as *mut ModuleResponse) };
+		let etc = unsafe { Box::from_raw(boxed.etc as *mut Response) };
+
 		drop(boxed);
+		drop(etc);
 	}
 }