From e88c9d1739695ed8172c0eceffdc3d2c6e656403 Mon Sep 17 00:00:00 2001 From: Soikk Date: Thu, 1 Jan 2026 19:49:02 +0100 Subject: Fixed receive_request for HTTPS connections and enabled HTTPS through the config file --- config.example | 6 +++--- src/net/net.c | 9 +++++---- src/worker.c | 6 ++++++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/config.example b/config.example index 06a9c26..32ea2f1 100644 --- a/config.example +++ b/config.example @@ -1,6 +1,6 @@ # lines that start with # are skipped name website.com # name of the server. the # isnt really needed here as the config reader skips to the next line once it reads the needed value -port 80 # port the server will be running on +port 443 # port the server will be running on backlog 15 # backlog for the socket logs { # adds logging (for all processes) to specified files, and can turn logging to stderr off (0) or on (not 0) @@ -16,8 +16,8 @@ root /home/user/server/ # where the server will look for files, basically files bundle /ca/bundle/location # location of ca bundle cert ssl/cert.pem # location of certificate key ssl/key.pem #location of private key -http # turns https off -# https # turns https on +# http # turns https off +https # turns https on ipv4 # ipv4 enabled ipv6 # ipv6 enabled rewrites { diff --git a/src/net/net.c b/src/net/net.c index 39b25e3..3d738ae 100755 --- a/src/net/net.c +++ b/src/net/net.c @@ -98,7 +98,6 @@ int setup_https(http_server *hs, str certfile, str keyfile){ log_error("Missing private key file"); return 1; } - if(hs->ssl != NULL){ SSL_free(hs->ssl); } @@ -192,18 +191,20 @@ static inline int server_read(http_server *hs, str *buf){ } int receive_request(http_server *hs, str *request){ - // SSL_has_pending can return 0 if you havent read any bytes yet (https://stackoverflow.com/questions/6616976/why-does-this-ssl-pending-call-always-return-zero) struct pollfd pfd[1] = { {.fd = hs->csocket, .events = POLLIN } }; while(poll(pfd, 1, 100)){ if(pfd[0].revents & POLLIN){ int rb = 0; if(hs->secure){ - if(SSL_has_pending(hs->ssl)){ + // SSL_has_pending can return 0 if you havent read any bytes yet + // https://stackoverflow.com/questions/6616976/why-does-this-ssl-pending-call-always-return-zero + // so we must perform a read first to advance the state machine + do{ rb = server_read(hs, request); if(rb == 0){ return pleasesslgivemetheerror(SSL_get_error(hs->ssl, rb)); } - } + }while(SSL_has_pending(hs->ssl)); }else{ rb = server_read(hs, request); if(rb == 0){ diff --git a/src/worker.c b/src/worker.c index 0c29ca7..8312a74 100755 --- a/src/worker.c +++ b/src/worker.c @@ -115,6 +115,12 @@ int init(str name){ log_error("Error setting up worker server"); return 1; } + if(conf.secure){ + if(setup_https(server, conf.cert, conf.key)){ + log_error("Error setting up HTTPS in the server"); + return 1; + } + } struct sigaction rnit = { .sa_sigaction = reinit, .sa_flags = SA_SIGINFO }; if(sigaction(SIGUSR1, &rnit, NULL) == -1){ log_error("Error setting up SIGUSR1 signal handler: %s", strerror(errno)); -- cgit v1.2.3