summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2017-11-06 10:42:44 +0100
committerJosé Bollo <jose.bollo@iot.bzh>2017-11-06 10:42:44 +0100
commit2b8dadf32dbb1a412bfb89830aa67f02264b58eb (patch)
tree4c75c6fd4961f2651d77bc07d4e5866c1619f22a
parent913e41d02977ee045490fbdcc94979c122c6a981 (diff)
afb-session: rewrite of cookie handling
Change-Id: I9852c1e2a211f79f25c7f32e9ac93acf7c5e6968 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r--src/afb-session.c103
1 files changed, 34 insertions, 69 deletions
diff --git a/src/afb-session.c b/src/afb-session.c
index 61bce09a..77a20fa0 100644
--- a/src/afb-session.c
+++ b/src/afb-session.c
@@ -406,60 +406,47 @@ const char *afb_session_token (struct afb_session *session)
return session->token;
}
-static struct cookie *cookie_search(struct afb_session *session, const void *key, int *idx)
-{
- struct cookie *cookie;
-
- cookie = session->cookies[*idx = cookeyidx(key)];
- while(cookie != NULL && cookie->key != key)
- cookie = cookie->next;
- return cookie;
-}
-
-static struct cookie *cookie_add(struct afb_session *session, int idx, const void *key, void *value, void (*freecb)(void*))
-{
- struct cookie *cookie;
-
- cookie = malloc(sizeof *cookie);
- if (!cookie)
- errno = ENOMEM;
- else {
- cookie->key = key;
- cookie->value = value;
- cookie->freecb = freecb;
- cookie->next = session->cookies[idx];
- session->cookies[idx] = cookie;
- }
- return cookie;
-}
-
void *afb_session_cookie(struct afb_session *session, const void *key, void *(*makecb)(void *closure), void (*freecb)(void *item), void *closure, int replace)
{
int idx;
void *value;
struct cookie *cookie;
+ idx = cookeyidx(key);
lock(session);
- cookie = cookie_search(session, key, &idx);
- if (cookie) {
- if (!replace)
- value = cookie->value;
- else {
+ cookie = session->cookies[idx];
+ for (;;) {
+ if (!cookie) {
value = makecb ? makecb(closure) : closure;
- if (cookie->value != value && cookie->freecb)
- cookie->freecb(cookie->value);
- cookie->value = value;
- cookie->freecb = freecb;
- }
- } else {
- value = makecb ? makecb(closure) : closure;
- if (replace || makecb || freecb) {
- cookie = cookie_add(session, idx, key, value, freecb);
- if (!cookie) {
- if (makecb && freecb)
- freecb(value);
- value = NULL;
+ if (replace || makecb || freecb) {
+ cookie = malloc(sizeof *cookie);
+ if (!cookie) {
+ errno = ENOMEM;
+ if (freecb)
+ freecb(value);
+ value = NULL;
+ } else {
+ cookie->key = key;
+ cookie->value = value;
+ cookie->freecb = freecb;
+ cookie->next = session->cookies[idx];
+ session->cookies[idx] = cookie;
+ }
}
+ break;
+ } else if (cookie->key == key) {
+ if (!replace)
+ value = cookie->value;
+ else {
+ value = makecb ? makecb(closure) : closure;
+ if (cookie->value != value && cookie->freecb)
+ cookie->freecb(cookie->value);
+ cookie->value = value;
+ cookie->freecb = freecb;
+ }
+ break;
+ } else {
+ cookie = cookie->next;
}
}
unlock(session);
@@ -468,33 +455,11 @@ void *afb_session_cookie(struct afb_session *session, const void *key, void *(*m
void *afb_session_get_cookie(struct afb_session *session, const void *key)
{
- int idx;
- void *value;
- struct cookie *cookie;
-
- lock(session);
- cookie = cookie_search(session, key, &idx);
- value = cookie ? cookie->value : NULL;
- unlock(session);
- return value;
+ return afb_session_cookie(session, key, NULL, NULL, NULL, 0);
}
int afb_session_set_cookie(struct afb_session *session, const void *key, void *value, void (*freecb)(void*))
{
- int idx;
- struct cookie *cookie;
-
- lock(session);
- cookie = cookie_search(session, key, &idx);
- if (!cookie)
- cookie = cookie_add(session, idx, key, value, freecb);
- else {
- if (cookie->value != value && cookie->freecb)
- cookie->freecb(cookie->value);
- cookie->value = value;
- cookie->freecb = freecb;
- }
- unlock(session);
- return -!cookie;
+ return -(value != afb_session_cookie(session, key, NULL, freecb, value, 1));
}