Linked lists are REALLY broken but im tired - fix later

This commit is contained in:
franchioping 2024-06-26 00:57:05 +01:00
parent 5bd01fe0b7
commit 2880a91032
13 changed files with 244 additions and 19 deletions

View File

@ -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)

View File

@ -4,7 +4,7 @@
#include <malloc.h>
#include "handle_client.h"
#include "request/handle_client.h"
int main(void) {

View File

@ -10,13 +10,25 @@
#include <sys/socket.h>
#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, " ");

8
src/request/headers.c Normal file
View File

@ -0,0 +1,8 @@
//
// Created by franchioping on 6/25/24.
//
#include "headers.h"

9
src/request/headers.h Normal file
View File

@ -0,0 +1,9 @@
//
// Created by franchioping on 6/25/24.
//
#ifndef HTTPSERVER_HEADERS_H
#define HTTPSERVER_HEADERS_H
#endif //HTTPSERVER_HEADERS_H

75
src/test/linked_list.c Normal file
View File

@ -0,0 +1,75 @@
//
// Created by franchioping on 6/25/24.
//
#include "linked_list.h"
#include <check.h>
#include <malloc.h>
#include "src/util/linkedlist/linkedlist.h"
#include <mcheck.h>
#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;
}

12
src/test/linked_list.h Normal file
View File

@ -0,0 +1,12 @@
//
// Created by franchioping on 6/25/24.
//
#ifndef HTTPSERVER_LINKED_LIST_H
#define HTTPSERVER_LINKED_LIST_H
#include <check.h>
Suite *linked_list_suite(void);
#endif //HTTPSERVER_LINKED_LIST_H

26
src/test/test_main.c Normal file
View File

@ -0,0 +1,26 @@
//
// Created by franchioping on 6/25/24.
//
#include <check.h>
#include <stdlib.h>
#include <stdio.h>
#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;
}

View File

@ -0,0 +1,38 @@
//
// Created by franchioping on 6/25/24.
//
#include <malloc.h>
#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);
}
}

View File

@ -0,0 +1,22 @@
//
// Created by franchioping on 6/25/24.
//
#ifndef HTTPSERVER_LINKEDLIST_H
#define HTTPSERVER_LINKEDLIST_H
#include <sys/types.h>
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