]> saetta.ns0.it Git - libzakcgi/commitdiff
Session: now expires after 5 minutes of inactivity or if from different ip (closes...
authorAndrea Zagli <azagli@libero.it>
Sat, 1 Aug 2015 15:43:42 +0000 (17:43 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 1 Aug 2015 15:43:42 +0000 (17:43 +0200)
src/session.c

index 0fed8778e0c15972c4254fe25624f3d897e6fc90..511ab3b6b70514bda0d9842ec535409454cdbc03 100644 (file)
@@ -20,6 +20,8 @@
        #include <config.h>
 #endif
 
+#include <syslog.h>
+
 #include <gio/gio.h>
 
 #include <string.h>
@@ -77,6 +79,8 @@ zak_cgi_session_init (ZakCgiSession *zak_cgi_session)
        priv->zakcgimain = NULL;
        priv->base_uri = NULL;
        priv->path = NULL;
+       priv->gfile = NULL;
+       priv->kfile = NULL;
 }
 
 /**
@@ -136,6 +140,62 @@ ZakCgiSession
                                }
                        else
                                {
+                                       val = g_key_file_get_value (priv->kfile, "ZAKCGI", "REMOTE_ADDR", NULL);
+                                       if (val == NULL
+                                               || g_strcmp0 (val, g_getenv ("REMOTE_ADDR")) != 0)
+                                               {
+                                                       zak_cgi_session_close (zak_cgi_session);
+                                               }
+
+                                       val = g_key_file_get_value (priv->kfile, "ZAKCGI", "TIMESTAMP", NULL);
+                                       if (val == NULL)
+                                               {
+                                                       zak_cgi_session_close (zak_cgi_session);
+                                               }
+                                       else
+                                               {
+                                                       GTimeVal tval;
+
+                                                       if (g_time_val_from_iso8601 (val, &tval))
+                                                               {
+                                                                       GDateTime *gdt;
+                                                                       GDateTime *gdt_now;
+                                                                       GDateTime *gdt_plus;
+
+                                                                       gdt = g_date_time_new_from_timeval_local (&tval);
+                                                                       if (gdt == NULL)
+                                                                               {
+                                                                                       zak_cgi_session_close (zak_cgi_session);
+                                                                               }
+                                                                       else
+                                                                               {
+                                                                                       /* 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)
+                                                                                               {
+                                                                                                       /* session expired */
+                                                                                                       zak_cgi_session_close (zak_cgi_session);
+                                                                                               }
+                                                                                       else
+                                                                                               {
+                                                                                                       /* update timestamp */
+                                                                                                       g_key_file_set_value (priv->kfile, "ZAKCGI", "TIMESTAMP", g_date_time_format (gdt_now, "%FT%T"));
+                                                                                                       g_key_file_save_to_file (priv->kfile, g_file_get_path (priv->gfile), NULL);
+                                                                                               }
+
+                                                                                       g_date_time_unref (gdt_plus);
+                                                                                       g_date_time_unref (gdt_now);
+                                                                               }
+
+                                                                       g_date_time_unref (gdt);
+                                                               }
+                                                       else
+                                                               {
+                                                                       zak_cgi_session_close (zak_cgi_session);
+                                                               }
+                                               }
                                }
                }
 
@@ -208,7 +268,7 @@ gchar
                                                        gdt = g_date_time_new_now_local ();
 
                                                        g_key_file_set_value (priv->kfile, "ZAKCGI", "REMOTE_ADDR", g_getenv ("REMOTE_ADDR"));
-                                                       g_key_file_set_value (priv->kfile, "ZAKCGI", "TIMESTAMP", g_date_time_format (gdt, "%F %T"));
+                                                       g_key_file_set_value (priv->kfile, "ZAKCGI", "TIMESTAMP", g_date_time_format (gdt, "%FT%T"));
                                                        g_key_file_save_to_file (priv->kfile, g_file_get_path (priv->gfile), NULL);
 
                                                        g_date_time_unref (gdt);