From: Andrea Zagli Date: Sat, 26 Mar 2016 15:13:06 +0000 (+0100) Subject: Added property ZakCgiUrl::remove_trailing_slashes. X-Git-Tag: v0.1.0~13 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=1573c107ad06c6e34f197fc8d14ee16e1928548b;p=libzakcgi Added property ZakCgiUrl::remove_trailing_slashes. --- diff --git a/src/url.c b/src/url.c index 2712953..565b769 100644 --- 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 */ diff --git a/src/url.h b/src/url.h index 08beb7d..3572ee6 100644 --- a/src/url.h +++ b/src/url.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 Andrea Zagli + * Copyright (C) 2015-2016 Andrea Zagli * * 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); diff --git a/tests/url.c b/tests/url.c index 1b7fc77..822614d 100644 --- a/tests/url.c +++ b/tests/url.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 Andrea Zagli + * Copyright (C) 2015-2016 Andrea Zagli * * 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, "/(?[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, "
%s\n", env);