commit a5ff506cf788066e78dda3b4e75c45abaea0671a
parent 681e3b2b6e45024c56a924b97faa8b251cc0afdc
Author: Joris Vink <joris@sanctorum.se>
Date: Sun, 2 Mar 2025 15:25:01 +0100
http: alter behaviour for on_body_chunk.
Before, the on_body_chunk callback was called after Kore handled
the incoming HTTP body data from the client via either file offload
or in-memory buffer append.
There are real cases where a developer might want to use the on_body_chunk
callback so they can handle the incoming HTTP body data differently.
So lets change the behaviour so that on_body_chunk has higher priority
than file offload or in-memory buffers.
This way a developer can set the callback to handle the incoming
HTTP body data themselves if they so want without Kore getting
in the way.
Diffstat:
3 files changed, 20 insertions(+), 16 deletions(-)
diff --git a/include/kore/kore.h b/include/kore/kore.h
@@ -282,7 +282,7 @@ struct kore_runtime {
#if !defined(KORE_NO_HTTP)
int (*http_request)(void *, struct http_request *);
void (*http_request_free)(void *, struct http_request *);
- void (*http_body_chunk)(void *,
+ int (*http_body_chunk)(void *,
struct http_request *, const void *, size_t);
int (*validator)(void *, struct http_request *, const void *);
void (*wsconnect)(void *, struct connection *);
@@ -1044,7 +1044,7 @@ int kore_runtime_http_request(struct kore_runtime_call *,
struct http_request *);
void kore_runtime_http_request_free(struct kore_runtime_call *,
struct http_request *);
-void kore_runtime_http_body_chunk(struct kore_runtime_call *,
+int kore_runtime_http_body_chunk(struct kore_runtime_call *,
struct http_request *, const void *, size_t);
int kore_runtime_validator(struct kore_runtime_call *,
struct http_request *, const void *);
diff --git a/src/http.c b/src/http.c
@@ -940,7 +940,8 @@ http_header_recv(struct netbuf *nb)
req->http_body_length = req->content_length;
- if (http_body_disk_offload > 0 &&
+ if (req->rt->on_body_chunk == NULL &&
+ http_body_disk_offload > 0 &&
req->content_length > http_body_disk_offload) {
req->http_body_path = kore_pool_get(&http_body_path);
l = snprintf(req->http_body_path, HTTP_BODY_PATH_MAX,
@@ -960,7 +961,7 @@ http_header_recv(struct netbuf *nb)
HTTP_STATUS_INTERNAL_ERROR);
return (KORE_RESULT_OK);
}
- } else {
+ } else if (req->rt->on_body_chunk == NULL) {
req->http_body_fd = -1;
req->http_body = kore_buf_alloc(req->content_length);
}
@@ -2338,7 +2339,15 @@ http_body_update(struct http_request *req, const void *data, size_t len)
SHA256Update(&req->hashctx, data, len);
- if (req->http_body_fd != -1) {
+ if (req->rt->on_body_chunk != NULL) {
+ if (kore_runtime_http_body_chunk(req->rt->on_body_chunk,
+ req, data, len) != KORE_RESULT_OK) {
+ req->flags |= HTTP_REQUEST_DELETE;
+ http_error_response(req->owner,
+ HTTP_STATUS_INTERNAL_ERROR);
+ return (KORE_RESULT_ERROR);
+ }
+ } else if (req->http_body_fd != -1) {
ret = write(req->http_body_fd, data, len);
if (ret == -1 || (size_t)ret != len) {
req->flags |= HTTP_REQUEST_DELETE;
@@ -2379,11 +2388,6 @@ http_body_update(struct http_request *req, const void *data, size_t len)
req->owner->rnb->extra = req;
}
- if (req->rt->on_body_chunk != NULL && len > 0) {
- kore_runtime_http_body_chunk(req->rt->on_body_chunk,
- req, data, len);
- }
-
return (KORE_RESULT_OK);
}
diff --git a/src/runtime.c b/src/runtime.c
@@ -39,7 +39,7 @@ static void native_runtime_configure(void *, int, char **);
#if !defined(KORE_NO_HTTP)
static int native_runtime_http_request(void *, struct http_request *);
static void native_runtime_http_request_free(void *, struct http_request *);
-static void native_runtime_http_body_chunk(void *, struct http_request *,
+static int native_runtime_http_body_chunk(void *, struct http_request *,
const void *, size_t);
static int native_runtime_validator(void *, struct http_request *,
const void *);
@@ -164,11 +164,11 @@ kore_runtime_http_request_free(struct kore_runtime_call *rcall,
rcall->runtime->http_request_free(rcall->addr, req);
}
-void
+int
kore_runtime_http_body_chunk(struct kore_runtime_call *rcall,
struct http_request *req, const void *data, size_t len)
{
- rcall->runtime->http_body_chunk(rcall->addr, req, data, len);
+ return (rcall->runtime->http_body_chunk(rcall->addr, req, data, len));
}
int
@@ -262,15 +262,15 @@ native_runtime_http_request_free(void *addr, struct http_request *req)
cb(req);
}
-static void
+static int
native_runtime_http_body_chunk(void *addr, struct http_request *req,
const void *data, size_t len)
{
- void (*cb)(struct http_request *, const void *, size_t);
+ int (*cb)(struct http_request *, const void *, size_t);
*(void **)&(cb) = addr;
- cb(req, data, len);
+ return (cb(req, data, len));
}
static int