From 076c2c886952bd18cd33e3a27e150d225ea97935 Mon Sep 17 00:00:00 2001 From: franchioping <43936644+franchioping@users.noreply.github.com> Date: Mon, 24 Jun 2024 20:04:27 +0100 Subject: [PATCH] Initial Commit --- .idea/.gitignore | 8 ++++++ .idea/misc.xml | 7 +++++ .idea/modules.xml | 8 ++++++ .idea/untitled1.iml | 2 ++ .idea/vcs.xml | 6 ++++ CMakeLists.txt | 10 +++++++ handle_client.c | 70 +++++++++++++++++++++++++++++++++++++++++++++ handle_client.h | 11 +++++++ main.c | 64 +++++++++++++++++++++++++++++++++++++++++ util.c | 17 +++++++++++ util.h | 12 ++++++++ 11 files changed, 215 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/untitled1.iml create mode 100644 .idea/vcs.xml create mode 100644 CMakeLists.txt create mode 100644 handle_client.c create mode 100644 handle_client.h create mode 100644 main.c create mode 100644 util.c create mode 100644 util.h diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0b76fe5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..4922bfe --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/untitled1.iml b/.idea/untitled1.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/untitled1.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..1f699d2 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.28) +project(HttpServer C) + +set(CMAKE_C_STANDARD 23) + +add_executable(HttpServer main.c + handle_client.c + handle_client.h + util.c + util.h) diff --git a/handle_client.c b/handle_client.c new file mode 100644 index 0000000..3e1ba8d --- /dev/null +++ b/handle_client.c @@ -0,0 +1,70 @@ +// +// 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/handle_client.h b/handle_client.h new file mode 100644 index 0000000..13c7c88 --- /dev/null +++ b/handle_client.h @@ -0,0 +1,11 @@ +// +// Created by franchioping on 6/24/24. +// + +#ifndef UNTITLED1_HANDLE_CLIENT_H +#define UNTITLED1_HANDLE_CLIENT_H + +void handle_client(void* arg); + + +#endif //UNTITLED1_HANDLE_CLIENT_H diff --git a/main.c b/main.c new file mode 100644 index 0000000..cefdb54 --- /dev/null +++ b/main.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include + + +#include "handle_client.h" + + +int main(void) { + int serv_sock_fd = socket(AF_INET, SOCK_STREAM, 0); + + if(serv_sock_fd < 0){ + printf("Error while creating socket\n"); + return -1; + } + + const int enable = 1; + if (setsockopt(serv_sock_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) + printf("setsockopt(SO_REUSEADDR) failed"); + printf("Created Socket File Descriptor Successfully.\n"); + + + + struct sockaddr_in server_addr; + + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(8080); + server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); + + if(bind(serv_sock_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0){ + printf("Couldn't bind to port\n"); + return -1; + } + printf("Socket Bound to Port Successfully.\n"); + + + + if(listen(serv_sock_fd, 10) < 0){ + printf("Couldn't listen to socket\n"); + return -1; + } + printf("Socket Listening...\n"); + + while(1){ + struct sockaddr_in client_addr; + socklen_t client_addr_len = sizeof(client_addr); + int *client_fd = malloc(sizeof(int)); + + *client_fd = accept(serv_sock_fd, (struct sockaddr *)&client_addr, &client_addr_len); + if(*client_fd < 0){ + perror("Accept failed\n"); + continue; + } + + printf("================================\n"); + printf("Client Connected, Handling...\n"); + handle_client((void*) client_fd); + + } + return 0; +} + + diff --git a/util.c b/util.c new file mode 100644 index 0000000..5256cea --- /dev/null +++ b/util.c @@ -0,0 +1,17 @@ +// +// Created by franchioping on 6/24/24. +// + +#include "util.h" + +#include + + +void print_string(char* string, size_t string_len){ + for(int i = 0; i < string_len; i++){ + if(string[i] == '\r'){ + continue; + } + printf("%c", string[i]); + } +} \ No newline at end of file diff --git a/util.h b/util.h new file mode 100644 index 0000000..b25339b --- /dev/null +++ b/util.h @@ -0,0 +1,12 @@ +// +// Created by franchioping on 6/24/24. +// + +#ifndef UNTITLED1_UTIL_H +#define UNTITLED1_UTIL_H +#include + +void print_string(char* sting, size_t string_len); + + +#endif //UNTITLED1_UTIL_H