From 5a6f30440b66fe6713acb9d979dc3e6624e4c36a Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Tue, 23 Sep 2025 22:03:57 +0200 Subject: Implement async HTTP responses Sometimes, library users cannot return a HTTP response as soon as the request is received, or the operations that are required to generate it can take a long time. In order to solve this, libweb adds a new member to struct http_response, namely step, which must be assigned to a function whenever a HTTP response should be generated in a non-blocking manner. Leaving the function pointer as null will fall back to the default behaviour. --- handler.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'handler.c') diff --git a/handler.c b/handler.c index 9ae9b10..8a777fa 100644 --- a/handler.c +++ b/handler.c @@ -28,6 +28,7 @@ struct handler struct handler *h; struct server_client *c; struct http_ctx *http; + int (*fn)(const struct http_payload *, struct http_response *, void *); struct client *next; } *clients; @@ -59,7 +60,19 @@ static int on_payload(const struct http_payload *const p, const struct elem *const e = &h->elem[i]; if (e->op == p->op && !wildcard_cmp(p->resource, e->url, true)) - return e->f(p, r, e->user); + { + int ret; + + if (c->fn) + ret = c->fn(p, r, e->user); + else + ret = e->f(p, r, e->user); + + if (!ret) + c->fn = r->step; + + return ret; + } } fprintf(stderr, "Not found: %s\n", p->resource); -- cgit v1.2.3