diff options
author | gennyble <gen@nyble.dev> | 2025-04-04 01:45:07 -0500 |
---|---|---|
committer | gennyble <gen@nyble.dev> | 2025-04-04 01:45:07 -0500 |
commit | 4410950c761970d640af00f245fdba859f8795b0 (patch) | |
tree | ae2ced50c1ea96f01eed51e1ee0394473475da81 /smalldog | |
parent | 31d650e75acfd447cf6c58d29ca4d6ad1010a65c (diff) | |
download | corgi-4410950c761970d640af00f245fdba859f8795b0.tar.gz corgi-4410950c761970d640af00f245fdba859f8795b0.zip |
rip out module system
Diffstat (limited to 'smalldog')
-rw-r--r-- | smalldog/Cargo.toml | 6 | ||||
-rw-r--r-- | smalldog/README.md | 43 | ||||
-rw-r--r-- | smalldog/src/lib.rs | 138 |
3 files changed, 0 insertions, 187 deletions
diff --git a/smalldog/Cargo.toml b/smalldog/Cargo.toml deleted file mode 100644 index 6ae5cc4..0000000 --- a/smalldog/Cargo.toml +++ /dev/null @@ -1,6 +0,0 @@ -[package] -name = "smalldog" -version = "0.1.0" -edition = "2024" - -[dependencies] diff --git a/smalldog/README.md b/smalldog/README.md deleted file mode 100644 index 2505f5f..0000000 --- a/smalldog/README.md +++ /dev/null @@ -1,43 +0,0 @@ -smalldog - -a crate for safely working with corgi's cgi module system. - -module's are loaded in a new thread at the time of request. - -if you want to build a module in C, these structs will be of use: -```c -// In Rust this is interpreted as a fixed size, 2-element array of char* -// but i cannot get C to like that kind of at all. sorry. -struct pair { - char *name; - char *value; -}; - -// The request from corgi. body may be null if one was not sent. -struct request { - u64_t headers_len; - struct pair *headers; - u64_t body_len; - u8_t *body; -}; - -// The request your module should return to corgi -struct response { - u16_t status; - u64_t headers_len; - struct pair *headers; - u64_t body_len; - u8_t *body; -}; -``` - -as well as that, there are two functions that corgi expects to exist and will call. - -`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 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/smalldog/src/lib.rs b/smalldog/src/lib.rs deleted file mode 100644 index 1b1ddbb..0000000 --- a/smalldog/src/lib.rs +++ /dev/null @@ -1,138 +0,0 @@ -use std::{ - borrow::Cow, - ffi::{CStr, c_void}, -}; - -use ffi::{ModuleRequest, ModuleResponse}; - -pub mod ffi { - use core::ffi; - use std::marker::PhantomData; - - #[repr(C)] - pub struct ModuleRequest<'req> { - pub headers_len: u64, - pub headers: *const [*const ffi::c_char; 2], - pub body_len: u64, - pub body: *const u8, - pub _phantom: PhantomData<&'req u8>, - } - - #[repr(C)] - pub struct ModuleResponse { - pub status: u16, - pub headers_len: u64, - 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, - } -} - -pub struct Request<'req> { - headers: Vec<(Cow<'req, str>, Cow<'req, str>)>, - body: Option<&'req [u8]>, -} - -impl<'req> Request<'req> { - pub fn from_mod_request(request: *const ModuleRequest<'req>) -> Self { - // SAFTEY: corgi will never give us a null pointer - let reqref = unsafe { request.as_ref() }.unwrap(); - - let headers_ffi = - unsafe { std::slice::from_raw_parts(reqref.headers, reqref.headers_len as usize) }; - - let mut headers = vec![]; - for pair in headers_ffi { - let k = unsafe { CStr::from_ptr(pair[0]) }.to_string_lossy(); - let v = unsafe { CStr::from_ptr(pair[1]) }.to_string_lossy(); - headers.push((k, v)); - } - - let body = if reqref.body.is_null() { - None - } else { - Some(unsafe { std::slice::from_raw_parts(reqref.body, reqref.body_len as usize) }) - }; - - Self { headers, body } - } - - pub fn header(&self, key: &str) -> Option<&str> { - for (hkey, hval) in &self.headers { - if hkey == key { - return Some(hval); - } - } - - None - } - - pub fn headers(&self) -> &[(Cow<str>, Cow<str>)] { - &self.headers - } - - pub fn body(&self) -> Option<&[u8]> { - self.body - } -} - -pub struct Response { - headers: Vec<(Cow<'static, CStr>, Cow<'static, CStr>)>, - ffi_headers: Vec<[*const i8; 2]>, - body: Vec<u8>, -} - -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 boxed_self = Box::new(self); - - 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 boxed = Box::new(ModuleResponse { - status, - 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) - } - - pub fn header<K: Into<Cow<'static, CStr>>, V: Into<Cow<'static, CStr>>>( - &mut self, - key: K, - value: V, - ) -> &mut Self { - self.headers.push((key.into(), value.into())); - self - } - - pub fn body(&mut self, vec: Vec<u8>) -> &mut Self { - self.body = vec; - self - } - - pub fn cleanup(response: *const ModuleResponse) { - 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); - } -} |