diff --git a/include/core/epoll_utils.h b/include/core/epoll.h similarity index 71% rename from include/core/epoll_utils.h rename to include/core/epoll.h index 4a6360c..27d2669 100644 --- a/include/core/epoll_utils.h +++ b/include/core/epoll.h @@ -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); diff --git a/include/core/server.h b/include/core/server.h index 7dbc0ef..f9e669d 100644 --- a/include/core/server.h +++ b/include/core/server.h @@ -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 diff --git a/include/core/socket_utils.h b/include/core/socket.h similarity index 100% rename from include/core/socket_utils.h rename to include/core/socket.h diff --git a/include/core/worker.h b/include/core/worker.h index ea78ac2..6175bda 100644 --- a/include/core/worker.h +++ b/include/core/worker.h @@ -6,7 +6,6 @@ #include #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 diff --git a/include/http/request.h b/include/http/request.h index 1f88929..066bdad 100644 --- a/include/http/request.h +++ b/include/http/request.h @@ -1,5 +1,5 @@ -#ifndef CWS_HTTP_H -#define CWS_HTTP_H +#ifndef CWS_REQUEST_H +#define CWS_REQUEST_H #include #include @@ -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 diff --git a/include/utils/debug.h b/include/utils/debug.h index 3c3367b..2fd8ddf 100644 --- a/include/utils/debug.h +++ b/include/utils/debug.h @@ -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" diff --git a/include/utils/net_utils.h b/include/utils/error.h similarity index 76% rename from include/utils/net_utils.h rename to include/utils/error.h index 72cebde..beb9759 100644 --- a/include/utils/net_utils.h +++ b/include/utils/error.h @@ -1,8 +1,5 @@ -#ifndef CWS_UTILS_H -#define CWS_UTILS_H - -#include -#include +#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 diff --git a/include/utils/hash_utils.h b/include/utils/hash.h similarity index 84% rename from include/utils/hash_utils.h rename to include/utils/hash.h index a5f7fcf..c14a916 100644 --- a/include/utils/hash_utils.h +++ b/include/utils/hash.h @@ -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); diff --git a/include/utils/net.h b/include/utils/net.h new file mode 100644 index 0000000..c7bfb08 --- /dev/null +++ b/include/utils/net.h @@ -0,0 +1,13 @@ +#ifndef CWS_NET_H +#define CWS_NET_H + +#include +#include + +#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 diff --git a/src/core/epoll_utils.c b/src/core/epoll_utils.c index b23ab60..0e6723d 100644 --- a/src/core/epoll_utils.c +++ b/src/core/epoll_utils.c @@ -1,4 +1,4 @@ -#include "core/epoll_utils.h" +#include "core/epoll.h" #include #include diff --git a/src/core/server.c b/src/core/server.c index 1e87420..e7246fc 100644 --- a/src/core/server.c +++ b/src/core/server.c @@ -5,10 +5,10 @@ #include #include -#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); diff --git a/src/core/socket_utils.c b/src/core/socket_utils.c index 5bcb993..30c2e15 100644 --- a/src/core/socket_utils.c +++ b/src/core/socket_utils.c @@ -1,4 +1,4 @@ -#include "core/socket_utils.h" +#include "core/socket.h" #include #include diff --git a/src/core/worker.c b/src/core/worker.c index 677acd8..61d829a 100644 --- a/src/core/worker.c +++ b/src/core/worker.c @@ -5,10 +5,11 @@ #include #include -#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; -} diff --git a/src/http/request.c b/src/http/request.c index a1f5b20..ef9fb2d 100644 --- a/src/http/request.c +++ b/src/http/request.c @@ -5,7 +5,7 @@ #include #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)); diff --git a/src/http/response.c b/src/http/response.c index d466e61..bae2df8 100644 --- a/src/http/response.c +++ b/src/http/response.c @@ -2,7 +2,7 @@ #include "http/mime.h" #include "utils/debug.h" -#include +#include #include #include #include diff --git a/src/utils/hash_utils.c b/src/utils/hash_utils.c index 400f0c8..8affa5e 100644 --- a/src/utils/hash_utils.c +++ b/src/utils/hash_utils.c @@ -1,4 +1,4 @@ -#include "utils/hash_utils.h" +#include "utils/hash.h" #include #include diff --git a/src/utils/net_utils.c b/src/utils/net_utils.c index c12a661..4efd6de 100644 --- a/src/utils/net_utils.c +++ b/src/utils/net_utils.c @@ -1,4 +1,4 @@ -#include "utils/net_utils.h" +#include "utils/net.h" #include #include