]> saetta.ns0.it Git - libzakcgi/commitdiff
Url: ::connect with variable number of arguments using regex (closes #937).
authorAndrea Zagli <azagli@libero.it>
Wed, 22 Jul 2015 18:51:58 +0000 (20:51 +0200)
committerAndrea Zagli <azagli@libero.it>
Wed, 22 Jul 2015 18:51:58 +0000 (20:51 +0200)
src/main.h
src/url.c
src/url.h
tests/url.c

index aa608f7a8fb1c9e2512304896f161f0fe5869609..f776155ad4807ad53f6eb51c9d87fe37eebcd115 100644 (file)
@@ -76,8 +76,6 @@ gchar *zak_cgi_main_get_stdin (ZakCgiMain *zakcgimain);
 
 GHashTable *zak_cgi_main_parse_stdin (const gchar *buf, const gchar *boundary);
 
-GHashTable *zak_cgi_main_parse_stdin_bin (const gchar *buf, const gchar *boundary);
-
 void zak_cgi_main_redirect (const gchar *url);
 
 
index 8f5f06ad256f4f2d6d5a25783548648fa90c740c..2712953639bc257658e5130aa3c0487f7042bcc9 100644 (file)
--- a/src/url.c
+++ b/src/url.c
@@ -45,9 +45,6 @@ typedef struct _ZakCgiUrlPrivate ZakCgiUrlPrivate;
 struct _ZakCgiUrlPrivate
        {
                ZakCgiMain *zakcgimain;
-               
-               gchar *controller;
-               gchar *action;
 
                GHashTable *ht_functions;
        };
@@ -73,9 +70,6 @@ zak_cgi_url_init (ZakCgiUrl *zak_cgi_url)
        ZakCgiUrlPrivate *priv = ZAK_CGI_URL_GET_PRIVATE (zak_cgi_url);
 
        priv->zakcgimain = NULL;
-       
-       priv->controller = NULL;
-       priv->action = NULL;
 
        priv->ht_functions = g_hash_table_new (g_str_hash, g_str_equal);
 }
@@ -92,38 +86,17 @@ ZakCgiUrl
        ZakCgiUrl *zak_cgi_url;
        ZakCgiUrlPrivate *priv;
 
-       GHashTable *ht_env;
-
-       GValue *url;
-
        zak_cgi_url = ZAK_CGI_URL (g_object_new (zak_cgi_url_get_type (), NULL));
 
        priv = ZAK_CGI_URL_GET_PRIVATE (zak_cgi_url);
        priv->zakcgimain = zakcgimain;
 
-       /* parsing */
-       ht_env = zak_cgi_main_get_parameters (priv->zakcgimain, NULL);
-       url = g_hash_table_lookup (ht_env, "_url");
-       if (url != NULL)
-               {
-                       gchar **splitted;
-
-                       splitted = g_strsplit (g_value_get_string (url), "/", -1);
-                       if (g_strv_length (splitted) >= 3)
-                               {
-                                       priv->controller = g_strdup (splitted[1]);
-                                       priv->action = g_strdup (splitted [2]);
-                               }
-                       g_strfreev (splitted);
-               }
-
        return zak_cgi_url;
 }
 
 void
 zak_cgi_url_connect (ZakCgiUrl *url,
-                                        const gchar *controller,
-                                        const gchar *action,
+                                        const gchar *regex,
                                         ZakCgiUrlConnectedFunction function,
                                         gpointer user_data)
 {
@@ -135,7 +108,7 @@ zak_cgi_url_connect (ZakCgiUrl *url,
        g_ptr_array_add (ar, function);
        g_ptr_array_add (ar, user_data);
 
-       g_hash_table_replace (priv->ht_functions, g_strdup_printf ("%s|%s", controller, action), g_ptr_array_ref (ar));
+       g_hash_table_replace (priv->ht_functions, g_strdup (regex), g_ptr_array_ref (ar));
 
        g_ptr_array_unref (ar);
 }
@@ -143,18 +116,50 @@ zak_cgi_url_connect (ZakCgiUrl *url,
 void
 zak_cgi_url_dispatch (ZakCgiUrl *url)
 {
-       gchar *name;
+       GError *error;
+
+       const gchar *_url;
+
+       GHashTable *ht_env;
+       GHashTableIter iter;
+       gpointer key;
+       gpointer value;
+
+       GRegex *regex;
+       GMatchInfo *minfo;
+       gchar *str_regex;
+
        GPtrArray *ar;
        ZakCgiUrlConnectedFunction function;
-       
+
        ZakCgiUrlPrivate *priv = ZAK_CGI_URL_GET_PRIVATE (url);
 
-       name = g_strdup_printf ("%s|%s", priv->controller, priv->action);
-       ar = (GPtrArray *)g_hash_table_lookup (priv->ht_functions, name);
-       if (ar != NULL)
+       ht_env = zak_cgi_main_get_parameters (priv->zakcgimain, NULL);
+       _url = g_value_get_string (g_hash_table_lookup (ht_env, "_url"));
+       if (_url != NULL)
                {
-                       function = g_ptr_array_index (ar, 0);
-                       (*function)(g_ptr_array_index (ar, 1));
+                       g_hash_table_iter_init (&iter, priv->ht_functions);
+                       while (g_hash_table_iter_next (&iter, &key, &value))
+                               {
+                                       error = NULL;
+                                       str_regex = g_strdup_printf ("%s$", (gchar *)key);
+                                       regex = g_regex_new (str_regex, 0, 0, &error);
+                                       g_free (str_regex);
+                                       if (regex == NULL
+                                               || error != NULL)
+                                               {
+                                                       syslog (LOG_MAKEPRI(LOG_SYSLOG, LOG_DEBUG), "Error on creating regex: %s.",
+                                                                       error->message != NULL ? error->message : "no details");
+                                                       return;
+                                               }
+
+                                       if (g_regex_match ((const GRegex *)regex, _url, 0, &minfo))
+                                               {
+                                                       ar = (GPtrArray *)value;
+                                                       function = g_ptr_array_index (ar, 0);
+                                                       (*function)(minfo, g_ptr_array_index (ar, 1));
+                                               }
+                               }
                }
 }
 
index 53571e3018e35814f0ec2bcb3c7816337fda7dca..08beb7db58268d2fc4973fe4ba4d8904bd9ad28e 100644 (file)
--- a/src/url.h
+++ b/src/url.h
@@ -53,11 +53,10 @@ GType zak_cgi_url_get_type (void);
 
 ZakCgiUrl *zak_cgi_url_new (ZakCgiMain *zakcgimain);
 
-typedef void (*ZakCgiUrlConnectedFunction) (GString *buf);
+typedef void (*ZakCgiUrlConnectedFunction) (GMatchInfo *minfo, GString *buf);
 
 void zak_cgi_url_connect (ZakCgiUrl *url,
-                                                 const gchar *controller,
-                                                 const gchar *action,
+                                                 const gchar *regex,
                                                  ZakCgiUrlConnectedFunction function,
                                                  gpointer user_data);
 
index 65727f54161f209cb05f8aca6301c6aa94e62406..3a71c036359a45bfdccdddfb3d20a845e74cc932 100644 (file)
 #include <url.h>
 
 void
-hook (gpointer user_data)
+hook (GMatchInfo *minfo, gpointer user_data)
 {
        GString *str = (GString *)user_data;
 
        g_string_append_printf (str, "FROM THE HOOK<br/><br/>\n");
+
+       while (g_match_info_matches (minfo))
+               {
+                       guint n = g_match_info_get_match_count (minfo);
+                       g_string_append_printf (str, "Match count: %d<br/><br/>\n", n);
+                       gchar *word = g_match_info_fetch (minfo, 0);
+                       g_string_append_printf (str, "Found: %s<br/><br/>\n", word);
+                       g_free (word);
+
+                       guint i;
+                       for (i = 1; i < n; i++)
+                               {
+                                       gchar *word = g_match_info_fetch (minfo, i);
+                                       g_string_append_printf (str, "sub %d: %s<br/><br/>\n", i, word);
+                                       g_free (word);
+                               }
+
+                       if (n > 1)
+                               {
+                                       word = g_match_info_fetch_named (minfo, "controller");
+                                       g_string_append_printf (str, "sub named controller: %s<br/><br/>\n", word);
+                                       g_free (word);
+                               }
+
+                       g_match_info_next (minfo, NULL);
+               }
 }
 
 int
@@ -34,11 +60,12 @@ main (int argc, char *argv[])
 
        str = g_string_new ("<html>\n"
                            "<head><title>Url</title></head>\n"
-                           "<body>\n");
+                           "<body>\n"
+                                               "FROM INIT<br/><br/>\n");
 
        url = zak_cgi_url_new (NULL);
 
-       zak_cgi_url_connect (url, "thecontroller", "theaction", (ZakCgiUrlConnectedFunction)hook, str);
+       zak_cgi_url_connect (url, "/(?<controller>[a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)", (ZakCgiUrlConnectedFunction)hook, str);
 
        zak_cgi_url_dispatch (url);
 
@@ -49,4 +76,3 @@ main (int argc, char *argv[])
 
        return 0;
 }
-