about summary refs log tree commit diff
path: root/corgi/src
diff options
context:
space:
mode:
Diffstat (limited to 'corgi/src')
-rw-r--r--corgi/src/caller.rs33
1 files changed, 11 insertions, 22 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()));
 		}