From c05372cc8ab71d927b680277e75e8964c47654c5 Mon Sep 17 00:00:00 2001 From: Francesco Cozzuto Date: Tue, 24 Sep 2024 20:33:03 +0200 Subject: [PATCH 1/3] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index aa81afc..654d63a 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This is a minimal web server designed to serve my blog. I'm writing it to be rob # Specs - Only runs on Linux - HTTP/1.1 support with pipelining and keep-alive -* HTTPS (TLS 1.2 using BearSSL) +- HTTPS (TLS 1.2 using BearSSL) - Uses request and connection timeouts - Access log, log file rotation, hard disk usage limits - No `Transfer-Encoding: Chunked` (when receiving a chunked request the server responds with `411 Length Required`, prompting the client to try again with the `Content-Length` header) From e1a5eda51eabd403bef4ece614d72b9802867efd Mon Sep 17 00:00:00 2001 From: Francesco Cozzuto Date: Tue, 24 Sep 2024 23:01:00 +0200 Subject: [PATCH 2/3] Assume keep-alive if not specified otherwise --- serve.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/serve.c b/serve.c index e37992c..6a99245 100644 --- a/serve.c +++ b/serve.c @@ -76,6 +76,8 @@ #define LOG_DIRECTORY_SIZE_LIMIT_MB 100 #endif +#define KEEPALIVE_MAXREQS 1000 + #define LOG_DIRECTORY "logs" #define HTTPS_KEY_FILE "key.pem" @@ -89,7 +91,7 @@ #define PROFILE 0 #define ACCESS_LOG 1 #define SHOW_IO 0 -#define SHOW_REQUESTS 0 +#define SHOW_REQUESTS 1 #define REQUEST_TIMEOUT_SEC 5 #define CLOSING_TIMEOUT_SEC 2 #define CONNECTION_TIMEOUT_SEC 60 @@ -1064,7 +1066,7 @@ bool should_keep_alive(Connection *conn) return false; // Don't keep alive if we served a lot of requests to this connection - if (conn->served_count > 100) + if (conn->served_count > KEEPALIVE_MAXREQS) return false; // Don't keep alive if the server is more than 70% full @@ -1201,11 +1203,11 @@ bool respond_to_available_requests(Connection *conn) // Reset the request timer conn->start_time = now; - conn->keep_alive = false; + conn->keep_alive = true; string keep_alive_header; if (find_header(&request, LIT("Connection"), &keep_alive_header)) { - if (string_match_case_insensitive(trim(keep_alive_header), LIT("Keep-Alive"))) - conn->keep_alive = true; + if (string_match_case_insensitive(trim(keep_alive_header), LIT("Close"))) + conn->keep_alive = false; } // Respond ResponseBuilder builder; @@ -1543,6 +1545,13 @@ int main(int argc, char **argv) init_globals(); + log_format("BACKTRACE=%d\n", BACKTRACE); + log_format("EOPALLOC=%d\n", EOPALLOC); + log_format("PROFILE=%d\n", PROFILE); + log_format("ACCESS_LOG=%d\n", ACCESS_LOG); + log_format("SHOW_IO=%d\n", SHOW_IO); + log_format("SHOW_REQUESTS=%d\n", SHOW_REQUESTS); + DEBUG("Globals initialized\n"); uint64_t last_log_time = 0; From 8e7dac92ea25529562565c31810a3edc3b9c4815 Mon Sep 17 00:00:00 2001 From: Francesco Cozzuto Date: Tue, 24 Sep 2024 23:14:40 +0200 Subject: [PATCH 3/3] Disable SHOW_REQUESTS by default --- serve.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serve.c b/serve.c index 6a99245..564cc7e 100644 --- a/serve.c +++ b/serve.c @@ -91,7 +91,7 @@ #define PROFILE 0 #define ACCESS_LOG 1 #define SHOW_IO 0 -#define SHOW_REQUESTS 1 +#define SHOW_REQUESTS 0 #define REQUEST_TIMEOUT_SEC 5 #define CLOSING_TIMEOUT_SEC 2 #define CONNECTION_TIMEOUT_SEC 60