1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Please see COPYING for details
8 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
10 * All rights reserved.
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "ccurl_thread.h"
29 #include "text_object.h"
35 #include <curl/curl.h>
36 #include <curl/types.h>
37 #include <curl/easy.h>
40 * The following code is the conky curl thread lib, which can be re-used to
41 * create any curl-based object (see weather and rss). Below is an
42 * implementation of a curl-only object ($curl) which can also be used as an
45 typedef struct _ccurl_memory_t {
50 /* finds a location based on uri in the list provided */
51 ccurl_location_t *ccurl_find_location(ccurl_location_t **locations_head, char *uri)
53 ccurl_location_t *tail = *locations_head;
54 ccurl_location_t *new = 0;
57 strcmp(tail->uri, uri) == EQUAL) {
62 if (!tail) { /* new location!!!!!!! */
63 DBGP("new curl location: '%s'", uri);
64 new = malloc(sizeof(ccurl_location_t));
65 memset(new, 0, sizeof(ccurl_location_t));
66 new->uri = strndup(uri, text_buffer_size);
67 tail = *locations_head;
68 while (tail && tail->next) {
72 /* omg the first one!!!!!!! */
73 *locations_head = new;
81 /* iterates over the list provided, frees stuff (list item, uri, result) */
82 void ccurl_free_locations(ccurl_location_t **locations_head)
84 ccurl_location_t *tail = *locations_head;
85 ccurl_location_t *last = 0;
88 if (tail->uri) free(tail->uri);
89 if (tail->result) free(tail->result);
97 /* callback used by curl for writing the received data */
98 size_t ccurl_write_memory_callback(void *ptr, size_t size, size_t nmemb, void *data)
100 size_t realsize = size * nmemb;
101 ccurl_memory_t *mem = (ccurl_memory_t*)data;
103 mem->memory = (char *) realloc(mem->memory, mem->size + realsize + 1);
105 memcpy(&(mem->memory[mem->size]), ptr, realsize);
106 mem->size += realsize;
107 mem->memory[mem->size] = 0;
113 /* fetch our datums */
114 void ccurl_fetch_data(ccurl_location_t *curloc)
120 ccurl_memory_t chunk;
125 if (curl_global_init(CURL_GLOBAL_ALL) == 0) {
126 curl = curl_easy_init();
128 DBGP("reading curl data from '%s'", curloc->uri);
129 curl_easy_setopt(curl, CURLOPT_URL, curloc->uri);
130 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
131 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ccurl_write_memory_callback);
132 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &chunk);
133 curl_easy_setopt(curl, CURLOPT_USERAGENT, "conky-curl/1.0");
135 res = curl_easy_perform(curl);
136 if (res == CURLE_OK && chunk.size) {
137 long http_status_code;
139 if(curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_status_code) == CURLE_OK && http_status_code == 200) {
140 timed_thread_lock(curloc->p_timed_thread);
141 (*curloc->process_function)(curloc->result, chunk.memory);
142 timed_thread_unlock(curloc->p_timed_thread);
144 NORM_ERR("curl: no data from server");
148 NORM_ERR("curl: no data from server");
151 curl_easy_cleanup(curl);
153 curl_global_cleanup();
157 void *ccurl_thread(void *) __attribute__((noreturn));
159 void ccurl_init_thread(ccurl_location_t *curloc, int interval)
162 assert(curloc->result);
164 curloc->p_timed_thread =
165 timed_thread_create(&ccurl_thread,
166 (void *)curloc, interval * 1000000);
168 if (!curloc->p_timed_thread) {
169 NORM_ERR("curl thread: error creating timed thread");
171 timed_thread_register(curloc->p_timed_thread,
172 &curloc->p_timed_thread);
173 if (timed_thread_run(curloc->p_timed_thread)) {
174 NORM_ERR("curl thread: error running timed thread");
178 void *ccurl_thread(void *arg)
180 ccurl_location_t *curloc = (ccurl_location_t*)arg;
183 ccurl_fetch_data(curloc);
184 if (timed_thread_test(curloc->p_timed_thread, 0)) {
185 timed_thread_exit(curloc->p_timed_thread);
193 * This is where the $curl section begins.
201 /* internal location pointer for use by $curl, no touchy */
202 static ccurl_location_t *ccurl_locations_head = 0;
204 /* used to free data used by $curl */
205 void ccurl_free_info(void)
207 ccurl_free_locations(&ccurl_locations_head);
210 /* straight copy, used by $curl */
211 static void ccurl_parse_data(void *result, const char *data)
213 strncpy(result, data, max_user_text);
216 /* prints result data to text buffer, used by $curl */
217 void ccurl_process_info(char *p, int p_max_size, char *uri, int interval)
219 ccurl_location_t *curloc = ccurl_find_location(&ccurl_locations_head, uri);
220 if (!curloc->p_timed_thread) {
221 curloc->result = malloc(max_user_text);
222 memset(curloc->result, 0, max_user_text);
223 curloc->process_function = &ccurl_parse_data;
224 ccurl_init_thread(curloc, interval);
225 if (!curloc->p_timed_thread) {
226 NORM_ERR("error setting up curl thread");
230 timed_thread_lock(curloc->p_timed_thread);
231 strncpy(p, curloc->result, p_max_size);
232 timed_thread_unlock(curloc->p_timed_thread);
235 void curl_parse_arg(struct text_object *obj, const char *arg)
238 struct curl_data *cd;
241 cd = malloc(sizeof(struct curl_data));
242 memset(cd, 0, sizeof(struct curl_data));
244 argc = sscanf(arg, "%127s %f", cd->uri, &interval);
247 NORM_ERR("wrong number of arguments for $curl");
250 cd->interval = interval > 0 ? interval * 60 : 15*60;
251 obj->data.opaque = cd;
254 void curl_print(struct text_object *obj, char *p, int p_max_size)
256 struct curl_data *cd = obj->data.opaque;
258 if (!cd || !cd->uri) {
259 NORM_ERR("error processing Curl data");
262 ccurl_process_info(p, p_max_size, cd->uri, cd->interval);
265 void curl_obj_free(struct text_object *obj)
267 if (obj->data.opaque) {
268 free(obj->data.opaque);
269 obj->data.opaque = NULL;