diff options
| author | Xavier Del Campo Romero <xavi92@disroot.org> | 2026-02-12 16:59:55 +0100 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi92@disroot.org> | 2026-02-12 16:59:55 +0100 |
| commit | 87807c690947f4636dc4dd7fddca15f944719f0c (patch) | |
| tree | 0696d61b91a3e5f40a34cf09ac6c715584af5744 | |
| parent | 16f07314e675dd7d9d9cfb44dfb33cd81b13590d (diff) | |
Add HTTP op and resource to length callback
Users might want to know which HTTP operation (i.e., POST or PUT) and/or
resource is being requested before determining whether the request
should be accepted or not.
| -rw-r--r-- | doc/man7/libweb_handler.7 | 4 | ||||
| -rw-r--r-- | doc/man7/libweb_http.7 | 14 | ||||
| -rw-r--r-- | examples/async/main.c | 6 | ||||
| -rw-r--r-- | examples/headers/main.c | 6 | ||||
| -rw-r--r-- | examples/hello/main.c | 6 | ||||
| -rw-r--r-- | examples/put/main.c | 6 | ||||
| -rw-r--r-- | handler.c | 10 | ||||
| -rw-r--r-- | http.c | 3 | ||||
| -rw-r--r-- | include/libweb/handler.h | 4 | ||||
| -rw-r--r-- | include/libweb/http.h | 9 |
10 files changed, 39 insertions, 29 deletions
diff --git a/doc/man7/libweb_handler.7 b/doc/man7/libweb_handler.7 index 5398675..6a2c93e 100644 --- a/doc/man7/libweb_handler.7 +++ b/doc/man7/libweb_handler.7 @@ -73,7 +73,7 @@ defined as: struct handler_cfg { const char *\fItmpdir\fP; - int (*\fIlength\fP)(unsigned long long len, const struct http_cookie *c, struct http_response *r, void *user); + int (*\fIlength\fP)(enum http_op op, const char *res, unsigned long long len, const struct http_cookie *c, struct http_response *r, void *user); void *\fIuser\fP; size_t \fImax_headers\fP; struct http_cfg_post \fIpost\fP; @@ -185,7 +185,7 @@ static int hello(const struct http_payload *const pl, return 0; } -static int on_length(const unsigned long long len, +static int on_length(const char *const res, const unsigned long long len, const struct http_cookie *const c, struct http_response *const r, void *const user) { diff --git a/doc/man7/libweb_http.7 b/doc/man7/libweb_http.7 index 39fd4a5..ba32a99 100644 --- a/doc/man7/libweb_http.7 +++ b/doc/man7/libweb_http.7 @@ -90,7 +90,7 @@ struct http_cfg int (*\fIread\fP)(void *\fIbuf\fP, size_t \fIn\fP, void *\fIuser\fP); int (*\fIwrite\fP)(const void *\fIbuf\fP, size_t \fIn\fP, void *\fIuser\fP); int (*\fIpayload\fP)(const struct http_payload *\fIp\fP, struct http_response *\fIr\fP, void *\fIuser\fP); - int (*\fIlength\fP)(unsigned long long \fIlen\fP, const struct http_cookie *\fIc\fP, struct http_response *\fIr\fP, void *\fIuser\fP); + int (*\fIlength\fP)(enum http_op \fIop\fP, const char *\fIres\fP, unsigned long long \fIlen\fP, const struct http_cookie *\fIc\fP, struct http_response *\fIr\fP, void *\fIuser\fP); const char *\fItmpdir\fP; void *\fIuser\fP; size_t \fImax_headers\fP; @@ -182,6 +182,11 @@ In the case of a request, .I len defines the length of the file body. +.I op +can be used to distinguish the request type, +whereas +.I res +can be used to determine which resource is being accessed by the request. .I c is a read-only pointer to a .I "struct http_cookie" @@ -200,8 +205,11 @@ only when the function returns a positive integer. This function returns zero on success, a negative integer in case of a fatal error or a positive integer in case of a non-fatal error caused by a malformed request, or to indicate a lack of support for -this feature. When a positive integer is returned, the connection -against the client shall be closed. +this feature. When a positive integer is returned, the response pointed +to by +.I r +is sent to the client and the connection +against the client shall be closed afterwards. .I tmpdir is a null-terminated string defining the path to a directory where diff --git a/examples/async/main.c b/examples/async/main.c index fbd766e..8b0ec82 100644 --- a/examples/async/main.c +++ b/examples/async/main.c @@ -120,9 +120,9 @@ static int hello(const struct http_payload *const pl, return 0; } -static int on_length(const unsigned long long len, - const struct http_cookie *const c, struct http_response *const r, - void *const user) +static int on_length(const enum http_op op, const char *const res, + const unsigned long long len, const struct http_cookie *const c, + struct http_response *const r, void *const user) { *r = (const struct http_response) { diff --git a/examples/headers/main.c b/examples/headers/main.c index a4eb5e6..b9ed388 100644 --- a/examples/headers/main.c +++ b/examples/headers/main.c @@ -40,9 +40,9 @@ static int hello(const struct http_payload *const pl, return 0; } -static int on_length(const unsigned long long len, - const struct http_cookie *const c, struct http_response *const r, - void *const user) +static int on_length(const enum http_op op, const char *const res, + const unsigned long long len, const struct http_cookie *const c, + struct http_response *const r, void *const user) { *r = (const struct http_response) { diff --git a/examples/hello/main.c b/examples/hello/main.c index 8ddf691..cb5a8ee 100644 --- a/examples/hello/main.c +++ b/examples/hello/main.c @@ -77,9 +77,9 @@ end: return ret; } -static int on_length(const unsigned long long len, - const struct http_cookie *const c, struct http_response *const r, - void *const user) +static int on_length(const enum http_op op, const char *const res, + const unsigned long long len, const struct http_cookie *const c, + struct http_response *const r, void *const user) { *r = (const struct http_response) { diff --git a/examples/put/main.c b/examples/put/main.c index 3d6454d..562a1fa 100644 --- a/examples/put/main.c +++ b/examples/put/main.c @@ -40,9 +40,9 @@ static int on_put(const struct http_payload *const pl, return 0; } -static int on_length(const unsigned long long len, - const struct http_cookie *const c, struct http_response *const r, - void *const user) +static int on_length(const enum http_op op, const char *const res, + const unsigned long long len, const struct http_cookie *const c, + struct http_response *const r, void *const user) { *r = (const struct http_response) { @@ -93,9 +93,9 @@ static int on_payload(const struct http_payload *const p, return 0; } -static int on_length(const unsigned long long len, - const struct http_cookie *const c, struct http_response *const r, - void *const user) +static int on_length(const enum http_op op, const char *const res, + const unsigned long long len, const struct http_cookie *const c, + struct http_response *const r, void *const user) { struct client *const cl = user; struct handler *const h = cl->h; @@ -107,9 +107,9 @@ static int on_length(const unsigned long long len, int ret; if (s->length) - ret = s->length(len, c, r, h->cfg.user, cl->args); + ret = s->length(op, res, len, c, r, h->cfg.user, cl->args); else - ret = h->cfg.length(len, c, r, h->cfg.user); + ret = h->cfg.length(op, res, len, c, r, h->cfg.user); if (!ret) { @@ -1608,7 +1608,8 @@ static int check_length(struct http_ctx *const h) .value = c->value }; - return h->cfg.length(c->u2.payload.len, &cookie, &h->wctx.r, h->cfg.user); + return h->cfg.length(c->op, c->resource, c->u2.payload.len, &cookie, + &h->wctx.r, h->cfg.user); } static int process_payload_expect(struct http_ctx *const h) diff --git a/include/libweb/handler.h b/include/libweb/handler.h index 97fdca0..9dacdbd 100644 --- a/include/libweb/handler.h +++ b/include/libweb/handler.h @@ -10,8 +10,8 @@ typedef int (*handler_fn)(const struct http_payload *p, struct handler_cfg { const char *tmpdir; - int (*length)(unsigned long long len, const struct http_cookie *c, - struct http_response *r, void *user); + int (*length)(enum http_op op, const char *res, unsigned long long len, + const struct http_cookie *c, struct http_response *r, void *user); void *user; size_t max_headers; struct http_cfg_post post; diff --git a/include/libweb/http.h b/include/libweb/http.h index bd7769e..ad4ebcd 100644 --- a/include/libweb/http.h +++ b/include/libweb/http.h @@ -115,8 +115,9 @@ struct http_response union http_step { - int (*length)(unsigned long long len, const struct http_cookie *c, - struct http_response *r, void *user, void *args); + int (*length)(enum http_op op, const char *res, unsigned long long len, + const struct http_cookie *c, struct http_response *r, void *user, + void *args); int (*payload)(const struct http_payload *p, struct http_response *r, void *user, void *args); } step; @@ -128,8 +129,8 @@ struct http_cfg int (*write)(const void *buf, size_t n, void *user); int (*payload)(const struct http_payload *p, struct http_response *r, void *user); - int (*length)(unsigned long long len, const struct http_cookie *c, - struct http_response *r, void *user); + int (*length)(enum http_op op, const char *res, unsigned long long len, + const struct http_cookie *c, struct http_response *r, void *user); const char *tmpdir; void *user; size_t max_headers; |
