added fail support

This commit is contained in:
Pin
2022-01-29 18:41:07 -05:00
parent 7ddfb06557
commit c17d674ea6
2 changed files with 33 additions and 8 deletions

View File

@@ -26,8 +26,19 @@ int parseHTTPRequest(char buffer[], struct HTTPRequest *r) {
token = strtok(checkLine, " ");
// HTTP Request Type
if (!strcmp(token, "GET")) {
// Grabbing HTTP Request Type
r->requestType = malloc(strlen(token));
strcpy(r->requestType, token);
// Grabbing HTTP Request Dir
token = strtok(NULL, " ");
r->requestDir = malloc(strlen(token));
strcpy(r->requestDir, token);
// Grabbing HTTP Request Version
token = strtok(NULL, "");
r->requestVersion = malloc(strlen(token));
strcpy(r->requestVersion, token);
} else {
return 1;
}
} else {
token = strtok(checkLine, ":");
@@ -52,18 +63,32 @@ int parseHTTPRequest(char buffer[], struct HTTPRequest *r) {
return 0;
}
int returnRequest(int socket) {
char *hello = "HTTP/1.1 200 OK\nContent-Length: 5\nConnection: close\n\nhello\n\n";
printf("Returning\n%s", hello);
send(socket, hello, strlen(hello), 0);
int returnRequest(int socket, char *message) {
send(socket, message, strlen(message), 0);
return 0;
}
int return200Request(int socket) {
char *message = "HTTP/1.1 200 OK\nContent-Length: 6\nConnection: close\n\nhello\n";
return returnRequest(socket, message);
}
int return404Request(int socket) {
char *message = "HTTP/1.1 404 Not Found\nContent-Length: 12\nConnection: close\n\n404 Request\n";
return returnRequest(socket, message);
}
int handleRequest(char buffer[], int socket) {
struct HTTPRequest r;
struct HTTPRequest r; // Holds relevant HTTP request information
int checkerr = 0;
// Grabbing relevant information out of request
parseHTTPRequest(buffer, &r);
returnRequest(socket);
checkerr = parseHTTPRequest(buffer, &r);
if (checkerr != 0) { // Checking for HTTP parsing error
perror("Badness!!!");
return return404Request(socket);
}
// Return response to socket
return200Request(socket);
return 0;
}
@@ -90,7 +115,6 @@ int main(int argc, char const *argv[]) {
exit(EXIT_FAILURE);
}
read(new_socket, buffer, 1024);
printf("Conn received:\n%s\n", buffer);
handleRequest(buffer, new_socket);
}