From 5d5239ef647b3867c425951db7e44edc6df0ede3 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 27 Jun 2015 15:05:26 +0200 Subject: [PATCH] Implemented session. --- configure.ac | 6 +- src/libzakcgi.h | 3 +- src/session.c | 135 ++++++++++++++++++++++++++++++++++++++++ src/session.h | 5 ++ tests/cookies.c | 3 + tests/provapostcgi.html | 13 ++++ tests/session.c | 86 +++++++++++++++++++++++++ 7 files changed, 247 insertions(+), 4 deletions(-) create mode 100644 tests/provapostcgi.html diff --git a/configure.ac b/configure.ac index 18a42b6..e8bb6e5 100644 --- a/configure.ac +++ b/configure.ac @@ -31,9 +31,9 @@ AC_PROG_RANLIB # Checks for libraries. PKG_CHECK_MODULES(ZAKCGI, [glib-2.0 >= 2.36 - gobject-2.0 >= 2.36 - gio-2.0 >= 2.36 - gio-unix-2.0 >= 2.36]) + gobject-2.0 >= 2.36 + gio-2.0 >= 2.36 + gio-unix-2.0 >= 2.36]) AC_SUBST(ZAKCGI_CFLAGS) AC_SUBST(ZAKCGI_LIBS) diff --git a/src/libzakcgi.h b/src/libzakcgi.h index c580f68..4f2523e 100644 --- a/src/libzakcgi.h +++ b/src/libzakcgi.h @@ -19,6 +19,7 @@ #ifndef __LIBZAKCGI_H__ #define __LIBZAKCGI_H__ -#include +#include +#include #endif /* __LIBZAKCGI_H__ */ diff --git a/src/session.c b/src/session.c index de5fe4a..e0a617e 100644 --- a/src/session.c +++ b/src/session.c @@ -20,6 +20,11 @@ #include #endif +#include + +#include + +#include "main.h" #include "session.h" static void zak_cgi_session_class_init (ZakCgiSessionClass *class); @@ -42,6 +47,9 @@ static void zak_cgi_session_finalize (GObject *gobject); typedef struct _ZakCgiSessionPrivate ZakCgiSessionPrivate; struct _ZakCgiSessionPrivate { + gchar *sid; + GFile *gfile; + GKeyFile *kfile; }; G_DEFINE_TYPE (ZakCgiSession, zak_cgi_session, G_TYPE_OBJECT) @@ -74,6 +82,10 @@ zak_cgi_session_init (ZakCgiSession *zak_cgi_session) ZakCgiSession *zak_cgi_session_new (void) { + GHashTable *ht_cookies; + + GError *error; + ZakCgiSession *zak_cgi_session; ZakCgiSessionPrivate *priv; @@ -81,10 +93,133 @@ ZakCgiSession priv = ZAK_CGI_SESSION_GET_PRIVATE (zak_cgi_session); + ht_cookies = zak_cgi_main_get_cookies (); + priv->sid = g_hash_table_lookup (ht_cookies, "ZAKCGISID"); + + if (priv->sid != NULL) + { + /* open the file */ + priv->gfile = g_file_new_for_path (g_build_filename (g_get_tmp_dir (), priv->sid, NULL)); + + error = NULL; + + /* TODO */ + /* check the content */ + priv->kfile = g_key_file_new (); + if (!g_key_file_load_from_file (priv->kfile, + g_file_get_path (priv->gfile), + G_KEY_FILE_NONE, + &error) + || error != NULL) + { + /* TODO */ + } + } return zak_cgi_session; } +/** + * zak_cgi_session_get_header: + * @session: + * + * Returns: the header that set the cookie session, if needed; else an empty string. + */ +gchar +*zak_cgi_session_get_header (ZakCgiSession *session) +{ + gchar *ret; + + GError *error; + GFileIOStream *iostream; + + ZakCgiSessionPrivate *priv = ZAK_CGI_SESSION_GET_PRIVATE (session); + + if (priv->sid == NULL) + { + /* create new random name */ + guint32 i; + gchar *tmp; + + i = g_random_int (); + + tmp = g_strdup_printf ("%d", i); + + priv->sid = g_compute_checksum_for_string (G_CHECKSUM_MD5, + tmp, + strlen (tmp)); + + g_free (tmp); + + /* see if file already exists */ + priv->gfile = g_file_new_for_path (g_build_filename (g_get_tmp_dir (), priv->sid, NULL)); + error = NULL; + iostream = g_file_replace_readwrite (priv->gfile, NULL, FALSE, G_FILE_CREATE_PRIVATE, NULL, &error); + if (iostream == NULL + || error != NULL) + { + /* TODO */ + } + else + { + /* TODO */ + /* insert some data (ex IP) */ + g_io_stream_close (G_IO_STREAM (iostream), NULL, NULL); + g_object_unref (iostream); + } + + ret = zak_cgi_main_set_cookie ("ZAKCGISID", priv->sid, NULL, NULL, NULL, FALSE, FALSE); + } + else + { + ret = g_strdup (""); + } + + return ret; +} + +/** + * zak_cgi_session_set_value: + * @session: + * @name: + * @value: + * + */ +void +zak_cgi_session_set_value (ZakCgiSession *session, const gchar *name, const gchar *value) +{ + ZakCgiSessionPrivate *priv = ZAK_CGI_SESSION_GET_PRIVATE (session); + + if (priv->kfile != NULL) + { + g_key_file_set_value (priv->kfile, "SESSION", name, value); + g_key_file_save_to_file (priv->kfile, g_file_get_path (priv->gfile), NULL); + } +} + +/** + * zak_cgi_session_get_value: + * @session: + * @name: + * + * Returns: a value from session. + */ +gchar +*zak_cgi_session_get_value (ZakCgiSession *session, const gchar *name) +{ + gchar *ret; + + ZakCgiSessionPrivate *priv = ZAK_CGI_SESSION_GET_PRIVATE (session); + + ret = NULL; + if (priv->kfile != NULL) + { + ret = g_key_file_get_value (priv->kfile, "SESSION", name, NULL); + } + + return ret; +} + /* PRIVATE */ static void zak_cgi_session_set_property (GObject *object, diff --git a/src/session.h b/src/session.h index 1beea2b..024fd55 100644 --- a/src/session.h +++ b/src/session.h @@ -51,6 +51,11 @@ GType zak_cgi_session_get_type (void); ZakCgiSession *zak_cgi_session_new (void); +gchar *zak_cgi_session_get_header (ZakCgiSession *session); + +void zak_cgi_session_set_value (ZakCgiSession *session, const gchar *name, const gchar *value); +gchar *zak_cgi_session_get_value (ZakCgiSession *session, const gchar *name); + G_END_DECLS diff --git a/tests/cookies.c b/tests/cookies.c index 0cb3569..59c0cd9 100644 --- a/tests/cookies.c +++ b/tests/cookies.c @@ -45,6 +45,9 @@ main (int argc, char *argv[]) "\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)); + g_string_append_printf (header, + "\n%s", + zak_cgi_main_set_cookie ("SECONDO", "il secondo cookie", NULL, NULL, NULL, FALSE, FALSE)); zak_cgi_main_out (header->str, str->str); g_string_free (str, TRUE); diff --git a/tests/provapostcgi.html b/tests/provapostcgi.html new file mode 100644 index 0000000..230155d --- /dev/null +++ b/tests/provapostcgi.html @@ -0,0 +1,13 @@ + + +
+ + + + + + + +
+ + diff --git a/tests/session.c b/tests/session.c index 718a8ed..4a7ed81 100644 --- a/tests/session.c +++ b/tests/session.c @@ -16,11 +16,97 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include + +#include #include int main (int argc, char *argv[]) { + gchar *env; + gchar *username; + GString *str; + GString *header; + GHashTable *ht; + GHashTable *ht_stdin; + ZakCgiSession *session; + + gchar *method; + + session = zak_cgi_session_new (); + + str = g_string_new ("\n" + "Session Cookie\n" + "\n"); + + ht = zak_cgi_main_get_env (); + if (ht != NULL) + { + method = g_hash_table_lookup (ht, "REQUEST_METHOD"); + if (g_strcmp0 (method, "POST") == 0) + { + const gchar *content_type = g_getenv ("CONTENT_TYPE"); + gchar **splitted = g_strsplit (content_type, ";", -1); + if (g_strv_length (splitted) == 2) + { + gchar **boundary = g_strsplit (splitted[1], "=", 2); + + env = zak_cgi_main_get_stdin (); + + ht_stdin = zak_cgi_main_parse_stdin (env, boundary[1]); + + zak_cgi_session_set_value (session, "user_name", (gchar *)g_value_get_string ((GValue *)g_hash_table_lookup (ht_stdin, "user"))); + + g_free (env); + g_strfreev (boundary); + } + g_strfreev (splitted); + } + } + + username = zak_cgi_session_get_value (session, "user_name"); + if (username == NULL) + { + g_string_append (str, + "
\n" + "User:
\n" + "Password:
\n" + "\n" + "
\n"); + } + else + { + g_string_append_printf (str, + "Welcome %s", + username); + + if (g_strcmp0 (method, "POST") == 0) + { + g_string_append (str, + "
\n" + "\n" + "
\n"); + } + else + { + g_string_append (str, ", on the second page."); + } + g_free (method); + } + + g_string_append (str, "\n"); + + header = g_string_new (ZAK_CGI_STANDARD_HEADER_HTML); + g_string_append_printf (header, "\n%s", zak_cgi_session_get_header (session)); + + zak_cgi_main_out (header->str, str->str); + g_string_free (str, TRUE); + + if (method != NULL) + { + g_free (method); + } return 0; } -- 2.49.0