summaryrefslogtreecommitdiff
path: root/file.c
diff options
context:
space:
mode:
authorcflip <cflip@cflip.net>2023-07-14 19:16:02 -0600
committercflip <cflip@cflip.net>2023-07-14 19:16:02 -0600
commit555f21649b909fd37a6153843249d72ca80ac427 (patch)
tree32b0a646b81d89278bf3769be0a971fb7729ac78 /file.c
parent8ddca3948791d484614f1b2a753f04c478072050 (diff)
Run executable files as CGI scripts
Now if the server encounters an executable file that doesn't have a .php extension, it will run it as a CGI script. There should maybe be better heuristics for determining whether a file actually is a CGI script (sometimes files copied from Windows can be marked as executable) but this works for now.
Diffstat (limited to 'file.c')
-rw-r--r--file.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/file.c b/file.c
index b64480c..4e42c58 100644
--- a/file.c
+++ b/file.c
@@ -48,6 +48,8 @@ const char *file_path_for_uri(const char *uri)
enum serve_method file_method_for_path(const char *filepath, enum http_res_code *code)
{
+ struct stat statbuf;
+
if (access(filepath, F_OK) != 0) {
*code = HTTP_RESPONSE_NOTFOUND;
return SERVE_METHOD_ERROR;
@@ -57,6 +59,11 @@ enum serve_method file_method_for_path(const char *filepath, enum http_res_code
if (strstr(filepath, ".php") != 0)
return SERVE_METHOD_PHP;
+ stat(filepath, &statbuf);
+ if (statbuf.st_mode & S_IXUSR) {
+ return SERVE_METHOD_CGI;
+ }
+
return SERVE_METHOD_FILE;
}
@@ -128,7 +135,7 @@ static void cgi_setup_env(const char *filepath, const struct http_request *req)
}
}
-int file_read_php(const char *filepath, const struct http_request *req, int sockfd)
+int file_read_cgi(const char *filepath, const char *program, const struct http_request *req, int sockfd)
{
int readfds[2];
int writefds[2];
@@ -171,7 +178,7 @@ int file_read_php(const char *filepath, const struct http_request *req, int sock
}
}
- execl("/usr/bin/php-cgi", "php-cgi", NULL);
+ execl(program, program, NULL);
/* We should only end up here if there's an error. */
perror("exec");
exit(1);