From f016a3d27e47076bfd367285c003478d990e08bb Mon Sep 17 00:00:00 2001
From: gennyble <gen@nyble.dev>
Date: Thu, 20 Mar 2025 10:35:01 -0500
Subject: Spawn a thread for the module

---
 parrot_module/src/lib.rs | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

(limited to 'parrot_module/src/lib.rs')

diff --git a/parrot_module/src/lib.rs b/parrot_module/src/lib.rs
index 148f41d..38148ae 100644
--- a/parrot_module/src/lib.rs
+++ b/parrot_module/src/lib.rs
@@ -21,7 +21,7 @@ const HEADERS: &'static [[*const ffi::c_char; 2]] =
 	&[[c"Content-Type".as_ptr(), c"text/plain".as_ptr()]];
 
 #[unsafe(no_mangle)]
-extern "C" fn handle(req: *const ModuleRequest) -> *const ModuleResponse {
+extern "C" fn cgi_handle(req: *const ModuleRequest) -> *const ModuleResponse {
 	let mut ret = String::new();
 
 	// unwrap is bad here
@@ -31,7 +31,14 @@ extern "C" fn handle(req: *const ModuleRequest) -> *const ModuleResponse {
 		let kvarr = reqref.headers[idx as usize];
 		let k = unsafe { CStr::from_ptr(kvarr[0]) }.to_string_lossy();
 		let v = unsafe { CStr::from_ptr(kvarr[1]) }.to_string_lossy();
-		ret.push_str(&format!("{k}: {v}\n"));
+
+		// While debugging I removed the format!() because it was SIGSEGVing
+		// as the String it allocated freed, and now that this is here it can stay
+		ret.push_str(&k);
+		ret.push(':');
+		ret.push(' ');
+		ret.push_str(&v);
+		ret.push('\n')
 	}
 
 	let body = CString::new(ret).unwrap();
@@ -45,13 +52,16 @@ extern "C" fn handle(req: *const ModuleRequest) -> *const ModuleResponse {
 	};
 
 	let boxed = Box::new(resp);
-
 	Box::<ModuleResponse>::into_raw(boxed)
 }
 
 #[unsafe(no_mangle)]
-extern "C" fn free(req: *const ModuleResponse) {
+extern "C" fn cgi_cleanup(req: *const ModuleResponse) {
 	// from_raw what we need to here so that these get dropped
 	let boxed = unsafe { Box::from_raw(req as *mut ModuleResponse) };
 	let body = unsafe { CString::from_raw(boxed.body as *mut i8) };
+
+	// Explicitly calling drop here to feel good about myself
+	drop(body);
+	drop(boxed);
 }
-- 
cgit 1.4.1-3-g733a5