]> saetta.ns0.it Git - libzakcgi/commitdiff
In ZakCgiSession::set_value if value is null the value is unsetted (closes #1004).
authorAndrea Zagli <azagli@libero.it>
Sat, 26 Mar 2016 10:57:47 +0000 (11:57 +0100)
committerAndrea Zagli <azagli@libero.it>
Sat, 26 Mar 2016 10:57:47 +0000 (11:57 +0100)
src/session.c
tests/session.c

index e10f1a0416ce1848ffa23288076b90d974414560..67da1acf62711d71c78689eff6066f69bd222dc6 100644 (file)
@@ -301,13 +301,27 @@ zak_cgi_session_set_value (ZakCgiSession *session, const gchar *name, const gcha
 
        if (priv->kfile != NULL)
                {
-                       g_key_file_set_value (priv->kfile, "SESSION", name, value);
+                       if (value == NULL)
+                               {
+                                       error = NULL;
+                                       if (!g_key_file_remove_key (priv->kfile, "SESSION", name, &error)
+                                               || error != NULL)
+                                               {
+                                                       g_warning ("Unable to unset key «%s»: %s.",
+                                                                          name,
+                                                                          error != NULL && error->message != NULL ? error->message : "no details");
+                                               }
+                               }
+                       else
+                               {
+                                       g_key_file_set_value (priv->kfile, "SESSION", name, value);
+                               }
 
                        error = NULL;
                        if (!g_key_file_save_to_file (priv->kfile, g_file_get_path (priv->gfile), &error)
                                || error != NULL)
                                {
-                                       g_warning ("Unable to write value tosession file: %s.",
+                                       g_warning ("Unable to write session file: %s.",
                                                           error != NULL && error->message != NULL ? error->message : "no details");
                                }
                }
@@ -325,12 +339,21 @@ gchar
 {
        gchar *ret;
 
+       GError *error;
+
        ZakCgiSessionPrivate *priv = ZAK_CGI_SESSION_GET_PRIVATE (session);
 
        ret = NULL;
        if (priv->kfile != NULL)
                {
-                       ret = g_key_file_get_value (priv->kfile, "SESSION", name, NULL);
+                       error = NULL;
+                       ret = g_key_file_get_value (priv->kfile, "SESSION", name, &error);
+                       if (error != NULL)
+                               {
+                                       g_warning ("Unable to get session value «%s»: %s",
+                                                          name,
+                                                          error->message != NULL ? error->message : "noo details.");
+                               }
                }
 
        return ret;
index e2dec75cb1e7982c92fa093ab1216001d474d1e3..146925044d3100f1f0e196a92cd176756db1ddbc 100644 (file)
@@ -73,8 +73,14 @@ main (int argc, char *argv[])
                                                        env = zak_cgi_main_get_stdin (zakcgimain);
 
                                                        ht_stdin = zak_cgi_main_parse_stdin (env, boundary[1]);
-
-                                                       zak_cgi_session_set_value (session, "user_name", (gchar *)g_value_get_string ((GValue *)g_hash_table_lookup (ht_stdin, "user")));
+                                                       if (g_hash_table_lookup (ht_stdin, "reset") != NULL)
+                                                               {
+                                                                       zak_cgi_session_set_value (session, "user_name", NULL);
+                                                               }
+                                                       else
+                                                               {
+                                                                       zak_cgi_session_set_value (session, "user_name", (gchar *)g_value_get_string ((GValue *)g_hash_table_lookup (ht_stdin, "user")));
+                                                               }
 
                                                        g_free (env);
                                                        g_strfreev (boundary);
@@ -108,7 +114,11 @@ main (int argc, char *argv[])
                                }
                        else
                                {
-                                       g_string_append (str, ", on the second page.");
+                                       g_string_append (str, ", on the second page.<br/><br/>");
+                                       g_string_append (str,
+                                                        "<form action=\"/cgi-bin/session\" method=\"post\" enctype=\"multipart/form-data\">\n"
+                                                        "<input type=\"submit\" name=\"reset\" value=\"Reset\" />\n"
+                                                        "</form>\n");
                                }
                        g_free (method);
                }