Fix segfault and memleaks in curl
authorNikolas Garofil <garo@dunaldi.garofil.be>
Sun, 8 Nov 2009 20:49:28 +0000 (21:49 +0100)
committerNikolas Garofil <garo@dunaldi.garofil.be>
Sun, 8 Nov 2009 20:49:28 +0000 (21:49 +0100)
src/ccurl_thread.c

index d69e860..f891754 100644 (file)
@@ -122,26 +122,35 @@ void ccurl_fetch_data(ccurl_location_t *curloc)
        chunk.memory = NULL;
        chunk.size = 0;
 
-       curl = curl_easy_init();
-       if (curl) {
-               DBGP("reading curl data from '%s'", curloc->uri);
-               curl_easy_setopt(curl, CURLOPT_URL, curloc->uri);
-               curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
-               curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ccurl_write_memory_callback);
-               curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &chunk);
-               curl_easy_setopt(curl, CURLOPT_USERAGENT, "conky-curl/1.0");
-
-               res = curl_easy_perform(curl);
-               if (res == CURLE_OK && chunk.size) {
-                       timed_thread_lock(curloc->p_timed_thread);
-                       (*curloc->process_function)(curloc->result, chunk.memory);
-                       timed_thread_unlock(curloc->p_timed_thread);
-                       free(chunk.memory);
-               } else {
-                       NORM_ERR("curl: no data from server");
+       if (curl_global_init(CURL_GLOBAL_ALL) == 0) {
+               curl = curl_easy_init();
+               if (curl) {
+                       DBGP("reading curl data from '%s'", curloc->uri);
+                       curl_easy_setopt(curl, CURLOPT_URL, curloc->uri);
+                       curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
+                       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ccurl_write_memory_callback);
+                       curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &chunk);
+                       curl_easy_setopt(curl, CURLOPT_USERAGENT, "conky-curl/1.0");
+
+                       res = curl_easy_perform(curl);
+                       if (res == CURLE_OK && chunk.size) {
+                               long http_status_code;
+
+                               if(curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_status_code) == CURLE_OK && http_status_code == 200) {
+                                       timed_thread_lock(curloc->p_timed_thread);
+                                       (*curloc->process_function)(curloc->result, chunk.memory);
+                                       timed_thread_unlock(curloc->p_timed_thread);
+                               } else {
+                                       NORM_ERR("curl: no data from server");
+                               }
+                               free(chunk.memory);
+                       } else {
+                               NORM_ERR("curl: no data from server");
+                       }
+
+                       curl_easy_cleanup(curl);
                }
-
-               curl_easy_cleanup(curl);
+               curl_global_cleanup();
        }
 }