style: run clang-format
This commit is contained in:
@@ -1,5 +1,10 @@
|
|||||||
|
Language: C
|
||||||
BasedOnStyle: Google
|
BasedOnStyle: Google
|
||||||
ColumnLimit: 160
|
|
||||||
UseTab: Always
|
UseTab: Always
|
||||||
IndentWidth: 4
|
|
||||||
TabWidth: 4
|
TabWidth: 4
|
||||||
|
IndentWidth: 4
|
||||||
|
|
||||||
|
ColumnLimit: 100
|
||||||
|
PointerAlignment: Right
|
||||||
|
AllowShortFunctionsOnASingleLine: None
|
||||||
|
|||||||
@@ -49,6 +49,15 @@ And then open the `docs/html/index.html`.
|
|||||||
|
|
||||||
This test was performed using `wrk`.
|
This test was performed using `wrk`.
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>wrk command</summary>
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ wrk -t12 -c400 -d30s http://127.0.0.1:3030/index.html
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
Running 30s test @ http://localhost:3030
|
Running 30s test @ http://localhost:3030
|
||||||
12 threads and 400 connections
|
12 threads and 400 connections
|
||||||
|
|||||||
@@ -12,11 +12,14 @@ static const cyaml_config_t cyaml_config = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static const cyaml_schema_field_t virtual_hosts_fields[] = {
|
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_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_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("cert", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, struct cws_vhost, cert,
|
||||||
CYAML_FIELD_STRING_PTR("key", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, struct cws_vhost, key, 0, CYAML_UNLIMITED),
|
0, CYAML_UNLIMITED),
|
||||||
|
CYAML_FIELD_STRING_PTR("key", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, struct cws_vhost, key,
|
||||||
|
0, CYAML_UNLIMITED),
|
||||||
CYAML_FIELD_END,
|
CYAML_FIELD_END,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -25,9 +28,11 @@ static cyaml_schema_value_t virtual_hosts_schema = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static const cyaml_schema_field_t top_schema_fields[] = {
|
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_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,
|
CYAML_FIELD_END,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -39,7 +44,8 @@ cws_config_s *cws_config_init(void) {
|
|||||||
char *path = "config.yaml";
|
char *path = "config.yaml";
|
||||||
cws_config_s *config;
|
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) {
|
if (err != CYAML_OK) {
|
||||||
CWS_LOG_ERROR("%s", cyaml_strerror(err));
|
CWS_LOG_ERROR("%s", cyaml_strerror(err));
|
||||||
|
|
||||||
|
|||||||
@@ -132,11 +132,13 @@ static cws_server_ret http_send_resource(cws_http_s *request) {
|
|||||||
char *response;
|
char *response;
|
||||||
size_t content_length = file_data(request->location_path->data, &data);
|
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;
|
size_t total_sent = 0;
|
||||||
while (total_sent < response_len) {
|
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 (sent < 0) {
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||||
continue;
|
continue;
|
||||||
@@ -153,7 +155,8 @@ static cws_server_ret http_send_resource(cws_http_s *request) {
|
|||||||
return CWS_SERVER_OK;
|
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];
|
char body[512];
|
||||||
memset(body, 0, sizeof body);
|
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 */
|
/* Parse headers until a \r\n */
|
||||||
request->headers =
|
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;
|
char *header_colon;
|
||||||
while (pch) {
|
while (pch) {
|
||||||
/* Get header line */
|
/* Get header line */
|
||||||
@@ -289,7 +293,8 @@ static size_t http_header_len(char *status_code, char *content_type, size_t body
|
|||||||
return len;
|
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);
|
char *status_code = http_status_string(status);
|
||||||
|
|
||||||
size_t header_len = http_header_len(status_code, content_type, body_len_bytes);
|
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;
|
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,
|
snprintf(*response, header_len + 1,
|
||||||
body_len_bytes);
|
"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);
|
// CWS_LOG_DEBUG("response: %s", *response);
|
||||||
|
|
||||||
/* Only append body if we have it */
|
/* 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);
|
http_send_resource(request);
|
||||||
break;
|
break;
|
||||||
case HTTP_NOT_FOUND: {
|
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);
|
sock_writeall(request->sockfd, response, len);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HTTP_NOT_IMPLEMENTED: {
|
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);
|
sock_writeall(request->sockfd, response, len);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -10,7 +10,9 @@
|
|||||||
#include "server/server.h"
|
#include "server/server.h"
|
||||||
#include "utils/debug.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) {
|
int main(void) {
|
||||||
struct sigaction act = {.sa_handler = cws_signal_handler, .sa_flags = 0, .sa_mask = {{0}}};
|
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);
|
CWS_LOG_INFO("Virtual hosts count: %d", config->virtual_hosts_count);
|
||||||
for (size_t i = 0; i < config->virtual_hosts_count; ++i) {
|
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;
|
cws_server_s server;
|
||||||
|
|||||||
@@ -11,7 +11,9 @@
|
|||||||
|
|
||||||
volatile sig_atomic_t cws_server_run = 1;
|
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) {
|
void cws_utils_get_client_ip(struct sockaddr_storage *sa, char *ip) {
|
||||||
if (sa->ss_family == AF_INET) {
|
if (sa->ss_family == AF_INET) {
|
||||||
@@ -54,9 +56,13 @@ bool my_str_equal_fn(const void *a, const void *b) {
|
|||||||
return false;
|
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) {
|
bool my_int_equal_fn(const void *a, const void *b) {
|
||||||
int ai = *(int *)a;
|
int ai = *(int *)a;
|
||||||
|
|||||||
Reference in New Issue
Block a user