about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgennyble <gen@nyble.dev>2025-04-02 06:53:54 -0500
committergennyble <gen@nyble.dev>2025-04-02 07:35:20 -0500
commit10925e207a28635e207459c5b78581385c9dbe9e (patch)
treeed04e75f6df7048c3a4cd345c75045234ba6332a
parent545a33aac755ede43e2509d0492851765d3fd832 (diff)
downloadcorgi-10925e207a28635e207459c5b78581385c9dbe9e.tar.gz
corgi-10925e207a28635e207459c5b78581385c9dbe9e.zip
remember to remove old ffi structs in corgi when using smalldog
-rw-r--r--corgi/src/caller.rs33
-rw-r--r--smalldog/README.md4
-rw-r--r--stats_module/src/lib.rs2
3 files changed, 14 insertions, 25 deletions
diff --git a/corgi/src/caller.rs b/corgi/src/caller.rs
index 4f75d32..aef6bc3 100644
--- a/corgi/src/caller.rs
+++ b/corgi/src/caller.rs
@@ -1,11 +1,13 @@
 use std::{
 	ffi::{self, CString},
+	marker::PhantomData,
 	net::IpAddr,
 	process::Stdio,
 	ptr,
 	str::FromStr,
 };
 
+use smalldog::ffi::{ModuleRequest, ModuleResponse};
 use tokio::{
 	io::AsyncWriteExt,
 	process::Command,
@@ -168,23 +170,6 @@ pub struct CgiResponse {
 	pub body: Option<Vec<u8>>,
 }
 
-#[repr(C)]
-struct ModuleRequest {
-	headers_len: ffi::c_ulong,
-	headers: *const [[*const ffi::c_char; 2]],
-	body_len: ffi::c_ulong,
-	body: *const u8,
-}
-
-#[repr(C)]
-struct ModuleResponse {
-	status: ffi::c_ushort,
-	headers_len: ffi::c_ulong,
-	headers: &'static [[*const ffi::c_char; 2]],
-	body_len: ffi::c_ulong,
-	body: *const u8,
-}
-
 type HandleFn = unsafe extern "C" fn(*const ModuleRequest) -> *const ModuleResponse;
 type CleanupFn = unsafe extern "C" fn(*const ModuleResponse);
 
@@ -216,9 +201,10 @@ unsafe fn module_thread(script: Script, req: HttpRequest, tx: Sender<CgiResponse
 
 	let modreq = ModuleRequest {
 		headers_len: headers.len() as u64,
-		headers: &headers[..] as *const [[*const ffi::c_char; 2]],
+		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 {
@@ -235,10 +221,13 @@ unsafe fn module_thread(script: Script, req: HttpRequest, tx: Sender<CgiResponse
 		let response = handle((&modreq) as *const ModuleRequest);
 		let response_ref = response.as_ref().unwrap();
 
-		for idx in 0..response_ref.headers_len {
-			let kvarr = response_ref.headers[idx as usize];
-			let k = ffi::CStr::from_ptr(kvarr[0]).to_string_lossy();
-			let v = ffi::CStr::from_ptr(kvarr[1]).to_string_lossy();
+		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()));
 		}
 
diff --git a/smalldog/README.md b/smalldog/README.md
index 87d3416..2505f5f 100644
--- a/smalldog/README.md
+++ b/smalldog/README.md
@@ -33,11 +33,11 @@ struct response {
 
 as well as that, there are two functions that corgi expects to exist and will call.
 
-`struct response *handle(struct request*);`  
+`struct response *cgi_handle(struct request*);`  
 this function is called after the module is loaded. here you should process the
 request and send back a response. be sure to keep track of any allocations so
 you can clean them up later.
 
-`void cleanup(struct response*);`  
+`void cgi_cleanup(struct response*);`  
 this is where you clean up, later. function is called after corgi copies all
 required data to an internal structure. free memory here.
\ No newline at end of file
diff --git a/stats_module/src/lib.rs b/stats_module/src/lib.rs
index 47a5e6f..18a7c2b 100644
--- a/stats_module/src/lib.rs
+++ b/stats_module/src/lib.rs
@@ -63,7 +63,7 @@ extern "C" fn cgi_handle(req: *const ffi::ModuleRequest) -> *const ffi::ModuleRe
 	}
 	body.push_str("</pre></code></p>");
 
-	response.body(body.into_bytes());
+	response.body(body.into_bytes()).header(c"Content-Type", c"text/html");
 	response.into_mod_response(200)
 }