style: run clang-format

This commit is contained in:
2025-10-24 19:54:55 +02:00
parent 01d29ff9f3
commit 9ab2482f8a
6 changed files with 59 additions and 22 deletions

View File

@@ -12,11 +12,14 @@ 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_vhost, domain, 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_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,
};
@@ -25,9 +28,11 @@ static cyaml_schema_value_t virtual_hosts_schema = {
};
static const cyaml_schema_field_t top_schema_fields[] = {
CYAML_FIELD_STRING_PTR("hostname", CYAML_FLAG_POINTER, struct cws_config, hostname, 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_SEQUENCE("virtual_hosts", CYAML_FLAG_POINTER, struct cws_config, virtual_hosts,
&virtual_hosts_schema, 0, CYAML_UNLIMITED),
CYAML_FIELD_END,
};
@@ -39,7 +44,8 @@ cws_config_s *cws_config_init(void) {
char *path = "config.yaml";
cws_config_s *config;
cyaml_err_t err = cyaml_load_file(path, &cyaml_config, &top_schema, (cyaml_data_t **)&config, NULL);
cyaml_err_t err =
cyaml_load_file(path, &cyaml_config, &top_schema, (cyaml_data_t **)&config, NULL);
if (err != CYAML_OK) {
CWS_LOG_ERROR("%s", cyaml_strerror(err));

View File

@@ -132,11 +132,13 @@ static cws_server_ret http_send_resource(cws_http_s *request) {
char *response;
size_t content_length = file_data(request->location_path->data, &data);
size_t response_len = http_response_builder(&response, HTTP_OK, content_type, data, content_length);
size_t response_len =
http_response_builder(&response, HTTP_OK, content_type, data, content_length);
size_t total_sent = 0;
while (total_sent < response_len) {
ssize_t sent = send(request->sockfd, response + total_sent, response_len - total_sent, MSG_NOSIGNAL);
ssize_t sent =
send(request->sockfd, response + total_sent, response_len - total_sent, MSG_NOSIGNAL);
if (sent < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
continue;
@@ -153,7 +155,8 @@ static cws_server_ret http_send_resource(cws_http_s *request) {
return CWS_SERVER_OK;
}
static size_t http_simple_html(char **response, cws_http_status_e status, char *title, char *description) {
static size_t http_simple_html(char **response, cws_http_status_e status, char *title,
char *description) {
char body[512];
memset(body, 0, sizeof body);
@@ -241,7 +244,8 @@ cws_http_s *cws_http_parse(string_s *request_str) {
/* Parse headers until a \r\n */
request->headers =
hm_new(my_str_hash_fn, my_str_equal_fn, my_str_free_fn, my_str_free_fn, sizeof(char) * CWS_HTTP_HEADER_MAX, sizeof(char) * CWS_HTTP_HEADER_CONTENT_MAX);
hm_new(my_str_hash_fn, my_str_equal_fn, my_str_free_fn, my_str_free_fn,
sizeof(char) * CWS_HTTP_HEADER_MAX, sizeof(char) * CWS_HTTP_HEADER_CONTENT_MAX);
char *header_colon;
while (pch) {
/* Get header line */
@@ -289,7 +293,8 @@ static size_t http_header_len(char *status_code, char *content_type, size_t body
return len;
}
size_t http_response_builder(char **response, cws_http_status_e status, char *content_type, char *body, size_t body_len_bytes) {
size_t http_response_builder(char **response, cws_http_status_e status, char *content_type,
char *body, size_t body_len_bytes) {
char *status_code = http_status_string(status);
size_t header_len = http_header_len(status_code, content_type, body_len_bytes);
@@ -300,8 +305,9 @@ size_t http_response_builder(char **response, cws_http_status_e status, char *co
return 0;
}
snprintf(*response, header_len + 1, "HTTP/1.1 %s\r\nContent-Type: %s\r\nContent-Length: %zu\r\nConnection: close\r\n\r\n", status_code, content_type,
body_len_bytes);
snprintf(*response, header_len + 1,
"HTTP/1.1 %s\r\nContent-Type: %s\r\nContent-Length: %zu\r\nConnection: close\r\n\r\n",
status_code, content_type, body_len_bytes);
// CWS_LOG_DEBUG("response: %s", *response);
/* Only append body if we have it */
@@ -323,13 +329,15 @@ void cws_http_send_response(cws_http_s *request, cws_http_status_e status) {
http_send_resource(request);
break;
case HTTP_NOT_FOUND: {
size_t len = http_simple_html(&response, HTTP_NOT_FOUND, "404 Not Found", "Resource not found, 404.");
size_t len = http_simple_html(&response, HTTP_NOT_FOUND, "404 Not Found",
"Resource not found, 404.");
sock_writeall(request->sockfd, response, len);
break;
}
case HTTP_NOT_IMPLEMENTED: {
size_t len = http_simple_html(&response, HTTP_NOT_IMPLEMENTED, "501 Not Implemented", "Method not implemented, 501.");
size_t len = http_simple_html(&response, HTTP_NOT_IMPLEMENTED, "501 Not Implemented",
"Method not implemented, 501.");
sock_writeall(request->sockfd, response, len);
break;

View File

@@ -10,7 +10,9 @@
#include "server/server.h"
#include "utils/debug.h"
void cws_signal_handler(int) { cws_server_run = 0; }
void cws_signal_handler(int) {
cws_server_run = 0;
}
int main(void) {
struct sigaction act = {.sa_handler = cws_signal_handler, .sa_flags = 0, .sa_mask = {{0}}};
@@ -33,7 +35,8 @@ int main(void) {
CWS_LOG_INFO("Virtual hosts count: %d", config->virtual_hosts_count);
for (size_t i = 0; i < config->virtual_hosts_count; ++i) {
CWS_LOG_DEBUG("%s (ssl: %d)", config->virtual_hosts[i].domain, config->virtual_hosts[i].ssl);
CWS_LOG_DEBUG("%s (ssl: %d)", config->virtual_hosts[i].domain,
config->virtual_hosts[i].ssl);
}
cws_server_s server;

View File

@@ -11,7 +11,9 @@
volatile sig_atomic_t cws_server_run = 1;
static void cws_utils_convert_ip(int family, void *addr, char *ip, size_t ip_len) { inet_ntop(family, addr, ip, ip_len); }
static void cws_utils_convert_ip(int family, void *addr, char *ip, size_t ip_len) {
inet_ntop(family, addr, ip, ip_len);
}
void cws_utils_get_client_ip(struct sockaddr_storage *sa, char *ip) {
if (sa->ss_family == AF_INET) {
@@ -54,9 +56,13 @@ bool my_str_equal_fn(const void *a, const void *b) {
return false;
}
void my_str_free_fn(void *value) { free(value); }
void my_str_free_fn(void *value) {
free(value);
}
unsigned int my_int_hash_fn(const void *key) { return *(int *)key; }
unsigned int my_int_hash_fn(const void *key) {
return *(int *)key;
}
bool my_int_equal_fn(const void *a, const void *b) {
int ai = *(int *)a;