]> saetta.ns0.it Git - libzakcgi/commitdiff
Added property ZakCgiUrl::remove_trailing_slashes.
authorAndrea Zagli <azagli@libero.it>
Sat, 26 Mar 2016 15:13:06 +0000 (16:13 +0100)
committerAndrea Zagli <azagli@libero.it>
Sat, 26 Mar 2016 15:37:24 +0000 (16:37 +0100)
src/url.c
src/url.h
tests/url.c

index 2712953639bc257658e5130aa3c0487f7042bcc9..565b76903aebc7ac03263f4d2a27c85555e6d5ca 100644 (file)
--- a/src/url.c
+++ b/src/url.c
@@ -47,6 +47,8 @@ struct _ZakCgiUrlPrivate
                ZakCgiMain *zakcgimain;
 
                GHashTable *ht_functions;
+
+               gboolean remove_trailing_slashes;
        };
 
 G_DEFINE_TYPE (ZakCgiUrl, zak_cgi_url, G_TYPE_OBJECT)
@@ -72,6 +74,7 @@ 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->remove_trailing_slashes = FALSE;
 }
 
 /**
@@ -94,6 +97,14 @@ ZakCgiUrl
        return zak_cgi_url;
 }
 
+/**
+ * zak_cgi_url_connect:
+ * @url:
+ * @regex:
+ * @function:
+ * @user_data:
+ *
+ */
 void
 zak_cgi_url_connect (ZakCgiUrl *url,
                                         const gchar *regex,
@@ -113,12 +124,47 @@ zak_cgi_url_connect (ZakCgiUrl *url,
        g_ptr_array_unref (ar);
 }
 
+/**
+ * zak_cgi_url_set_remove_trailing_slashes:
+ * @url:
+ * @remove:
+ *
+ */
+void
+zak_cgi_url_set_remove_trailing_slashes (ZakCgiUrl *url, gboolean remove)
+{
+       ZakCgiUrlPrivate *priv = ZAK_CGI_URL_GET_PRIVATE (url);
+
+       priv->remove_trailing_slashes = remove;
+}
+
+/**
+ * zak_cgi_url_get_remove_trailing_slashes:
+ * @url:
+ *
+ * Returns:
+ */
+gboolean
+zak_cgi_url_get_remove_trailing_slashes (ZakCgiUrl *url)
+{
+       ZakCgiUrlPrivate *priv = ZAK_CGI_URL_GET_PRIVATE (url);
+
+       return priv->remove_trailing_slashes;
+}
+
+/**
+ * zak_cgi_url_dispatch:
+ * @url:
+ *
+ */
 void
 zak_cgi_url_dispatch (ZakCgiUrl *url)
 {
        GError *error;
 
-       const gchar *_url;
+       GString *_url;
+
+       gint pos;
 
        GHashTable *ht_env;
        GHashTableIter iter;
@@ -135,9 +181,26 @@ zak_cgi_url_dispatch (ZakCgiUrl *url)
        ZakCgiUrlPrivate *priv = ZAK_CGI_URL_GET_PRIVATE (url);
 
        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)
+       _url = g_string_new (g_value_get_string (g_hash_table_lookup (ht_env, "_url")));
+       if (_url != NULL
+               && g_strcmp0 (_url->str, "") != 0)
                {
+                       if (priv->remove_trailing_slashes)
+                               {
+                                       /* removing trailing slashes */
+                                       for (pos = _url->len; pos > 0; pos--)
+                                               {
+                                                       if (_url->str[pos - 1] != '/')
+                                                               {
+                                                                       break;
+                                                               }
+                                               }
+                                       if (pos > 0 && pos < (gint)_url->len)
+                                               {
+                                                       g_string_truncate (_url, pos);
+                                               }
+                               }
+
                        g_hash_table_iter_init (&iter, priv->ht_functions);
                        while (g_hash_table_iter_next (&iter, &key, &value))
                                {
@@ -153,7 +216,7 @@ zak_cgi_url_dispatch (ZakCgiUrl *url)
                                                        return;
                                                }
 
-                                       if (g_regex_match ((const GRegex *)regex, _url, 0, &minfo))
+                                       if (g_regex_match ((const GRegex *)regex, _url->str, 0, &minfo))
                                                {
                                                        ar = (GPtrArray *)value;
                                                        function = g_ptr_array_index (ar, 0);
@@ -161,6 +224,8 @@ zak_cgi_url_dispatch (ZakCgiUrl *url)
                                                }
                                }
                }
+
+       g_string_free (_url, TRUE);
 }
 
 /* PRIVATE */
index 08beb7db58268d2fc4973fe4ba4d8904bd9ad28e..3572ee6cd2531f5f5edf22f3475bd80da580cf29 100644 (file)
--- a/src/url.h
+++ b/src/url.h
@@ -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
@@ -60,6 +60,9 @@ void zak_cgi_url_connect (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);
+
 void zak_cgi_url_dispatch (ZakCgiUrl *url);
 
 
index 1b7fc772be1040e611ffa89cff959dc308233204..822614d54936c4848c55c41081500b4bacfa2539 100644 (file)
@@ -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 General Public
@@ -70,6 +70,8 @@ main (int argc, char *argv[])
 
        zak_cgi_url_connect (url, "/(?<controller>[a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)", (ZakCgiUrlConnectedFunction)hook, str);
 
+       zak_cgi_url_set_remove_trailing_slashes (url, TRUE);
+
        zak_cgi_url_dispatch (url);
 
        g_string_append_printf (str, "<hr/>%s</body>\n", env);