diff options
Diffstat (limited to 'src/fcgi.c')
| -rw-r--r-- | src/fcgi.c | 76 |
1 files changed, 76 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); +} |