aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/http.c19
-rw-r--r--lib/http.h1
2 files changed, 15 insertions, 5 deletions
diff --git a/lib/http.c b/lib/http.c
index 265e79e..69e8fd3 100644
--- a/lib/http.c
+++ b/lib/http.c
@@ -124,10 +124,17 @@ void *get_context_value(context *ctx, char* key) {
return NULL;
}
-void http_handler_runner_middleware(http_middleware *self, struct request req) {
- (self->handler)(req);
}
+void http_run_next(http_middleware *self, struct request req) {
+ if (self->next == NULL) {
+ (self->handler)(req);
+ return;
+ }
+
+ (self->next->func)(self->next, req);
+};
+
void http_register_handler_middleware(arena *arena, handler *hand, http_middleware_func func) {
http_middleware *middleware = arena_alloc(arena, sizeof(http_middleware));
middleware->func = func;
@@ -222,7 +229,6 @@ handler *http_handle_path(struct server *serv, const_string method, const_string
.path = path,
.func = handler,
};
- http_register_handler_middleware(serv->arena, &hand, http_handler_runner_middleware);
arena_da_append(serv->arena, &serv->handlers, hand);
return &serv->handlers.data[serv->handlers.len-1];
@@ -497,16 +503,19 @@ void *process_request(void *args) {
set_context_value(&req_arena, &req_context, *(serv.global_ctx.data + i));
}
+ http_global_middleware *g_mid = serv.global_middleware;
if (!resp.sent) {
for (size_t i = 0; i < serv.handlers.len; i++) {
struct handler handler = serv.handlers.data[i];
if (check_handler(handler, req, &req_arena, &req_context)) {
if (serv.global_middleware != NULL) {
- http_global_middleware *g_mid = serv.global_middleware;
g_mid->end->next = handler.middleware;
+ g_mid->end->handler = handler.func;
g_mid->start->func(g_mid->start, req);
- } else {
+ } else if (handler.middleware != NULL) {
handler.middleware->func(handler.middleware, req);
+ } else {
+ handler.func(req);
}
if (!resp.sent) {
diff --git a/lib/http.h b/lib/http.h
index 0657095..32e6fe6 100644
--- a/lib/http.h
+++ b/lib/http.h
@@ -170,6 +170,7 @@ int listen_and_serve(struct server *serv);
handler *http_handle_path(struct server *serv, const_string method, const_string path, handler_func handler);
+void http_run_next(http_middleware *self, struct request req);
void http_register_global_middleware(struct server *hand, http_middleware_func func);
void http_register_handler_middleware(arena *arena, handler *hand, http_middleware_func func);