From 6e6f8a68e5d0aa51dc906151aa8c62de3e907ae8 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Tue, 11 Mar 2025 22:46:39 +0100 Subject: Add request timeout --- main.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index 0166fe3..9f96e3a 100755 --- a/main.c +++ b/main.c @@ -78,6 +78,8 @@ exit $? # */ #define CGI_CGIT "/usr/lib/cgit/cgit.cgi" enum { + REQUEST_TIMEOUT = 2, + DEFAULT_SCALE = 100, DEFAULT_WIDTH = 42, @@ -3277,10 +3279,19 @@ void setup_theme_cookie(c8 const *cookie, c8 const *query) { } } +void on_alarm(int s) { + fflush(stdout); + printf("Status: 408 Request Timeout\r\n"); + exit(0); +} + i32 main(i32 argc, c8 **argv) { (void) argc; (void) argv; + signal(SIGALRM, on_alarm); + alarm(REQUEST_TIMEOUT); + FILE *_in = freopen(NULL, "rb", stdin); FILE *_out = freopen(NULL, "wb", stdout); (void) _in; @@ -3306,7 +3317,7 @@ i32 main(i32 argc, c8 **argv) { strlen(content_type) > MAX_CONTENT_TYPE_SIZE || (!str_eq(request_method, NULL, "GET", NULL) && !str_eq(request_method, NULL, "POST", NULL))) { - printf("Status: 500 Internal Server Error 1\r\n"); + printf("Status: 500 Internal Server Error\r\n"); return 0; } @@ -3329,7 +3340,7 @@ i32 main(i32 argc, c8 **argv) { for (i64 i = 0; i < (i64)(sizeof redirects / sizeof *redirects); ++i) if (str_eq_with_slash(document_uri, redirects[i].alias)) { if (redirects[i].url == NULL) { - printf("Status: 500 Internal Server Error 2\r\n"); + printf("Status: 500 Internal Server Error\r\n"); return 0; } -- cgit v1.2.3