From 5bd01fe0b7fe0bc93f18eb5d53f55477ae542638 Mon Sep 17 00:00:00 2001 From: franchioping <43936644+franchioping@users.noreply.github.com> Date: Tue, 25 Jun 2024 16:27:41 +0100 Subject: [PATCH] refactoring --- CMakeLists.txt | 10 +- handle_client.c | 70 -------------- src/handle_client.c | 126 +++++++++++++++++++++++++ handle_client.h => src/handle_client.h | 0 main.c => src/main.c | 7 +- util.c => src/util.c | 0 util.h => src/util.h | 0 7 files changed, 135 insertions(+), 78 deletions(-) delete mode 100644 handle_client.c create mode 100644 src/handle_client.c rename handle_client.h => src/handle_client.h (100%) rename main.c => src/main.c (96%) rename util.c => src/util.c (100%) rename util.h => src/util.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f699d2..a21b2e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,8 +3,8 @@ project(HttpServer C) set(CMAKE_C_STANDARD 23) -add_executable(HttpServer main.c - handle_client.c - handle_client.h - util.c - util.h) +add_executable(HttpServer src/main.c + src/handle_client.c + src/handle_client.h + src/util.c + src/util.h) diff --git a/handle_client.c b/handle_client.c deleted file mode 100644 index 3e1ba8d..0000000 --- a/handle_client.c +++ /dev/null @@ -1,70 +0,0 @@ -// -// Created by franchioping on 6/24/24. -// - -#include "handle_client.h" - -#include -#include -#include -#include - - -#include "util.h" - - -#define BUFFER_SIZE 256 - -char** request_get_lines_from_buff(const char* buff, size_t buff_len, int* lines_count){ - int 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); - int line_start = 0; - int line_num = 0; - for(int i = 0; i < buff_len; i++){ - if(buff[i] == '\r' && buff[i+1]=='\n'){ - char* line = malloc(sizeof(char) * (i - line_start + 1)); - memcpy(line, &buff[line_start], i - line_start); - line[i - line_start] = '\0'; - - lines[line_num++] = line; - - line_start=i+2; - } - } - *lines_count = line_count; - return lines; -} - -void handle_client(void* arg){ - int client_fd = *((int*) arg); - - char* buff = (char*) malloc(BUFFER_SIZE * sizeof(char)); - - ssize_t bytes_received = recv(client_fd, buff, BUFFER_SIZE, 0); - if(bytes_received < 0){ - printf("Failed to receive.\n"); - return; - } - - // Print Request - printf("Bytes Received: %lu\n", bytes_received); - printf("Request:\n"); - - //print_string(buff, bytes_received); - - int lines_count = 0; - char** lines = request_get_lines_from_buff(buff, bytes_received, &lines_count); - - for(int i = 0; i < lines_count; i++){ - char* line = lines[i]; - printf("%s\n", line); - free(line); - } - printf("\n"); - - free(lines); -} \ No newline at end of file diff --git a/src/handle_client.c b/src/handle_client.c new file mode 100644 index 0000000..f2911e9 --- /dev/null +++ b/src/handle_client.c @@ -0,0 +1,126 @@ +// +// Created by franchioping on 6/24/24. +// + +#include "handle_client.h" + +#include +#include +#include +#include + + +#include "util.h" + +#define BUFF_SIZE 128 +#define MAX_BUFF_SIZE 8192 + + +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]) + return false; + } + return true; + +} + +char** request_get_lines_from_buff(const char* buff, size_t buff_len, int* lines_count){ + int 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); + int line_start = 0; + int line_num = 0; + for(int i = 0; i < buff_len; i++){ + if(buff[i] == '\r' && buff[i+1]=='\n'){ + char* line = malloc(sizeof(char) * (i - line_start + 1)); + memcpy(line, &buff[line_start], i - line_start); + line[i - line_start] = '\0'; + + lines[line_num++] = line; + + line_start=i+2; + } + } + *lines_count = line_count; + return lines; +} + +ssize_t receive_data(int client_fd, char** data){ + char* buff = (char*) malloc(BUFF_SIZE); + int buff_size = BUFF_SIZE; + while(true){ + ssize_t msg_length = recv(client_fd, &buff[buff_size-BUFF_SIZE], BUFF_SIZE, 0); + if(msg_length == BUFF_SIZE){ + buff_size += BUFF_SIZE; + if(buff_size > MAX_BUFF_SIZE){ + *data = nullptr; + return -1; + } + char* temp = (char*) realloc(buff, buff_size); + if(temp == NULL){ + *data = nullptr; + return -1; + } + buff = temp; + } + if((msg_length < 0) || msg_length < BUFF_SIZE){ + ssize_t new_buff_size = buff_size - (BUFF_SIZE - msg_length); + if(check_if_request_end(buff, new_buff_size)){ + char* temp = (char*) realloc(buff, buff_size); + if(temp == NULL){ + *data = buff; + return buff_size; + } + buff = temp; + *data = buff; + return new_buff_size; + } + return -1; + } + } +} + + +void handle_client(void* arg){ + int client_fd = *((int*) arg); + + // Set time-out on recv + struct timeval tv; + tv.tv_sec = 10; + tv.tv_usec = 0; + if(setsockopt(client_fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv) < 0) + printf("setsockopt(SO_RCVTIMEO) Failed."); + + + + + char* data; + ssize_t data_length = receive_data(client_fd, &data); + printf("Request Lenght: %lu\n", data_length); + printf("Request:\n"); + + int 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); + + } + + + + + // Clean-Up + + for(int i = 0; i < lines_count; i++){ + free(lines[i]); + } + free(lines); + free(data); +} \ No newline at end of file diff --git a/handle_client.h b/src/handle_client.h similarity index 100% rename from handle_client.h rename to src/handle_client.h diff --git a/main.c b/src/main.c similarity index 96% rename from main.c rename to src/main.c index cefdb54..cacf544 100644 --- a/main.c +++ b/src/main.c @@ -17,11 +17,12 @@ int main(void) { const int enable = 1; if (setsockopt(serv_sock_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) - printf("setsockopt(SO_REUSEADDR) failed"); + printf("setsockopt(SO_REUSEADDR) Failed"); + + + printf("Created Socket File Descriptor Successfully.\n"); - - struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; diff --git a/util.c b/src/util.c similarity index 100% rename from util.c rename to src/util.c diff --git a/util.h b/src/util.h similarity index 100% rename from util.h rename to src/util.h