style: rename structs/enums
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user