aboutsummaryrefslogtreecommitdiff
path: root/handler.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2025-09-23 22:03:57 +0200
committerXavier Del Campo Romero <xavi92@disroot.org>2025-09-24 12:33:35 +0200
commit5a6f30440b66fe6713acb9d979dc3e6624e4c36a (patch)
tree9a4410f7f7ee7a37f4b28fc8963b3667c3a0a900 /handler.c
parentf7864cb7d49a8ca5bddf8d1f68b71ecd5ed85adc (diff)
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.
Diffstat (limited to 'handler.c')
-rw-r--r--handler.c15
1 files changed, 14 insertions, 1 deletions
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);