From 2ea3111d64b3becbf8f412b8332dee2fa265d320 Mon Sep 17 00:00:00 2001 From: spl3g Date: Sat, 22 Mar 2025 11:18:36 +0300 Subject: Add internal server error and not found handlers --- lib/http.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/http.c b/lib/http.c index 7861e1c..050e50e 100644 --- a/lib/http.c +++ b/lib/http.c @@ -124,6 +124,16 @@ void *get_context_value(context *ctx, char* key) { return NULL; } +void http_internal_server_error_handler(struct request req) { + const_string *error = get_context_value(req.ctx, "error"); + req.resp->code = INTERNAL_SERVER_ERROR; + req.resp->body = *error; + http_send(req); +} + +void http_not_found_handler(struct request req) { + req.resp->code = NOT_FOUND; + http_send(req); } void http_run_next(http_middleware *self, struct request req) { @@ -523,10 +533,11 @@ void *process_request(void *args) { } if (!resp.sent) { - char *error = "Server handled the request but did not send anything"; - http_log(HTTP_ERROR, error); - resp.code = INTERNAL_SERVER_ERROR; - resp.body = CS(error); + const_string error = CS("Server handled the request but did not send anything\n"); + http_log(HTTP_ERROR, "%s", error.data); + set_context_value(&req_arena, &req_context, (context_value){CS("error"), (void *)&error}); + g_mid->end->handler = http_internal_server_error_handler; + g_mid->start->func(g_mid->start, req); } break; } @@ -534,15 +545,10 @@ void *process_request(void *args) { } if (!resp.code) { - resp.code = NOT_FOUND; + g_mid->end->handler = http_not_found_handler; + g_mid->start->func(g_mid->start, req); } - if (!resp.sent) { - http_send(req); - } - - - close(inc_fd); arena_free(&req_arena); -- cgit v1.2.3