[libgrss] Some extra check on input URLs



commit 2ffbb2d439f0d7476404b32a6d18b4ce45c51866
Author: Roberto Guido <bob4job gmail com>
Date:   Tue Dec 24 23:59:35 2013 +0100

    Some extra check on input URLs

 src/feed-channel.c |   99 +++++++++++++++++++++++++++++++++++++--------------
 src/feed-channel.h |   10 +++---
 src/feed-item.c    |   44 ++++++++++++++++++-----
 src/feed-item.h    |    6 ++--
 src/utils.c        |   26 ++++++++++++++
 src/utils.h        |    3 ++
 6 files changed, 143 insertions(+), 45 deletions(-)
---
diff --git a/src/feed-channel.c b/src/feed-channel.c
index 927a24d..0bc2fa1 100644
--- a/src/feed-channel.c
+++ b/src/feed-channel.c
@@ -257,15 +257,21 @@ grss_feed_channel_get_format (GrssFeedChannel *channel)
  * @source: URL of the feed.
  *
  * To assign the URL where to fetch the feed.
+ * 
+ * Return value: %TRUE if @source is a valid URL, %FALSE otherwise
  */
-void
+gboolean
 grss_feed_channel_set_source (GrssFeedChannel *channel, gchar *source)
 {
-       /*
-               TODO    Check if the provided string is a valid URL
-       */
        FREE_STRING (channel->priv->source);
-       channel->priv->source = g_strdup (source);
+
+       if (test_url ((const gchar*) source) == TRUE) {
+               channel->priv->source = SET_STRING (source);
+               return TRUE;
+       }
+       else {
+               return FALSE;
+       }
 }
 
 /**
@@ -316,12 +322,21 @@ grss_feed_channel_get_title (GrssFeedChannel *channel)
  * @homepage: homepage for the main website.
  *
  * To set the homepage of the site the @channel belongs.
+ * 
+ * Return value: %TRUE if @homepage is a valid URL, %FALSE otherwise
  */
-void
+gboolean
 grss_feed_channel_set_homepage (GrssFeedChannel *channel, gchar *homepage)
 {
        FREE_STRING (channel->priv->homepage);
-       channel->priv->homepage = g_strdup (homepage);
+
+       if (test_url ((const gchar*) homepage) == TRUE) {
+               channel->priv->homepage = SET_STRING (homepage);
+               return TRUE;
+       }
+       else {
+               return FALSE;
+       }
 }
 
 /**
@@ -372,12 +387,21 @@ grss_feed_channel_get_description (GrssFeedChannel *channel)
  * @image: URL of the image.
  *
  * To set a rappresentative image to @channel.
+ * 
+ * Return value: %TRUE if @image is a valid URL, %FALSE otherwise
  */
-void
+gboolean
 grss_feed_channel_set_image (GrssFeedChannel *channel, gchar *image)
 {
        FREE_STRING (channel->priv->image);
-       channel->priv->image = g_strdup (image);
+
+       if (test_url ((const gchar*) image) == TRUE) {
+               channel->priv->image = SET_STRING (image);
+               return TRUE;
+       }
+       else {
+               return FALSE;
+       }
 }
 
 /**
@@ -400,12 +424,21 @@ grss_feed_channel_get_image (GrssFeedChannel *channel)
  * @icon: URL where to retrieve the favicon.
  *
  * To set the URL of the icon rappresenting @channel.
+ * 
+ * Return value: %TRUE if @icon is a valid URL, %FALSE otherwise
  */
-void
+gboolean
 grss_feed_channel_set_icon (GrssFeedChannel *channel, gchar *icon)
 {
        FREE_STRING (channel->priv->icon);
-       channel->priv->icon = g_strdup (icon);
+
+       if (test_url ((const gchar*) icon) == TRUE) {
+               channel->priv->icon = SET_STRING (icon);
+               return TRUE;
+       }
+       else {
+               return FALSE;
+       }
 }
 
 /**
@@ -486,14 +519,21 @@ grss_feed_channel_get_category (GrssFeedChannel *channel)
  *
  * To set information about PubSubHubbub for the channel. To unset the hub,
  * pass %NULL as parameter.
+ * 
+ * Return value: %TRUE if @hub is a valid URL, %FALSE otherwise
  */
-void
+gboolean
 grss_feed_channel_set_pubsubhub (GrssFeedChannel *channel, gchar *hub)
 {
        FREE_STRING (channel->priv->pubsub.hub);
 
-       if (hub != NULL)
-               channel->priv->pubsub.hub = g_strdup (hub);
+       if (test_url ((const gchar*) hub) == TRUE) {
+               channel->priv->pubsub.hub = SET_STRING (hub);
+               return TRUE;
+       }
+       else {
+               return FALSE;
+       }
 }
 
 /**
@@ -652,7 +692,10 @@ grss_feed_channel_get_contributors (GrssFeedChannel *channel)
 /**
  * grss_feed_channel_add_cookie:
  * @channel: a #GrssFeedChannel.
- * @cookie: HTML cookie to add to the channel's cookie jar
+ * @cookie: HTML cookie to add to the #GrssFeedChannel's cookie jar
+ * 
+ * To add a cookie related to the @channel, will be involved in HTTP sessions
+ * while fetching it. More cookie can be added to every #GrssFeedChannel
  */
 void
 grss_feed_channel_add_cookie (GrssFeedChannel *channel, SoupCookie *cookie)
@@ -680,6 +723,7 @@ grss_feed_channel_get_cookies (GrssFeedChannel *channel)
 {
        if (channel->priv->jar != NULL)
                return soup_cookie_jar_all_cookies(channel->priv->jar);
+
        return NULL;
 }
 
@@ -726,18 +770,6 @@ grss_feed_channel_set_generator (GrssFeedChannel *channel, gchar *generator)
 }
 
 /**
- * grss_feed_channel_set_gzip_compression:
- * Set the Gzip compression for the channel to on or off
- * @value either enable (TRUE) or disable compression (FALSE)
- *
- */
-void
-grss_feed_channel_set_gzip_compression(GrssFeedChannel *channel, gboolean value)
-{
-       channel->priv->gzip = value;
-}
-
-/**
  * grss_feed_channel_get_generator:
  * @channel: a #GrssFeedChannel.
  *
@@ -752,6 +784,19 @@ grss_feed_channel_get_generator (GrssFeedChannel *channel)
 }
 
 /**
+ * grss_feed_channel_set_gzip_compression:
+ * @channel: a #GrssFeedChannel.
+ * @value: %TRUE to enable GZIP compression when fetching the channel
+ * 
+ * Set the GZIP compression for the channel to on or off.
+ */
+void
+grss_feed_channel_set_gzip_compression(GrssFeedChannel *channel, gboolean value)
+{
+       channel->priv->gzip = value;
+}
+
+/**
  * grss_feed_channel_get_gzip_compression:
  *
  * GZip Compression of the channel is either on or off
diff --git a/src/feed-channel.h b/src/feed-channel.h
index 00828ca..8287c75 100644
--- a/src/feed-channel.h
+++ b/src/feed-channel.h
@@ -50,23 +50,23 @@ GrssFeedChannel*    grss_feed_channel_new_from_file         (const gchar *path, GError **e
 
 void                   grss_feed_channel_set_format            (GrssFeedChannel *channel, gchar *format);
 const gchar*           grss_feed_channel_get_format            (GrssFeedChannel *channel);
-void                   grss_feed_channel_set_source            (GrssFeedChannel *channel, gchar *source);
+gboolean               grss_feed_channel_set_source            (GrssFeedChannel *channel, gchar *source);
 const gchar*           grss_feed_channel_get_source            (GrssFeedChannel *channel);
 void                   grss_feed_channel_set_title             (GrssFeedChannel *channel, gchar *title);
 const gchar*           grss_feed_channel_get_title             (GrssFeedChannel *channel);
-void                   grss_feed_channel_set_homepage          (GrssFeedChannel *channel, gchar *homepage);
+gboolean               grss_feed_channel_set_homepage          (GrssFeedChannel *channel, gchar *homepage);
 const gchar*           grss_feed_channel_get_homepage          (GrssFeedChannel *channel);
 void                   grss_feed_channel_set_description       (GrssFeedChannel *channel, gchar 
*description);
 const gchar*           grss_feed_channel_get_description       (GrssFeedChannel *channel);
-void                   grss_feed_channel_set_image             (GrssFeedChannel *channel, gchar *image);
+gboolean               grss_feed_channel_set_image             (GrssFeedChannel *channel, gchar *image);
 const gchar*           grss_feed_channel_get_image             (GrssFeedChannel *channel);
-void                   grss_feed_channel_set_icon              (GrssFeedChannel *channel, gchar *icon);
+gboolean               grss_feed_channel_set_icon              (GrssFeedChannel *channel, gchar *icon);
 const gchar*           grss_feed_channel_get_icon              (GrssFeedChannel *channel);
 void                   grss_feed_channel_set_language          (GrssFeedChannel *channel, gchar *language);
 const gchar*           grss_feed_channel_get_language          (GrssFeedChannel *channel);
 void                   grss_feed_channel_set_category          (GrssFeedChannel *channel, gchar *category);
 const gchar*           grss_feed_channel_get_category          (GrssFeedChannel *channel);
-void                   grss_feed_channel_set_pubsubhub         (GrssFeedChannel *channel, gchar *hub);
+gboolean               grss_feed_channel_set_pubsubhub         (GrssFeedChannel *channel, gchar *hub);
 gboolean               grss_feed_channel_get_pubsubhub         (GrssFeedChannel *channel, gchar **hub);
 void                   grss_feed_channel_set_rsscloud          (GrssFeedChannel *channel, gchar *path, gchar 
*protocol);
 gboolean               grss_feed_channel_get_rsscloud          (GrssFeedChannel *channel, gchar **path, 
gchar **protocol);
diff --git a/src/feed-item.c b/src/feed-item.c
index c9eed0b..5d05f1a 100644
--- a/src/feed-item.c
+++ b/src/feed-item.c
@@ -33,9 +33,6 @@
  * attributes.
  */
 
-/*
-       TODO    Drop this structure and migrate to libchamplain
-*/
 typedef struct {
        gboolean        has;
        double          lat;
@@ -298,12 +295,21 @@ grss_feed_item_get_categories (GrssFeedItem *item)
  * @source: URL of the item.
  *
  * To set the source of the @item.
+ * 
+ * Return value: %TRUE if @source is a valid URL, %FALSE otherwise
  */
-void
+gboolean
 grss_feed_item_set_source (GrssFeedItem *item, gchar *source)
 {
        FREE_STRING (item->priv->source);
-       item->priv->source = g_strdup (source);
+
+       if (test_url ((const gchar*) source) == TRUE) {
+               item->priv->source = SET_STRING (source);
+               return TRUE;
+       }
+       else {
+               return FALSE;
+       }
 }
 
 /**
@@ -328,14 +334,23 @@ grss_feed_item_get_source (GrssFeedItem *item)
  *
  * To set an alternative real source for @item. This parameter is used by web
  * aggregators to explicit the origin of a content reproduced in them.
+ * 
+ * Return value: %TRUE if @realsource is a valid URL, %FALSE otherwise
  */
-void
+gboolean
 grss_feed_item_set_real_source (GrssFeedItem *item, gchar *realsource, gchar *title)
 {
        FREE_STRING (item->priv->real_source_url);
-       item->priv->real_source_url = g_strdup (realsource);
        FREE_STRING (item->priv->real_source_title);
-       item->priv->real_source_title = g_strdup (title);
+
+       if (test_url ((const gchar*) realsource) == TRUE) {
+               item->priv->real_source_url = SET_STRING (realsource);
+               item->priv->real_source_title = SET_STRING (title);
+               return TRUE;
+       }
+       else {
+               return FALSE;
+       }
 }
 
 /**
@@ -480,12 +495,21 @@ grss_feed_item_get_contributors (GrssFeedItem *item)
  * @url: URL where to retrieve comments to the item.
  *
  * To assign the URL where to fetch comments for the item.
+ * 
+ * Return value: %TRUE if @url is a valid URL, %FALSE otherwise
  */
-void
+gboolean
 grss_feed_item_set_comments_url (GrssFeedItem *item, gchar *url)
 {
        FREE_STRING (item->priv->comments_url);
-       item->priv->comments_url = g_strdup (url);
+
+       if (test_url ((const gchar*) url) == TRUE) {
+               item->priv->comments_url = SET_STRING (url);
+               return TRUE;
+       }
+       else {
+               return FALSE;
+       }
 }
 
 /**
diff --git a/src/feed-item.h b/src/feed-item.h
index 9671178..f8db3bd 100644
--- a/src/feed-item.h
+++ b/src/feed-item.h
@@ -56,9 +56,9 @@ void                  grss_feed_item_set_description  (GrssFeedItem *item, gchar 
*description);
 const gchar*           grss_feed_item_get_description  (GrssFeedItem *item);
 void                   grss_feed_item_add_category     (GrssFeedItem *item, gchar *category);
 const GList*           grss_feed_item_get_categories   (GrssFeedItem *item);
-void                   grss_feed_item_set_source       (GrssFeedItem *item, gchar *source);
+gboolean               grss_feed_item_set_source       (GrssFeedItem *item, gchar *source);
 const gchar*           grss_feed_item_get_source       (GrssFeedItem *item);
-void                   grss_feed_item_set_real_source  (GrssFeedItem *item, gchar *realsource, gchar *title);
+gboolean               grss_feed_item_set_real_source  (GrssFeedItem *item, gchar *realsource, gchar *title);
 void                   grss_feed_item_get_real_source  (GrssFeedItem *item, const gchar **realsource, const 
gchar **title);
 void                   grss_feed_item_set_related      (GrssFeedItem *item, gchar *related);
 const gchar*           grss_feed_item_get_related      (GrssFeedItem *item);
@@ -69,7 +69,7 @@ void                  grss_feed_item_set_author       (GrssFeedItem *item, gchar *author);
 const gchar*           grss_feed_item_get_author       (GrssFeedItem *item);
 void                   grss_feed_item_add_contributor  (GrssFeedItem *item, gchar *contributor);
 const GList*           grss_feed_item_get_contributors (GrssFeedItem *item);
-void                   grss_feed_item_set_comments_url (GrssFeedItem *item, gchar *url);
+gboolean               grss_feed_item_set_comments_url (GrssFeedItem *item, gchar *url);
 const gchar*           grss_feed_item_get_comments_url (GrssFeedItem *item);
 
 void                   grss_feed_item_set_geo_point    (GrssFeedItem *item, double latitude, double 
longitude);
diff --git a/src/utils.c b/src/utils.c
index 6bf66c6..619ce00 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -629,3 +629,29 @@ address_seems_public (GInetAddress *addr)
                g_inet_address_get_is_mc_org_local (addr) == FALSE &&
                g_inet_address_get_is_mc_global (addr) == FALSE);
 }
+
+gboolean
+test_url (const gchar *url)
+{
+       gboolean ret;
+       SoupURI *test_uri;
+
+       if (url == NULL)
+               return TRUE;
+
+       ret = TRUE;
+       test_uri = soup_uri_new (url);
+
+       if (test_uri == NULL) {
+               ret = FALSE;
+       }
+       else {
+               if (SOUP_URI_VALID_FOR_HTTP (test_uri) == FALSE)
+                       ret = FALSE;
+
+               soup_uri_free (test_uri);
+       }
+
+       return ret;
+}
+
diff --git a/src/utils.h b/src/utils.h
index f71b166..4ba0992 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -41,6 +41,7 @@
 
 #define FREE_STRING(__str)     if (__str) { g_free (__str); __str = NULL; }
 #define FREE_OBJECT(__obj)     if (__obj) { g_object_unref (__obj); __obj = NULL; }
+#define SET_STRING(__str)      (__str == NULL ? NULL : g_strdup (__str))
 
 gchar*         unhtmlize               (gchar *string);
 gchar*         unxmlize                (gchar * string);
@@ -56,4 +57,6 @@ gchar*                date_to_ISO8601         (time_t date);
 GInetAddress*  detect_internet_address ();
 gboolean       address_seems_public    (GInetAddress *addr);
 
+gboolean       test_url                (const gchar *url);
+
 #endif /* __UTILS_LIBGRSS_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]