From: Andrea Zagli <azagli@libero.it>
Date: Sat, 26 Mar 2016 16:07:48 +0000 (+0100)
Subject: Added function ZakCgiUrl::connect_not_found (closes #948).
X-Git-Tag: v0.1.0~12
X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=2a81d0457fcf4e8df7121e9db0fb85450e9ecaf3;p=libzakcgi

Added function ZakCgiUrl::connect_not_found (closes #948).
---

diff --git a/src/url.c b/src/url.c
index 565b769..7676026 100644
--- 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);
diff --git a/src/url.h b/src/url.h
index 3572ee6..8124e33 100644
--- 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);
 
diff --git a/tests/url.c b/tests/url.c
index 822614d..dbad74b 100644
--- a/tests/url.c
+++ b/tests/url.c
@@ -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);