From 300bde7a4e7e7d2fe7fb325c7e49e13af0e74195 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 9 Aug 2015 11:02:48 +0200 Subject: [PATCH] Added ZakCgiMain::get_stdin_field and ::stdin_foreach (refs #962). --- src/main.c | 111 ++++++++++++++++++++++++++++++++++++++++------------ src/main.h | 2 + tests/env.c | 86 +++++++++++++++++++--------------------- 3 files changed, 130 insertions(+), 69 deletions(-) diff --git a/src/main.c b/src/main.c index 911bfb1..3bc5bc7 100644 --- a/src/main.c +++ b/src/main.c @@ -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 */ diff --git a/src/main.h b/src/main.h index 3758944..10e6b3b 100644 --- a/src/main.h +++ b/src/main.h @@ -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); diff --git a/tests/env.c b/tests/env.c index ca2c521..6c726c7 100644 --- a/tests/env.c +++ b/tests/env.c @@ -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, + "%s[%d]%s\n", + (gchar *)key, i, ret); + g_free (ret); + } + } + else + { + ret = get_value ((GValue *)value); + g_string_append_printf (str, + "%s%s\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, "

\n" "%s", env); - ht = zak_cgi_main_parse_stdin (env, NULL); + g_string_append_printf (str, "

\n\n"); - if (g_hash_table_size (ht) > 0) - { - g_string_append_printf (str, "

\n
\n"); + g_string_append_printf (str, + "\n", + zak_cgi_main_is_get (zakcgimain) ? "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 (zakcgimain) ? "TRUE" : "FALSE"); - g_string_append_printf (str, - "\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, - "\n", - (gchar *)key, i, ret); - g_free (ret); - } - } - else - { - ret = get_value ((GValue *)value); - g_string_append_printf (str, - "\n", - (gchar *)key, ret); - g_free (ret); - } - } - - g_string_append_printf (str, "
IS GET?%s
IS GET?%s
IS POST?%s
IS POST?%s
%s[%d]%s
%s%s
\n"); - } + zak_cgi_main_stdin_foreach (zakcgimain, ht_foreach_stdin, str); - g_free (env); + g_string_append_printf (str, "\n"); } g_string_append (str, "\n"); -- 2.49.0