about summary refs log tree commit diff
path: root/stats_module
diff options
context:
space:
mode:
authorgennyble <gen@nyble.dev>2025-03-27 11:48:36 -0500
committergennyble <gen@nyble.dev>2025-03-27 11:48:36 -0500
commitceb1047ed6bcd45f756bc2b9b0e41ddfdd694d56 (patch)
tree4154140c7e71383b50e25fd48820e234165e970b /stats_module
parentb79b84ce06ee34b5957d7f19aa19ebeff2af1df9 (diff)
downloadcorgi-ceb1047ed6bcd45f756bc2b9b0e41ddfdd694d56.tar.gz
corgi-ceb1047ed6bcd45f756bc2b9b0e41ddfdd694d56.zip
improve saftey
Diffstat (limited to 'stats_module')
-rw-r--r--stats_module/src/lib.rs20
1 files changed, 8 insertions, 12 deletions
diff --git a/stats_module/src/lib.rs b/stats_module/src/lib.rs
index 96cbaa9..a56d22d 100644
--- a/stats_module/src/lib.rs
+++ b/stats_module/src/lib.rs
@@ -1,5 +1,3 @@
-use std::ffi::CStr;
-
 use rusqlite::{Connection, params};
 use smalldog::{ModuleRequest, ModuleResponse, Request, Response};
 use time::{Duration, OffsetDateTime};
@@ -7,6 +5,7 @@ use time::{Duration, OffsetDateTime};
 #[unsafe(no_mangle)]
 extern "C" fn cgi_handle(req: *const ModuleRequest) -> *const ModuleResponse {
 	let mut response = Response::new();
+	let mut body = String::new();
 
 	let request = Request::from_mod_request(req);
 	let db = if let Some(path) = request.header("CORGI_STATS_DB") {
@@ -32,27 +31,24 @@ extern "C" fn cgi_handle(req: *const ModuleRequest) -> *const ModuleResponse {
 
 	agents.sort_by(|a, b| a.0.cmp(&b.0).reverse());
 
-	response.push_str("<p>In the last fifteen minutes:<br/><code><pre>");
-	response.push_str("total | req/m | agent\n");
+	body.push_str("<p>In the last fifteen minutes:<br/><code><pre>");
+	body.push_str("total | req/m | agent\n");
 	for (count, agent) in &agents {
-		response.push_str(&format!(
+		body.push_str(&format!(
 			"{count:<5} | {:<5.1} | {agent}\n",
 			*count as f32 / 15.0
 		));
 	}
-	response.push_str("</pre></code></p>");
+	body.push_str("</pre></code></p>");
+	response.body(body.into_bytes());
 
 	response.into_mod_response(200)
 }
 
 fn make_error<S: AsRef<str>>(code: u16, msg: S) -> *const ModuleResponse {
-	unsafe {
-		smalldog::HEADERS[0][0] = c"Content-Length".as_ptr();
-		smalldog::HEADERS[0][1] = c"text/html".as_ptr();
-	}
-
 	let mut response = Response::new();
-	response.push_str(msg.as_ref());
+	response.header(c"Content-Length", c"text/html");
+	response.body(msg.as_ref().as_bytes().to_vec());
 
 	response.into_mod_response(code)
 }