From: Andrea Zagli Date: Mon, 15 Jun 2015 19:54:26 +0000 (+0200) Subject: Added functions ZakCgiMain::out, ::get_env and ::dump_env. X-Git-Tag: v0.0.1~35 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=06b6aacf19f37b32063140fdaa8e39860e51ff89;p=libzakcgi Added functions ZakCgiMain::out, ::get_env and ::dump_env. --- diff --git a/.gitignore b/.gitignore index 8129d06..a46cc6c 100644 --- a/.gitignore +++ b/.gitignore @@ -50,3 +50,4 @@ intltool-* Rules-quot *.exe *.csv +tests/env diff --git a/autogen.sh b/autogen.sh old mode 100644 new mode 100755 diff --git a/src/main.c b/src/main.c index dca4f0d..ce7b54a 100644 --- a/src/main.c +++ b/src/main.c @@ -20,8 +20,12 @@ #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); @@ -85,6 +89,82 @@ ZakCgiMain return zak_cgi_main; } +/** + * zak_cgi_main_out: + * body: + * + */ +void +zak_cgi_main_out (const gchar *body) +{ + g_printf ("%s%c%c\n%s\n", + "Content-Type: text/html; charset=UTF-8", 13, 10, + body); +} + +/** + * zak_cgi_main_get_env: + * + * Returns: a #GHashTable with all the environment variables. + */ +GHashTable +*zak_cgi_main_get_env (void) +{ + GHashTable *ht; + + guint l; + guint i; + gchar **envs; + + ht = g_hash_table_new (g_str_hash, g_str_equal); + + l = g_strv_length (environ); + for (i = 0; i < l; i++) + { + envs = g_strsplit (environ[i], "=", 2); + g_hash_table_replace (ht, g_strdup (envs[0]), g_strdup (envs[1])); + g_strfreev (envs); + } + + return ht; +} + +gchar +*zak_cgi_main_dump_env () +{ + GHashTable *ht_env; + GHashTableIter iter; + + GString *str; + gchar *ret; + + gpointer key; + gpointer value; + + ht_env = zak_cgi_main_get_env (); + + str = g_string_new (""); + + 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"); + } + + ret = g_strdup (str->str); + g_string_free (str, TRUE); + + return ret; +} + /* PRIVATE */ static void zak_cgi_main_set_property (GObject *object, diff --git a/src/main.h b/src/main.h index 6672062..52602ab 100644 --- a/src/main.h +++ b/src/main.h @@ -51,6 +51,11 @@ GType zak_cgi_main_get_type (void); ZakCgiMain *zak_cgi_main_new (void); +void zak_cgi_main_out (const gchar *body); + +GHashTable *zak_cgi_main_get_env (void); +gchar *zak_cgi_main_dump_env (); + G_END_DECLS diff --git a/tests/Makefile.am b/tests/Makefile.am index c682487..a70c5c9 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -8,4 +8,5 @@ LIBS = $(ZAKCGI_LIBS) \ LDADD = $(top_builddir)/src/libzakcgi.la -noinst_PROGRAMS = +noinst_PROGRAMS = \ + env diff --git a/tests/env.c b/tests/env.c new file mode 100644 index 0000000..0a3e519 --- /dev/null +++ b/tests/env.c @@ -0,0 +1,41 @@ +/* + * 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[]) +{ + gchar *env; + GString *str; + + env = zak_cgi_main_dump_env (); + + str = g_string_new ("\n" + "Environment variables\n" + "\n"); + + g_string_append_printf (str, "%s\n", env); + g_free (env); + + zak_cgi_main_out (str->str); + g_string_free (str, TRUE); + + return 0; +} +