aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorspl3g <spleefer6@yandex.ru>2025-03-22 11:18:36 +0300
committerspl3g <spleefer6@yandex.ru>2025-03-22 11:18:36 +0300
commit2ea3111d64b3becbf8f412b8332dee2fa265d320 (patch)
tree71b33bfd749e18dd09dd34c6af47e2dce0786c43
parentab23f9bdc4e14f2643c2eb446f29716054d97deb (diff)
Add internal server error and not found handlers
-rw-r--r--lib/http.c28
1 files 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);