]> saetta.ns0.it Git - libzakcgi/commitdiff
Added ZakCgiMain::get_parameter and ::parameters_foreach (refs #962).
authorAndrea Zagli <azagli@libero.it>
Sun, 9 Aug 2015 08:37:42 +0000 (10:37 +0200)
committerAndrea Zagli <azagli@libero.it>
Sun, 9 Aug 2015 08:37:42 +0000 (10:37 +0200)
src/main.c
src/main.h
tests/querystring.c

index dff6ff7920180d379bd1d5d5babe7ab2a7111b77..911bfb189c1682b47fdd69377879ead0ca964cec 100644 (file)
@@ -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)
                {
index 42d1f8f9a67a152afb16b124a030b81805d04aae..375894426955edb8883be15a532a0823cdde28c4 100644 (file)
@@ -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;
index c5d680ae36ce99a138ce9267e0db610ab6990d1f..7316cb13347aca711b19d8736a84996b02c3972f 100644 (file)
 
 #include <main.h>
 
+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,
+                                                                                       "<tr><td>%s[%d]</td><td>%s</td></tr>\n",
+                                                                                       (gchar *)key,
+                                                                                       i,
+                                                                                       (gchar *)g_ptr_array_index (ar, i));
+                               }
+               }
+       else
+               {
+                       g_string_append_printf (str,
+                                                                       "<tr><td>%s</td><td>%s</td></tr>\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 ("<html>\n"
                            "<head><title>Query string</title></head>\n"
                            "<body>\n");
 
-       if (g_hash_table_size (ht_env) > 0)
-               {
-                       g_string_append_printf (str, "<table>\n");
+       g_string_append_printf (str, "<table>\n");
 
-                       g_string_append_printf (str,
-                                                                       "<tr><td>IS GET?</td><td>%s</td></tr>\n",
-                                                                       zak_cgi_main_is_get (NULL) ? "TRUE" : "FALSE");
+       g_string_append_printf (str,
+                                                       "<tr><td>IS GET?</td><td>%s</td></tr>\n",
+                                                       zak_cgi_main_is_get (NULL) ? "TRUE" : "FALSE");
 
-                       g_string_append_printf (str,
-                                                                       "<tr><td>IS POST?</td><td>%s</td></tr>\n",
-                                                                       zak_cgi_main_is_post (NULL) ? "TRUE" : "FALSE");
+       g_string_append_printf (str,
+                                                       "<tr><td>IS POST?</td><td>%s</td></tr>\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,
-                                                                                                                       "<tr><td>%s[%d]</td><td>%s</td></tr>\n",
-                                                                                                                       (gchar *)key,
-                                                                                                                       i,
-                                                                                                                       (gchar *)g_ptr_array_index (ar, i));
-                                                               }
-                                               }
-                                       else
-                                               {
-                                                       g_string_append_printf (str,
-                                                                                                       "<tr><td>%s</td><td>%s</td></tr>\n",
-                                                                                                       (gchar *)key,
-                                                                                                       (gchar *)g_value_get_string ((GValue *)value));
-                                               }
-                               }
+       zak_cgi_main_parameters_foreach (main, ht_foreach, str);
 
-                       g_string_append_printf (str, "</table>\n");
-               }
+       g_string_append_printf (str, "</table>\n");
 
        g_string_append_printf (str, "</body>\n");