]> saetta.ns0.it Git - libzakcgi/commitdiff
Added ZakCgiMain::get_stdin_field and ::stdin_foreach (refs #962).
authorAndrea Zagli <azagli@libero.it>
Sun, 9 Aug 2015 09:02:48 +0000 (11:02 +0200)
committerAndrea Zagli <azagli@libero.it>
Sun, 9 Aug 2015 09:02:48 +0000 (11:02 +0200)
src/main.c
src/main.h
tests/env.c

index 911bfb189c1682b47fdd69377879ead0ca964cec..3bc5bc7b0a7a6eefe42fd2f6b96a58c561fd2a01 100644 (file)
@@ -720,7 +720,7 @@ gchar
                        priv = ZAK_CGI_MAIN_GET_PRIVATE (zakcgimain);
                        if (priv->stdin != NULL)
                                {
-                                       return g_strdup (priv->stdin);
+                                       return priv->stdin;
                                }
                }
 
@@ -816,13 +816,8 @@ zak_cgi_file_free (ZakCgiFile *file)
 G_DEFINE_BOXED_TYPE (ZakCgiFile, zak_cgi_file, zak_cgi_file_copy, zak_cgi_file_free)
 
 
-/**
- * zak_cgi_main_parse_stdin:
- *
- * Returns:
- */
-GHashTable
-*zak_cgi_main_parse_stdin (const gchar *buf, const gchar *boundary)
+static GHashTable
+*_zak_cgi_main_parse_stdin (const gchar *buf, const gchar *boundary)
 {
        GHashTable *ht;
 
@@ -1057,6 +1052,64 @@ GHashTable
        return ht;
 }
 
+/**
+ * zak_cgi_main_parse_stdin:
+ *
+ * Returns:
+ */
+GHashTable
+*zak_cgi_main_parse_stdin (const gchar *buf, const gchar *boundary)
+{
+       return _zak_cgi_main_parse_stdin (buf, boundary);
+}
+
+/**
+ * zak_cgi_main_get_stdin_field:
+ * @zakcgimain:
+ * @field:
+ *
+ * Returns:
+ */
+GValue
+*zak_cgi_main_get_stdin_field (ZakCgiMain *zakcgimain, const gchar *field)
+{
+       ZakCgiMainPrivate *priv;
+
+       GHashTable *ht;
+
+       GValue *ret;
+
+       g_return_val_if_fail (ZAK_CGI_IS_MAIN (zakcgimain), NULL);
+
+       ht = _zak_cgi_main_parse_stdin (zak_cgi_main_get_stdin (zakcgimain), NULL);
+
+       ret = g_hash_table_lookup (ht, field);
+
+       return ret;
+}
+
+/**
+ * zak_cgi_main_stdin_foreach:
+ * @zakcgimain:
+ * @func:
+ * @user_data:
+ *
+ */
+void
+zak_cgi_main_stdin_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_parse_stdin (zak_cgi_main_get_stdin (zakcgimain), NULL);
+
+       g_hash_table_foreach (ht, func, user_data);
+}
+
 void
 zak_cgi_main_redirect (ZakCgiMain *zakcgimain, const gchar *url)
 {
@@ -1116,45 +1169,55 @@ zak_cgi_main_redirect (ZakCgiMain *zakcgimain, const gchar *url)
        g_string_free (_url, TRUE);
 }
 
+/**
+ * zak_cgi_main_is_request_method:
+ * @zakcgimain:
+ * @method:
+ *
+ * Returns:
+ */
 gboolean
 zak_cgi_main_is_request_method (ZakCgiMain *zakcgimain, const gchar *method)
 {
        gboolean ret;
 
-       GHashTable *ht;
-
-       gchar *param;
+       GValue *gval;
+       const gchar *param;
 
        ret = FALSE;
 
-       ht = _zak_cgi_main_get_env (zakcgimain);
-       param = (gchar *)g_hash_table_lookup (ht, "REQUEST_METHOD");
-       if (param != NULL)
+       gval = zak_cgi_main_get_env_field (zakcgimain, "REQUEST_METHOD");
+       if (gval != NULL)
                {
+                       param = g_value_get_string (gval);
                        ret = (g_strcmp0 (param, method) == 0);
                }
 
        return ret;
 }
 
+/**
+ * zak_cgi_main_is_get:
+ * @zakcgimain:
+ *
+ * Returns:
+ */
 gboolean
 zak_cgi_main_is_get (ZakCgiMain *zakcgimain)
 {
-       gboolean ret;
-
-       ret = zak_cgi_main_is_request_method (zakcgimain, "GET");
-
-       return ret;
+       return zak_cgi_main_is_request_method (zakcgimain, "GET");
 }
 
+/**
+ * zak_cgi_main_is_post:
+ * @zakcgimain:
+ *
+ * Returns:
+ */
 gboolean
 zak_cgi_main_is_post (ZakCgiMain *zakcgimain)
 {
-       gboolean ret;
-
-       ret = zak_cgi_main_is_request_method (zakcgimain, "POST");
-
-       return ret;
+       return zak_cgi_main_is_request_method (zakcgimain, "POST");
 }
 
 /* PRIVATE */
index 375894426955edb8883be15a532a0823cdde28c4..10e6b3b7c188499b31d75a9a6f06f03f4bd86e1a 100644 (file)
@@ -96,6 +96,8 @@ GType zak_cgi_file_get_type ();
 gchar *zak_cgi_main_get_stdin (ZakCgiMain *zakcgimain);
 
 GHashTable *zak_cgi_main_parse_stdin (const gchar *buf, const gchar *boundary);
+GValue *zak_cgi_main_get_stdin_field (ZakCgiMain *zakcgimain, const gchar *field);
+void zak_cgi_main_stdin_foreach (ZakCgiMain *zakcgimain, GHFunc func, gpointer user_data);
 
 void zak_cgi_main_redirect (ZakCgiMain *zakcgimain, const gchar *url);
 
index ca2c521b2c20c7c30ebfe28231e34667403cbfcb..6c726c7f1f24b7d20450b6e13510171bb17a7ab5 100644 (file)
@@ -81,6 +81,38 @@ ht_foreach (gpointer key,
                                                        (gchar *)key, g_value_get_string ((GValue *)value));
 }
 
+void
+ht_foreach_stdin (gpointer key,
+                       gpointer value,
+                       gpointer user_data)
+{
+       gchar *ret;
+
+       GString *str = (GString *)user_data;
+
+       if (G_VALUE_HOLDS (value, G_TYPE_PTR_ARRAY))
+               {
+                       guint i;
+                       GPtrArray *ar = (GPtrArray *)g_value_get_boxed (value);
+                       for (i = 0; i < ar->len; i++)
+                               {
+                                       ret = get_value (g_ptr_array_index (ar, i));
+                                       g_string_append_printf (str,
+                                                                                       "<tr><td>%s[%d]</td><td>%s</td></tr>\n",
+                                                                                       (gchar *)key, i, ret);
+                                       g_free (ret);
+                               }
+               }
+       else
+               {
+                       ret = get_value ((GValue *)value);
+                       g_string_append_printf (str,
+                                                                       "<tr><td>%s</td><td>%s</td></tr>\n",
+                                                                       (gchar *)key, ret);
+                       g_free (ret);
+               }
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -104,60 +136,24 @@ main (int argc, char *argv[])
        /*syslog (LOG_MAKEPRI(LOG_SYSLOG, LOG_DEBUG), "stdin: %s", env);*/
        if (env != NULL)
                {
-                       GHashTableIter iter;
-
-                       gpointer key;
-                       gpointer value;
-
                        g_string_append_printf (str,
                                                                        "<br/><hr/>\n"
                                                                        "%s",
                                                                        env);
 
-                       ht = zak_cgi_main_parse_stdin (env, NULL);
+                       g_string_append_printf (str, "<br/><hr/>\n<table>\n");
 
-                       if (g_hash_table_size (ht) > 0)
-                               {
-                                       g_string_append_printf (str, "<br/><hr/>\n<table>\n");
+                       g_string_append_printf (str,
+                                                                       "<tr><td>IS GET?</td><td>%s</td></tr>\n",
+                                                                       zak_cgi_main_is_get (zakcgimain) ? "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 (zakcgimain) ? "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);
-                                       while (g_hash_table_iter_next (&iter, &key, &value))
-                                               {
-                                                       if (G_VALUE_HOLDS (value, G_TYPE_PTR_ARRAY))
-                                                               {
-                                                                       guint i;
-                                                                       GPtrArray *ar = (GPtrArray *)g_value_get_boxed (value);
-                                                                       for (i = 0; i < ar->len; i++)
-                                                                               {
-                                                                                       ret = get_value (g_ptr_array_index (ar, i));
-                                                                                       g_string_append_printf (str,
-                                                                                                                                       "<tr><td>%s[%d]</td><td>%s</td></tr>\n",
-                                                                                                                                       (gchar *)key, i, ret);
-                                                                                       g_free (ret);
-                                                                               }
-                                                               }
-                                                       else
-                                                               {
-                                                                       ret = get_value ((GValue *)value);
-                                                                       g_string_append_printf (str,
-                                                                                                                       "<tr><td>%s</td><td>%s</td></tr>\n",
-                                                                                                                       (gchar *)key, ret);
-                                                                       g_free (ret);
-                                                               }
-                                               }
-
-                                       g_string_append_printf (str, "</table>\n");
-                               }
+                       zak_cgi_main_stdin_foreach (zakcgimain, ht_foreach_stdin, str);
 
-                       g_free (env);
+                       g_string_append_printf (str, "</table>\n");
                }
 
        g_string_append (str, "\n</body>");