2010-01-07 03:45:19 +00:00
|
|
|
/* -*- mode: c++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
|
|
|
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
|
2009-07-28 21:44:22 +00:00
|
|
|
*
|
2009-07-20 15:21:33 +00:00
|
|
|
* Conky, a system monitor, based on torsmo
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
2010-01-01 23:46:17 +00:00
|
|
|
* Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
|
2009-07-20 15:21:33 +00:00
|
|
|
* (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"
|
2009-10-04 19:06:52 +00:00
|
|
|
#include "text_object.h"
|
2010-01-04 04:50:02 +00:00
|
|
|
#include <mutex>
|
2009-07-20 15:21:33 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <curl/types.h>
|
|
|
|
#include <curl/easy.h>
|
|
|
|
|
2009-07-20 18:02:53 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-07-20 15:21:33 +00:00
|
|
|
typedef struct _ccurl_memory_t {
|
|
|
|
char *memory;
|
|
|
|
size_t size;
|
|
|
|
} ccurl_memory_t;
|
|
|
|
|
2010-10-14 03:00:46 +00:00
|
|
|
typedef struct _ccurl_headers_t {
|
|
|
|
char *last_modified;
|
|
|
|
char *etag;
|
|
|
|
} ccurl_headers_t;
|
|
|
|
|
2009-07-20 18:02:53 +00:00
|
|
|
/* finds a location based on uri in the list provided */
|
2010-10-15 00:28:57 +00:00
|
|
|
ccurl_location_ptr ccurl_find_location(ccurl_location_list &locations, const std::string &uri)
|
2009-07-20 15:21:33 +00:00
|
|
|
{
|
2010-01-04 04:50:02 +00:00
|
|
|
for (ccurl_location_list::iterator i = locations.begin();
|
|
|
|
i != locations.end(); i++) {
|
2010-10-15 00:28:57 +00:00
|
|
|
if ((*i)->uri == std::string(uri)) {
|
2010-01-04 04:50:02 +00:00
|
|
|
return *i;
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-04 04:50:02 +00:00
|
|
|
ccurl_location_ptr next = ccurl_location_ptr(new ccurl_location_t);
|
2010-10-15 00:28:57 +00:00
|
|
|
DBGP("new curl location: '%s'", uri.c_str());
|
2010-11-12 19:00:24 +00:00
|
|
|
next->uri = uri;
|
2010-01-04 04:50:02 +00:00
|
|
|
locations.push_back(next);
|
|
|
|
return next;
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
|
|
|
|
2009-07-20 18:02:53 +00:00
|
|
|
/* iterates over the list provided, frees stuff (list item, uri, result) */
|
2010-01-04 04:50:02 +00:00
|
|
|
void ccurl_free_locations(ccurl_location_list &locations)
|
2009-07-20 15:21:33 +00:00
|
|
|
{
|
2010-01-04 04:50:02 +00:00
|
|
|
for (ccurl_location_list::iterator i = locations.begin();
|
|
|
|
i != locations.end(); i++) {
|
2010-02-23 13:54:03 +00:00
|
|
|
free_and_zero((*i)->result);
|
2010-04-19 20:25:50 +00:00
|
|
|
(*i)->p_timed_thread.reset();
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
2010-01-04 04:50:02 +00:00
|
|
|
locations.clear();
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
|
|
|
|
2010-10-14 03:00:46 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2009-07-20 18:02:53 +00:00
|
|
|
/* callback used by curl for writing the received data */
|
2009-07-20 15:21:33 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-20 18:02:53 +00:00
|
|
|
/* fetch our datums */
|
2010-06-17 13:19:31 +00:00
|
|
|
void ccurl_fetch_data(thread_handle &handle, const ccurl_location_ptr &curloc)
|
2009-07-20 15:21:33 +00:00
|
|
|
{
|
|
|
|
CURL *curl = NULL;
|
|
|
|
CURLcode res;
|
2010-10-14 03:00:46 +00:00
|
|
|
struct curl_slist *headers = NULL;
|
2009-07-20 15:21:33 +00:00
|
|
|
|
2010-10-15 00:28:57 +00:00
|
|
|
/* curl temps */
|
2009-07-20 15:21:33 +00:00
|
|
|
ccurl_memory_t chunk;
|
2010-10-14 03:00:46 +00:00
|
|
|
ccurl_headers_t response_headers;
|
2009-07-20 15:21:33 +00:00
|
|
|
|
|
|
|
chunk.memory = NULL;
|
|
|
|
chunk.size = 0;
|
2010-10-14 03:00:46 +00:00
|
|
|
memset(&response_headers, 0, sizeof(ccurl_headers_t));
|
2009-07-20 15:21:33 +00:00
|
|
|
|
2010-08-29 11:31:18 +00:00
|
|
|
curl = curl_easy_init();
|
|
|
|
if (curl) {
|
2010-10-15 00:28:57 +00:00
|
|
|
DBGP("reading curl data from '%s'", curloc->uri.c_str());
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, curloc->uri.c_str());
|
2010-08-29 11:31:18 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
|
2010-10-14 03:00:46 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, ccurl_parse_header_callback);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void *) &response_headers);
|
2010-08-29 11:31:18 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ccurl_write_memory_callback);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &chunk);
|
2010-10-14 03:00:46 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, "conky-curl/1.1");
|
2010-10-05 18:53:55 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
|
2010-10-05 19:06:49 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1000);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 60);
|
2010-08-29 11:31:18 +00:00
|
|
|
|
2010-10-15 00:28:57 +00:00
|
|
|
if (!curloc->last_modified.empty()) {
|
2010-10-14 03:00:46 +00:00
|
|
|
const char *header = "If-Modified-Since: ";
|
2010-10-15 00:28:57 +00:00
|
|
|
int len = strlen(header) + curloc->last_modified.size() + 1;
|
2010-10-14 03:00:46 +00:00
|
|
|
char *str = (char*) malloc(len);
|
2010-10-15 00:28:57 +00:00
|
|
|
snprintf(str, len, "%s%s", header, curloc->last_modified.c_str());
|
2010-10-14 03:00:46 +00:00
|
|
|
headers = curl_slist_append(headers, str);
|
|
|
|
free(str);
|
|
|
|
}
|
2010-10-15 00:28:57 +00:00
|
|
|
if (!curloc->etag.empty()) {
|
2010-10-14 03:00:46 +00:00
|
|
|
const char *header = "If-None-Match: ";
|
2010-10-15 00:28:57 +00:00
|
|
|
int len = strlen(header) + curloc->etag.size() + 1;
|
2010-10-14 03:00:46 +00:00
|
|
|
char *str = (char*) malloc(len);
|
2010-10-15 00:28:57 +00:00
|
|
|
snprintf(str, len, "%s%s", header, curloc->etag.c_str());
|
2010-10-14 03:00:46 +00:00
|
|
|
headers = curl_slist_append(headers, str);
|
|
|
|
free(str);
|
|
|
|
}
|
|
|
|
if (headers) {
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
|
|
|
}
|
2010-08-29 11:31:18 +00:00
|
|
|
|
|
|
|
res = curl_easy_perform(curl);
|
2010-10-14 03:00:46 +00:00
|
|
|
if (res == CURLE_OK) {
|
2010-08-29 11:31:18 +00:00
|
|
|
long http_status_code;
|
|
|
|
|
2010-10-05 18:53:55 +00:00
|
|
|
if (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE,
|
|
|
|
&http_status_code) == CURLE_OK) {
|
|
|
|
switch (http_status_code) {
|
|
|
|
case 200:
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(handle.mutex());
|
2010-10-15 00:28:57 +00:00
|
|
|
curloc->last_modified.clear();
|
|
|
|
curloc->etag.clear();
|
2010-10-14 03:00:46 +00:00
|
|
|
if (response_headers.last_modified) {
|
|
|
|
curloc->last_modified =
|
2010-10-15 00:28:57 +00:00
|
|
|
std::string(response_headers.last_modified);
|
2010-10-14 03:00:46 +00:00
|
|
|
}
|
|
|
|
if (response_headers.etag) {
|
2010-10-15 00:28:57 +00:00
|
|
|
curloc->etag = std::string(response_headers.etag);
|
2010-10-14 03:00:46 +00:00
|
|
|
}
|
2010-10-05 18:53:55 +00:00
|
|
|
curloc->process_function(curloc->result, chunk.memory);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 304:
|
|
|
|
break;
|
|
|
|
default:
|
2010-10-05 20:22:27 +00:00
|
|
|
NORM_ERR("curl: no data from server, got HTTP status %ld",
|
2010-10-05 18:53:55 +00:00
|
|
|
http_status_code);
|
|
|
|
break;
|
|
|
|
}
|
2009-11-08 20:49:28 +00:00
|
|
|
} else {
|
2010-10-05 18:53:55 +00:00
|
|
|
NORM_ERR("curl: no HTTP status from server");
|
2009-11-08 20:49:28 +00:00
|
|
|
}
|
2010-08-29 11:31:18 +00:00
|
|
|
free(chunk.memory);
|
|
|
|
} else {
|
2010-10-14 03:00:46 +00:00
|
|
|
NORM_ERR("curl: could not retrieve data from server");
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
2010-08-29 11:31:18 +00:00
|
|
|
|
2010-10-14 03:00:46 +00:00
|
|
|
free_and_zero(response_headers.last_modified);
|
|
|
|
free_and_zero(response_headers.etag);
|
|
|
|
curl_slist_free_all(headers);
|
2010-08-29 11:31:18 +00:00
|
|
|
curl_easy_cleanup(curl);
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-17 13:19:31 +00:00
|
|
|
void ccurl_thread(thread_handle &handle, const ccurl_location_ptr &curloc);
|
2009-07-20 15:21:33 +00:00
|
|
|
|
2010-06-17 13:19:31 +00:00
|
|
|
void ccurl_init_thread(const ccurl_location_ptr &curloc, int interval)
|
2009-07-20 15:21:33 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
assert(curloc->result);
|
|
|
|
#endif /* DEBUG */
|
2010-02-24 19:10:26 +00:00
|
|
|
curloc->p_timed_thread = timed_thread::create(std::bind(ccurl_thread,
|
2010-06-17 13:12:42 +00:00
|
|
|
std::placeholders::_1, curloc), std::chrono::seconds(interval));
|
2009-07-20 15:21:33 +00:00
|
|
|
|
|
|
|
if (!curloc->p_timed_thread) {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("curl thread: error creating timed thread");
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-17 13:19:31 +00:00
|
|
|
void ccurl_thread(thread_handle &handle, const ccurl_location_ptr &curloc)
|
2009-07-20 15:21:33 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
while (1) {
|
2010-01-04 04:50:02 +00:00
|
|
|
ccurl_fetch_data(handle, curloc);
|
|
|
|
if (handle.test(0)) {
|
|
|
|
return;
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* never reached */
|
|
|
|
}
|
|
|
|
|
2009-07-20 18:02:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is where the $curl section begins.
|
|
|
|
*/
|
|
|
|
|
2009-10-04 19:17:43 +00:00
|
|
|
struct curl_data {
|
|
|
|
char uri[128];
|
|
|
|
float interval;
|
|
|
|
};
|
|
|
|
|
2009-07-20 18:02:53 +00:00
|
|
|
/* internal location pointer for use by $curl, no touchy */
|
2010-01-04 04:50:02 +00:00
|
|
|
static ccurl_location_list ccurl_locations;
|
2009-07-20 15:21:33 +00:00
|
|
|
|
2009-07-20 18:02:53 +00:00
|
|
|
/* used to free data used by $curl */
|
2009-07-20 15:21:33 +00:00
|
|
|
void ccurl_free_info(void)
|
|
|
|
{
|
2010-01-04 04:50:02 +00:00
|
|
|
ccurl_free_locations(ccurl_locations);
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
|
|
|
|
2009-07-20 18:02:53 +00:00
|
|
|
/* straight copy, used by $curl */
|
2010-01-04 04:50:02 +00:00
|
|
|
static void ccurl_parse_data(char *result, const char *data)
|
2009-07-20 15:21:33 +00:00
|
|
|
{
|
2010-08-29 18:01:05 +00:00
|
|
|
if(result) strncpy(result, data, max_user_text.get(*state));
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
|
|
|
|
2009-07-20 18:02:53 +00:00
|
|
|
/* prints result data to text buffer, used by $curl */
|
2010-10-15 00:28:57 +00:00
|
|
|
void ccurl_process_info(char *p, int p_max_size, const std::string &uri, int interval)
|
2009-07-20 15:21:33 +00:00
|
|
|
{
|
2010-01-04 04:50:02 +00:00
|
|
|
ccurl_location_ptr curloc = ccurl_find_location(ccurl_locations, uri);
|
2009-07-20 15:21:33 +00:00
|
|
|
if (!curloc->p_timed_thread) {
|
2010-08-29 18:01:05 +00:00
|
|
|
curloc->result = (char*)malloc(max_user_text.get(*state));
|
|
|
|
memset(curloc->result, 0, max_user_text.get(*state));
|
2010-02-24 19:10:26 +00:00
|
|
|
curloc->process_function = std::bind(ccurl_parse_data,
|
|
|
|
std::placeholders::_1, std::placeholders::_2);
|
2009-07-20 15:21:33 +00:00
|
|
|
ccurl_init_thread(curloc, interval);
|
|
|
|
if (!curloc->p_timed_thread) {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("error setting up curl thread");
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-04 04:50:02 +00:00
|
|
|
|
2010-02-24 19:10:26 +00:00
|
|
|
std::lock_guard<std::mutex> lock(curloc->p_timed_thread->mutex());
|
2009-07-20 15:21:33 +00:00
|
|
|
strncpy(p, curloc->result, p_max_size);
|
|
|
|
}
|
|
|
|
|
2009-10-04 19:06:52 +00:00
|
|
|
void curl_parse_arg(struct text_object *obj, const char *arg)
|
|
|
|
{
|
|
|
|
int argc;
|
2009-10-04 19:17:43 +00:00
|
|
|
struct curl_data *cd;
|
2009-10-04 19:06:52 +00:00
|
|
|
float interval = 0;
|
|
|
|
|
2010-01-04 04:50:02 +00:00
|
|
|
cd = (struct curl_data*)malloc(sizeof(struct curl_data));
|
2009-10-04 19:17:43 +00:00
|
|
|
memset(cd, 0, sizeof(struct curl_data));
|
|
|
|
|
|
|
|
argc = sscanf(arg, "%127s %f", cd->uri, &interval);
|
2009-10-04 19:06:52 +00:00
|
|
|
if (argc < 1) {
|
2009-10-04 19:17:43 +00:00
|
|
|
free(cd);
|
2009-10-04 19:06:52 +00:00
|
|
|
NORM_ERR("wrong number of arguments for $curl");
|
|
|
|
return;
|
|
|
|
}
|
2010-04-19 20:25:50 +00:00
|
|
|
if (argc == 1)
|
|
|
|
cd->interval = 15*60;
|
|
|
|
else
|
2010-09-11 12:25:19 +00:00
|
|
|
cd->interval = interval > 0 ? interval * 60 : active_update_interval();
|
2009-10-04 19:17:43 +00:00
|
|
|
obj->data.opaque = cd;
|
2009-10-04 19:06:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void curl_print(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 04:50:02 +00:00
|
|
|
struct curl_data *cd = (struct curl_data *)obj->data.opaque;
|
2009-10-04 19:17:43 +00:00
|
|
|
|
|
|
|
if (!cd || !cd->uri) {
|
2009-10-04 19:06:52 +00:00
|
|
|
NORM_ERR("error processing Curl data");
|
|
|
|
return;
|
|
|
|
}
|
2009-10-04 19:17:43 +00:00
|
|
|
ccurl_process_info(p, p_max_size, cd->uri, cd->interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
void curl_obj_free(struct text_object *obj)
|
|
|
|
{
|
2010-02-23 13:54:03 +00:00
|
|
|
free_and_zero(obj->data.opaque);
|
2009-10-04 19:06:52 +00:00
|
|
|
}
|