From f06a31313aac9e621954398483bd0e102ea4587b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Sat, 28 Mar 2009 18:15:43 +0100 Subject: [PATCH] Exec either INTERPRETER or SCRIPT_FILENAME --- fcgi-cgi.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/fcgi-cgi.c b/fcgi-cgi.c index 52824a6..9d60f62 100644 --- a/fcgi-cgi.c +++ b/fcgi-cgi.c @@ -10,9 +10,11 @@ #include #include #include +#include #define MAX_BUFFER_SIZE (64*1024) +#define CONST_STR_LEN(x) (x), sizeof(x) - 1 #define GSTR_LEN(x) (x) ? (x)->str : "", (x) ? (x)->len : 0 #define UNUSED(x) ((void)(x)) @@ -405,12 +407,54 @@ error: fcgi_cgi_child_error(cld); } +static void fcgi_cgi_direct_result(fastcgi_connection *fcon, int status) { + GString *s = g_string_new(0); + g_string_append_printf(s, "Status: %i\r\n\r\n", status); + fastcgi_send_out(fcon, s); + fastcgi_send_out(fcon, NULL); + fastcgi_send_err(fcon, NULL); + fastcgi_end_request(fcon, 0, FCGI_REQUEST_COMPLETE); +} + static void fcgi_cgi_new_request(fastcgi_connection *fcon) { fcgi_cgi_child *cld = (fcgi_cgi_child*) fcon->data; + gchar *binpath; + struct stat st; if (cld) return; + + binpath = fastcgi_connection_environ_lookup(fcon, CONST_STR_LEN("INTERPRETER")); + if (!binpath) binpath = fastcgi_connection_environ_lookup(fcon, CONST_STR_LEN("SCRIPT_FILENAME")); + + if (!binpath) { + fcgi_cgi_direct_result(fcon, 500); + return; + } + + if (-1 == stat(binpath, &st)) { + switch (errno) { + case EACCES: + case ENOTDIR: + fcgi_cgi_direct_result(fcon, 403); + break; + case ENOENT: + fcgi_cgi_direct_result(fcon, 404); + break; + default: + fcgi_cgi_direct_result(fcon, 500); + break; + } + return; + } + + if (!S_ISREG(st.st_mode) || !((S_IXUSR | S_IXGRP | S_IXOTH) && st.st_mode)) { + fcgi_cgi_direct_result(fcon, 403); + return; + } + cld = fcgi_cgi_child_create(fcon->fsrv->data, fcon); fcon->data = cld; - fcgi_cgi_child_start(cld, "/usr/bin/php5-cgi"); + + fcgi_cgi_child_start(cld, binpath); } static void fcgi_cgi_wrote_data(fastcgi_connection *fcon) {