diff --git a/include/http/http.h b/include/http/http.h index ee80251..0d89ccc 100644 --- a/include/http/http.h +++ b/include/http/http.h @@ -10,32 +10,32 @@ #define CWS_HTTP_HEADER_MAX 512 #define CWS_HTTP_HEADER_CONTENT_MAX 1024 -typedef enum cws_http_method_t { +typedef enum cws_http_method { CWS_HTTP_GET, CWS_HTTP_POST, CWS_HTTP_PUT, CWS_HTTP_DELETE, CWS_HTTP_HEAD, -} cws_http_method; +} cws_http_method_e; -typedef enum cws_http_status_t { +typedef enum cws_http_status { CWS_HTTP_OK, CWS_HTTP_NOT_FOUND, CWS_HTTP_NOT_IMPLEMENTED, -} cws_http_status; +} cws_http_status_e; /** * @brief HTTP request struct * */ -typedef struct cws_http_t { - int sockfd; /**< Socket file descriptor */ - cws_http_method method; /**< HTTP request method */ - string_s *location; /**< Resource requested */ - string_s *location_path; /**< Full resource path */ - string_s *http_version; /**< HTTP version */ - hashmap_s *headers; /**< Headers hash map */ -} cws_http; +typedef struct cws_http { + int sockfd; /**< Socket file descriptor */ + cws_http_method_e method; /**< HTTP request method */ + string_s *location; /**< Resource requested */ + string_s *location_path; /**< Full resource path */ + string_s *http_version; /**< HTTP version */ + hashmap_s *headers; /**< Headers hash map */ +} cws_http_s; /** * @brief Parses a HTTP request @@ -43,22 +43,22 @@ typedef struct cws_http_t { * @param[in] request_str The http request sent to the server * @return Returns a http_t pointer to the request */ -cws_http *cws_http_parse(string_s *request_str, int sockfd, cws_config *config); +cws_http_s *cws_http_parse(string_s *request_str, int sockfd, cws_config_s *config); -int cws_http_get_content_type(cws_http *request, char *content_type); +int cws_http_get_content_type(cws_http_s *request, char *content_type); /** * @brief Build the http response * * @return Returns the size of the response */ -size_t cws_http_response_builder(char **response, char *http_version, cws_http_status status, char *content_type, char *connection, char *body, +size_t cws_http_response_builder(char **response, char *http_version, cws_http_status_e status, char *content_type, char *connection, char *body, size_t body_len_bytes); -void cws_http_send_response(cws_http *request, cws_http_status status); -int cws_http_send_resource(cws_http *request); -void cws_http_send_simple_html(cws_http *request, cws_http_status status, char *title, char *description); +void cws_http_send_response(cws_http_s *request, cws_http_status_e status); +int cws_http_send_resource(cws_http_s *request); +void cws_http_send_simple_html(cws_http_s *request, cws_http_status_e status, char *title, char *description); -void cws_http_free(cws_http *request); +void cws_http_free(cws_http_s *request); #endif diff --git a/include/server/server.h b/include/server/server.h index 4c62b7c..33feda1 100644 --- a/include/server/server.h +++ b/include/server/server.h @@ -20,7 +20,7 @@ /* Main server loop */ extern volatile sig_atomic_t cws_server_run; -typedef enum cws_server_ret_t { +typedef enum cws_server_ret { CWS_SERVER_OK, CWS_SERVER_CONFIG, CWS_SERVER_FD_ERROR, @@ -45,8 +45,8 @@ typedef enum cws_server_ret_t { CWS_SERVER_WORKER_ERROR, } cws_server_ret; -cws_server_ret cws_server_start(cws_config *config); -cws_server_ret cws_server_loop(int server_fd, cws_config *config); +cws_server_ret cws_server_start(cws_config_s *config); +cws_server_ret cws_server_loop(int server_fd, cws_config_s *config); int cws_server_handle_new_client(int server_fd, hashmap_s *clients); int cws_server_accept_client(int server_fd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size); cws_server_ret cws_fd_set_nonblocking(int sockfd); diff --git a/include/server/worker.h b/include/server/worker.h index 0254cd9..028172b 100644 --- a/include/server/worker.h +++ b/include/server/worker.h @@ -6,22 +6,22 @@ #include "server/server.h" -typedef struct cws_worker_t { +typedef struct cws_worker { int epfd; int pipefd[2]; size_t clients_num; pthread_t thread; hashmap_s *clients; - cws_config *config; -} cws_worker; + cws_config_s *config; +} cws_worker_s; -cws_worker **cws_worker_init(size_t workers_num, hashmap_s *clients, cws_config *config); -void cws_worker_free(cws_worker **workers, size_t workers_num); +cws_worker_s **cws_worker_init(size_t workers_num, hashmap_s *clients, cws_config_s *config); +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, hashmap_s *clients); cws_server_ret cws_epoll_add(int epfd, int sockfd, uint32_t events); cws_server_ret cws_epoll_del(int epfd, int sockfd); -cws_server_ret cws_server_handle_client_data(int epfd, int client_fd, hashmap_s *clients, cws_config *config); +cws_server_ret cws_server_handle_client_data(int epfd, int client_fd, hashmap_s *clients, cws_config_s *config); #endif diff --git a/include/utils/config.h b/include/utils/config.h index d6d0d7b..0b30ad8 100644 --- a/include/utils/config.h +++ b/include/utils/config.h @@ -3,24 +3,22 @@ #include -struct cws_virtual_host_t { +typedef struct cws_vhost { char *domain; char *root; bool ssl; char *cert; char *key; -}; -typedef struct cws_virtual_host_t cws_virtual_host; +} cws_vhost_s; -struct cws_config_t { +typedef struct cws_config { char *hostname; char *port; - cws_virtual_host *virtual_hosts; + cws_vhost_s *virtual_hosts; unsigned virtual_hosts_count; -}; -typedef struct cws_config_t cws_config; +} cws_config_s; -cws_config *cws_config_init(void); -void cws_config_free(cws_config *config); +cws_config_s *cws_config_init(void); +void cws_config_free(cws_config_s *config); #endif diff --git a/src/http/http.c b/src/http/http.c index 28532cd..5986e89 100644 --- a/src/http/http.c +++ b/src/http/http.c @@ -10,8 +10,8 @@ #include "utils/debug.h" #include "utils/utils.h" -static void cws_http_init(cws_http **request) { - *request = calloc(1, sizeof(cws_http)); +static void cws_http_init(cws_http_s **request) { + *request = calloc(1, sizeof(cws_http_s)); if (!*request) { return; } @@ -21,7 +21,7 @@ static void cws_http_init(cws_http **request) { (*request)->location_path = string_new("", 512); } -static int cws_http_parse_method(cws_http *request, const char *method) { +static int cws_http_parse_method(cws_http_s *request, const char *method) { if (strcmp(method, "GET") == 0) { request->method = CWS_HTTP_GET; return 0; @@ -35,12 +35,12 @@ static int cws_http_parse_method(cws_http *request, const char *method) { return -1; } -cws_http *cws_http_parse(string_s *request_str, int sockfd, cws_config *config) { +cws_http_s *cws_http_parse(string_s *request_str, int sockfd, cws_config_s *config) { if (!request_str || !config) { return NULL; } - cws_http *request; + cws_http_s *request; cws_http_init(&request); if (request == NULL) { return NULL; @@ -150,7 +150,7 @@ cws_http *cws_http_parse(string_s *request_str, int sockfd, cws_config *config) return request; } -static char *cws_http_status_string(cws_http_status status) { +static char *cws_http_status_string(cws_http_status_e status) { switch (status) { case CWS_HTTP_OK: { return "200 OK"; @@ -169,7 +169,7 @@ static char *cws_http_status_string(cws_http_status status) { return "?"; } -size_t cws_http_response_builder(char **response, char *http_version, cws_http_status status, char *content_type, char *connection, char *body, +size_t cws_http_response_builder(char **response, char *http_version, cws_http_status_e status, char *content_type, char *connection, char *body, size_t body_len_bytes) { char *status_code = cws_http_status_string(status); @@ -199,7 +199,7 @@ size_t cws_http_response_builder(char **response, char *http_version, cws_http_s return total_len; } -void cws_http_send_response(cws_http *request, cws_http_status status) { +void cws_http_send_response(cws_http_s *request, cws_http_status_e status) { switch (status) { case CWS_HTTP_OK: break; @@ -214,7 +214,7 @@ void cws_http_send_response(cws_http *request, cws_http_status status) { } } -int cws_http_send_resource(cws_http *request) { +int cws_http_send_resource(cws_http_s *request) { /* keep-alive by default */ int keepalive = 1; @@ -293,7 +293,7 @@ int cws_http_send_resource(cws_http *request) { return keepalive; } -int cws_http_get_content_type(cws_http *request, char *content_type) { +int cws_http_get_content_type(cws_http_s *request, char *content_type) { char *ptr = strrchr(string_cstr(request->location_path), '.'); if (ptr == NULL) { return -1; @@ -316,7 +316,7 @@ int cws_http_get_content_type(cws_http *request, char *content_type) { return 0; } -void cws_http_send_simple_html(cws_http *request, cws_http_status status, char *title, char *description) { +void cws_http_send_simple_html(cws_http_s *request, cws_http_status_e status, char *title, char *description) { char body[512]; memset(body, 0, sizeof(body)); @@ -352,7 +352,7 @@ void cws_http_send_simple_html(cws_http *request, cws_http_status status, char * free(response); } -void cws_http_free(cws_http *request) { +void cws_http_free(cws_http_s *request) { hm_free(request->headers); string_free(request->http_version); string_free(request->location); diff --git a/src/main.c b/src/main.c index 3129938..d94bf2b 100644 --- a/src/main.c +++ b/src/main.c @@ -17,7 +17,7 @@ int main(void) { return EXIT_FAILURE; } - cws_config *config = cws_config_init(); + cws_config_s *config = cws_config_init(); if (config == NULL) { CWS_LOG_ERROR("Unable to read config file"); return EXIT_FAILURE; diff --git a/src/server/server.c b/src/server/server.c index a914dd6..a7e4c1b 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -28,7 +28,7 @@ static void cws_server_setup_hints(struct addrinfo *hints, const char *hostname) } } -cws_server_ret cws_server_start(cws_config *config) { +cws_server_ret cws_server_start(cws_config_s *config) { if (!config || !config->hostname || !config->port) { return CWS_SERVER_CONFIG; } @@ -96,7 +96,7 @@ static cws_server_ret cws_server_setup_epoll(int server_fd, int *epfd_out) { return CWS_SERVER_OK; } -cws_server_ret cws_server_loop(int server_fd, cws_config *config) { +cws_server_ret cws_server_loop(int server_fd, cws_config_s *config) { int epfd = 0; cws_server_ret ret; @@ -112,7 +112,7 @@ cws_server_ret cws_server_loop(int server_fd, cws_config *config) { size_t workers_num = 6; size_t workers_index = 0; - cws_worker **workers = cws_worker_init(workers_num, clients, config); + cws_worker_s **workers = cws_worker_init(workers_num, clients, config); if (workers == NULL) { hm_free(clients); return CWS_SERVER_WORKER_ERROR; diff --git a/src/server/worker.c b/src/server/worker.c index 361b880..a6dbd54 100644 --- a/src/server/worker.c +++ b/src/server/worker.c @@ -11,7 +11,7 @@ #include "http/http.h" #include "utils/debug.h" -static int cws_worker_setup_epoll(cws_worker *worker) { +static int cws_worker_setup_epoll(cws_worker_s *worker) { worker->epfd = epoll_create1(0); if (worker->epfd == -1) { return -1; @@ -34,15 +34,15 @@ static int cws_worker_setup_epoll(cws_worker *worker) { return 0; } -cws_worker **cws_worker_init(size_t workers_num, hashmap_s *clients, cws_config *config) { - cws_worker **workers = malloc(sizeof(cws_worker) * workers_num); +cws_worker_s **cws_worker_init(size_t workers_num, hashmap_s *clients, cws_config_s *config) { + cws_worker_s **workers = malloc(sizeof(cws_worker_s) * workers_num); if (workers == NULL) { return NULL; } - memset(workers, 0, sizeof(cws_worker) * workers_num); + memset(workers, 0, sizeof(cws_worker_s) * workers_num); for (size_t i = 0; i < workers_num; ++i) { - workers[i] = malloc(sizeof(cws_worker)); + workers[i] = malloc(sizeof(cws_worker_s)); if (workers[i] == NULL) { for (size_t j = 0; j < i; ++j) { free(workers[j]); @@ -52,7 +52,7 @@ cws_worker **cws_worker_init(size_t workers_num, hashmap_s *clients, cws_config free(workers); } - memset(workers[i], 0, sizeof(cws_worker)); + memset(workers[i], 0, sizeof(cws_worker_s)); workers[i]->config = config; workers[i]->clients = clients; @@ -78,7 +78,7 @@ cws_worker **cws_worker_init(size_t workers_num, hashmap_s *clients, cws_config return workers; } -void cws_worker_free(cws_worker **workers, size_t workers_num) { +void cws_worker_free(cws_worker_s **workers, size_t workers_num) { for (size_t i = 0; i < workers_num; ++i) { pthread_join(workers[i]->thread, NULL); free(workers[i]); @@ -88,7 +88,7 @@ void cws_worker_free(cws_worker **workers, size_t workers_num) { } void *cws_worker_loop(void *arg) { - cws_worker *worker = arg; + cws_worker_s *worker = arg; struct epoll_event events[64]; int nfds; @@ -186,7 +186,7 @@ static size_t cws_read_data(int sockfd, string_s **str) { return total_bytes; } -cws_server_ret cws_server_handle_client_data(int epfd, int client_fd, hashmap_s *clients, cws_config *config) { +cws_server_ret cws_server_handle_client_data(int epfd, int client_fd, hashmap_s *clients, cws_config_s *config) { /* Read data from socket */ string_s *data = NULL; size_t total_bytes = cws_read_data(client_fd, &data); @@ -200,7 +200,7 @@ cws_server_ret cws_server_handle_client_data(int epfd, int client_fd, hashmap_s } /* Parse HTTP request */ - cws_http *request = cws_http_parse(data, client_fd, config); + cws_http_s *request = cws_http_parse(data, client_fd, config); string_free(data); if (request == NULL) { diff --git a/src/utils/config.c b/src/utils/config.c index 1f6c0d6..bfcfb47 100644 --- a/src/utils/config.c +++ b/src/utils/config.c @@ -12,32 +12,32 @@ static const cyaml_config_t cyaml_config = { }; static const cyaml_schema_field_t virtual_hosts_fields[] = { - CYAML_FIELD_STRING_PTR("domain", CYAML_FLAG_POINTER, struct cws_virtual_host_t, domain, 0, CYAML_UNLIMITED), - CYAML_FIELD_STRING_PTR("root", CYAML_FLAG_POINTER, struct cws_virtual_host_t, root, 0, CYAML_UNLIMITED), - CYAML_FIELD_BOOL("ssl", CYAML_FLAG_DEFAULT, struct cws_virtual_host_t, ssl), - CYAML_FIELD_STRING_PTR("cert", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, struct cws_virtual_host_t, cert, 0, CYAML_UNLIMITED), - CYAML_FIELD_STRING_PTR("key", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, struct cws_virtual_host_t, key, 0, CYAML_UNLIMITED), + CYAML_FIELD_STRING_PTR("domain", CYAML_FLAG_POINTER, struct cws_vhost, domain, 0, CYAML_UNLIMITED), + CYAML_FIELD_STRING_PTR("root", CYAML_FLAG_POINTER, struct cws_vhost, root, 0, CYAML_UNLIMITED), + CYAML_FIELD_BOOL("ssl", CYAML_FLAG_DEFAULT, struct cws_vhost, ssl), + CYAML_FIELD_STRING_PTR("cert", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, struct cws_vhost, cert, 0, CYAML_UNLIMITED), + CYAML_FIELD_STRING_PTR("key", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, struct cws_vhost, key, 0, CYAML_UNLIMITED), CYAML_FIELD_END, }; static cyaml_schema_value_t virtual_hosts_schema = { - CYAML_VALUE_MAPPING(CYAML_FLAG_DEFAULT, struct cws_virtual_host_t, virtual_hosts_fields), + CYAML_VALUE_MAPPING(CYAML_FLAG_DEFAULT, struct cws_vhost, virtual_hosts_fields), }; static const cyaml_schema_field_t top_schema_fields[] = { - CYAML_FIELD_STRING_PTR("hostname", CYAML_FLAG_POINTER, struct cws_config_t, hostname, 0, CYAML_UNLIMITED), - CYAML_FIELD_STRING_PTR("port", CYAML_FLAG_POINTER, struct cws_config_t, port, 0, CYAML_UNLIMITED), - CYAML_FIELD_SEQUENCE("virtual_hosts", CYAML_FLAG_POINTER, struct cws_config_t, virtual_hosts, &virtual_hosts_schema, 0, CYAML_UNLIMITED), + CYAML_FIELD_STRING_PTR("hostname", CYAML_FLAG_POINTER, struct cws_config, hostname, 0, CYAML_UNLIMITED), + CYAML_FIELD_STRING_PTR("port", CYAML_FLAG_POINTER, struct cws_config, port, 0, CYAML_UNLIMITED), + CYAML_FIELD_SEQUENCE("virtual_hosts", CYAML_FLAG_POINTER, struct cws_config, virtual_hosts, &virtual_hosts_schema, 0, CYAML_UNLIMITED), CYAML_FIELD_END, }; static cyaml_schema_value_t top_schema = { - CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER, struct cws_config_t, top_schema_fields), + CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER, struct cws_config, top_schema_fields), }; -cws_config *cws_config_init(void) { +cws_config_s *cws_config_init(void) { char *path = "config.yaml"; - cws_config *config; + cws_config_s *config; cyaml_err_t err = cyaml_load_file(path, &cyaml_config, &top_schema, (cyaml_data_t **)&config, NULL); if (err != CYAML_OK) { @@ -49,7 +49,7 @@ cws_config *cws_config_init(void) { return config; } -void cws_config_free(cws_config *config) { +void cws_config_free(cws_config_s *config) { cyaml_err_t err = cyaml_free(&cyaml_config, &top_schema, config, 0); if (err != CYAML_OK) { /* TODO: Handle */