From: Andrea Zagli Date: Mon, 29 Jun 2015 20:43:38 +0000 (+0200) Subject: Implemented ZakCgiUrl (routing). X-Git-Tag: v0.0.1~24 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=9d5bd614b5a1cbdbc1f023239e9ec1ed79c31638;p=libzakcgi Implemented ZakCgiUrl (routing). Only controller and action. --- diff --git a/.gitignore b/.gitignore index 3db7834..bb5048c 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,4 @@ tests/env tests/querystring tests/redirect tests/session +tests/url diff --git a/src/url.c b/src/url.c index 0a871c5..ed79052 100644 --- a/src/url.c +++ b/src/url.c @@ -20,6 +20,9 @@ #include #endif +#include + +#include "main.h" #include "url.h" static void zak_cgi_url_class_init (ZakCgiUrlClass *class); @@ -42,6 +45,10 @@ static void zak_cgi_url_finalize (GObject *gobject); typedef struct _ZakCgiUrlPrivate ZakCgiUrlPrivate; struct _ZakCgiUrlPrivate { + gchar *controller; + gchar *action; + + GHashTable *ht_functions; }; G_DEFINE_TYPE (ZakCgiUrl, zak_cgi_url, G_TYPE_OBJECT) @@ -64,6 +71,10 @@ zak_cgi_url_init (ZakCgiUrl *zak_cgi_url) { ZakCgiUrlPrivate *priv = ZAK_CGI_URL_GET_PRIVATE (zak_cgi_url); + priv->controller = NULL; + priv->action = NULL; + + priv->ht_functions = g_hash_table_new (g_str_hash, g_str_equal); } /** @@ -77,14 +88,71 @@ 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); + /* parsing */ + ht_env = zak_cgi_main_get_parameters (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, + ZakCgiUrlConnectedFunction function, + gpointer user_data) +{ + GPtrArray *ar; + + ZakCgiUrlPrivate *priv = ZAK_CGI_URL_GET_PRIVATE (url); + + ar = g_ptr_array_new (); + 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_ptr_array_unref (ar); +} + +void +zak_cgi_url_dispatch (ZakCgiUrl *url) +{ + gchar *name; + 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) + { + function = g_ptr_array_index (ar, 0); + (*function)(g_ptr_array_index (ar, 1)); + } +} + /* PRIVATE */ static void zak_cgi_url_set_property (GObject *object, diff --git a/src/url.h b/src/url.h index 01dfb61..5e53601 100644 --- a/src/url.h +++ b/src/url.h @@ -51,6 +51,16 @@ GType zak_cgi_url_get_type (void); ZakCgiUrl *zak_cgi_url_new (void); +typedef void (*ZakCgiUrlConnectedFunction) (GString *buf); + +void zak_cgi_url_connect (ZakCgiUrl *url, + const gchar *controller, + const gchar *action, + ZakCgiUrlConnectedFunction function, + gpointer user_data); + +void zak_cgi_url_dispatch (ZakCgiUrl *url); + G_END_DECLS diff --git a/tests/Makefile.am b/tests/Makefile.am index 7e12799..b070e92 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -13,4 +13,5 @@ noinst_PROGRAMS = \ env \ querystring \ redirect \ - session + session \ + url diff --git a/tests/url.c b/tests/url.c new file mode 100644 index 0000000..22ddfe5 --- /dev/null +++ b/tests/url.c @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2015 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +void +hook (gpointer user_data) +{ + GString *str = (GString *)user_data; + + g_string_append_printf (str, "FROM THE HOOK

\n"); +} + +int +main (int argc, char *argv[]) +{ + ZakCgiUrl *url; + GString *str; + + str = g_string_new ("\n" + "Url\n" + "\n"); + + url = zak_cgi_url_new (); + + zak_cgi_url_connect (url, "thecontroller", "theaction", (ZakCgiUrlConnectedFunction)hook, str); + + zak_cgi_url_dispatch (url); + + g_string_append_printf (str, "\n"); + + zak_cgi_main_out (NULL, str->str); + g_string_free (str, TRUE); + + return 0; +} +