talons

Fork of Claws Mail https://www.claws-mail
Log | Files | Refs | README | LICENSE

commit 1d559b2de3bc4832936f462fb7a34f0252053518
parent d9b0f9d8937451a7f4e1e1041a17eaccf0d044d9
Author: Ricardo Mones <ricardo@mones.org>
Date:   Sat, 15 Nov 2014 12:30:14 +0100

RSSyl: support HTTP basic auth in libfeed

Diffstat:
Msrc/plugins/rssyl/libfeed/feed.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/plugins/rssyl/libfeed/feed.h | 19++++++++++++++++++-
2 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/src/plugins/rssyl/libfeed/feed.c b/src/plugins/rssyl/libfeed/feed.c @@ -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. */ diff --git a/src/plugins/rssyl/libfeed/feed.h b/src/plugins/rssyl/libfeed/feed.h @@ -30,9 +30,22 @@ 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);