From: Andrea Zagli Date: Sat, 27 Jun 2015 09:23:53 +0000 (+0200) Subject: Added functions ZakMain::get_cookies, ::dump_cookies and ::set:cookie. X-Git-Tag: v0.0.1~28 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=7af1b5a614e79aa9481102644bc7bfba9e098d9a;p=libzakcgi Added functions ZakMain::get_cookies, ::dump_cookies and ::set:cookie. --- diff --git a/.gitignore b/.gitignore index 4696acc..3db7834 100644 --- a/.gitignore +++ b/.gitignore @@ -50,6 +50,8 @@ intltool-* Rules-quot *.exe *.csv +tests/cookies tests/env tests/querystring tests/redirect +tests/session diff --git a/src/Makefile.am b/src/Makefile.am index eebe153..a15bb40 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,12 +6,14 @@ AM_CPPFLAGS = $(ZAKCGI_CFLAGS) \ lib_LTLIBRARIES = libzakcgi.la libzakcgi_la_SOURCES = main.c \ - url.c + session.c \ + url.c libzakcgi_la_LDFLAGS = -no-undefined libzakcgi_include_HEADERS = libzakcgi.h \ main.h \ + session.h \ url.h libzakcgi_includedir = $(includedir)/libzakcgi diff --git a/src/main.c b/src/main.c index c860ced..bcd981f 100644 --- a/src/main.c +++ b/src/main.c @@ -22,6 +22,7 @@ #include #include +#include #include @@ -98,11 +99,25 @@ ZakCgiMain * */ void -zak_cgi_main_out (const gchar *body) +zak_cgi_main_out (const gchar *header, const gchar *body) { + gchar *_header; + + if (header == NULL) + { + _header = g_strdup (ZAK_CGI_STANDARD_HEADER_HTML); + } + else + { + _header = g_strdup (header); + } + g_printf ("%s%c%c\n%s\n", - "Content-Type: text/html; charset=UTF-8", 13, 10, + _header, + 13, 10, body); + + g_free (_header); } /** @@ -140,7 +155,7 @@ GHashTable * Returns: an html table with each environment variables. */ gchar -*zak_cgi_main_dump_env () +*zak_cgi_main_dump_env (void) { GHashTable *ht_env; GHashTableIter iter; @@ -175,6 +190,126 @@ gchar return ret; } +/** + * zak_cgi_main_get_cookies: + * + * Returns: a #GHashTable with all the cookies. + */ +GHashTable +*zak_cgi_main_get_cookies (void) +{ + GHashTable *ht; + GHashTable *ht_env; + + guint l; + guint i; + gchar *cookies; + gchar **strv_cookies; + gchar **parts; + + ht = g_hash_table_new (g_str_hash, g_str_equal); + + ht_env = zak_cgi_main_get_env (); + + cookies = g_hash_table_lookup (ht_env, "HTTP_COOKIE"); + if (cookies != NULL) + { + strv_cookies = g_strsplit (cookies, ";", -1); + l = g_strv_length (strv_cookies); + for (i = 0; i < l; i++) + { + parts = g_strsplit (strv_cookies[i], "=", 2); + g_hash_table_replace (ht, g_strstrip (g_strdup (parts[0])), g_strstrip (g_strdup (parts[1]))); + g_strfreev (parts); + } + } + + return ht; +} + +/** + * zak_cgi_main_dump_cookies: + * + * Returns: an html table with each cookies. + */ +gchar +*zak_cgi_main_dump_cookies (void) +{ + GHashTable *ht_env; + GHashTableIter iter; + + GString *str; + gchar *ret; + + gpointer key; + gpointer value; + + ht_env = zak_cgi_main_get_cookies (); + + 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; +} + +/** + * zak_cgi_main_set_cookie: + * @name: + * @value: + * @expires: + * @domain: + * @path: + * @secure: + * @http_only: + * + * Returns: the string to set the cookie. + */ +gchar +*zak_cgi_main_set_cookie (const gchar *name, + const gchar *value, + GDateTime *expires, + const gchar *domain, + const gchar *path, + gboolean secure, + gboolean http_only) +{ + char *ret; + + char *cur = g_strdup (setlocale (LC_TIME, NULL)); + + setlocale (LC_NUMERIC, "C"); + + ret = g_strdup_printf ("Set-Cookie: %s=%s%s", + name, + value, + expires != NULL ? g_date_time_format (expires, "; expires=%a, %d-%b-%Y %H:%M:%S GMT") : "", + domain != NULL ? g_strdup_printf ("; domain=%s", domain) : "", + path != NULL ? g_strdup_printf ("; path=%s", path) : "", + secure ? g_strdup ("; secure") : "", + http_only ? g_strdup ("; httponly") : ""); + + setlocale (LC_TIME, cur); + g_free (cur); + + return ret; +} + /** * zak_cgi_main_get_parameters: * diff --git a/src/main.h b/src/main.h index 2e42f39..c3c6cde 100644 --- a/src/main.h +++ b/src/main.h @@ -49,12 +49,26 @@ struct _ZakCgiMainClass GType zak_cgi_main_get_type (void); +#define ZAK_CGI_STANDARD_HEADER_HTML "Content-Type: text/html; charset=UTF-8" + + ZakCgiMain *zak_cgi_main_new (void); -void zak_cgi_main_out (const gchar *body); +void zak_cgi_main_out (const gchar *header, const gchar *body); GHashTable *zak_cgi_main_get_env (void); -gchar *zak_cgi_main_dump_env (); +gchar *zak_cgi_main_dump_env (void); + +GHashTable *zak_cgi_main_get_cookies (void); +gchar *zak_cgi_main_dump_cookies (void); + +gchar *zak_cgi_main_set_cookie (const gchar *name, + const gchar *value, + GDateTime *expires, + const gchar *domain, + const gchar *path, + gboolean secure, + gboolean http_only); GHashTable *zak_cgi_main_get_parameters (void); diff --git a/src/session.c b/src/session.c new file mode 100644 index 0000000..de5fe4a --- /dev/null +++ b/src/session.c @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2015 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser 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 Lesser 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 + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include "session.h" + +static void zak_cgi_session_class_init (ZakCgiSessionClass *class); +static void zak_cgi_session_init (ZakCgiSession *zak_cgi_session); + +static void zak_cgi_session_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void zak_cgi_session_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void zak_cgi_session_dispose (GObject *gobject); +static void zak_cgi_session_finalize (GObject *gobject); + +#define ZAK_CGI_SESSION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_CGI_TYPE_SESSION, ZakCgiSessionPrivate)) + +typedef struct _ZakCgiSessionPrivate ZakCgiSessionPrivate; +struct _ZakCgiSessionPrivate + { + }; + +G_DEFINE_TYPE (ZakCgiSession, zak_cgi_session, G_TYPE_OBJECT) + +static void +zak_cgi_session_class_init (ZakCgiSessionClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + + object_class->set_property = zak_cgi_session_set_property; + object_class->get_property = zak_cgi_session_get_property; + object_class->dispose = zak_cgi_session_dispose; + object_class->finalize = zak_cgi_session_finalize; + + g_type_class_add_private (object_class, sizeof (ZakCgiSessionPrivate)); +} + +static void +zak_cgi_session_init (ZakCgiSession *zak_cgi_session) +{ + ZakCgiSessionPrivate *priv = ZAK_CGI_SESSION_GET_PRIVATE (zak_cgi_session); + +} + +/** + * zak_cgi_session_new: + * + * Returns: the newly created #ZakCgiSession object. + */ +ZakCgiSession +*zak_cgi_session_new (void) +{ + ZakCgiSession *zak_cgi_session; + ZakCgiSessionPrivate *priv; + + zak_cgi_session = ZAK_CGI_SESSION (g_object_new (zak_cgi_session_get_type (), NULL)); + + priv = ZAK_CGI_SESSION_GET_PRIVATE (zak_cgi_session); + + + return zak_cgi_session; +} + +/* PRIVATE */ +static void +zak_cgi_session_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + ZakCgiSession *zak_cgi_session = (ZakCgiSession *)object; + ZakCgiSessionPrivate *priv = ZAK_CGI_SESSION_GET_PRIVATE (zak_cgi_session); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_cgi_session_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + ZakCgiSession *zak_cgi_session = (ZakCgiSession *)object; + ZakCgiSessionPrivate *priv = ZAK_CGI_SESSION_GET_PRIVATE (zak_cgi_session); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_cgi_session_dispose (GObject *gobject) +{ + ZakCgiSession *zak_cgi_session = (ZakCgiSession *)gobject; + ZakCgiSessionPrivate *priv = ZAK_CGI_SESSION_GET_PRIVATE (zak_cgi_session); + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->dispose (gobject); +} + +static void +zak_cgi_session_finalize (GObject *gobject) +{ + ZakCgiSession *zak_cgi_session = (ZakCgiSession *)gobject; + ZakCgiSessionPrivate *priv = ZAK_CGI_SESSION_GET_PRIVATE (zak_cgi_session); + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->finalize (gobject); +} diff --git a/src/session.h b/src/session.h new file mode 100644 index 0000000..1beea2b --- /dev/null +++ b/src/session.h @@ -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 Lesser 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 Lesser 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 + */ + +#ifndef __ZAK_CGI_SESSION_H__ +#define __ZAK_CGI_SESSION_H__ + + +#include + + +G_BEGIN_DECLS + + +#define ZAK_CGI_TYPE_SESSION (zak_cgi_session_get_type ()) +#define ZAK_CGI_SESSION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ZAK_CGI_TYPE_SESSION, ZakCgiSession)) +#define ZAK_CGI_SESSION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ZAK_CGI_TYPE_SESSION, ZakCgiSessionClass)) +#define ZAK_CGI_IS_SESSION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ZAK_CGI_TYPE_SESSION)) +#define ZAK_CGI_IS_SESSION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ZAK_CGI_TYPE_SESSION)) +#define ZAK_CGI_SESSION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ZAK_CGI_TYPE_SESSION, ZakCgiSessionClass)) + +typedef struct _ZakCgiSession ZakCgiSession; +typedef struct _ZakCgiSessionClass ZakCgiSessionClass; + +struct _ZakCgiSession + { + GObject parent_instance; + }; + +struct _ZakCgiSessionClass + { + GObjectClass parent_class; + }; + +GType zak_cgi_session_get_type (void); + + +ZakCgiSession *zak_cgi_session_new (void); + + +G_END_DECLS + + +#endif /* __ZAK_CGI_SESSION_H__ */ diff --git a/tests/Makefile.am b/tests/Makefile.am index 704f382..7e12799 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -9,6 +9,8 @@ LIBS = $(ZAKCGI_LIBS) \ LDADD = $(top_builddir)/src/libzakcgi.la noinst_PROGRAMS = \ + cookies \ env \ querystring \ - redirect + redirect \ + session diff --git a/tests/cookies.c b/tests/cookies.c new file mode 100644 index 0000000..0cb3569 --- /dev/null +++ b/tests/cookies.c @@ -0,0 +1,54 @@ +/* + * 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 +#include + +#include + +#include + +int +main (int argc, char *argv[]) +{ + gchar *env; + GString *str; + GString *header; + GHashTable *ht; + + env = zak_cgi_main_dump_cookies (); + + str = g_string_new ("\n" + "Cookies\n" + "\n"); + + g_string_append_printf (str, "%s\n", env); + g_free (env); + + header = g_string_new (ZAK_CGI_STANDARD_HEADER_HTML); + g_string_append_printf (header, + "\n%s", + zak_cgi_main_set_cookie ("PRIMO", "ilvaloredelcookie1234 56 6 7 7 8", + g_date_time_add_months (g_date_time_new_now_utc (), 3), NULL, NULL, FALSE, FALSE)); + + zak_cgi_main_out (header->str, str->str); + g_string_free (str, TRUE); + + return 0; +} + diff --git a/tests/env.c b/tests/env.c index 6f5d512..d63504f 100644 --- a/tests/env.c +++ b/tests/env.c @@ -111,7 +111,7 @@ main (int argc, char *argv[]) g_free (env); } - zak_cgi_main_out (str->str); + zak_cgi_main_out (NULL, str->str); g_string_free (str, TRUE); return 0; diff --git a/tests/querystring.c b/tests/querystring.c index b0b472a..e433401 100644 --- a/tests/querystring.c +++ b/tests/querystring.c @@ -50,7 +50,7 @@ main (int argc, char *argv[]) g_string_append_printf (str, "\n"); - zak_cgi_main_out (str->str); + zak_cgi_main_out (NULL, str->str); g_string_free (str, TRUE); return 0; diff --git a/tests/session.c b/tests/session.c new file mode 100644 index 0000000..718a8ed --- /dev/null +++ b/tests/session.c @@ -0,0 +1,27 @@ +/* + * 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[]) +{ + + return 0; +} +