fix http parser and config

This commit is contained in:
2025-05-08 16:58:42 +02:00
parent 8e880f59be
commit 8ff2bb608c
11 changed files with 50 additions and 35 deletions

View File

@@ -8,7 +8,7 @@
#include "utils/colors.h"
#include "utils/utils.h"
cws_http *cws_http_parse(char *request_str, int sockfd) {
cws_http *cws_http_parse(char *request_str, int sockfd, cws_config *config) {
cws_http *request = malloc(sizeof(cws_http));
if (request == NULL) {
return NULL;
@@ -21,6 +21,8 @@ cws_http *cws_http_parse(char *request_str, int sockfd) {
/* Parse HTTP method */
char *pch = strtok(request_str, " ");
if (pch == NULL) {
cws_http_free(request);
return NULL;
}
CWS_LOG_DEBUG("[http] method: %s", pch);
@@ -29,6 +31,8 @@ cws_http *cws_http_parse(char *request_str, int sockfd) {
/* Parse location */
pch = strtok(NULL, " ");
if (pch == NULL) {
cws_http_free(request);
return NULL;
}
CWS_LOG_DEBUG("[http] location: %s", pch);
@@ -36,15 +40,17 @@ cws_http *cws_http_parse(char *request_str, int sockfd) {
/* Adjust location path */
if (strcmp(request->location, "/") == 0) {
snprintf(request->location_path, CWS_HTTP_LOCATION_PATH_LEN, "%s/index.html", CWS_WWW);
snprintf(request->location_path, CWS_HTTP_LOCATION_PATH_LEN, "%s/index.html", config->www);
} else {
snprintf(request->location_path, CWS_HTTP_LOCATION_PATH_LEN, "%s%s", CWS_WWW, request->location);
snprintf(request->location_path, CWS_HTTP_LOCATION_PATH_LEN, "%s%s", config->www, request->location);
}
CWS_LOG_DEBUG("[http] location path: %s", request->location_path);
/* Parse HTTP version */
pch = strtok(NULL, " \r\n");
if (pch == NULL) {
cws_http_free(request);
return NULL;
}
CWS_LOG_DEBUG("[http] version: %s", pch);

View File

@@ -12,18 +12,21 @@ void cws_signal_handler(int signo) { cws_server_run = false; }
int main(int argc, char **argv) {
int ret;
struct sigaction act = {.sa_handler = cws_signal_handler};
ret = sigaction(SIGINT, &act, NULL);
if (!ret) {
CWS_LOG_ERROR("sigaction()");
return 1;
}
cws_config *config = cws_config_init();
if (config == NULL) {
CWS_LOG_ERROR("Unable to read config file");
return 1;
}
struct sigaction act = {.sa_handler = cws_signal_handler};
ret = sigaction(SIGINT, &act, NULL);
CWS_LOG_INFO("Running cws on http://%s:%s...", config->host, config->port);
ret = cws_server_start(config->host, config->port);
CWS_LOG_INFO("Running cws on http://%s:%s...", config->hostname, config->port);
ret = cws_server_start(config);
if (ret < 0) {
CWS_LOG_ERROR("Unable to start web server");
}

View File

@@ -13,18 +13,17 @@
#include "http/http.h"
#include "utils/colors.h"
#include "utils/hashmap.h"
#include "utils/utils.h"
volatile bool cws_server_run = 1;
int cws_server_start(const char *hostname, const char *service) {
int cws_server_start(cws_config *config) {
struct addrinfo hints;
struct addrinfo *res;
cws_server_setup_hints(&hints, sizeof hints, hostname);
cws_server_setup_hints(&hints, sizeof hints, config->hostname);
int status = getaddrinfo(hostname, service, &hints, &res);
int status = getaddrinfo(config->hostname, config->port, &hints, &res);
if (status != 0) {
CWS_LOG_ERROR("getaddrinfo() error: %s", gai_strerror(status));
exit(EXIT_FAILURE);
@@ -51,7 +50,7 @@ int cws_server_start(const char *hostname, const char *service) {
exit(EXIT_FAILURE);
}
cws_server_loop(sockfd);
cws_server_loop(sockfd, config);
freeaddrinfo(res);
close(sockfd);
@@ -74,7 +73,7 @@ void cws_server_setup_hints(struct addrinfo *hints, size_t len, const char *host
}
}
void cws_server_loop(int sockfd) {
void cws_server_loop(int sockfd, cws_config *config) {
struct sockaddr_storage their_sa;
socklen_t theirsa_size = sizeof their_sa;
@@ -132,7 +131,7 @@ void cws_server_loop(int sockfd) {
data[bytes_read] = '\0';
/* Parse HTTP request */
cws_http *request = cws_http_parse(data, client_fd);
cws_http *request = cws_http_parse(data, client_fd, config);
if (request == NULL) {
cws_server_close_client(epfd, client_fd, clients);

View File

@@ -9,10 +9,13 @@ static const cyaml_config_t cyaml_config = {
};
static const cyaml_schema_field_t top_mapping_schema[] = {
CYAML_FIELD_STRING_PTR("host", CYAML_FLAG_POINTER, cws_config, host, 0, CYAML_UNLIMITED),
CYAML_FIELD_STRING_PTR("hostname", CYAML_FLAG_POINTER, cws_config, hostname, 0, CYAML_UNLIMITED),
CYAML_FIELD_STRING_PTR("port", CYAML_FLAG_POINTER, cws_config, port, 0, CYAML_UNLIMITED),
CYAML_FIELD_STRING_PTR("www", CYAML_FLAG_POINTER, cws_config, www, 0, CYAML_UNLIMITED),
CYAML_FIELD_STRING_PTR("cert", CYAML_FLAG_POINTER, cws_config, cert, 0, CYAML_UNLIMITED),
CYAML_FIELD_STRING_PTR("key", CYAML_FLAG_POINTER, cws_config, key, 0, CYAML_UNLIMITED), CYAML_FIELD_END};
CYAML_FIELD_STRING_PTR("key", CYAML_FLAG_POINTER, cws_config, key, 0, CYAML_UNLIMITED),
CYAML_FIELD_END
};
static const cyaml_schema_value_t top_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER, cws_config, top_mapping_schema),