RSSyl: support HTTP basic auth in libfeed
authorRicardo Mones <ricardo@mones.org>
Sat, 15 Nov 2014 11:30:14 +0000 (12:30 +0100)
committerRicardo Mones <ricardo@mones.org>
Tue, 13 Jan 2015 18:33:47 +0000 (19:33 +0100)
src/plugins/rssyl/libfeed/feed.c
src/plugins/rssyl/libfeed/feed.h

index ca048eb..cd50704 100644 (file)
@@ -40,6 +40,7 @@ Feed *feed_new(gchar *url)
 
        feed->timeout = FEED_DEFAULT_TIMEOUT;
        feed->url = g_strdup(url);
+       feed->auth = NULL;
        feed->title = NULL;
        feed->description = NULL;
        feed->language = NULL;
@@ -59,12 +60,28 @@ static void _free_items(gpointer item, gpointer nada)
        feed_item_free(item);
 }
 
+static void _free_auth(Feed *feed)
+{
+       if (feed == NULL)
+               return;
+
+       if (feed->auth != NULL) {
+                if (feed->auth->username != NULL)
+                        g_free(feed->auth->username);
+                if (feed->auth->password != NULL)
+                        g_free(feed->auth->password);
+                g_free(feed->auth);
+               feed->auth = NULL;
+        }
+}
+
 void feed_free(Feed *feed)
 {
        if( feed == NULL )
                return; /* Return silently, without printing a glib error. */
 
        g_free(feed->url);
+       _free_auth(feed);
        g_free(feed->title);
        g_free(feed->description);
        g_free(feed->language);
@@ -128,6 +145,25 @@ gchar *feed_get_url(Feed *feed)
        return feed->url;
 }
 
+/* Auth */
+void feed_set_auth(Feed *feed, FeedAuth *auth)
+{
+       g_return_if_fail(feed != NULL);
+       g_return_if_fail(auth != NULL);
+
+       _free_auth(feed);
+       feed->auth = g_new0(FeedAuth, 1);
+       feed->auth->type = auth->type;
+       feed->auth->username = g_strdup(auth->username);
+       feed->auth->password = g_strdup(auth->password);
+}
+
+FeedAuth *feed_get_auth(Feed *feed)
+{
+       g_return_val_if_fail(feed != NULL, NULL);
+       return feed->auth;
+}
+
 /* Title */
 gchar *feed_get_title(Feed *feed)
 {
@@ -272,6 +308,23 @@ guint feed_update(Feed *feed, time_t last_update)
        if(feed->cookies_path != NULL)
                curl_easy_setopt(eh, CURLOPT_COOKIEFILE, feed->cookies_path);
 
+       if (feed->auth != NULL) {
+               switch (feed->auth->type) {
+               case FEED_AUTH_NONE:
+                       break;
+               case FEED_AUTH_BASIC:
+                       curl_easy_setopt(eh, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
+                       curl_easy_setopt(eh, CURLOPT_USERNAME,
+                                        feed->auth->username);
+                       curl_easy_setopt(eh, CURLOPT_PASSWORD,
+                                        feed->auth->password);
+                       break;
+               default:
+                       response_code = FEED_ERR_UNAUTH; /* unknown auth */
+                       goto cleanup;
+               }
+       }
+
        res = curl_easy_perform(eh);
        XML_Parse(feed_ctx->parser, "", 0, TRUE);
 
@@ -281,6 +334,7 @@ guint feed_update(Feed *feed, time_t last_update)
        } else
                curl_easy_getinfo(eh, CURLINFO_RESPONSE_CODE, &response_code);
 
+cleanup:
        curl_easy_cleanup(eh);
 
        /* Cleanup, we should be done. */
index af9fc3b..5abde34 100644 (file)
 typedef struct _Feed Feed;
 typedef struct _FeedItem FeedItem;
 typedef struct _FeedParserCtx FeedParserCtx;
+typedef struct _FeedAuth FeedAuth;
+
+typedef enum {
+       FEED_AUTH_NONE,
+       FEED_AUTH_BASIC
+} FeedAuthType;
+
+struct _FeedAuth {
+       FeedAuthType type;
+       gchar *username;
+       gchar *password;
+};
        
 struct _Feed {
        gchar *url;
+       FeedAuth *auth;
        gchar *title;
        gchar *description;
        gchar *language;
@@ -66,7 +79,8 @@ typedef enum {
        FEED_ERR_NOFEED,
        FEED_ERR_NOURL,
        FEED_ERR_INIT,
-       FEED_ERR_FETCH
+       FEED_ERR_FETCH,
+       FEED_ERR_UNAUTH
 } FeedErrCodes;
 
 /* ---------------- Prototypes */
@@ -82,6 +96,9 @@ guint feed_get_timeout(Feed *feed);
 void feed_set_url(Feed *feed, gchar *url);
 gchar *feed_get_url(Feed *feed);
 
+void feed_set_auth(Feed *feed, FeedAuth *auth);
+FeedAuth *feed_get_auth(Feed *feed);
+
 gchar *feed_get_title(Feed *feed);
 void feed_set_title(Feed *feed, gchar *new_title);