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
|
|
|
|
*
|
2012-05-03 23:34:44 +00:00
|
|
|
* Copyright (c) 2005-2012 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-12-31 15:43:49 +00:00
|
|
|
#include <cmath>
|
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/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
|
|
|
|
2010-12-31 15:43:49 +00:00
|
|
|
namespace priv {
|
|
|
|
/* callback used by curl for parsing the header data */
|
|
|
|
size_t curl_internal::parse_header_cb(void *ptr, size_t size, size_t nmemb, void *data)
|
|
|
|
{
|
|
|
|
curl_internal *obj = static_cast<curl_internal *>(data);
|
|
|
|
const char *value = static_cast<const char *>(ptr);
|
|
|
|
size_t realsize = size * nmemb;
|
|
|
|
|
|
|
|
if(realsize > 0 && (value[realsize-1] == '\r' || value[realsize-1] == 0))
|
|
|
|
--realsize;
|
|
|
|
|
|
|
|
if (strncmp(value, "Last-Modified: ", 15) == EQUAL) {
|
|
|
|
obj->last_modified = std::string(value + 15, realsize - 15);
|
|
|
|
} else if (strncmp(value,"ETag: ", 6) == EQUAL) {
|
|
|
|
obj->etag = std::string(value + 6, realsize - 6);
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
|
|
|
|
2010-12-31 15:43:49 +00:00
|
|
|
return size*nmemb;
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
|
|
|
|
2010-12-31 15:43:49 +00:00
|
|
|
/* callback used by curl for writing the received data */
|
|
|
|
size_t curl_internal::write_cb(void *ptr, size_t size, size_t nmemb, void *data)
|
|
|
|
{
|
|
|
|
curl_internal *obj = static_cast<curl_internal *>(data);
|
|
|
|
const char *value = static_cast<const char *>(ptr);
|
|
|
|
size_t realsize = size * nmemb;
|
2010-10-14 03:00:46 +00:00
|
|
|
|
2010-12-31 15:43:49 +00:00
|
|
|
obj->data += std::string(value, realsize);
|
2010-10-14 03:00:46 +00:00
|
|
|
|
2010-12-31 15:43:49 +00:00
|
|
|
return realsize;
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
|
|
|
|
2010-12-31 15:43:49 +00:00
|
|
|
curl_internal::curl_internal(const std::string &url)
|
|
|
|
: curl(curl_easy_init())
|
|
|
|
{
|
|
|
|
if(not curl)
|
|
|
|
throw std::runtime_error("curl_easy_init() failed");
|
2009-07-20 15:21:33 +00:00
|
|
|
|
2010-08-29 11:31:18 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
|
2010-12-31 15:43:49 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_HEADERDATA, this);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, parse_header_cb);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
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);
|
2012-10-20 11:04:11 +00:00
|
|
|
|
|
|
|
// curl's usage of alarm()+longjmp() is a really bad idea for multi-threaded applications
|
|
|
|
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
|
|
|
|
2010-12-31 15:43:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* fetch our datums */
|
|
|
|
void curl_internal::do_work()
|
|
|
|
{
|
|
|
|
CURLcode res;
|
|
|
|
struct headers_ {
|
|
|
|
struct curl_slist *h;
|
|
|
|
|
|
|
|
headers_() : h(NULL) {}
|
|
|
|
~headers_() { curl_slist_free_all(h); }
|
|
|
|
} headers;
|
|
|
|
|
|
|
|
data.clear();
|
2010-08-29 11:31:18 +00:00
|
|
|
|
2010-12-31 15:43:49 +00:00
|
|
|
if (not last_modified.empty()) {
|
|
|
|
headers.h = curl_slist_append(headers.h, ("If-Modified-Since: " + last_modified).c_str());
|
|
|
|
last_modified.clear();
|
2010-10-14 03:00:46 +00:00
|
|
|
}
|
2010-12-31 15:43:49 +00:00
|
|
|
if (not etag.empty()) {
|
|
|
|
headers.h = curl_slist_append(headers.h, ("If-None-Match: " + etag).c_str());
|
|
|
|
etag.clear();
|
2010-10-14 03:00:46 +00:00
|
|
|
}
|
2015-08-19 20:04:42 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers.h);
|
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-12-31 15:43:49 +00:00
|
|
|
if (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_status_code) == CURLE_OK) {
|
2010-10-05 18:53:55 +00:00
|
|
|
switch (http_status_code) {
|
|
|
|
case 200:
|
2010-12-31 15:43:49 +00:00
|
|
|
process_data();
|
2010-10-05 18:53:55 +00:00
|
|
|
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
|
|
|
} 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-12-31 15:43:49 +00:00
|
|
|
namespace {
|
|
|
|
class simple_curl_cb: public curl_callback<std::string> {
|
|
|
|
typedef curl_callback<std::string> Base;
|
2009-07-20 15:21:33 +00:00
|
|
|
|
2010-12-31 15:43:49 +00:00
|
|
|
protected:
|
|
|
|
virtual void process_data()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(result_mutex);
|
|
|
|
result = data;
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
2010-12-31 15:43:49 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
simple_curl_cb(uint32_t period, const std::string &uri)
|
|
|
|
: Base(period, Tuple(uri))
|
|
|
|
{}
|
|
|
|
};
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/* 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
|
|
|
{
|
2012-07-13 18:17:54 +00:00
|
|
|
uint32_t period = std::max(lround(interval/active_update_interval()), 1l);
|
2010-12-31 15:43:49 +00:00
|
|
|
auto cb = conky::register_cb<simple_curl_cb>(period, uri);
|
2010-01-04 04:50:02 +00:00
|
|
|
|
2011-01-02 15:23:46 +00:00
|
|
|
strncpy(p, cb->get_result_copy().c_str(), p_max_size);
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|