]> saetta.ns0.it Git - libzakcgi/commitdiff
Added function ZakCgiUrl::connect_not_found (closes #948).
authorAndrea Zagli <azagli@libero.it>
Sat, 26 Mar 2016 16:07:48 +0000 (17:07 +0100)
committerAndrea Zagli <azagli@libero.it>
Sat, 26 Mar 2016 16:07:48 +0000 (17:07 +0100)
src/url.c
src/url.h
tests/url.c

index 565b76903aebc7ac03263f4d2a27c85555e6d5ca..7676026b464abd3fd49c53387e99a39746e00cf2 100644 (file)
--- a/src/url.c
+++ b/src/url.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-2016 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -47,6 +47,7 @@ struct _ZakCgiUrlPrivate
                ZakCgiMain *zakcgimain;
 
                GHashTable *ht_functions;
+               GPtrArray *ar_not_found;
 
                gboolean remove_trailing_slashes;
        };
@@ -74,6 +75,8 @@ zak_cgi_url_init (ZakCgiUrl *zak_cgi_url)
        priv->zakcgimain = NULL;
 
        priv->ht_functions = g_hash_table_new (g_str_hash, g_str_equal);
+       priv->ar_not_found = NULL;
+
        priv->remove_trailing_slashes = FALSE;
 }
 
@@ -124,6 +127,34 @@ zak_cgi_url_connect (ZakCgiUrl *url,
        g_ptr_array_unref (ar);
 }
 
+/**
+ * zak_cgi_url_connect_not_found:
+ * @url:
+ * @function:
+ * @user_data:
+ *
+ */
+void
+zak_cgi_url_connect_not_found (ZakCgiUrl *url,
+                                                          ZakCgiUrlConnectedFunction function,
+                                                          gpointer user_data)
+{
+       ZakCgiUrlPrivate *priv = ZAK_CGI_URL_GET_PRIVATE (url);
+
+       if (priv->ar_not_found != NULL)
+               {
+                       g_ptr_array_unref (priv->ar_not_found);
+                       priv->ar_not_found = NULL;
+               }
+
+       if (function != NULL)
+               {
+                       priv->ar_not_found = g_ptr_array_new ();
+                       g_ptr_array_add (priv->ar_not_found, function);
+                       g_ptr_array_add (priv->ar_not_found, user_data);
+               }
+}
+
 /**
  * zak_cgi_url_set_remove_trailing_slashes:
  * @url:
@@ -178,6 +209,8 @@ zak_cgi_url_dispatch (ZakCgiUrl *url)
        GPtrArray *ar;
        ZakCgiUrlConnectedFunction function;
 
+       gboolean not_found;
+
        ZakCgiUrlPrivate *priv = ZAK_CGI_URL_GET_PRIVATE (url);
 
        ht_env = zak_cgi_main_get_parameters (priv->zakcgimain, NULL);
@@ -201,6 +234,8 @@ zak_cgi_url_dispatch (ZakCgiUrl *url)
                                                }
                                }
 
+                       not_found = TRUE;
+
                        g_hash_table_iter_init (&iter, priv->ht_functions);
                        while (g_hash_table_iter_next (&iter, &key, &value))
                                {
@@ -221,8 +256,15 @@ zak_cgi_url_dispatch (ZakCgiUrl *url)
                                                        ar = (GPtrArray *)value;
                                                        function = g_ptr_array_index (ar, 0);
                                                        (*function)(minfo, g_ptr_array_index (ar, 1));
+                                                       not_found = FALSE;
                                                }
                                }
+
+                       if (not_found && priv->ar_not_found != NULL)
+                               {
+                                       function = g_ptr_array_index (priv->ar_not_found, 0);
+                                       (*function)(NULL, g_ptr_array_index (priv->ar_not_found, 1));
+                               }
                }
 
        g_string_free (_url, TRUE);
index 3572ee6cd2531f5f5edf22f3475bd80da580cf29..8124e338a14132f88412365b4ef94958bacc16cf 100644 (file)
--- a/src/url.h
+++ b/src/url.h
@@ -60,6 +60,10 @@ void zak_cgi_url_connect (ZakCgiUrl *url,
                                                  ZakCgiUrlConnectedFunction function,
                                                  gpointer user_data);
 
+void zak_cgi_url_connect_not_found (ZakCgiUrl *url,
+                                                                       ZakCgiUrlConnectedFunction function,
+                                                                       gpointer user_data);
+
 void zak_cgi_url_set_remove_trailing_slashes (ZakCgiUrl *url, gboolean remove);
 gboolean zak_cgi_url_get_remove_trailing_slashes (ZakCgiUrl *url);
 
index 822614d54936c4848c55c41081500b4bacfa2539..dbad74b47968d19d0174766a330b8cb4555c40ca 100644 (file)
@@ -52,6 +52,14 @@ hook (GMatchInfo *minfo, gpointer user_data)
                }
 }
 
+void
+hook_not_found (GMatchInfo *minfo, gpointer user_data)
+{
+       GString *str = (GString *)user_data;
+
+       g_string_append_printf (str, "NOT FOUND<br/><br/>\n");
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -68,7 +76,9 @@ main (int argc, char *argv[])
 
        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_connect_not_found (url, (ZakCgiUrlConnectedFunction)hook_not_found, str);
+
+       zak_cgi_url_connect (url, "/(?<controller>[a-z][a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)", (ZakCgiUrlConnectedFunction)hook, str);
 
        zak_cgi_url_set_remove_trailing_slashes (url, TRUE);