aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob L. Kreuze2026-06-06 10:37:26 -0400
committerJakob L. Kreuze2026-06-06 10:37:26 -0400
commit7c68674a5125a1e540a448f5aeea340171a6c1ab (patch)
treef9362f713a79a2118954c3e74a801f8527c03386
parentb747932a138f184cbf85420130efcb50a6fe3f7b (diff)
Wrapper for libfcgi
-rw-r--r--src/fcgi.c76
-rw-r--r--weeb/fcgi.scm24
2 files changed, 100 insertions, 0 deletions
diff --git a/src/fcgi.c b/src/fcgi.c
new file mode 100644
index 0000000..1ff8293
--- /dev/null
+++ b/src/fcgi.c
@@ -0,0 +1,76 @@
+/* Weeb --- Web microframework for GNU Guile
+ * Copyright © 2026 Jakob Kreuze <zerodaysfordays@sdf.org>
+ *
+ * This file is part of Weeb.
+ *
+ * Weeb is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * Weeb is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
+ * Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with Weeb. If not, see <https://www.gnu.org/licenses/>. */
+
+// gcc $(pkg-config --cflags guile-3.0) -shared -o gfcgi.so -lfcgi -fPIC fcgi.c
+
+#include "libguile/foreign.h"
+#include "libguile/gc.h"
+#include "libguile/strings.h"
+#include <libguile.h>
+#include <fcgi_stdio.h>
+
+SCM FCGI_Accept_wrapper(void) { return scm_from_int(FCGI_Accept()); }
+
+SCM FCGI_fwrite_wrapper(SCM x) {
+ char *output = scm_to_utf8_string(x);
+ int result = printf("%s", output);
+ fflush(stdout);
+ return scm_from_int(result);
+}
+
+SCM FCGI_fread_wrapper(void) {
+ size_t capacity = 1024;
+ size_t size = 0;
+ char *buffer = scm_gc_malloc_pointerless(capacity, "buffer");
+
+ if (!buffer) {
+ return NULL;
+ }
+
+ for (;;) {
+ size_t remaining = capacity - size;
+ size_t n = fread(buffer + size, 1, remaining, stdin);
+
+ if (n == 0)
+ break;
+
+ size += n;
+
+ if (size == capacity) {
+ capacity *= 2;
+ char *tmp = scm_gc_realloc(buffer, size, capacity, "buffer");
+ if (!tmp) {
+ free(buffer);
+ return NULL;
+ }
+ buffer = tmp;
+ } else {
+ break;
+ }
+ }
+
+ SCM result = scm_from_pointer(buffer, NULL);
+ result = scm_pointer_to_bytevector(result, SCM_BOOL_F, SCM_BOOL_F, scm_from_int32(size));
+ return result;
+}
+
+void init_libfcgi(void) {
+ scm_c_define_gsubr("fcgi-accept", 0, 0, 0, FCGI_Accept_wrapper);
+ scm_c_define_gsubr("fcgi-display", 1, 0, 0, FCGI_fwrite_wrapper);
+ scm_c_define_gsubr("fcgi-read", 0, 0, 0, FCGI_fread_wrapper);
+}
diff --git a/weeb/fcgi.scm b/weeb/fcgi.scm
new file mode 100644
index 0000000..8d28edf
--- /dev/null
+++ b/weeb/fcgi.scm
@@ -0,0 +1,24 @@
+;;; Weeb --- Web microframework for GNU Guile
+;;; Copyright © 2026 Jakob Kreuze <zerodaysfordays@sdf.org>
+;;;
+;;; This file is part of Weeb.
+;;;
+;;; Weeb is free software: you can redistribute it and/or modify it
+;;; under the terms of the GNU Affero General Public License as
+;;; published by the Free Software Foundation, either version 3 of the
+;;; License, or (at your option) any later version.
+;;;
+;;; Weeb is distributed in the hope that it will be useful, but WITHOUT
+;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+;;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
+;;; Public License for more details.
+;;;
+;;; You should have received a copy of the GNU Affero General Public
+;;; License along with Weeb. If not, see <https://www.gnu.org/licenses/>.
+
+(define-module (weeb fcgi)
+ #:export (fcgi-accept
+ fcgi-display
+ fcgi-read))
+
+(load-extension "./gfcgi" "init_libfcgi")

© 2015 - 2026 Jakob L. Kreuze