added php execution
This commit is contained in:
134
cmd/server.c
134
cmd/server.c
@@ -16,8 +16,8 @@
|
||||
#include "server.h"
|
||||
#include "utils.h"
|
||||
|
||||
//#define WEB_ROOT "/var/www/html/"
|
||||
#define WEB_ROOT "content/"
|
||||
#define WEB_ROOT "/var/www/html/"
|
||||
//#define WEB_ROOT "content/"
|
||||
#define BUFF_READ 1024
|
||||
|
||||
static int verbose_flag = 0;
|
||||
@@ -32,6 +32,51 @@ int printDebug(char message[]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void php_cgi(char *sPath) {
|
||||
int phpPipe[2];
|
||||
char *buf = NULL;
|
||||
size_t bufLen = 1024;
|
||||
|
||||
buf = malloc(bufLen);
|
||||
pipe(phpPipe);
|
||||
|
||||
pid_t pid;
|
||||
pid = fork();
|
||||
|
||||
if (pid == 0) {
|
||||
char script[500];
|
||||
close(phpPipe[0]);
|
||||
dup2(phpPipe[1], STDOUT_FILENO);
|
||||
strcpy(script, "SCRIPT_FILENAME=");
|
||||
strcat(script, WEB_ROOT);
|
||||
strcat(script, sPath);
|
||||
putenv("GATEWAY_INTERFACE=CGI/1.1");
|
||||
putenv(script);
|
||||
putenv("QUERY_STRING=");
|
||||
putenv("REQUEST_METHOD=GET");
|
||||
putenv("REDIRECT_STATUS=true");
|
||||
putenv("SERVER_PROTOCOL=HTTP/1.1");
|
||||
putenv("REMOTE_HOST=127.0.0.1");
|
||||
execl("/usr/bin/php-cgi", "php-cgi", NULL);
|
||||
} else if (pid < 0) {
|
||||
printDebug("Error forking php exec");
|
||||
} else {
|
||||
size_t buffCount;
|
||||
close(phpPipe[1]);
|
||||
do {
|
||||
buffCount = read(phpPipe[0], buf, BUFF_READ);
|
||||
if (strlen(buf) == bufLen) {
|
||||
bufLen *= 2;
|
||||
buf = realloc(buf, bufLen);
|
||||
}
|
||||
} while (buffCount == 0);
|
||||
}
|
||||
|
||||
close(phpPipe[0]);
|
||||
printf("PHP: %s\n", buf);
|
||||
return;
|
||||
}
|
||||
|
||||
int parseHTTPRequest(unsigned char *buffer, struct HTTPRequest *r) {
|
||||
char temp[1]; // Used to check newlines
|
||||
char *token = calloc(8, sizeof(char));
|
||||
@@ -96,6 +141,18 @@ int parseHTTPRequest(unsigned char *buffer, struct HTTPRequest *r) {
|
||||
token = strtok(NULL, "");
|
||||
r->requestHost = malloc(strlen(token));
|
||||
strcpy(r->requestHost, token);
|
||||
} else if (!strcmp(token, "Content-Type")) {
|
||||
token = strtok(NULL, ":");
|
||||
r->requestConType = malloc(strlen(token));
|
||||
strcpy(r->requestConType, token);
|
||||
} else if (!strcmp(token, "Content-Length")) {
|
||||
token = strtok(NULL, ":");
|
||||
if (token == NULL) {
|
||||
printDebug("Content-length parsing error");
|
||||
return -1;
|
||||
}
|
||||
r->requestConLen = malloc(strlen(token));
|
||||
strcpy(r->requestConLen, token);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -224,50 +281,68 @@ int handleDELETERequest(int socket, struct HTTPRequest *r, SSL *ssl) {
|
||||
}
|
||||
|
||||
int handlePOSTRequest(int socket, struct HTTPRequest *r, SSL *ssl) {
|
||||
return501Request(socket, ssl);
|
||||
return 0;
|
||||
// Supported data types application/x-www-form-urlencoded application/json
|
||||
if (r->requestConLen == NULL) { // Return 411 if length var not specified
|
||||
printDebug("Content Length was not sent in POST request");
|
||||
return return411Request(socket, ssl);
|
||||
}
|
||||
//r->requestBody[strlen((char *)r->requestBody) - 1] = '\0'; // Resetting request content length to remove trailing newline
|
||||
|
||||
if (r->requestConType != NULL) {
|
||||
if (!strcmp(r->requestConType, "application/json")) {
|
||||
printf("JSON Request\n");
|
||||
} else if (!strcmp(r->requestConType, "application/x-www-form-utlencoded")) {
|
||||
printf("Application form\n");
|
||||
}
|
||||
} else {
|
||||
printf("HERE\n");
|
||||
}
|
||||
|
||||
php_cgi(r->requestDir);
|
||||
|
||||
return return501Request(socket, ssl);
|
||||
}
|
||||
|
||||
int handleRequest(unsigned char buffer[], int socket, SSL *ssl) {
|
||||
struct HTTPRequest r; // Holds relevant HTTP request information
|
||||
r.requestConLen = malloc(sizeof(char));
|
||||
r.requestConType = NULL;
|
||||
int checkerr = 0;
|
||||
|
||||
// Grabbing relevant information out of request
|
||||
checkerr = parseHTTPRequest(buffer, &r);
|
||||
if (checkerr != 0) { // Checking for HTTP parsing error
|
||||
printDebug("Error ;(");
|
||||
if (checkerr == -1) {
|
||||
printDebug("Error reading request, returning empty 500");
|
||||
return return500Request(socket, ssl);
|
||||
return500Request(socket, ssl);
|
||||
} else {
|
||||
printDebug("Error parsing, returning 501");
|
||||
return return501Request(socket, ssl);
|
||||
return501Request(socket, ssl);
|
||||
}
|
||||
} else {
|
||||
checkerr = checkHTTPVersion(r.requestVersion);
|
||||
if (checkerr != 0) {
|
||||
return return505Request(socket, ssl);
|
||||
return505Request(socket, ssl);
|
||||
} else {
|
||||
if (!strcmp(r.requestType, "GET")) {
|
||||
handleGetRequest(socket, &r, ssl);
|
||||
} else if (!strcmp(r.requestType, "POST")) {
|
||||
handlePOSTRequest(socket, &r, ssl);
|
||||
} else if (!strcmp(r.requestType, "PUT")) {
|
||||
handlePUTRequest(socket, &r, ssl);
|
||||
} else if (!strcmp(r.requestType, "DELETE")) {
|
||||
handleDELETERequest(socket, &r, ssl);
|
||||
} else if (!strcmp(r.requestType, "CONNECT")) {
|
||||
return200Request(socket, NULL, ssl);
|
||||
} else {
|
||||
return500Request(socket, ssl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!strcmp(r.requestType, "GET")) {
|
||||
handleGetRequest(socket, &r, ssl);
|
||||
return 0;
|
||||
} else if (!strcmp(r.requestType, "POST")) {
|
||||
handlePOSTRequest(socket, &r, ssl);
|
||||
return 0;
|
||||
} else if (!strcmp(r.requestType, "PUT")) {
|
||||
handlePUTRequest(socket, &r, ssl);
|
||||
return 0;
|
||||
} else if (!strcmp(r.requestType, "DELETE")) {
|
||||
handleDELETERequest(socket, &r, ssl);
|
||||
return 0;
|
||||
} else if (!strcmp(r.requestType, "CONNECT")) {
|
||||
return200Request(socket, NULL, ssl);
|
||||
return 0;
|
||||
} else {
|
||||
return500Request(socket, ssl);
|
||||
return 0;
|
||||
}
|
||||
free(r.requestConLen);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void timeoutChild(int sig) {
|
||||
@@ -394,7 +469,6 @@ int main(int argc, char **argv) {
|
||||
perror("Accept connection error");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Forking process
|
||||
pid_t pid;
|
||||
pid = fork();
|
||||
@@ -420,7 +494,7 @@ int main(int argc, char **argv) {
|
||||
} while(buffCont == 0);
|
||||
if (strlen((char *)buffer) != 0) {
|
||||
handleRequest(buffer, new_socket, ssl);
|
||||
buffer = calloc(bufSize, sizeof(unsigned char));
|
||||
free(buffer);
|
||||
} else {
|
||||
printDebug("Error reading from socket");
|
||||
}
|
||||
@@ -435,7 +509,7 @@ int main(int argc, char **argv) {
|
||||
} while(buffCont == 0);
|
||||
|
||||
handleRequest(buffer, new_socket, NULL);
|
||||
buffer = calloc(bufSize, sizeof(unsigned char));
|
||||
free(buffer);
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
} else if (pid2 < 0) {
|
||||
@@ -464,7 +538,6 @@ int main(int argc, char **argv) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
free(privKeyFile);
|
||||
free(certFile);
|
||||
free(listenAddr);
|
||||
@@ -472,3 +545,4 @@ int main(int argc, char **argv) {
|
||||
close(server_fd);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user