finishing additions

This commit is contained in:
Pin
2022-03-12 23:08:47 -05:00
committed by BuildTools
parent 3b8405361c
commit fbe58d57d6
6 changed files with 80 additions and 13 deletions

View File

@@ -27,6 +27,10 @@ char *WEB_ROOT_DIR = NULL;
static int timeout = 0;
/*
* Used to print debug messages to the screen
* when the --verbose flag is passed
*/
int printDebug(char message[]) {
if (verbose_flag == 1) {
printf("[Debug] %s\n", message);
@@ -34,6 +38,12 @@ int printDebug(char message[]) {
return 0;
}
/*
* This function handles incoming requests and stores
* the parsed data into the HTTPRequest struct passed
* into the function. If any errors are encountered
* during this process a negative number will be returned
*/
int parseHTTPRequest(unsigned char *buffer, struct HTTPRequest *r) {
char temp[1]; // Used to check newlines
char *token = calloc(8, sizeof(char));
@@ -45,6 +55,8 @@ int parseHTTPRequest(unsigned char *buffer, struct HTTPRequest *r) {
char * varCheck;
// Creating empty requestBody
// Setting values to 0 helps if variable is not
// set and determining that later on in handling
r->requestBody = malloc(sizeof(char));
r->requestBodyLen = 0;
r->requestVars = malloc(sizeof(char));
@@ -129,11 +141,13 @@ int parseHTTPRequest(unsigned char *buffer, struct HTTPRequest *r) {
}
line++;
} else {
// Appending char to checkLine
strcat((char *)checkLine, temp);
checkLineLen++;
}
}
// Return error if request type is not set
if (strlen(r->requestType) == 0) {
free(logLine);
free(checkLine);
@@ -145,6 +159,10 @@ int parseHTTPRequest(unsigned char *buffer, struct HTTPRequest *r) {
return 0;
}
/*
* This function is the entry point for all new requests; this
* function will always return 0 regardless of outcome
*/
int handleRequest(unsigned char buffer[], int socket, SSL *ssl) {
struct HTTPRequest r; // Holds relevant HTTP request information
r.requestConLen = malloc(sizeof(char));
@@ -189,6 +207,10 @@ int handleRequest(unsigned char buffer[], int socket, SSL *ssl) {
return 0;
}
/*
* This function is used as a signal to kill child processes that
* are triggered by the corresponding alarm
*/
void timeoutChild(int sig) {
timeout = 1;
}
@@ -229,6 +251,7 @@ int main(int argc, char **argv) {
{0, 0, 0, 0}
};
// Define help message
const char* usage =
"Usage: seaweb [options]\n\n"
" -h --help\t\t\tShows this message\n"
@@ -241,6 +264,7 @@ int main(int argc, char **argv) {
" --verbose\t\t\tPrints debug messages\n"
"\n";
// Parsing options
int c;
while (1) {
int option_index = 0;
@@ -293,14 +317,14 @@ int main(int argc, char **argv) {
enableHTTPS = 1;
}
if ( enableHTTPS == 1 ) {
if ( enableHTTPS == 1 ) { // Open secure socket
printf("Opening secure socket on port: %d\n", port);
checkerr = createSecureSocket(port, &server_fd, &address, &addrlen, listenAddrNum,
&ctx, certFile, privKeyFile);
if ( ctx == NULL ) {
printf("Error creating ctx\n");
}
} else {
} else { // Open socket
printf("Opening socket on port: %d\n", port);
checkerr = createSocket(port, &server_fd, &address, &addrlen, listenAddrNum);
}
@@ -321,11 +345,11 @@ int main(int argc, char **argv) {
pid_t pid;
pid = fork();
if (pid == 0) {
if (pid == 0) { // Child process handles sessions
pid_t pid2;
pid2 = fork();
if (pid2 == 0) {
if (pid2 == 0) { // Child child process reads requests and handles return
bufSize = BUFF_READ;
if ( enableHTTPS ) {
size_t buffCont = 1;
@@ -362,14 +386,14 @@ int main(int argc, char **argv) {
exit(EXIT_SUCCESS);
} else if (pid2 < 0) {
printDebug("Error forking supervisor...");
} else {
} else { // Parent process waits to see if child timeout is triggered
int status;
signal(SIGALRM, timeoutChild);
alarm(2);
pause();
if (timeout) {
status = waitpid(pid, NULL, WNOHANG);
if (status == 0) {
if (status == 0) { // If status is not 0 kill child process
printDebug("Killing child");
kill(pid2, 9);
wait(NULL);
@@ -380,7 +404,7 @@ int main(int argc, char **argv) {
close(new_socket);
} else if (pid < 0) {
printDebug("Error forking...");
} else {
} else { // Ignore exit status of spawned child (proc is not a zombie)
signal(SIGCHLD, SIG_IGN);
close(new_socket);
continue;