hooks: Allow to remove hooking (and/or trace)
[src/app-framework-binder.git] / src / afb-session.c
index fdc88fd..70e8c3c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015-2018 "IoT.bzh"
+ * Copyright (C) 2015-2019 "IoT.bzh"
  * Author "Fulup Ar Foll"
  * Author: José Bollo <jose.bollo@iot.bzh>
  *
@@ -65,6 +65,7 @@ struct afb_session
        time_t expiration;      /**< expiration time of the token */
        pthread_mutex_t mutex;  /**< mutex of the session */
        struct cookie *cookies[COOKIECOUNT]; /**< cookies of the session */
+       char *lang;             /**< current language setting for the session */
        uint8_t closed: 1;      /**< is the session closed ? */
        uint8_t autoclose: 1;   /**< close the session when unreferenced */
        uint8_t notinset: 1;    /**< session removed from the set of sessions */
@@ -239,8 +240,10 @@ static void session_close(struct afb_session *session)
                /* close it now */
                session->closed = 1;
 
+#if WITH_AFB_HOOK
                /* emit the hook */
                afb_hook_session_close(session);
+#endif
 
                /* release cookies */
                for (idx = 0 ; idx < COOKIECOUNT ; idx++) {
@@ -257,8 +260,11 @@ static void session_close(struct afb_session *session)
 /* destroy the 'session' */
 static void session_destroy (struct afb_session *session)
 {
+#if WITH_AFB_HOOK
        afb_hook_session_destroy(session);
+#endif
        pthread_mutex_destroy(&session->mutex);
+       free(session->lang);
        free(session);
 }
 
@@ -314,7 +320,9 @@ static struct afb_session *session_add(const char *uuid, int timeout, time_t now
                return NULL;
        }
 
+#if WITH_AFB_HOOK
        afb_hook_session_create(session);
+#endif
 
        return session;
 }
@@ -511,7 +519,9 @@ end:
 struct afb_session *afb_session_addref(struct afb_session *session)
 {
        if (session != NULL) {
+#if WITH_AFB_HOOK
                afb_hook_session_addref(session);
+#endif
                session_lock(session);
                session->refcount++;
                session_unlock(session);
@@ -525,7 +535,9 @@ void afb_session_unref(struct afb_session *session)
        if (session == NULL)
                return;
 
+#if WITH_AFB_HOOK
        afb_hook_session_unref(session);
+#endif
        session_lock(session);
        if (!--session->refcount) {
                if (session->autoclose)
@@ -574,7 +586,7 @@ int afb_session_check_token (struct afb_session *session, const char *token)
 {
        int r;
 
-       session_unlock(session);
+       session_lock(session);
        r = !session->closed
          && session->expiration >= NOW
          && !(session->token[0] && strcmp (token, session->token));
@@ -585,10 +597,12 @@ int afb_session_check_token (struct afb_session *session, const char *token)
 /* generate a new token and update client context */
 void afb_session_new_token (struct afb_session *session)
 {
-       session_unlock(session);
+       session_lock(session);
        new_uuid(session->token);
        session_update_expiration(session, NOW);
+#if WITH_AFB_HOOK
        afb_hook_session_renew(session);
+#endif
        session_unlock(session);
 }
 
@@ -738,3 +752,37 @@ int afb_session_set_cookie(struct afb_session *session, const void *key, void *v
        return -(value != afb_session_cookie(session, key, NULL, freecb, value, 1));
 }
 
+/**
+ * Set the language attached to the session
+ *
+ * @param session the session to set
+ * @param lang    the language specifiction to set to session
+ *
+ * @return 0 in case of success or -1 in case of error
+ */
+int afb_session_set_language(struct afb_session *session, const char *lang)
+{
+       char *oldl, *newl;
+
+       newl = strdup(lang);
+       if (newl == NULL)
+               return -1;
+
+       oldl = session->lang;
+       session->lang = newl;
+       free(oldl);
+       return 0;
+}
+
+/**
+ * Get the language attached to the session
+ *
+ * @param session the session to query
+ * @param lang    a default language specifiction
+ *
+ * @return the langauage specification to use for session
+ */
+const char *afb_session_get_language(struct afb_session *session, const char *lang)
+{
+       return session->lang ?: lang;
+}