commit c4c60e1960c380d8775aed02744d92d4aea10ada
parent f6f37437dac824a5fc931658d12fa1f399023d60
Author: Joris Vink <joris@coders.se>
Date: Fri, 19 Sep 2014 14:49:12 +0200
Oops, make sure the new simple api can handle > 1 request without borking.
Diffstat:
3 files changed, 34 insertions(+), 25 deletions(-)
diff --git a/examples/pgsql/src/pgsql_simple.c b/examples/pgsql/src/pgsql_simple.c
@@ -36,10 +36,10 @@ void page_simple_result(struct http_request *, struct kore_pgsql_simple *);
* Set our callbacks for initialization, result and completion.
* At least init and done MUST be set.
*/
-static struct kore_pgsql_simple simple_query = {
- .init = page_simple_init,
- .done = page_simple_done,
- .result = page_simple_result
+static struct kore_pgsql_functions simple_query = {
+ page_simple_init,
+ page_simple_done,
+ page_simple_result
};
int
diff --git a/includes/pgsql.h b/includes/pgsql.h
@@ -38,10 +38,13 @@ struct kore_pgsql {
};
struct kore_pgsql_simple {
- char *query;
- void *udata;
- struct kore_pgsql sql;
+ struct kore_pgsql sql;
+ void *fun;
+ char *query;
+ void *udata;
+};
+struct kore_pgsql_functions {
int (*init)(struct http_request *, struct kore_pgsql_simple *);
void (*done)(struct http_request *, struct kore_pgsql_simple *);
void (*result)(struct http_request *, struct kore_pgsql_simple *);
@@ -54,7 +57,7 @@ void kore_pgsql_init(void);
void kore_pgsql_handle(void *, int);
void kore_pgsql_cleanup(struct kore_pgsql *);
void kore_pgsql_continue(struct http_request *, struct kore_pgsql *);
-int kore_pgsql_run(struct http_request *, struct kore_pgsql_simple *);
+int kore_pgsql_run(struct http_request *, struct kore_pgsql_functions *);
int kore_pgsql_async(struct kore_pgsql *,
struct http_request *, const char *);
diff --git a/src/pgsql.c b/src/pgsql.c
@@ -146,9 +146,23 @@ kore_pgsql_async(struct kore_pgsql *pgsql, struct http_request *req,
}
int
-kore_pgsql_run(struct http_request *req, struct kore_pgsql_simple *query)
+kore_pgsql_run(struct http_request *req, struct kore_pgsql_functions *fun)
{
- req->hdlr_extra = query;
+ struct kore_pgsql_simple *simple;
+
+ if (req->hdlr_extra == NULL) {
+ if (fun->init == NULL || fun->done == NULL)
+ fatal("kore_pgsql_run: missing callback functions");
+
+ simple = kore_malloc(sizeof(*simple));
+ simple->fun = fun;
+ simple->query = NULL;
+ simple->udata = NULL;
+ simple->sql.state = 0;
+
+ req->hdlr_extra = simple;
+ }
+
return (http_state_run(pgsql_states, sizeof(pgsql_states), req));
}
@@ -437,18 +451,10 @@ static int
pgsql_simple_state_init(struct http_request *req)
{
struct kore_pgsql_simple *simple = req->hdlr_extra;
+ struct kore_pgsql_functions *fun = simple->fun;
- if (simple->init == NULL || simple->done == NULL)
- fatal("pgsql_simple_state_init: missing callbacks");
-
- simple->query = NULL;
- simple->udata = NULL;
- simple->sql.state = 0;
-
- if (simple->init(req, simple) != KORE_RESULT_OK) {
- req->hdlr_extra = NULL;
+ if (fun->init(req, simple) != KORE_RESULT_OK)
return (HTTP_STATE_COMPLETE);
- }
req->fsm_state = PGSQL_SIMPLE_STATE_QUERY;
return (HTTP_STATE_CONTINUE);
@@ -507,9 +513,10 @@ static int
pgsql_simple_state_result(struct http_request *req)
{
struct kore_pgsql_simple *simple = req->hdlr_extra;
+ struct kore_pgsql_functions *fun = simple->fun;
- if (simple->result)
- simple->result(req, simple);
+ if (fun->result)
+ fun->result(req, simple);
kore_pgsql_continue(req, &simple->sql);
req->fsm_state = PGSQL_SIMPLE_STATE_WAIT;
@@ -521,10 +528,9 @@ static int
pgsql_simple_state_done(struct http_request *req)
{
struct kore_pgsql_simple *simple = req->hdlr_extra;
+ struct kore_pgsql_functions *fun = simple->fun;
- req->hdlr_extra = NULL;
- simple->done(req, simple);
-
+ fun->done(req, simple);
if (simple->sql.state != 0)
kore_pgsql_cleanup(&simple->sql);