1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
* vim: ts=4 sw=4 noet ai cindent syntax=c
*
* Conky, a system monitor, based on torsmo
*
* Please see COPYING for details
*
* Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
* (see AUTHORS)
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "conky.h"
#include "logging.h"
#include "ccurl_thread.h"
#include "text_object.h"
#ifdef DEBUG
#include <assert.h>
#endif /* DEBUG */
#include <curl/curl.h>
#include <curl/easy.h>
/*
* The following code is the conky curl thread lib, which can be re-used to
* create any curl-based object (see weather and rss). Below is an
* implementation of a curl-only object ($curl) which can also be used as an
* example.
*/
typedef struct _ccurl_memory_t {
char *memory;
size_t size;
} ccurl_memory_t;
typedef struct _ccurl_headers_t {
char *last_modified;
char *etag;
} ccurl_headers_t;
/* finds a location based on uri in the list provided */
ccurl_location_t *ccurl_find_location(ccurl_location_t **locations_head, char *uri)
{
ccurl_location_t *tail = *locations_head;
ccurl_location_t *new = 0;
while (tail) {
if (tail->uri &&
strcmp(tail->uri, uri) == EQUAL) {
return tail;
}
tail = tail->next;
}
if (!tail) { /* new location!!!!!!! */
DBGP("new curl location: '%s'", uri);
new = malloc(sizeof(ccurl_location_t));
memset(new, 0, sizeof(ccurl_location_t));
new->uri = strndup(uri, text_buffer_size);
tail = *locations_head;
while (tail && tail->next) {
tail = tail->next;
}
if (!tail) {
/* omg the first one!!!!!!! */
*locations_head = new;
} else {
tail->next = new;
}
}
return new;
}
/* iterates over the list provided, frees stuff (list item, uri, result) */
void ccurl_free_locations(ccurl_location_t **locations_head)
{
ccurl_location_t *tail = *locations_head;
ccurl_location_t *last = 0;
while (tail) {
if (tail->uri) free(tail->uri);
if (tail->result) free(tail->result);
if (tail->last_modified) free(tail->last_modified);
if (tail->etag) free(tail->etag);
last = tail;
tail = tail->next;
free(last);
}
*locations_head = 0;
}
/* callback used by curl for parsing the header data */
size_t ccurl_parse_header_callback(void *ptr, size_t size, size_t nmemb, void *data)
{
size_t realsize = size * nmemb;
const char *value = (const char*)ptr;
char *end;
ccurl_headers_t *headers = (ccurl_headers_t*)data;
if (strncmp(value, "Last-Modified: ", 15) == EQUAL) {
headers->last_modified = strndup(value + 15, realsize - 15);
if ((end = strchr(headers->last_modified, '\r')) != NULL) {
*end = '\0';
}
} else if (strncmp(value,"ETag: ", 6) == EQUAL) {
headers->etag = strndup(value + 6, realsize - 6);
if ((end = strchr(headers->etag, '\r')) != NULL) {
*end = '\0';
}
}
return realsize;
}
/* callback used by curl for writing the received data */
size_t ccurl_write_memory_callback(void *ptr, size_t size, size_t nmemb, void *data)
{
size_t realsize = size * nmemb;
ccurl_memory_t *mem = (ccurl_memory_t*)data;
mem->memory = (char *) realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory) {
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
}
return realsize;
}
/* fetch our datums */
void ccurl_fetch_data(ccurl_location_t *curloc)
{
CURL *curl = NULL;
CURLcode res;
struct curl_slist *headers = NULL;
// curl temps
ccurl_memory_t chunk;
ccurl_headers_t response_headers;
chunk.memory = NULL;
chunk.size = 0;
memset(&response_headers, 0, sizeof(ccurl_headers_t));
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_HEADERFUNCTION, ccurl_parse_header_callback);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void *) &response_headers);
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.1");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1000);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 60);
if (curloc->last_modified) {
const char *header = "If-Modified-Since: ";
int len = strlen(header) + strlen(curloc->last_modified) + 1;
char *str = (char*) malloc(len);
snprintf(str, len, "%s%s", header, curloc->last_modified);
headers = curl_slist_append(headers, str);
free(str);
}
if (curloc->etag) {
const char *header = "If-None-Match: ";
int len = strlen(header) + strlen(curloc->etag) + 1;
char *str = (char*) malloc(len);
snprintf(str, len, "%s%s", header, curloc->etag);
headers = curl_slist_append(headers, str);
free(str);
}
if (headers) {
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
}
res = curl_easy_perform(curl);
if (res == CURLE_OK) {
long http_status_code;
if (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE,
&http_status_code) == CURLE_OK) {
switch (http_status_code) {
case 200:
timed_thread_lock(curloc->p_timed_thread);
if(curloc->last_modified) {
free(curloc->last_modified);
curloc->last_modified = NULL;
}
if(curloc->etag) {
free(curloc->etag);
curloc->etag = NULL;
}
if (response_headers.last_modified) {
curloc->last_modified =
strdup(response_headers.last_modified);
}
if (response_headers.etag) {
curloc->etag = strdup(response_headers.etag);
}
(*curloc->process_function)(curloc->result, chunk.memory);
timed_thread_unlock(curloc->p_timed_thread);
break;
case 304:
break;
default:
NORM_ERR("curl: no data from server, got HTTP status %ld",
http_status_code);
break;
}
} else {
NORM_ERR("curl: no HTTP status from server");
}
free(chunk.memory);
} else {
NORM_ERR("curl: could not retrieve data from server");
}
if(response_headers.last_modified) {
free(response_headers.last_modified);
}
if(response_headers.etag) {
free(response_headers.etag);
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
}
void *ccurl_thread(void *) __attribute__((noreturn));
void ccurl_init_thread(ccurl_location_t *curloc, int interval)
{
#ifdef DEBUG
assert(curloc->result);
#endif /* DEBUG */
curloc->p_timed_thread =
timed_thread_create(&ccurl_thread,
(void *)curloc, interval * 1000000);
if (!curloc->p_timed_thread) {
NORM_ERR("curl thread: error creating timed thread");
}
timed_thread_register(curloc->p_timed_thread,
&curloc->p_timed_thread);
if (timed_thread_run(curloc->p_timed_thread)) {
NORM_ERR("curl thread: error running timed thread");
}
}
void *ccurl_thread(void *arg)
{
ccurl_location_t *curloc = (ccurl_location_t*)arg;
while (1) {
ccurl_fetch_data(curloc);
if (timed_thread_test(curloc->p_timed_thread, 0)) {
timed_thread_exit(curloc->p_timed_thread);
}
}
/* never reached */
}
/*
* This is where the $curl section begins.
*/
struct curl_data {
char uri[128];
float interval;
};
/* internal location pointer for use by $curl, no touchy */
static ccurl_location_t *ccurl_locations_head = 0;
/* used to free data used by $curl */
void ccurl_free_info(void)
{
ccurl_free_locations(&ccurl_locations_head);
}
/* straight copy, used by $curl */
static void ccurl_parse_data(void *result, const char *data)
{
strncpy(result, data, max_user_text);
}
/* prints result data to text buffer, used by $curl */
void ccurl_process_info(char *p, int p_max_size, char *uri, int interval)
{
ccurl_location_t *curloc = ccurl_find_location(&ccurl_locations_head, uri);
if (!curloc->p_timed_thread) {
curloc->result = malloc(max_user_text);
memset(curloc->result, 0, max_user_text);
curloc->process_function = &ccurl_parse_data;
ccurl_init_thread(curloc, interval);
if (!curloc->p_timed_thread) {
NORM_ERR("error setting up curl thread");
}
}
timed_thread_lock(curloc->p_timed_thread);
strncpy(p, curloc->result, p_max_size);
timed_thread_unlock(curloc->p_timed_thread);
}
void curl_parse_arg(struct text_object *obj, const char *arg)
{
int argc;
struct curl_data *cd;
float interval = 0;
cd = malloc(sizeof(struct curl_data));
memset(cd, 0, sizeof(struct curl_data));
argc = sscanf(arg, "%127s %f", cd->uri, &interval);
if (argc < 1) {
free(cd);
NORM_ERR("wrong number of arguments for $curl");
return;
}
cd->interval = interval > 0 ? interval * 60 : 15*60;
obj->data.opaque = cd;
}
void curl_print(struct text_object *obj, char *p, int p_max_size)
{
struct curl_data *cd = obj->data.opaque;
if (!cd || !cd->uri) {
NORM_ERR("error processing Curl data");
return;
}
ccurl_process_info(p, p_max_size, cd->uri, cd->interval);
}
void curl_obj_free(struct text_object *obj)
{
if (obj->data.opaque) {
free(obj->data.opaque);
obj->data.opaque = NULL;
}
}
|