From 50f4f018e64f995e9223fafd7d10cfc6fc4f9d2e Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 9 Aug 2015 10:37:42 +0200 Subject: [PATCH] Added ZakCgiMain::get_parameter and ::parameters_foreach (refs #962). --- src/main.c | 76 +++++++++++++++++++++++++++++++++++------ src/main.h | 2 ++ tests/querystring.c | 83 +++++++++++++++++++++------------------------ 3 files changed, 105 insertions(+), 56 deletions(-) diff --git a/src/main.c b/src/main.c index dff6ff7..911bfb1 100644 --- a/src/main.c +++ b/src/main.c @@ -274,7 +274,7 @@ gchar return ret; } -GHashTable +static GHashTable *_zak_cgi_main_get_cookies (ZakCgiMain *zakcgimain) { ZakCgiMainPrivate *priv; @@ -516,15 +516,8 @@ form_decode (char *part) } /* end from libsoup */ -/** - * zak_cgi_main_get_parameters: - * @zakcgimain: - * @query_string: - * - * Returns: - */ -GHashTable -*zak_cgi_main_get_parameters (ZakCgiMain *zakcgimain, const gchar *query_string) +static GHashTable +*_zak_cgi_main_get_parameters (ZakCgiMain *zakcgimain, const gchar *query_string) { ZakCgiMainPrivate *priv; GHashTable *ht; @@ -640,6 +633,67 @@ GHashTable return ht; } +/** + * zak_cgi_main_get_parameters: + * @zakcgimain: + * @query_string: + * + * Returns: + */ +G_DEPRECATED_FOR (zak_cgi_main_get_parameter) +GHashTable +*zak_cgi_main_get_parameters (ZakCgiMain *zakcgimain, const gchar *query_string) +{ + return _zak_cgi_main_get_parameters (zakcgimain, query_string); +} + +/** + * zak_cgi_main_get_param: + * @zakcgimain: + * @param: + * + * Returns: + */ +GValue +*zak_cgi_main_get_param (ZakCgiMain *zakcgimain, const gchar *param) +{ + ZakCgiMainPrivate *priv; + + GHashTable *ht; + + GValue *ret; + + g_return_val_if_fail (ZAK_CGI_IS_MAIN (zakcgimain), NULL); + + ht = _zak_cgi_main_get_parameters (zakcgimain, NULL); + + ret = g_hash_table_lookup (ht, param); + + return ret; +} + +/** + * zak_cgi_main_parameters_foreach: + * @zakcgimain: + * @func: + * @user_data: + * + */ +void +zak_cgi_main_parameters_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_parameters (zakcgimain, NULL); + + g_hash_table_foreach (ht, func, user_data); +} + /** * zak_cgi_main_get_stdin: * @zakcgimain: @@ -811,7 +865,7 @@ GHashTable if (g_strcmp0 (content_type, "") == 0 || g_strcmp0 (splitted[0], "application/x-www-form-urlencoded") == 0) { - return zak_cgi_main_get_parameters (NULL, buf); + return _zak_cgi_main_get_parameters (NULL, buf); } else if (g_strcmp0 (splitted[0], "multipart/form-data") == 0) { diff --git a/src/main.h b/src/main.h index 42d1f8f..3758944 100644 --- a/src/main.h +++ b/src/main.h @@ -75,6 +75,8 @@ gchar *zak_cgi_main_set_cookie (const gchar *name, gboolean http_only); GHashTable *zak_cgi_main_get_parameters (ZakCgiMain *zakcgimain, const gchar *query_string); +gchar *zak_cgi_main_get_parameter (ZakCgiMain *zakcgimain, const gchar *param); +void zak_cgi_main_parameters_foreach (ZakCgiMain *zakcgimain, GHFunc func, gpointer user_data); typedef struct _ZakCgiFile ZakCgiFile; diff --git a/tests/querystring.c b/tests/querystring.c index c5d680a..7316cb1 100644 --- a/tests/querystring.c +++ b/tests/querystring.c @@ -18,68 +18,61 @@ #include +void +ht_foreach (gpointer key, + gpointer value, + gpointer user_data) +{ + GString *str = (GString *)user_data; + + if (G_VALUE_HOLDS ((GValue *)value, G_TYPE_BOXED)) + { + guint i; + GPtrArray *ar = (GPtrArray *)g_value_get_boxed ((GValue *)value); + for (i = 0; i < ar->len; i++) + { + g_string_append_printf (str, + "%s[%d]%s\n", + (gchar *)key, + i, + (gchar *)g_ptr_array_index (ar, i)); + } + } + else + { + g_string_append_printf (str, + "%s%s\n", + (gchar *)key, + (gchar *)g_value_get_string ((GValue *)value)); + } +} + int main (int argc, char *argv[]) { - GHashTable *ht_env; GString *str; - GHashTableIter iter; - gpointer key; - gpointer value; - ZakCgiMain *main; main = zak_cgi_main_new (); - ht_env = zak_cgi_main_get_parameters (main, NULL); - - /* test #960 */ - ht_env = zak_cgi_main_get_parameters (main, NULL); - str = g_string_new ("\n" "Query string\n" "\n"); - if (g_hash_table_size (ht_env) > 0) - { - g_string_append_printf (str, "\n"); + g_string_append_printf (str, "
\n"); - g_string_append_printf (str, - "\n", - zak_cgi_main_is_get (NULL) ? "TRUE" : "FALSE"); + g_string_append_printf (str, + "\n", + zak_cgi_main_is_get (NULL) ? "TRUE" : "FALSE"); - g_string_append_printf (str, - "\n", - zak_cgi_main_is_post (NULL) ? "TRUE" : "FALSE"); + g_string_append_printf (str, + "\n", + zak_cgi_main_is_post (NULL) ? "TRUE" : "FALSE"); - g_hash_table_iter_init (&iter, ht_env); - while (g_hash_table_iter_next (&iter, &key, &value)) - { - if (G_VALUE_HOLDS ((GValue *)value, G_TYPE_BOXED)) - { - guint i; - GPtrArray *ar = (GPtrArray *)g_value_get_boxed ((GValue *)value); - for (i = 0; i < ar->len; i++) - { - g_string_append_printf (str, - "\n", - (gchar *)key, - i, - (gchar *)g_ptr_array_index (ar, i)); - } - } - else - { - g_string_append_printf (str, - "\n", - (gchar *)key, - (gchar *)g_value_get_string ((GValue *)value)); - } - } + zak_cgi_main_parameters_foreach (main, ht_foreach, str); - g_string_append_printf (str, "
IS GET?%s
IS GET?%s
IS POST?%s
IS POST?%s
%s[%d]%s
%s%s
\n"); - } + g_string_append_printf (str, "\n"); g_string_append_printf (str, "\n"); -- 2.49.0