Linked lists are REALLY broken but im tired - fix later
This commit is contained in:
parent
5bd01fe0b7
commit
2880a91032
|
|
@ -3,8 +3,26 @@ project(HttpServer C)
|
||||||
|
|
||||||
set(CMAKE_C_STANDARD 23)
|
set(CMAKE_C_STANDARD 23)
|
||||||
|
|
||||||
add_executable(HttpServer src/main.c
|
include_directories(".")
|
||||||
src/handle_client.c
|
|
||||||
src/handle_client.h
|
|
||||||
src/util.c
|
set(COMMON_SOURCES src/request/handle_client.c
|
||||||
src/util.h)
|
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)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
|
|
||||||
|
|
||||||
#include "handle_client.h"
|
#include "request/handle_client.h"
|
||||||
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
|
|
||||||
|
|
@ -10,13 +10,25 @@
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
|
||||||
#include "util.h"
|
#include "../util/util.h"
|
||||||
|
|
||||||
#define BUFF_SIZE 128
|
#define BUFF_SIZE 128
|
||||||
#define MAX_BUFF_SIZE 8192
|
#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";
|
char* end = "\r\n\r\n";
|
||||||
for(int i = 0; i < 4; i++){
|
for(int i = 0; i < 4; i++){
|
||||||
if(end[i] != data[data_length - 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;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
char** request_get_lines_from_buff(const char* buff, size_t buff_len, ssize_t * lines_count){
|
||||||
char** request_get_lines_from_buff(const char* buff, size_t buff_len, int* lines_count){
|
ssize_t line_count = 0;
|
||||||
int line_count = 0;
|
|
||||||
for(int i = 0; i < buff_len-1; i++){
|
for(int i = 0; i < buff_len-1; i++){
|
||||||
if(buff[i] == '\r' && buff[i+1]=='\n')
|
if(buff[i] == '\r' && buff[i+1]=='\n')
|
||||||
line_count ++;
|
line_count ++;
|
||||||
}
|
}
|
||||||
char** lines = (char**) malloc(sizeof(char*) * line_count);
|
char** lines = (char**) malloc(sizeof(char*) * line_count);
|
||||||
|
if(lines == NULL){
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
int line_start = 0;
|
int line_start = 0;
|
||||||
int line_num = 0;
|
int line_num = 0;
|
||||||
for(int i = 0; i < buff_len; i++){
|
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;
|
*lines_count = line_count;
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t receive_data(int client_fd, char** data){
|
ssize_t receive_data(int client_fd, char** data){
|
||||||
char* buff = (char*) malloc(BUFF_SIZE);
|
char* buff = (char*) malloc(BUFF_SIZE);
|
||||||
|
if(buff == NULL){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
int buff_size = BUFF_SIZE;
|
int buff_size = BUFF_SIZE;
|
||||||
while(true){
|
while(true){
|
||||||
ssize_t msg_length = recv(client_fd, &buff[buff_size-BUFF_SIZE], BUFF_SIZE, 0);
|
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
|
// Set time-out on recv
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
tv.tv_sec = 10;
|
tv.tv_sec = 1;
|
||||||
tv.tv_usec = 0;
|
tv.tv_usec = 0;
|
||||||
if(setsockopt(client_fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv) < 0)
|
if(setsockopt(client_fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv) < 0)
|
||||||
printf("setsockopt(SO_RCVTIMEO) Failed.");
|
printf("setsockopt(SO_RCVTIMEO) Failed.");
|
||||||
|
|
@ -104,15 +120,16 @@ void handle_client(void* arg){
|
||||||
printf("Request Lenght: %lu\n", data_length);
|
printf("Request Lenght: %lu\n", data_length);
|
||||||
printf("Request:\n");
|
printf("Request:\n");
|
||||||
|
|
||||||
int lines_count = 0;
|
ssize_t lines_count = 0;
|
||||||
char** lines = request_get_lines_from_buff(data, data_length, &lines_count);
|
char** lines = request_get_lines_from_buff(data, data_length, &lines_count);
|
||||||
|
if(lines == NULL){
|
||||||
for(int i = 0; i < lines_count; i++){
|
printf("Failed to get lines from buffer. Aborting...");
|
||||||
char* line = lines[i];
|
return;
|
||||||
printf(" %s\n", line);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
print_request(lines, lines_count, " ");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
//
|
||||||
|
// Created by franchioping on 6/25/24.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "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
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
Loading…
Reference in New Issue