added get request return

This commit is contained in:
Pin
2022-02-21 01:52:03 -05:00
parent 2999c1973e
commit 44a6447047
8 changed files with 112 additions and 16 deletions

View File

@@ -13,6 +13,8 @@
#include "returnRequest.h"
#include "server.h"
#define WEB_ROOT "content/"
static int verbose_flag = 0;
bool enableHTTPS = 0;
@@ -23,13 +25,13 @@ int printDebug(char message[]) {
return 0;
}
int parseHTTPRequest(char buffer[], struct HTTPRequest *r) {
int parseHTTPRequest(unsigned char *buffer, struct HTTPRequest *r) {
char temp[1]; // Used to check newlines
char *token = calloc(8, sizeof(char));
int line = 0;
char *checkLine = calloc(1000, sizeof(char));
for (int i = 0; i < strlen(buffer); i++) {
for (int i = 0; i < strlen((char *)buffer); i++) {
temp[0] = buffer[i];
if ((!strcmp(temp, "\n")) && (i != 0)) {
// Config Check
@@ -48,8 +50,11 @@ int parseHTTPRequest(char buffer[], struct HTTPRequest *r) {
token = strtok(NULL, "");
r->requestVersion = malloc(strlen(token));
strcpy(r->requestVersion, token);
} else if (!strcmp(token, "POST")) {
printDebug("POST RECEIVED");
return 2;
} else {
return 1;
return 2;
}
} else {
token = strtok(checkLine, ":");
@@ -70,11 +75,60 @@ int parseHTTPRequest(char buffer[], struct HTTPRequest *r) {
strcat(checkLine, temp);
}
}
if (strlen(r->requestType) == 0) {
return -1;
}
free(checkLine);
return 0;
}
int handleRequest(char buffer[], int socket, SSL *ssl) {
int handleGetRequest(int socket, struct HTTPRequest *r, SSL *ssl) {
char errResponse[256];
char ch;
size_t size = 8;
int i = 0;
unsigned char *fileContent = calloc(size, sizeof(unsigned char));
FILE *fp;
char *workingReqDir;
// If requesting root directory change to index.html
if (!strcmp(r->requestDir, "/")) {
workingReqDir = "index.html";
} else {
workingReqDir = r->requestDir;
}
char *reqDir = calloc(strlen(WEB_ROOT) + strlen(workingReqDir), sizeof(char));
sprintf(reqDir, "%s%s", WEB_ROOT, workingReqDir);
fp = fopen(reqDir, "r");
if (fp == NULL) {
sprintf(errResponse, "Error opening file: %s", workingReqDir);
printDebug(errResponse);
return404Request(socket, ssl);
return -1;
}
while((ch = fgetc(fp)) != EOF) {
if (i == size) {
size *= 2;
fileContent = realloc(fileContent, size);
}
fileContent[i] = ch;
i++;
}
return200Request(socket, fileContent, ssl);
fclose(fp);
free(reqDir);
free(fileContent);
return 0;
}
int handleRequest(unsigned char buffer[], int socket, SSL *ssl) {
struct HTTPRequest r; // Holds relevant HTTP request information
int checkerr = 0;
@@ -83,11 +137,21 @@ int handleRequest(char buffer[], int socket, SSL *ssl) {
// Grabbing relevant information out of request
checkerr = parseHTTPRequest(buffer, &r);
if (checkerr != 0) { // Checking for HTTP parsing error
printf("Error parsing: exit code %d\n", checkerr);
return return404Request(socket, ssl);
if (checkerr == -1) {
printDebug("Error reading request, returning empty 404");
} else {
printDebug("Error parsing, returning 500");
// return return500Request(socket, ssl);
}
return return400Request(socket, ssl);
}
if (!strcmp(r.requestType, "GET")) {
handleGetRequest(socket, &r, ssl);
return 0;
}
// Return response to socket
return200Request(socket, ssl);
return200Request(socket, NULL ,ssl);
return 0;
}
@@ -98,18 +162,18 @@ int main(int argc, char **argv) {
int checkerr = 0; // Used for error checking
int addrlen = sizeof(address);
char *certFile = malloc(1);
char *privKeyFile = malloc(1);
char *certFile = malloc(sizeof(char));
char *privKeyFile = malloc(sizeof(char));
certFile[0] = '\0';
privKeyFile[0] = '\0';
uint32_t listenAddrNum = -1;
char *listenAddr = malloc(1);
char *listenAddr = malloc(sizeof(char));
listenAddr[0] = '\0';
SSL_CTX *ctx = NULL;
char buffer[1024] = {};
unsigned char *buffer = calloc(8096, sizeof(unsigned char));
// Setting up options
static const struct option long_options[] = {
@@ -215,7 +279,7 @@ int main(int argc, char **argv) {
ssl = SSL_new(ctx);
SSL_set_fd(ssl, new_socket);
SSL_accept(ssl);
SSL_read(ssl, buffer, sizeof(buffer));
SSL_read(ssl, buffer, 8096);
handleRequest(buffer, new_socket, ssl);
} else {
read(new_socket, buffer, 1024);
@@ -228,6 +292,7 @@ int main(int argc, char **argv) {
free(privKeyFile);
free(certFile);
free(listenAddr);
free(buffer);
close(server_fd);
exit(EXIT_SUCCESS);
}