From 2880a91032809e9669c08d5fcb60ae6897502097 Mon Sep 17 00:00:00 2001 From: franchioping <43936644+franchioping@users.noreply.github.com> Date: Wed, 26 Jun 2024 00:57:05 +0100 Subject: [PATCH] Linked lists are REALLY broken but im tired - fix later --- CMakeLists.txt | 28 +++++++++--- src/main.c | 2 +- src/{ => request}/handle_client.c | 43 ++++++++++++------ src/{ => request}/handle_client.h | 0 src/request/headers.c | 8 ++++ src/request/headers.h | 9 ++++ src/test/linked_list.c | 75 +++++++++++++++++++++++++++++++ src/test/linked_list.h | 12 +++++ src/test/test_main.c | 26 +++++++++++ src/util/linkedlist/linkedlist.c | 38 ++++++++++++++++ src/util/linkedlist/linkedlist.h | 22 +++++++++ src/{ => util}/util.c | 0 src/{ => util}/util.h | 0 13 files changed, 244 insertions(+), 19 deletions(-) rename src/{ => request}/handle_client.c (77%) rename src/{ => request}/handle_client.h (100%) create mode 100644 src/request/headers.c create mode 100644 src/request/headers.h create mode 100644 src/test/linked_list.c create mode 100644 src/test/linked_list.h create mode 100644 src/test/test_main.c create mode 100644 src/util/linkedlist/linkedlist.c create mode 100644 src/util/linkedlist/linkedlist.h rename src/{ => util}/util.c (100%) rename src/{ => util}/util.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index a21b2e3..d8ed754 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,8 +3,26 @@ project(HttpServer C) set(CMAKE_C_STANDARD 23) -add_executable(HttpServer src/main.c - src/handle_client.c - src/handle_client.h - src/util.c - src/util.h) +include_directories(".") + + +set(COMMON_SOURCES src/request/handle_client.c + src/request/handle_client.h + src/util/util.c + src/util/util.h + src/request/headers.c + src/request/headers.h + src/util/linkedlist/linkedlist.c + src/util/linkedlist/linkedlist.h) + +set(TARGET_SOURCES src/main.c) + +set(TEST_SOURCES src/test/test_main.c + src/test/linked_list.c + src/test/linked_list.h) + +add_executable(HttpServer ${COMMON_SOURCES} ${TARGET_SOURCES}) + +add_executable(tests ${COMMON_SOURCES} ${TEST_SOURCES}) +target_link_libraries(tests -pthread -lcheck) + diff --git a/src/main.c b/src/main.c index cacf544..2c86c9a 100644 --- a/src/main.c +++ b/src/main.c @@ -4,7 +4,7 @@ #include -#include "handle_client.h" +#include "request/handle_client.h" int main(void) { diff --git a/src/handle_client.c b/src/request/handle_client.c similarity index 77% rename from src/handle_client.c rename to src/request/handle_client.c index f2911e9..ce2179d 100644 --- a/src/handle_client.c +++ b/src/request/handle_client.c @@ -10,13 +10,25 @@ #include -#include "util.h" +#include "../util/util.h" #define BUFF_SIZE 128 #define MAX_BUFF_SIZE 8192 +void print_request(char** lines, ssize_t lines_count, const char* prefix); +bool check_if_request_end(const char* data, ssize_t data_length); +char** request_get_lines_from_buff(const char* buff, size_t buff_len, ssize_t * lines_count); + + +void print_request(char** lines, ssize_t lines_count, const char* prefix){ + for(ssize_t i = 0; i < lines_count; i++){ + char* line = lines[i]; + printf("%s%s\n", prefix, line); + + } +} +bool check_if_request_end(const char* data, ssize_t data_length){ -bool check_if_request_end(char* data, ssize_t data_length){ char* end = "\r\n\r\n"; for(int i = 0; i < 4; i++){ if(end[i] != data[data_length - 4 + i]) @@ -25,14 +37,16 @@ bool check_if_request_end(char* data, ssize_t data_length){ return true; } - -char** request_get_lines_from_buff(const char* buff, size_t buff_len, int* lines_count){ - int line_count = 0; +char** request_get_lines_from_buff(const char* buff, size_t buff_len, ssize_t * lines_count){ + ssize_t line_count = 0; for(int i = 0; i < buff_len-1; i++){ if(buff[i] == '\r' && buff[i+1]=='\n') line_count ++; } char** lines = (char**) malloc(sizeof(char*) * line_count); + if(lines == NULL){ + return NULL; + } int line_start = 0; int line_num = 0; for(int i = 0; i < buff_len; i++){ @@ -49,9 +63,11 @@ char** request_get_lines_from_buff(const char* buff, size_t buff_len, int* lines *lines_count = line_count; return lines; } - ssize_t receive_data(int client_fd, char** data){ char* buff = (char*) malloc(BUFF_SIZE); + if(buff == NULL){ + return -1; + } int buff_size = BUFF_SIZE; while(true){ ssize_t msg_length = recv(client_fd, &buff[buff_size-BUFF_SIZE], BUFF_SIZE, 0); @@ -91,7 +107,7 @@ void handle_client(void* arg){ // Set time-out on recv struct timeval tv; - tv.tv_sec = 10; + tv.tv_sec = 1; tv.tv_usec = 0; if(setsockopt(client_fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv) < 0) printf("setsockopt(SO_RCVTIMEO) Failed."); @@ -104,15 +120,16 @@ void handle_client(void* arg){ printf("Request Lenght: %lu\n", data_length); printf("Request:\n"); - int lines_count = 0; + ssize_t lines_count = 0; char** lines = request_get_lines_from_buff(data, data_length, &lines_count); - - for(int i = 0; i < lines_count; i++){ - char* line = lines[i]; - printf(" %s\n", line); - + if(lines == NULL){ + printf("Failed to get lines from buffer. Aborting..."); + return; } + print_request(lines, lines_count, " "); + + diff --git a/src/handle_client.h b/src/request/handle_client.h similarity index 100% rename from src/handle_client.h rename to src/request/handle_client.h diff --git a/src/request/headers.c b/src/request/headers.c new file mode 100644 index 0000000..19b60ae --- /dev/null +++ b/src/request/headers.c @@ -0,0 +1,8 @@ +// +// Created by franchioping on 6/25/24. +// + +#include "headers.h" + + + diff --git a/src/request/headers.h b/src/request/headers.h new file mode 100644 index 0000000..08ab051 --- /dev/null +++ b/src/request/headers.h @@ -0,0 +1,9 @@ +// +// Created by franchioping on 6/25/24. +// + +#ifndef HTTPSERVER_HEADERS_H +#define HTTPSERVER_HEADERS_H + + +#endif //HTTPSERVER_HEADERS_H diff --git a/src/test/linked_list.c b/src/test/linked_list.c new file mode 100644 index 0000000..ef16eed --- /dev/null +++ b/src/test/linked_list.c @@ -0,0 +1,75 @@ +// +// Created by franchioping on 6/25/24. +// + +#include "linked_list.h" +#include +#include +#include "src/util/linkedlist/linkedlist.h" +#include + +#define TEST_LIST_LENGTH 9 + +int* generate_data(const int length){ + int* ints = (int*) malloc(sizeof(int) * length); + for(int i = 0; i < length; i++){ + ints[i] = i; + } + return ints; +} + +listnode* create_linked_list(const int list_length, const int* data){ + + listnode *start = (listnode*) malloc(sizeof(listnode)); + listnode* node = start; + for(int i = 0; i < list_length; i++){ + node->data = (void*) &data[i]; + node->next_node = (listnode*) malloc(sizeof(listnode)); + node = node->next_node; + } + return start; +} + +START_TEST (test_linked_list_get_index) +{ + int* ints= generate_data(TEST_LIST_LENGTH); + + listnode* list = create_linked_list(TEST_LIST_LENGTH, ints); + + for(int i = 0; i < TEST_LIST_LENGTH -1; i++){ + listnode* node = list_get_at_index(list, i); + ck_assert_int_eq(*((int*)(node->data)), i); + } + + free(ints); + list_free(list); +} +END_TEST + +START_TEST (test_linked_list_length) +{ + int* ints= generate_data(TEST_LIST_LENGTH); + + listnode* list = create_linked_list(TEST_LIST_LENGTH, ints); + + ck_assert_uint_eq(list_get_length(list), TEST_LIST_LENGTH); + free(ints); + list_free(list); +} +END_TEST + + +Suite *linked_list_suite(void) { + Suite *s; + TCase *tc_core; + + s = suite_create("Test Linked List Suite"); + tc_core = tcase_create("Core"); + + tcase_add_test(tc_core, test_linked_list_length); + tcase_add_test(tc_core, test_linked_list_get_index); + + suite_add_tcase(s, tc_core); + + return s; +} \ No newline at end of file diff --git a/src/test/linked_list.h b/src/test/linked_list.h new file mode 100644 index 0000000..58399e1 --- /dev/null +++ b/src/test/linked_list.h @@ -0,0 +1,12 @@ +// +// Created by franchioping on 6/25/24. +// + +#ifndef HTTPSERVER_LINKED_LIST_H +#define HTTPSERVER_LINKED_LIST_H + +#include + +Suite *linked_list_suite(void); + +#endif //HTTPSERVER_LINKED_LIST_H diff --git a/src/test/test_main.c b/src/test/test_main.c new file mode 100644 index 0000000..670c7c5 --- /dev/null +++ b/src/test/test_main.c @@ -0,0 +1,26 @@ +// +// Created by franchioping on 6/25/24. +// + +#include +#include +#include +#include "linked_list.h" + + +int run_suite(Suite* suite){ + SRunner *runner = srunner_create(suite); + + srunner_run_all(runner, CK_NORMAL); + int no_failed = srunner_ntests_failed(runner); + srunner_free(runner); + return no_failed; +} + +int main(void) { + int no_failed = 0; + + no_failed += run_suite(linked_list_suite()); + + return (no_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} \ No newline at end of file diff --git a/src/util/linkedlist/linkedlist.c b/src/util/linkedlist/linkedlist.c new file mode 100644 index 0000000..d29e5b7 --- /dev/null +++ b/src/util/linkedlist/linkedlist.c @@ -0,0 +1,38 @@ +// +// Created by franchioping on 6/25/24. +// + +#include +#include "linkedlist.h" + + +listnode* list_get_at_index(listnode* list_start, ssize_t index){ + listnode* node = list_start; + for(int i = 0; i < index; i++){ + node = node->next_node; + } + return node; +} + +ssize_t list_get_length(listnode* list_start){ + ssize_t length = 1; + while(list_start->next_node){ + length++; + list_start = list_start->next_node; + } + return length; +} + +void list_free(listnode* list_start){ + if(list_start == NULL) + return; + listnode* head = list_start; + listnode * tmp; + + while (head != NULL) + { + tmp = head; + head = head->next_node; + free(tmp); + } +} diff --git a/src/util/linkedlist/linkedlist.h b/src/util/linkedlist/linkedlist.h new file mode 100644 index 0000000..8621866 --- /dev/null +++ b/src/util/linkedlist/linkedlist.h @@ -0,0 +1,22 @@ +// +// Created by franchioping on 6/25/24. +// + +#ifndef HTTPSERVER_LINKEDLIST_H +#define HTTPSERVER_LINKEDLIST_H + +#include + +typedef struct listnode { + struct listnode* next_node; + void* data; +} listnode; + +listnode* list_get_at_index(listnode* list_start, ssize_t index); +ssize_t list_get_length(listnode* list_start); +void list_free(listnode* list_start); + + + + +#endif //HTTPSERVER_LINKEDLIST_H diff --git a/src/util.c b/src/util/util.c similarity index 100% rename from src/util.c rename to src/util/util.c diff --git a/src/util.h b/src/util/util.h similarity index 100% rename from src/util.h rename to src/util/util.h