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.
This commit is contained in:
Joris Vink 2019-04-28 21:48:16 +02:00
parent 9ac77d0c9a
commit d0a6958747
5 changed files with 14 additions and 5 deletions

View File

@ -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")) {

View File

@ -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")) {

View File

@ -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

View File

@ -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 *);

View File

@ -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;