Skip to content

Is it possible to run FastCGI application just with spawn-fcgi? #113

@serfcity

Description

@serfcity

I wrote "a + b" FastCGI application, but I failed to try start it by spawn-fcgi. Maybe I do something wrong, but I guess problem in spawn-fcgi. If it is, what is the easiest way to run FastCGI application on FreeBSD (with nginx)?

#include <sys/types.h> /* size_t, ssize_t */
#include <stdarg.h> /* va_list */
#include <stddef.h> /* NULL */
#include <stdint.h> /* int64_t */
#include <kcgi.h>

enum key {
	KEY_A = 0,
	KEY_B,
	KEY__MAX
};

static const struct kvalid keys[KEY__MAX] = {
	{ kvalid_int, "a" }, /* KEY_A */
	{ kvalid_int, "b" }, /* KEY_B */
};

enum page {
	PAGE_INDEX = 0,
	PAGE__MAX
};
const char *const pages[PAGE__MAX] = {
	"index", /* PAGE_INDEX */
};

static void
response_bad_request_and_free(struct kreq *req)
{
	khttp_head(req, kresps[KRESP_STATUS],
		   "%s", khttps[KHTTP_400]);
	khttp_head(req, kresps[KRESP_CONTENT_TYPE],
		   "%s", kmimetypes[KMIME_TEXT_PLAIN]);
	khttp_body(req);
	khttp_puts(req, "Bad request\n");
	khttp_free(req);
}

int
main(void)
{
	struct kreq req;
	struct kfcgi *fcgi;
     
	if (khttp_fcgi_init(&fcgi, keys, KEY__MAX,
			    pages, PAGE__MAX, PAGE_INDEX) != KCGI_OK)
		return 1;

	if (khttp_parse(&req, keys, KEY__MAX,
				pages, PAGE__MAX, PAGE_INDEX) != KCGI_OK) {
			kutil_err(&req, NULL, "khttp_parse failed");
	}
	
	while (khttp_fcgi_parse(fcgi, &req) == KCGI_OK) {
		struct kpair *p1, *p2;
		
		if ((p1 = req.fieldmap[KEY_A])) {
			response_bad_request_and_free(&req);
			continue;
		}
		if ((p2 = req.fieldmap[KEY_B])) {
			response_bad_request_and_free(&req);
			continue;
		}
		khttp_head(&req, kresps[KRESP_STATUS],
			   "%s", khttps[KHTTP_200]);
		khttp_head(&req, kresps[KRESP_CONTENT_TYPE],
			   "%s", kmimetypes[KMIME_TEXT_PLAIN]);
		khttp_body(&req);
		khttp_printf(&req, "%ld\n", p1->parsed.i + p2->parsed.i);
		khttp_free(&req);
	}
     
	khttp_fcgi_free(fcgi);
	return 0;
}

It crushes with error "Bad file descriptor" when I run it as spawn-fcgi -f ./a_plus_b -n -p 9000

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions