RSSyl: support HTTP basic auth in libfeed
[claws.git] / src / plugins / rssyl / libfeed / feed.c
index cc8c49f86af0b652202df1c0d2dc92164d178ae3..cd507049d22b09b4cf72d8ac40aac0fc26086972 100644 (file)
@@ -40,11 +40,13 @@ 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;
        feed->author = NULL;
        feed->generator = NULL;
+       feed->link = NULL;
        feed->items = NULL;
 
        feed->fetcherr = NULL;
@@ -58,17 +60,34 @@ 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);
        g_free(feed->author);
        g_free(feed->generator);
+       g_free(feed->link);
        g_free(feed->fetcherr);
        g_free(feed->cookies_path);
 
@@ -126,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)
 {
@@ -270,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);
 
@@ -279,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. */