From c6535ce2a42449fefadb594d60d6cac87bae2830 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 9 Aug 2015 08:55:52 +0200 Subject: [PATCH] Added ZakCgiMain::get_cookie and :cookes_foreach (refs #962). --- src/main.c | 106 ++++++++++++++++++++++++++++++++++++------------ src/main.h | 2 + tests/cookies.c | 18 ++++++-- tests/env.c | 21 ++++++++-- 4 files changed, 114 insertions(+), 33 deletions(-) diff --git a/src/main.c b/src/main.c index ec03e8d..dff6ff7 100644 --- a/src/main.c +++ b/src/main.c @@ -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 (""); diff --git a/src/main.h b/src/main.h index 332c272..42d1f8f 100644 --- a/src/main.h +++ b/src/main.h @@ -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, diff --git a/tests/cookies.c b/tests/cookies.c index 9843ed0..8d755c0 100644 --- a/tests/cookies.c +++ b/tests/cookies.c @@ -23,24 +23,34 @@ #include +void +ht_foreach (gpointer key, + gpointer value, + gpointer user_data) +{ + GString *str = (GString *)user_data; + + g_string_append_printf (str, "%s%s\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 ("\n" "Cookies\n" "\n"); - g_string_append_printf (str, "%s\n", env); - g_free (env); + g_string_append_printf (str, "\n"); + zak_cgi_main_cookies_foreach (zakcgimain, ht_foreach, str); + g_string_append_printf (str, "
\n"); header = g_string_new (ZAK_CGI_STANDARD_HEADER_HTML); g_string_append_printf (header, diff --git a/tests/env.c b/tests/env.c index be407e4..ca2c521 100644 --- a/tests/env.c +++ b/tests/env.c @@ -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, "%s%s\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 ("\n" "Environment variables\n" "\n"); - g_string_append_printf (str, "%s\n", env); - g_free (env); + g_string_append_printf (str, "\n"); + zak_cgi_main_env_foreach (zakcgimain, ht_foreach, str); + g_string_append_printf (str, "
\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"); + zak_cgi_main_out (NULL, str->str); g_string_free (str, TRUE); -- 2.49.0