summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcflip <cflip@cflip.net>2023-06-01 09:48:38 -0600
committercflip <cflip@cflip.net>2023-06-01 10:08:34 -0600
commit6370f5e0bf1e23fe87dc08aaf1a01b48bd142abc (patch)
tree6c41ccd9df49368c57982fb52324b3726109d49c
parent02a542644617f65cf7285f0facebc827fd036b45 (diff)
Allow query strings to be read by PHP scripts
php-cgi is now run without any command line arguments and information such as the script filename and the query string are passed in via environment variables.
-rw-r--r--cfws.c2
-rw-r--r--file.c13
-rw-r--r--file.h2
3 files changed, 8 insertions, 9 deletions
diff --git a/cfws.c b/cfws.c
index 9ccf7b2..afe2a1d 100644
--- a/cfws.c
+++ b/cfws.c
@@ -56,7 +56,7 @@ static void handle_request(const struct http_request *req, int sockfd)
file_read(filepath, sockfd);
break;
case SERVE_METHOD_PHP:
- file_read_php(filepath, sockfd);
+ file_read_php(filepath, req->query_str, sockfd);
break;
case SERVE_METHOD_ERROR: {
const char *errmsg = "Content-Type: text/plain\r\n\r\nEpic fail";
diff --git a/file.c b/file.c
index 622345f..c40cfdc 100644
--- a/file.c
+++ b/file.c
@@ -69,19 +69,18 @@ int file_read(const char *filepath, int sockfd)
return 0;
}
-int file_read_php(const char *filepath, int sockfd)
+int file_read_php(const char *filepath, const char *query_str, int sockfd)
{
FILE *fp;
- char cmdbuf[PATH_MAX];
char buffer[FILE_READBUF_SIZE];
size_t bytes_read;
- strcpy(cmdbuf, "php-cgi ");
- strcat(cmdbuf, filepath);
+ setenv("REQUEST_METHOD", "GET", 1);
+ setenv("SCRIPT_FILENAME", filepath, 1);
+ if (query_str)
+ setenv("QUERY_STRING", query_str, 1);
- printf("r %s\n", cmdbuf);
-
- fp = popen(cmdbuf, "r");
+ fp = popen("php-cgi", "r");
if (fp == NULL) {
perror("Failed to read command");
return 1;
diff --git a/file.h b/file.h
index 52d8915..3081244 100644
--- a/file.h
+++ b/file.h
@@ -16,6 +16,6 @@ const char *file_path_for_uri(const char *);
enum serve_method file_method_for_path(const char *, enum http_res_code *);
int file_read(const char *, int);
-int file_read_php(const char *, int);
+int file_read_php(const char *, const char *, int);
#endif