refactor: moved resetting of ctx to separate function

c-web-server
Jef Roosens 2023-05-28 18:24:22 +02:00
parent 590f0e5186
commit 62348c14f5
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
5 changed files with 35 additions and 17 deletions

View File

@ -118,4 +118,11 @@ typedef enum http_response_type {
http_network_authentication_required = 511
} http_response_type;
typedef struct http_response {
http_response_type type;
const char *body;
size_t body_len;
size_t body_written;
} http_response;
#endif

View File

@ -38,6 +38,8 @@ http_loop_gctx *http_loop_gctx_init();
*/
typedef struct http_loop_ctx {
http_request req;
http_response res;
bool flush;
http_route *route;
size_t current_step;
http_loop_gctx *g;
@ -48,6 +50,8 @@ typedef struct http_loop_ctx {
*/
http_loop_ctx *http_loop_ctx_init(http_loop_gctx *g);
void http_loop_ctx_reset(http_loop_ctx *ctx);
/*
* Free a context struct
*/

View File

@ -1,21 +1,6 @@
#include "http_loop.h"
#include "http.h"
http_loop_gctx *http_loop_gctx_init() {
http_loop_gctx *gctx = calloc(sizeof(http_loop_gctx), 1);
return gctx;
}
http_loop_ctx *http_loop_ctx_init(http_loop_gctx *g) {
http_loop_ctx *ctx = calloc(sizeof(http_loop_ctx), 1);
ctx->g = g;
return ctx;
}
void http_loop_ctx_free(http_loop_ctx *ctx) { free(ctx); }
bool http_loop_handle_request(event_loop_conn *conn) {
// Prevents the request handler function from looping indefinitely without
// ever consuming new data

View File

@ -0,0 +1,23 @@
#include "http.h"
#include "http_loop.h"
http_loop_gctx *http_loop_gctx_init() {
http_loop_gctx *gctx = calloc(sizeof(http_loop_gctx), 1);
return gctx;
}
http_loop_ctx *http_loop_ctx_init(http_loop_gctx *g) {
http_loop_ctx *ctx = calloc(sizeof(http_loop_ctx), 1);
ctx->g = g;
return ctx;
}
void http_loop_ctx_free(http_loop_ctx *ctx) { free(ctx); }
void http_loop_ctx_reset(http_loop_ctx *ctx) {
ctx->route = NULL;
ctx->current_step = 0;
ctx->flush = false;
}

View File

@ -84,8 +84,7 @@ void http_loop_process_request(event_loop_conn *conn) {
// If we've reached the end of the list of step functions, we report the
// request as finished by clearing its route
if (ctx->route->steps[ctx->current_step] == NULL) {
ctx->route = NULL;
ctx->current_step = 0;
http_loop_ctx_reset(ctx);
}
}