aboutsummaryrefslogtreecommitdiff
path: root/src/fcgi.c
blob: 1f4cbb2e0638414c6926fe2cbb5164ebd0c67dae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* 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) -fPIC -shared -o gfcgi.so $(pkg-config --libs guile-3.0) -lfcgi src/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);
		SCM length  = scm_from_size_t(size);
		SCM offset  = scm_from_size_t(0);
        SCM u8_type = scm_from_utf8_symbol("u8");
		return scm_pointer_to_bytevector(result, length, offset, u8_type);
}

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);
}

© 2015 - 2026 Jakob L. Kreuze