From 9b66223a57201a77bafba5f80c44839bbb820818 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Mon, 29 May 2023 12:03:40 +0200 Subject: [PATCH] refactor: move some stuff, add some docs --- include/http_loop.h | 35 +++++++++++++++++++++++++++-------- src/http_loop/http_loop_res.c | 35 ++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/include/http_loop.h b/include/http_loop.h index abca2f8..4d07b87 100644 --- a/include/http_loop.h +++ b/include/http_loop.h @@ -49,6 +49,10 @@ typedef struct http_loop_ctx { */ http_loop_ctx *http_loop_ctx_init(http_loop_gctx *g); +/* + * Resets an already allocated context so that it can be reused for a new + * request. + */ void http_loop_ctx_reset(http_loop_ctx *ctx); /* @@ -56,25 +60,40 @@ void http_loop_ctx_reset(http_loop_ctx *ctx); */ void http_loop_ctx_free(http_loop_ctx *ctx); -/** - * Process incoming data as an HTTP request +/* + * Process incoming data as an HTTP request. This is the "handle_data" function + * for the event loop. */ bool http_loop_handle_request(event_loop_conn *conn); +/* + * Write the HTTP response to the file descriptor. This is the "write_data" + * function for the event loop. + */ void http_loop_write_response(event_loop_conn *conn); +/* + * Try to parse the incoming data as an HTTP request. + */ http_parse_error http_loop_parse_request(event_loop_conn *conn); +/* + * Try to match the parsed request with one of the defined routes, aka route the + * request. + */ void http_loop_route_request(event_loop_conn *conn); +/* + * Advance the processing of the routed request's processing by cycling through + * the request's various steps. + */ void http_loop_process_request(event_loop_conn *conn); -void http_loop_res_set_body(const char *body, size_t body_len); - -void http_loop_res_set_type(http_response_type type); - -void http_loop_write_standard_response(event_loop_conn *conn, - http_response_type type); +/* + * Set the request body to the given buffer. If owned is set to true, the body + * buffer will be free'd after the request has finished. + */ +void http_loop_res_set_body(const char *body, size_t body_len, bool owned); /** * Initialize a new http loop diff --git a/src/http_loop/http_loop_res.c b/src/http_loop/http_loop_res.c index b38efd6..7148cd4 100644 --- a/src/http_loop/http_loop_res.c +++ b/src/http_loop/http_loop_res.c @@ -5,26 +5,31 @@ #define MIN(x, y) (((x) < (y)) ? (x) : (y)) +static const char *http_response_format = "HTTP/1.1 %i %s\n" + "Content-Length: %lu\n\n"; + +void http_loop_init_header(http_response *res) { + const char *response_type_name = + http_response_type_names[res->type / 100 - 1][res->type % 100]; + + // Running snprintf with size 0 prevents it from writing any bytes, while + // still letting it calculate how many bytes it would have written + int buf_size = snprintf(NULL, 0, http_response_format, res->type, + response_type_name, res->body_len); + char *buf = malloc(buf_size + 1); + sprintf(buf, http_response_format, res->type, response_type_name, + res->body_len); + + res->head = buf; + res->head_len = buf_size; +} + void http_loop_write_response(event_loop_conn *conn) { http_response *res = &((http_loop_ctx *)conn->ctx)->res; // Create head response if (res->head == NULL) { - const char *response_type_name = - http_response_type_names[res->type / 100 - 1][res->type % 100]; - - char *format = "HTTP/1.1 %i %s\n" - "Content-Length: %lu\n\n"; - - // Running snprintf with size 0 prevents it from writing any bytes, while - // still letting it calculate how many bytes it would have written - int buf_size = - snprintf(NULL, 0, format, res->type, response_type_name, res->body_len); - char *buf = malloc(buf_size + 1); - sprintf(buf, format, res->type, response_type_name, res->body_len); - - res->head = buf; - res->head_len = buf_size; + http_loop_init_header(res); } // The final iteration marks the end of the response, after which we reset the