]> saetta.ns0.it Git - libzakcgi/commitdiff
Added ZakCgiMain::get_cookie and :cookes_foreach (refs #962).
authorAndrea Zagli <azagli@libero.it>
Sun, 9 Aug 2015 06:55:52 +0000 (08:55 +0200)
committerAndrea Zagli <azagli@libero.it>
Sun, 9 Aug 2015 06:55:52 +0000 (08:55 +0200)
src/main.c
src/main.h
tests/cookies.c
tests/env.c

index ec03e8da9041a4e37056a31acbb0c22d4a9926d0..dff6ff7920180d379bd1d5d5babe7ab2a7111b77 100644 (file)
@@ -274,22 +274,15 @@ gchar
        return ret;
 }
 
-/**
- * zak_cgi_main_get_cookies:
- * @zakcgimain:
- *
- * Returns: a #GHashTable with all the cookies.
- */
 GHashTable
-*zak_cgi_main_get_cookies (ZakCgiMain *zakcgimain)
+*_zak_cgi_main_get_cookies (ZakCgiMain *zakcgimain)
 {
        ZakCgiMainPrivate *priv;
        GHashTable *ht;
-       GHashTable *ht_env;
 
        guint l;
        guint i;
-       gchar *cookies;
+       const gchar *cookies;
        gchar **strv_cookies;
        gchar **parts;
        GValue *gval;
@@ -299,42 +292,105 @@ GHashTable
                        priv = ZAK_CGI_MAIN_GET_PRIVATE (zakcgimain);
                        if (priv->ht_cookies != NULL)
                                {
-                                       return g_hash_table_ref (priv->ht_cookies);
+                                       return priv->ht_cookies;
                                }
                }
        ht = g_hash_table_new (g_str_hash, g_str_equal);
        if (zakcgimain != NULL)
                {
-                       priv->ht_cookies = g_hash_table_ref (ht);
+                       priv->ht_cookies = ht;
                }
 
-       ht_env = _zak_cgi_main_get_env (zakcgimain);
-
-       cookies = g_hash_table_lookup (ht_env, "HTTP_COOKIE");
-       if (cookies != NULL)
+       gval = zak_cgi_main_get_env_field (zakcgimain, "HTTP_COOKIE");
+       if (gval != NULL)
                {
-                       strv_cookies = g_strsplit (cookies, ";", -1);
-                       l = g_strv_length (strv_cookies);
-                       for (i = 0; i < l; i++)
+                       cookies = g_value_get_string (gval);
+                       if (cookies != NULL)
                                {
-                                       parts = g_strsplit (strv_cookies[i], "=", 2);
-                                       gval = (GValue *)g_new0 (GValue, 1);
-                                       g_value_init (gval, G_TYPE_STRING);
-                                       g_value_take_string (gval, g_strstrip (g_strdup (parts[1])));
-                                       g_hash_table_replace (ht, g_strstrip (g_strdup (parts[0])), gval);
-                                       g_strfreev (parts);
+                                       strv_cookies = g_strsplit (cookies, ";", -1);
+                                       l = g_strv_length (strv_cookies);
+                                       for (i = 0; i < l; i++)
+                                               {
+                                                       parts = g_strsplit (strv_cookies[i], "=", 2);
+                                                       gval = (GValue *)g_new0 (GValue, 1);
+                                                       g_value_init (gval, G_TYPE_STRING);
+                                                       g_value_take_string (gval, g_strdup (parts[1]));
+                                                       g_hash_table_replace (ht, g_strdup (parts[0]), gval);
+                                                       g_strfreev (parts);
+                                               }
                                }
                }
 
        return ht;
 }
 
+/**
+ * zak_cgi_main_get_cookies:
+ * @zakcgimain:
+ *
+ * Returns: a #GHashTable with all the cookies.
+ */
+G_DEPRECATED_FOR (zak_cgi_main_get_cookie)
+GHashTable
+*zak_cgi_main_get_cookies (ZakCgiMain *zakcgimain)
+{
+       return _zak_cgi_main_get_cookies (zakcgimain);
+}
+
+/**
+ * zak_cgi_main_get_cookie:
+ * @zakcgimain:
+ * @cookie:
+ *
+ * Returns:
+ */
+GValue
+*zak_cgi_main_get_cookie (ZakCgiMain *zakcgimain, const gchar *cookie)
+{
+       ZakCgiMainPrivate *priv;
+
+       GHashTable *ht;
+
+       GValue *ret;
+
+       g_return_val_if_fail (ZAK_CGI_IS_MAIN (zakcgimain), NULL);
+
+       ht = _zak_cgi_main_get_cookies (zakcgimain);
+
+       ret = g_hash_table_lookup (ht, cookie);
+
+       return ret;
+}
+
+/**
+ * zak_cgi_main_cookies_foreach:
+ * @zakcgimain:
+ * @func:
+ * @user_data:
+ *
+ */
+void
+zak_cgi_main_cookies_foreach (ZakCgiMain *zakcgimain, GHFunc func, gpointer user_data)
+{
+       ZakCgiMainPrivate *priv;
+
+       GHashTable *ht;
+
+       g_return_if_fail (ZAK_CGI_IS_MAIN (zakcgimain));
+       g_return_if_fail (func != NULL);
+
+       ht = _zak_cgi_main_get_cookies (zakcgimain);
+
+       g_hash_table_foreach (ht, func, user_data);
+}
+
 /**
  * zak_cgi_main_dump_cookies:
  * @zakcgimain:
  *
  * Returns: an html table with each cookies.
  */
+G_DEPRECATED_FOR (zak_cgi_main_cookies_foreach)
 gchar
 *zak_cgi_main_dump_cookies (ZakCgiMain *zakcgimain)
 {
@@ -347,7 +403,7 @@ gchar
        gpointer key;
        gpointer value;
 
-       ht_env = zak_cgi_main_get_cookies (zakcgimain);
+       ht_env = _zak_cgi_main_get_cookies (zakcgimain);
 
        str = g_string_new ("");
 
index 332c27276b28fa57b1b15c18977b7d3f48035fe2..42d1f8f9a67a152afb16b124a030b81805d04aae 100644 (file)
@@ -62,6 +62,8 @@ void zak_cgi_main_env_foreach (ZakCgiMain *zakcgimain, GHFunc func, gpointer use
 gchar *zak_cgi_main_dump_env (ZakCgiMain *zakcgimain);
 
 GHashTable *zak_cgi_main_get_cookies (ZakCgiMain *zakcgimain);
+GValue *zak_cgi_main_get_cookie (ZakCgiMain *zakcgimain, const gchar *cookie);
+void zak_cgi_main_cookies_foreach (ZakCgiMain *zakcgimain, GHFunc func, gpointer user_data);
 gchar *zak_cgi_main_dump_cookies (ZakCgiMain *zakcgimain);
 
 gchar *zak_cgi_main_set_cookie (const gchar *name,
index 9843ed0fe611b636ac204f0628f0ecce8834c6c0..8d755c000fac3360d7428e9353ab9ddefe063a1b 100644 (file)
 
 #include <main.h>
 
+void
+ht_foreach (gpointer key,
+                       gpointer value,
+                       gpointer user_data)
+{
+       GString *str = (GString *)user_data;
+
+       g_string_append_printf (str, "<tr><td>%s</td><td>%s</td></tr>\n",
+                                                       (gchar *)key, g_value_get_string ((GValue *)value));
+}
+
 int
 main (int argc, char *argv[])
 {
        ZakCgiMain *zakcgimain;
-       gchar *env;
        GString *str;
        GString *header;
        GHashTable *ht;
 
        zakcgimain = zak_cgi_main_new ();
-       env = zak_cgi_main_dump_cookies (zakcgimain);
 
        str = g_string_new ("<html>\n"
                            "<head><title>Cookies</title></head>\n"
                            "<body>\n");
 
-       g_string_append_printf (str, "%s\n</body>", env);
-       g_free (env);
+       g_string_append_printf (str, "<table>\n");
+       zak_cgi_main_cookies_foreach (zakcgimain, ht_foreach, str);
+       g_string_append_printf (str, "</table>\n");
 
        header = g_string_new (ZAK_CGI_STANDARD_HEADER_HTML);
        g_string_append_printf (header,
index be407e48491c752b0969379044c84605254d2b00..ca2c521b2c20c7c30ebfe28231e34667403cbfcb 100644 (file)
@@ -70,24 +70,35 @@ gchar
        return ret;
 }
 
+void
+ht_foreach (gpointer key,
+                       gpointer value,
+                       gpointer user_data)
+{
+       GString *str = (GString *)user_data;
+
+       g_string_append_printf (str, "<tr><td>%s</td><td>%s</td></tr>\n",
+                                                       (gchar *)key, g_value_get_string ((GValue *)value));
+}
+
 int
 main (int argc, char *argv[])
 {
        ZakCgiMain *zakcgimain;
-       gchar *env;
        GString *str;
        GHashTable *ht;
+       gchar *env;
        gchar *ret;
 
        zakcgimain = zak_cgi_main_new ();
-       env = zak_cgi_main_dump_env (zakcgimain);
 
        str = g_string_new ("<html>\n"
                                                "<head><title>Environment variables</title></head>\n"
                                                "<body>\n");
 
-       g_string_append_printf (str, "%s\n</body>", env);
-       g_free (env);
+       g_string_append_printf (str, "<table>\n");
+       zak_cgi_main_env_foreach (zakcgimain, ht_foreach, str);
+       g_string_append_printf (str, "</table>\n");
 
        env = zak_cgi_main_get_stdin (zakcgimain);
        /*syslog (LOG_MAKEPRI(LOG_SYSLOG, LOG_DEBUG), "stdin: %s", env);*/
@@ -149,6 +160,8 @@ main (int argc, char *argv[])
                        g_free (env);
                }
 
+       g_string_append (str, "\n</body>");
+
        zak_cgi_main_out (NULL, str->str);
        g_string_free (str, TRUE);