]> saetta.ns0.it Git - libzakcgi/commitdiff
Added ZakCgiSession property "minutes" and new constructor ::new_minutes (closes...
authorAndrea Zagli <azagli@libero.it>
Wed, 18 May 2016 15:05:15 +0000 (17:05 +0200)
committerAndrea Zagli <azagli@libero.it>
Wed, 18 May 2016 15:14:01 +0000 (17:14 +0200)
src/session.c
src/session.h
tests/session.c

index c321ecb96004f373107c605c53be3fb41478db7f..d9926221e4e97d62e704dd3d27c1e48f086d3068 100644 (file)
 
 #include "session.h"
 
+enum
+{
+       PROP_0,
+       PROP_MINUTES,
+};
+
 static void zak_cgi_session_class_init (ZakCgiSessionClass *class);
 static void zak_cgi_session_init (ZakCgiSession *zak_cgi_session);
 
@@ -48,6 +54,8 @@ static void zak_cgi_session_create_file (ZakCgiSession *session);
 
 #define ZAK_CGI_SESSION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_CGI_TYPE_SESSION, ZakCgiSessionPrivate))
 
+#define MINUTES_DEFAULT 5
+
 typedef struct _ZakCgiSessionPrivate ZakCgiSessionPrivate;
 struct _ZakCgiSessionPrivate
        {
@@ -57,6 +65,8 @@ struct _ZakCgiSessionPrivate
                gchar *sid;
                GFile *gfile;
                GKeyFile *kfile;
+
+               gint minutes;
        };
 
 G_DEFINE_TYPE (ZakCgiSession, zak_cgi_session, G_TYPE_OBJECT)
@@ -71,6 +81,15 @@ zak_cgi_session_class_init (ZakCgiSessionClass *class)
        object_class->dispose = zak_cgi_session_dispose;
        object_class->finalize = zak_cgi_session_finalize;
 
+       g_object_class_install_property (object_class, PROP_MINUTES,
+                                        g_param_spec_int ("minutes",
+                                                                                                          "Minutes",
+                                                                                                          "Minutes",
+                                                                                                          -1,
+                                                                                                          G_MAXINT,
+                                                                                                          MINUTES_DEFAULT,
+                                                                                                          G_PARAM_READABLE));
+
        g_type_class_add_private (object_class, sizeof (ZakCgiSessionPrivate));
 }
 
@@ -84,6 +103,8 @@ zak_cgi_session_init (ZakCgiSession *zak_cgi_session)
        priv->path = NULL;
        priv->gfile = NULL;
        priv->kfile = NULL;
+
+       priv->minutes = MINUTES_DEFAULT;
 }
 
 /**
@@ -91,13 +112,15 @@ zak_cgi_session_init (ZakCgiSession *zak_cgi_session)
  * @zakcgimain:
  * @base_uri:
  * @path:
+ * @minutes:
  *
  * Returns: the newly created #ZakCgiSession object.
  */
 ZakCgiSession
-*zak_cgi_session_new (ZakCgiMain *zakcgimain,
-                                         const gchar *base_uri,
-                                         const gchar *path)
+*zak_cgi_session_new_minutes (ZakCgiMain *zakcgimain,
+                                                         const gchar *base_uri,
+                                                         const gchar *path,
+                                                         gint minutes)
 {
        GHashTable *ht_cookies;
 
@@ -119,6 +142,8 @@ ZakCgiSession
                        priv->path = g_strdup (path);
                }
 
+       priv->minutes = MAX (-1, minutes);
+
        ht_cookies = zak_cgi_main_get_cookies (priv->zakcgimain);
        priv->sid = g_strdup (g_value_get_string (g_hash_table_lookup (ht_cookies, "ZAKCGISID")));
 
@@ -141,6 +166,35 @@ ZakCgiSession
        return zak_cgi_session;
 }
 
+/**
+ * zak_cgi_session_new:
+ * @zakcgimain:
+ * @base_uri:
+ * @path:
+ *
+ * Returns: the newly created #ZakCgiSession object.
+ */
+ZakCgiSession
+*zak_cgi_session_new (ZakCgiMain *zakcgimain,
+                                         const gchar *base_uri,
+                                         const gchar *path)
+{
+       return zak_cgi_session_new_minutes (zakcgimain, base_uri, path, MINUTES_DEFAULT);
+}
+
+/**
+ * zak_cgi_session_get_minutes:
+ * @session:
+ *
+ */
+gint
+zak_cgi_session_get_minutes (ZakCgiSession *session)
+{
+       ZakCgiSessionPrivate *priv = ZAK_CGI_SESSION_GET_PRIVATE (session);
+
+       return priv->minutes;
+}
+
 /**
  * zak_cgi_session_get_header:
  * @session:
@@ -261,46 +315,48 @@ zak_cgi_session_is_valid (ZakCgiSession *session)
                                        if (val != NULL
                                                && g_strcmp0 (val, g_getenv ("REMOTE_ADDR")) == 0)
                                                {
-                                                       val = g_key_file_get_string (priv->kfile, "ZAKCGI", "TIMESTAMP", NULL);
-                                                       if (val != NULL)
+                                                       /* if priv->minutes <= -1 the session never expires */
+                                                       if (priv->minutes > -1)
                                                                {
-                                                                       if (g_time_val_from_iso8601 (val, &tval))
+                                                                       val = g_key_file_get_string (priv->kfile, "ZAKCGI", "TIMESTAMP", NULL);
+                                                                       if (val != NULL)
                                                                                {
-                                                                                       gdt = g_date_time_new_from_timeval_local (&tval);
-                                                                                       if (gdt != NULL)
+                                                                                       if (g_time_val_from_iso8601 (val, &tval))
                                                                                                {
-                                                                                                       /* TODO
-                                                                                                        * add a property for minutes number */
-                                                                                                       gdt_plus = g_date_time_add_minutes (gdt, 5);
-                                                                                                       gdt_now = g_date_time_new_now_local ();
-                                                                                                       if (g_date_time_compare (gdt_plus, gdt_now) > -1)
+                                                                                                       gdt = g_date_time_new_from_timeval_local (&tval);
+                                                                                                       if (gdt != NULL)
                                                                                                                {
-                                                                                                                       ret = TRUE;
+                                                                                                                       gdt_plus = g_date_time_add_minutes (gdt, priv->minutes);
+                                                                                                                       gdt_now = g_date_time_new_now_local ();
+                                                                                                                       if (g_date_time_compare (gdt_plus, gdt_now) > -1)
+                                                                                                                               {
+                                                                                                                                       ret = TRUE;
+                                                                                                                               }
+                                                                                                                       else
+                                                                                                                               {
+                                                                                                                                       g_warning ("Session expired.");
+                                                                                                                               }
+
+                                                                                                                       g_date_time_unref (gdt_plus);
+                                                                                                                       g_date_time_unref (gdt_now);
                                                                                                                }
                                                                                                        else
                                                                                                                {
                                                                                                                        g_warning ("Session expired.");
                                                                                                                }
 
-                                                                                                       g_date_time_unref (gdt_plus);
-                                                                                                       g_date_time_unref (gdt_now);
+                                                                                                       g_date_time_unref (gdt);
                                                                                                }
                                                                                        else
                                                                                                {
                                                                                                        g_warning ("Session expired.");
                                                                                                }
-
-                                                                                       g_date_time_unref (gdt);
                                                                                }
                                                                        else
                                                                                {
                                                                                        g_warning ("Session expired.");
                                                                                }
                                                                }
-                                                       else
-                                                               {
-                                                                       g_warning ("Session expired.");
-                                                               }
                                                }
                                        else
                                                {
@@ -359,9 +415,9 @@ zak_cgi_session_set_property (GObject *object,
 
        switch (property_id)
                {
-                       default:
-                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
-                               break;
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                       break;
                }
 }
 
@@ -376,9 +432,13 @@ zak_cgi_session_get_property (GObject *object,
 
        switch (property_id)
                {
-                       default:
-                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
-                               break;
+               case PROP_MINUTES:
+                       g_value_set_int (value, zak_cgi_session_get_minutes (zak_cgi_session));
+                       break;
+
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                       break;
                }
 }
 
index 74230c8837b4508e606cdbc32d281f228299fe71..682373569bbeb2d42ec0637bbf2b88f29c5fef40 100644 (file)
@@ -51,10 +51,17 @@ struct _ZakCgiSessionClass
 GType zak_cgi_session_get_type (void);
 
 
+ZakCgiSession *zak_cgi_session_new_minutes (ZakCgiMain *zakcgimain,
+                                                                                       const gchar *base_uri,
+                                                                                       const gchar *path,
+                                                                                       gint minutes);
+
 ZakCgiSession *zak_cgi_session_new (ZakCgiMain *zakcgimain,
                                                                        const gchar *base_uri,
                                                                        const gchar *path);
 
+gint zak_cgi_session_get_minutes (ZakCgiSession *session);
+
 gchar *zak_cgi_session_get_header (ZakCgiSession *session);
 
 void zak_cgi_session_set_value (ZakCgiSession *session, const gchar *name, const gchar *value);
index 529656344eda67a3ff82f2d315ade90cd1833236..5c71d2acce6ab75d2e7b23867ed296afe4f3de4e 100644 (file)
@@ -35,7 +35,7 @@ main (int argc, char *argv[])
 
        zakcgimain = zak_cgi_main_new ();
 
-       session = zak_cgi_session_new (zakcgimain, NULL, NULL);
+       session = zak_cgi_session_new_minutes (zakcgimain, NULL, NULL, 1);
 
        str = g_string_new ("<html>\n"
                            "<head><title>Session Cookie</title></head>\n"