]> saetta.ns0.it Git - libzakcgi/commitdiff
Session cookie is now create with path base_uri or CONTEXT_PREFIX (closes #949).
authorAndrea Zagli <azagli@libero.it>
Tue, 28 Jul 2015 19:53:21 +0000 (21:53 +0200)
committerAndrea Zagli <azagli@libero.it>
Tue, 28 Jul 2015 19:53:21 +0000 (21:53 +0200)
src/session.c
src/session.h
tests/session.c
tests/url.c

index 8567c1b71a7ea13a88585a146d73bcbecbeb05a1..6390c0dd6d1d8d7bb0ca92d8cb88a15f54aef596 100644 (file)
@@ -47,6 +47,7 @@ typedef struct _ZakCgiSessionPrivate ZakCgiSessionPrivate;
 struct _ZakCgiSessionPrivate
        {
                ZakCgiMain *zakcgimain;
+               gchar *base_uri;
                gchar *sid;
                GFile *gfile;
                GKeyFile *kfile;
@@ -72,17 +73,20 @@ zak_cgi_session_init (ZakCgiSession *zak_cgi_session)
 {
        ZakCgiSessionPrivate *priv = ZAK_CGI_SESSION_GET_PRIVATE (zak_cgi_session);
 
-       priv->zakcgimain;
+       priv->zakcgimain = NULL;
+       priv->base_uri = NULL;
 }
 
 /**
  * zak_cgi_session_new:
  * @zakcgimain:
+ * @base_uri:
  *
  * Returns: the newly created #ZakCgiSession object.
  */
 ZakCgiSession
-*zak_cgi_session_new (ZakCgiMain *zakcgimain)
+*zak_cgi_session_new (ZakCgiMain *zakcgimain,
+                                         const gchar *base_uri)
 {
        GHashTable *ht_cookies;
 
@@ -95,6 +99,10 @@ ZakCgiSession
 
        priv = ZAK_CGI_SESSION_GET_PRIVATE (zak_cgi_session);
        priv->zakcgimain = zakcgimain;
+       if (base_uri != NULL)
+               {
+                       priv->base_uri = g_strdup (base_uri);
+               }
 
        ht_cookies = zak_cgi_main_get_cookies (priv->zakcgimain);
        priv->sid = g_hash_table_lookup (ht_cookies, "ZAKCGISID");
@@ -136,18 +144,21 @@ gchar
        GError *error;
        GFileIOStream *iostream;
 
+       guint32 i;
+       gchar *tmp;
+
+       GHashTable *ht_env;
+
        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));
@@ -171,7 +182,11 @@ gchar
                                        g_object_unref (iostream);
                                }
 
-                       ret = zak_cgi_main_set_cookie ("ZAKCGISID", priv->sid, NULL, NULL, NULL, FALSE, FALSE);
+                       ht_env = zak_cgi_main_get_env (priv->zakcgimain);
+
+                       ret = zak_cgi_main_set_cookie ("ZAKCGISID", priv->sid, NULL, NULL,
+                                                                                  priv->base_uri != NULL ? priv->base_uri : g_value_get_string (g_hash_table_lookup (ht_env, "CONTEXT_PREFIX")),
+                                                                                  FALSE, FALSE);
                }
        else
                {
index f6f1839022fb1c8d6989c4a46b5d342ba0d869c9..3475ba2734140883e4fc5377e2d535e21d00c979 100644 (file)
@@ -51,7 +51,8 @@ struct _ZakCgiSessionClass
 GType zak_cgi_session_get_type (void);
 
 
-ZakCgiSession *zak_cgi_session_new (ZakCgiMain *zakcgimain);
+ZakCgiSession *zak_cgi_session_new (ZakCgiMain *zakcgimain,
+                                                                       const gchar *base_uri);
 
 gchar *zak_cgi_session_get_header (ZakCgiSession *session);
 
index f86ffc73a86adce316402588fe195b4f1d6b725c..6644d21fca70a9a3b1a6d6c6f493d64d98082ffa 100644 (file)
@@ -34,7 +34,7 @@ main (int argc, char *argv[])
 
        gchar *method;
 
-       session = zak_cgi_session_new (NULL);
+       session = zak_cgi_session_new (NULL, NULL);
 
        str = g_string_new ("<html>\n"
                            "<head><title>Session Cookie</title></head>\n"
@@ -110,4 +110,3 @@ main (int argc, char *argv[])
 
        return 0;
 }
-
index 3a71c036359a45bfdccdddfb3d20a845e74cc932..1b7fc772be1040e611ffa89cff959dc308233204 100644 (file)
@@ -55,6 +55,7 @@ hook (GMatchInfo *minfo, gpointer user_data)
 int
 main (int argc, char *argv[])
 {
+       gchar *env;
        ZakCgiUrl *url;
        GString *str;
 
@@ -63,13 +64,15 @@ main (int argc, char *argv[])
                            "<body>\n"
                                                "FROM INIT<br/><br/>\n");
 
+       env = zak_cgi_main_dump_env (NULL);
+
        url = zak_cgi_url_new (NULL);
 
        zak_cgi_url_connect (url, "/(?<controller>[a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)", (ZakCgiUrlConnectedFunction)hook, str);
 
        zak_cgi_url_dispatch (url);
 
-       g_string_append_printf (str, "</body>\n");
+       g_string_append_printf (str, "<hr/>%s</body>\n", env);
 
        zak_cgi_main_out (NULL, str->str);
        g_string_free (str, TRUE);