From 72fae76ef6239e4a8cd4f0e07cabcafd859c81ff Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Fri, 3 Nov 2023 14:41:55 +0100 Subject: [PATCH] fix(http): don't segfault on non-routed requests --- include/http_loop.h | 2 ++ src/http_loop/http_loop.c | 3 +++ src/http_loop/http_loop_res.c | 9 ++++++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/include/http_loop.h b/include/http_loop.h index 4737301..fe13382 100644 --- a/include/http_loop.h +++ b/include/http_loop.h @@ -34,6 +34,8 @@ typedef enum http_route_type { */ typedef bool (*step)(event_loop_conn *conn); +extern const step http_default_res_steps[HTTP_LOOP_MAX_STEPS]; + /** * Struct describing a route a request can take. */ diff --git a/src/http_loop/http_loop.c b/src/http_loop/http_loop.c index dc76061..d65ea1c 100644 --- a/src/http_loop/http_loop.c +++ b/src/http_loop/http_loop.c @@ -4,6 +4,9 @@ #include "http_loop.h" #include "log.h" +const step http_default_res_steps[HTTP_LOOP_MAX_STEPS] = { + http_loop_step_write_header, http_loop_step_write_body, NULL}; + bool http_loop_handle_request(event_loop_conn *conn) { // Prevents the request handler function from looping indefinitely without // ever consuming new data diff --git a/src/http_loop/http_loop_res.c b/src/http_loop/http_loop_res.c index 7cbf9d3..cb372da 100644 --- a/src/http_loop/http_loop_res.c +++ b/src/http_loop/http_loop_res.c @@ -105,9 +105,12 @@ bool http_loop_step_write_body(event_loop_conn *conn) { void http_loop_handle_response(event_loop_conn *conn) { http_loop_ctx *ctx = conn->ctx; + // Non-routed requests also need to be processed + const step *steps = + ctx->route != NULL ? ctx->route->steps_res : http_default_res_steps; + while ((conn->state == event_loop_conn_state_res) && - (ctx->route->steps_res[ctx->current_step] != NULL) && - ctx->route->steps_res[ctx->current_step](conn)) { + (steps[ctx->current_step] != NULL) && steps[ctx->current_step](conn)) { ctx->current_step++; } @@ -115,7 +118,7 @@ void http_loop_handle_response(event_loop_conn *conn) { // After response processing has finished its work, we reset the context to // prepare for a new request if ((conn->state != event_loop_conn_state_res) || - (ctx->route->steps_res[ctx->current_step] == NULL)) { + (steps[ctx->current_step] == NULL)) { http_loop_ctx_reset(ctx); conn->state = event_loop_conn_state_req;