From: Andrea Zagli Date: Sat, 26 Mar 2016 10:57:47 +0000 (+0100) Subject: In ZakCgiSession::set_value if value is null the value is unsetted (closes #1004). X-Git-Tag: v0.1.0~3^2~13 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=858d82fdd766c44430767a679be41a48a8dc1613;p=libzakcgi In ZakCgiSession::set_value if value is null the value is unsetted (closes #1004). --- diff --git a/src/session.c b/src/session.c index e10f1a0..67da1ac 100644 --- a/src/session.c +++ b/src/session.c @@ -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; diff --git a/tests/session.c b/tests/session.c index e2dec75..1469250 100644 --- a/tests/session.c +++ b/tests/session.c @@ -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.

"); + g_string_append (str, + "
\n" + "\n" + "
\n"); } g_free (method); }