From 42674ea1581864998d82cafaab12ab08accd838b Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Mon, 15 Jun 2015 22:14:16 +0200 Subject: [PATCH] Added function ZakCgiMain::get_parameters. --- .gitignore | 1 + src/main.c | 38 +++++++++++++++++++++++++---- src/main.h | 2 ++ tests/Makefile.am | 3 ++- tests/querystring.c | 58 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 tests/querystring.c diff --git a/.gitignore b/.gitignore index a46cc6c..0eae689 100644 --- a/.gitignore +++ b/.gitignore @@ -51,3 +51,4 @@ Rules-quot *.exe *.csv tests/env +tests/querystring diff --git a/src/main.c b/src/main.c index ce7b54a..dde008e 100644 --- a/src/main.c +++ b/src/main.c @@ -20,12 +20,8 @@ #include #endif -#include - #include "main.h" -extern char **environ; - static void zak_cgi_main_class_init (ZakCgiMainClass *class); static void zak_cgi_main_init (ZakCgiMain *zak_cgi_main); @@ -112,12 +108,14 @@ GHashTable { GHashTable *ht; + gchar **environ; guint l; guint i; gchar **envs; ht = g_hash_table_new (g_str_hash, g_str_equal); + environ = g_get_environ (); l = g_strv_length (environ); for (i = 0; i < l; i++) { @@ -165,6 +163,38 @@ gchar return ret; } +/** + * zak_cgi_main_get_parameters: + * + * Returns: + */ +GHashTable +*zak_cgi_main_get_parameters (void) +{ + GHashTable *ht; + + const gchar *qstring; + + gchar **params; + gchar **parts; + guint i; + guint l; + + ht = g_hash_table_new (g_str_hash, g_str_equal); + + qstring = g_getenv ("QUERY_STRING"); + params = g_strsplit (qstring, "&", -1); + l = g_strv_length (params); + for (i = 0; i < l; i++) + { + parts = g_strsplit (params[i], "=", 2); + g_hash_table_replace (ht, g_strdup (parts[0]), g_strdup (parts[1])); + g_strfreev (parts); + } + + return ht; +} + /* PRIVATE */ static void zak_cgi_main_set_property (GObject *object, diff --git a/src/main.h b/src/main.h index 52602ab..69a3c29 100644 --- a/src/main.h +++ b/src/main.h @@ -56,6 +56,8 @@ void zak_cgi_main_out (const gchar *body); GHashTable *zak_cgi_main_get_env (void); gchar *zak_cgi_main_dump_env (); +GHashTable *zak_cgi_main_get_parameters (void); + G_END_DECLS diff --git a/tests/Makefile.am b/tests/Makefile.am index a70c5c9..dbdd3e9 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -9,4 +9,5 @@ LIBS = $(ZAKCGI_LIBS) \ LDADD = $(top_builddir)/src/libzakcgi.la noinst_PROGRAMS = \ - env + env \ + querystring diff --git a/tests/querystring.c b/tests/querystring.c new file mode 100644 index 0000000..b0b472a --- /dev/null +++ b/tests/querystring.c @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2015 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +int +main (int argc, char *argv[]) +{ + GHashTable *ht_env; + GString *str; + + GHashTableIter iter; + gpointer key; + gpointer value; + + ht_env = zak_cgi_main_get_parameters (); + + str = g_string_new ("\n" + "Query string\n" + "\n"); + + if (g_hash_table_size (ht_env) > 0) + { + g_string_append_printf (str, "\n"); + + g_hash_table_iter_init (&iter, ht_env); + while (g_hash_table_iter_next (&iter, &key, &value)) + { + g_string_append_printf (str, "\n", + (gchar *)key, (gchar *)value); + } + + g_string_append_printf (str, "
%s%s
\n"); + } + + g_string_append_printf (str, "\n"); + + zak_cgi_main_out (str->str); + g_string_free (str, TRUE); + + return 0; +} + -- 2.49.0