refactor: simplify file name

This commit is contained in:
2025-10-26 18:03:21 +01:00
parent 4fced17513
commit 1863c58b4f
17 changed files with 75 additions and 78 deletions

View File

@@ -1,5 +1,5 @@
#ifndef CWS_EPOLL_UTILS_H
#define CWS_EPOLL_UTILS_H
#ifndef CWS_EPOLL_H
#define CWS_EPOLL_H
int cws_epoll_add(int epfd, int sockfd);

View File

@@ -7,7 +7,7 @@
#include "config/config.h"
#include "core/worker.h"
#include "utils/net_utils.h"
#include "utils/error.h"
/* Clients max queue */
#define CWS_SERVER_BACKLOG 128

View File

@@ -6,7 +6,6 @@
#include <signal.h>
#include "config/config.h"
#include "utils/net_utils.h"
extern volatile sig_atomic_t cws_server_run;
@@ -23,8 +22,4 @@ void cws_worker_free(cws_worker_s **workers, size_t workers_num);
void *cws_worker_loop(void *arg);
void cws_server_close_client(int epfd, int client_fd);
cws_server_ret cws_server_handle_client_data(int epfd, int client_fd);
#endif

View File

@@ -1,5 +1,5 @@
#ifndef CWS_HTTP_H
#define CWS_HTTP_H
#ifndef CWS_REQUEST_H
#define CWS_REQUEST_H
#include <myclib/myhashmap.h>
#include <myclib/mystring.h>
@@ -35,11 +35,6 @@ typedef struct cws_http {
cws_http_s *cws_http_parse(string_s *request_str);
void cws_http_send_response(cws_http_s *request, cws_http_status_e status);
size_t http_response_builder(char **response, cws_http_status_e status, char *content_type,
char *body, size_t body_len_bytes);
void cws_http_free(cws_http_s *request);
#endif

View File

@@ -1,5 +1,5 @@
#ifndef CWS_COLORS_H
#define CWS_COLORS_H
#ifndef CWS_DEBUG_H
#define CWS_DEBUG_H
/* ANSI color escape sequences */
#define RED "\033[31m"

View File

@@ -1,8 +1,5 @@
#ifndef CWS_UTILS_H
#define CWS_UTILS_H
#include <stdbool.h>
#include <sys/socket.h>
#ifndef CWS_ERROR_H
#define CWS_ERROR_H
typedef enum cws_server_ret {
CWS_SERVER_OK,
@@ -29,8 +26,4 @@ typedef enum cws_server_ret {
CWS_SERVER_WORKER_ERROR,
} cws_server_ret;
cws_server_ret cws_fd_set_nonblocking(int sockfd);
void cws_utils_get_client_ip(struct sockaddr_storage *sa, char *ip);
#endif

View File

@@ -1,5 +1,5 @@
#ifndef CWS_HASH_UTILS_H
#define CWS_HASH_UTILS_H
#ifndef CWS_HASH_H
#define CWS_HASH_H
unsigned int my_str_hash_fn(const void *key);

13
include/utils/net.h Normal file
View File

@@ -0,0 +1,13 @@
#ifndef CWS_NET_H
#define CWS_NET_H
#include <stdbool.h>
#include <sys/socket.h>
#include "utils/error.h"
cws_server_ret cws_fd_set_nonblocking(int sockfd);
void cws_utils_get_client_ip(struct sockaddr_storage *sa, char *ip);
#endif

View File

@@ -1,4 +1,4 @@
#include "core/epoll_utils.h"
#include "core/epoll.h"
#include <stdio.h>
#include <sys/epoll.h>

View File

@@ -5,10 +5,10 @@
#include <string.h>
#include <sys/epoll.h>
#include "core/epoll_utils.h"
#include "core/epoll.h"
#include "core/worker.h"
#include "utils/debug.h"
#include "utils/net_utils.h"
#include "utils/net.h"
static void cws_server_setup_hints(struct addrinfo *hints, const char *hostname) {
memset(hints, 0, sizeof *hints);

View File

@@ -1,4 +1,4 @@
#include "core/socket_utils.h"
#include "core/socket.h"
#include <errno.h>
#include <sys/socket.h>

View File

@@ -5,10 +5,11 @@
#include <sys/epoll.h>
#include <unistd.h>
#include "core/epoll_utils.h"
#include "core/socket_utils.h"
#include "core/epoll.h"
#include "core/socket.h"
#include "http/request.h"
#include "utils/net_utils.h"
#include "http/response.h"
#include "utils/error.h"
static cws_server_ret cws_worker_setup_epoll(cws_worker_s *worker) {
worker->epfd = epoll_create1(0);
@@ -19,6 +20,45 @@ static cws_server_ret cws_worker_setup_epoll(cws_worker_s *worker) {
return CWS_SERVER_OK;
}
static void cws_server_close_client(int epfd, int client_fd) {
cws_epoll_del(epfd, client_fd);
close(client_fd);
}
static int cws_server_handle_client_data(int epfd, int client_fd) {
string_s *data = string_new("", 4096);
ssize_t total_bytes = cws_read_data(client_fd, data);
if (total_bytes == 0) {
/* Request not completed yet */
string_free(data);
return CWS_SERVER_OK;
}
if (total_bytes <= 0) {
/* Something happened, close connection */
string_free(data);
cws_server_close_client(epfd, client_fd);
return CWS_SERVER_CLIENT_DISCONNECTED_ERROR;
}
cws_http_s *request = cws_http_parse(data);
string_free(data);
if (request == NULL) {
cws_server_close_client(epfd, client_fd);
return CWS_SERVER_HTTP_PARSE_ERROR;
}
request->sockfd = client_fd;
cws_http_send_response(request, HTTP_OK);
cws_http_free(request);
cws_server_close_client(epfd, client_fd);
return CWS_SERVER_OK;
}
cws_worker_s **cws_worker_new(size_t workers_num, cws_config_s *config) {
cws_worker_s **workers = malloc(workers_num * sizeof *workers);
if (workers == NULL) {
@@ -98,42 +138,3 @@ void *cws_worker_loop(void *arg) {
return NULL;
}
void cws_server_close_client(int epfd, int client_fd) {
cws_epoll_del(epfd, client_fd);
close(client_fd);
}
cws_server_ret cws_server_handle_client_data(int epfd, int client_fd) {
string_s *data = string_new("", 4096);
ssize_t total_bytes = cws_read_data(client_fd, data);
if (total_bytes == 0) {
/* Request not completed yet */
string_free(data);
return CWS_SERVER_OK;
}
if (total_bytes <= 0) {
/* Something happened, close connection */
string_free(data);
cws_server_close_client(epfd, client_fd);
return CWS_SERVER_CLIENT_DISCONNECTED_ERROR;
}
cws_http_s *request = cws_http_parse(data);
string_free(data);
if (request == NULL) {
cws_server_close_client(epfd, client_fd);
return CWS_SERVER_HTTP_PARSE_ERROR;
}
request->sockfd = client_fd;
cws_http_send_response(request, HTTP_OK);
cws_http_free(request);
cws_server_close_client(epfd, client_fd);
return CWS_SERVER_OK;
}

View File

@@ -5,7 +5,7 @@
#include <string.h>
#include "utils/debug.h"
#include "utils/hash_utils.h"
#include "utils/hash.h"
static cws_http_s *http_new() {
cws_http_s *request = malloc(sizeof(cws_http_s));

View File

@@ -2,7 +2,7 @@
#include "http/mime.h"
#include "utils/debug.h"
#include <core/socket_utils.h>
#include <core/socket.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

View File

@@ -1,4 +1,4 @@
#include "utils/hash_utils.h"
#include "utils/hash.h"
#include <stdlib.h>
#include <string.h>

View File

@@ -1,4 +1,4 @@
#include "utils/net_utils.h"
#include "utils/net.h"
#include <arpa/inet.h>
#include <fcntl.h>