Fix bug 4325 "Following redirects when retrieving image"
[claws.git] / src / plugins / libravatar / libravatar_image.c
index 02c7e4b342f53c87287c1e659b315c85d8b4d674..b5675aac2dd2ea499c96214f2b80d3944de78123 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <common/claws.h>
 #include <prefs_common.h>
 
 #include <common/claws.h>
 #include <prefs_common.h>
+#include <file-utils.h>
 
 #include "libravatar.h"
 #include "libravatar_prefs.h"
 
 #include "libravatar.h"
 #include "libravatar_prefs.h"
@@ -35,8 +36,8 @@
 
 static size_t write_image_data_cb(void *ptr, size_t size, size_t nmemb, void *stream)
 {
 
 static size_t write_image_data_cb(void *ptr, size_t size, size_t nmemb, void *stream)
 {
-       size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
-       debug_print("received %zu bytes from avatar server\n", written);
+       size_t written = claws_fwrite(ptr, size, nmemb, (FILE *)stream);
+       debug_print("received %"G_GSIZE_FORMAT" bytes from avatar server\n", written);
 
        return written;
 }
 
        return written;
 }
@@ -71,9 +72,10 @@ static GdkPixbuf *pixbuf_from_url(const gchar *url, const gchar *md5, const gcha
        GdkPixbuf *image = NULL;
        FILE *file;
        CURL *curl;
        GdkPixbuf *image = NULL;
        FILE *file;
        CURL *curl;
+       CURLcode res;
        long filesize;
 
        long filesize;
 
-       file = fopen(filename, "wb");
+       file = claws_fopen(filename, "wb");
        if (file == NULL) {
                g_warning("could not open '%s' for writing", filename);
                return NULL;
        if (file == NULL) {
                g_warning("could not open '%s' for writing", filename);
                return NULL;
@@ -81,7 +83,7 @@ static GdkPixbuf *pixbuf_from_url(const gchar *url, const gchar *md5, const gcha
        curl = curl_easy_init();
        if (curl == NULL) {
                g_warning("could not initialize curl to get image from URL");
        curl = curl_easy_init();
        if (curl == NULL) {
                g_warning("could not initialize curl to get image from URL");
-               fclose(file);
+               claws_fclose(file);
                return NULL;
        }
 
                return NULL;
        }
 
@@ -97,28 +99,37 @@ static GdkPixbuf *pixbuf_from_url(const gchar *url, const gchar *md5, const gcha
        curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
 
        if (libravatarprefs.allow_redirects) {
        curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
 
        if (libravatarprefs.allow_redirects) {
-               long maxredirs = (libravatarprefs.default_mode == DEF_MODE_URL)? 3L
-                       : ((libravatarprefs.default_mode == DEF_MODE_MM)? 2L: 1L);
+               long maxredirs = (libravatarprefs.default_mode == DEF_MODE_URL)
+                       ? libravatarprefs.max_redirects_url
+                       : ((libravatarprefs.default_mode == DEF_MODE_MM)
+                               ? libravatarprefs.max_redirects_mm
+                               : libravatarprefs.max_redirects_url);
+               debug_print("setting max redirects to %ld\n", maxredirs);
                curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
                curl_easy_setopt(curl, CURLOPT_MAXREDIRS, maxredirs);
        }
        curl_easy_setopt(curl, CURLOPT_FILE, file);
        debug_print("retrieving URL to file: %s -> %s\n", url, filename);
                curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
                curl_easy_setopt(curl, CURLOPT_MAXREDIRS, maxredirs);
        }
        curl_easy_setopt(curl, CURLOPT_FILE, file);
        debug_print("retrieving URL to file: %s -> %s\n", url, filename);
-       curl_easy_perform(curl);
-       filesize = ftell(file);
-       fclose(file);
-       if (filesize < MIN_PNG_SIZE)
-               debug_print("not enough data for an avatar image: %ld bytes\n", filesize);
-       else
-               image = image_pixbuf_from_filename(filename);
-
-       if (!libravatarprefs.cache_icons || filesize == 0) {
-               if (g_unlink(filename) < 0)
-                       g_warning("failed to delete cache file '%s'", filename);
-       }
+       res = curl_easy_perform(curl);
+       if (res != CURLE_OK) {
+               debug_print("curl_easy_perfom failed: %s", curl_easy_strerror(res));
+               claws_safe_fclose(file);
+       } else {
+               filesize = ftell(file);
+               claws_safe_fclose(file);
+               if (filesize < MIN_PNG_SIZE)
+                       debug_print("not enough data for an avatar image: %ld bytes\n", filesize);
+               else
+                       image = image_pixbuf_from_filename(filename);
+
+               if (!libravatarprefs.cache_icons || filesize == 0) {
+                       if (g_unlink(filename) < 0)
+                               g_warning("failed to delete cache file '%s'", filename);
+               }
 
 
-       if (filesize == 0)
-               missing_add_md5(libravatarmisses, md5);
+               if (filesize == 0)
+                       missing_add_md5(libravatarmisses, md5);
+       }
 
        curl_easy_cleanup(curl);
 
 
        curl_easy_cleanup(curl);