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;
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)
{
gpointer key;
gpointer value;
- ht_env = zak_cgi_main_get_cookies (zakcgimain);
+ ht_env = _zak_cgi_main_get_cookies (zakcgimain);
str = g_string_new ("");
#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,
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);*/
g_free (env);
}
+ g_string_append (str, "\n</body>");
+
zak_cgi_main_out (NULL, str->str);
g_string_free (str, TRUE);