kore

a fork of the worlds most advanced web framework
Log | Files | Refs | README | LICENSE

commit d0a6958747f34dace249561b97bf4efbdbae3850
parent 9ac77d0c9a4d13ce884a2365ef29053ffd0f0714
Author: Joris Vink <joris@coders.se>
Date:   Sun, 28 Apr 2019 21:48:16 +0200

Let http_state_create() take an "onfree" callback.

This function is called when an HTTP request is being free'd,
allowing you to perform any sort of state cleanup attached
to the HTTP request.

Diffstat:
Mexamples/async-curl/src/ftp.c | 2+-
Mexamples/async-curl/src/http.c | 2+-
Mexamples/pgsql/src/pgsql.c | 2+-
Minclude/kore/http.h | 5++++-
Msrc/http.c | 8+++++++-
5 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/examples/async-curl/src/ftp.c b/examples/async-curl/src/ftp.c @@ -44,7 +44,7 @@ state_setup(struct http_request *req) { struct kore_curl *client; - client = http_state_create(req, sizeof(*client)); + client = http_state_create(req, sizeof(*client), NULL); if (!kore_curl_init(client, "http://ftp.eu.openbsd.org/pub/OpenBSD/README")) { diff --git a/examples/async-curl/src/http.c b/examples/async-curl/src/http.c @@ -59,7 +59,7 @@ state_setup(struct http_request *req) { struct kore_curl *client; - client = http_state_create(req, sizeof(*client)); + client = http_state_create(req, sizeof(*client), NULL); /* Initialize curl. */ if (!kore_curl_init(client, "https://kore.io")) { diff --git a/examples/pgsql/src/pgsql.c b/examples/pgsql/src/pgsql.c @@ -85,7 +85,7 @@ request_perform_init(struct http_request *req) /* Setup our state context (if not yet set). */ if (!http_state_exists(req)) { - state = http_state_create(req, sizeof(*state)); + state = http_state_create(req, sizeof(*state), NULL); /* * Initialize the kore_pgsql data structure and bind it diff --git a/include/kore/http.h b/include/kore/http.h @@ -249,6 +249,8 @@ struct http_request { size_t state_len; char *query_string; struct kore_module_handle *hdlr; + void (*onfree)(struct http_request *); + #if defined(KORE_USE_PYTHON) void *py_coro; #endif @@ -346,7 +348,8 @@ const char *http_media_type(const char *); void *http_state_get(struct http_request *); int http_state_exists(struct http_request *); void http_state_cleanup(struct http_request *); -void *http_state_create(struct http_request *, size_t); +void *http_state_create(struct http_request *, size_t, + void (*onfree)(struct http_request *)); int http_argument_urldecode(char *); int http_header_recv(struct netbuf *); diff --git a/src/http.c b/src/http.c @@ -406,6 +406,9 @@ http_request_free(struct http_request *req) struct http_header *hdr, *next; struct http_cookie *ck, *cknext; + if (req->onfree != NULL) + req->onfree(req); + #if defined(KORE_USE_TASKS) pending_tasks = 0; for (t = LIST_FIRST(&(req->tasks)); t != NULL; t = nt) { @@ -1379,12 +1382,14 @@ http_state_exists(struct http_request *req) } void * -http_state_create(struct http_request *req, size_t len) +http_state_create(struct http_request *req, size_t len, + void (*onfree)(struct http_request *)) { if (req->hdlr_extra != NULL) fatal("http_state_create: state already exists"); req->state_len = len; + req->onfree = onfree; req->hdlr_extra = kore_calloc(1, len); return (req->hdlr_extra); @@ -1530,6 +1535,7 @@ http_request_new(struct connection *c, const char *host, req->method = m; req->hdlr = hdlr; req->agent = NULL; + req->onfree = NULL; req->referer = NULL; req->flags = flags; req->fsm_state = 0;