diff --git a/include/http.h b/include/http.h index dbd7f0a..54625ba 100644 --- a/include/http.h +++ b/include/http.h @@ -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 diff --git a/include/http_loop.h b/include/http_loop.h index 7e5b10b..a928ae5 100644 --- a/include/http_loop.h +++ b/include/http_loop.h @@ -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 */ diff --git a/src/http_loop/http_loop.c b/src/http_loop/http_loop.c index b7a83e8..240b684 100644 --- a/src/http_loop/http_loop.c +++ b/src/http_loop/http_loop.c @@ -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 diff --git a/src/http_loop/http_loop_ctx.c b/src/http_loop/http_loop_ctx.c new file mode 100644 index 0000000..3e08dd1 --- /dev/null +++ b/src/http_loop/http_loop_ctx.c @@ -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; +} diff --git a/src/http_loop/http_loop_route.c b/src/http_loop/http_loop_route.c index fa31a8d..ce248ac 100644 --- a/src/http_loop/http_loop_route.c +++ b/src/http_loop/http_loop_route.c @@ -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); } }