2018-05-12 16:03:00 +00:00
|
|
|
/*
|
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
|
|
|
|
*
|
2021-02-27 15:14:19 +00:00
|
|
|
* Copyright (c) 2005-2021 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 "ccurl_thread.h"
|
2010-12-31 15:43:49 +00:00
|
|
|
#include <cmath>
|
2010-01-04 04:50:02 +00:00
|
|
|
#include <mutex>
|
2018-05-12 16:03:00 +00:00
|
|
|
#include "conky.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "text_object.h"
|
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 {
|
2018-05-12 16:03:00 +00:00
|
|
|
/* 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;
|
|
|
|
|
2019-02-23 19:43:44 +00:00
|
|
|
if (realsize > 0 &&
|
|
|
|
(value[realsize - 1] == '\r' || value[realsize - 1] == 0)) {
|
2018-05-12 16:03:00 +00:00
|
|
|
--realsize;
|
2019-02-23 19:43:44 +00:00
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return size * nmemb;
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +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;
|
|
|
|
|
|
|
|
obj->data += std::string(value, realsize);
|
|
|
|
|
|
|
|
return realsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_internal::curl_internal(const std::string &url) : curl(curl_easy_init()) {
|
2018-12-20 15:22:08 +00:00
|
|
|
if (!curl) throw std::runtime_error("curl_easy_init() failed");
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
|
|
|
|
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());
|
|
|
|
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);
|
|
|
|
|
|
|
|
// curl's usage of alarm()+longjmp() is a really bad idea for multi-threaded
|
|
|
|
// applications
|
|
|
|
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fetch our datums */
|
|
|
|
void curl_internal::do_work() {
|
|
|
|
CURLcode res;
|
|
|
|
struct headers_ {
|
|
|
|
struct curl_slist *h;
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
headers_() : h(nullptr) {}
|
2018-05-12 16:03:00 +00:00
|
|
|
~headers_() { curl_slist_free_all(h); }
|
|
|
|
} headers;
|
|
|
|
|
|
|
|
data.clear();
|
|
|
|
|
2018-12-20 15:22:08 +00:00
|
|
|
if (!last_modified.empty()) {
|
2018-05-12 16:03:00 +00:00
|
|
|
headers.h = curl_slist_append(
|
|
|
|
headers.h, ("If-Modified-Since: " + last_modified).c_str());
|
|
|
|
last_modified.clear();
|
|
|
|
}
|
2018-12-20 15:22:08 +00:00
|
|
|
if (!etag.empty()) {
|
2018-05-12 16:03:00 +00:00
|
|
|
headers.h =
|
|
|
|
curl_slist_append(headers.h, ("If-None-Match: " + etag).c_str());
|
|
|
|
etag.clear();
|
|
|
|
}
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers.h);
|
|
|
|
|
|
|
|
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:
|
|
|
|
process_data();
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NORM_ERR("curl: could not retrieve data from server");
|
|
|
|
}
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
} // namespace priv
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class simple_curl_cb : public curl_callback<std::string> {
|
|
|
|
typedef curl_callback<std::string> Base;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void process_data() {
|
|
|
|
std::lock_guard<std::mutex> lock(result_mutex);
|
|
|
|
result = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
simple_curl_cb(uint32_t period, const std::string &uri)
|
|
|
|
: Base(period, Tuple(uri)) {}
|
|
|
|
};
|
|
|
|
} // namespace
|
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 {
|
2019-11-28 05:54:28 +00:00
|
|
|
char *uri;
|
2018-05-12 16:03:00 +00:00
|
|
|
float interval;
|
2009-10-04 19:17:43 +00:00
|
|
|
};
|
|
|
|
|
2009-07-20 18:02:53 +00:00
|
|
|
/* prints result data to text buffer, used by $curl */
|
2018-05-12 16:03:00 +00:00
|
|
|
void ccurl_process_info(char *p, int p_max_size, const std::string &uri,
|
|
|
|
int interval) {
|
|
|
|
uint32_t period = std::max(lround(interval / active_update_interval()), 1l);
|
|
|
|
auto cb = conky::register_cb<simple_curl_cb>(period, uri);
|
2010-01-04 04:50:02 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
strncpy(p, cb->get_result_copy().c_str(), p_max_size);
|
2009-07-20 15:21:33 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void curl_parse_arg(struct text_object *obj, const char *arg) {
|
|
|
|
struct curl_data *cd;
|
|
|
|
float interval = 0;
|
2019-11-28 05:54:28 +00:00
|
|
|
char *space;
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2019-11-28 05:54:28 +00:00
|
|
|
if (strlen(arg) < 1) {
|
2018-05-12 16:03:00 +00:00
|
|
|
NORM_ERR("wrong number of arguments for $curl");
|
|
|
|
return;
|
|
|
|
}
|
2019-11-28 05:54:28 +00:00
|
|
|
|
|
|
|
cd = static_cast<struct curl_data *>(malloc(sizeof(struct curl_data)));
|
|
|
|
memset(cd, 0, sizeof(struct curl_data));
|
|
|
|
|
|
|
|
// Default to a 15 minute interval
|
|
|
|
cd->interval = 15 * 60;
|
|
|
|
|
|
|
|
cd->uri = strdup(arg);
|
|
|
|
space = strchr(cd->uri, ' ');
|
|
|
|
if (space) {
|
|
|
|
// If an explicit interval was given, use that
|
|
|
|
char *interval_str = &space[1];
|
|
|
|
*space = '\0';
|
|
|
|
sscanf(interval_str, "%f", &interval);
|
2018-05-12 16:03:00 +00:00
|
|
|
cd->interval = interval > 0 ? interval * 60 : active_update_interval();
|
2019-02-23 19:43:44 +00:00
|
|
|
}
|
2019-11-28 05:54:28 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
obj->data.opaque = cd;
|
2009-10-04 19:06:52 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void curl_print(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2019-02-23 19:43:44 +00:00
|
|
|
struct curl_data *cd = static_cast<struct curl_data *>(obj->data.opaque);
|
2009-10-04 19:17:43 +00:00
|
|
|
|
2018-12-21 02:20:43 +00:00
|
|
|
if (!cd) {
|
2018-05-12 16:03:00 +00:00
|
|
|
NORM_ERR("error processing Curl data");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ccurl_process_info(p, p_max_size, cd->uri, cd->interval);
|
2009-10-04 19:17:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 05:54:28 +00:00
|
|
|
void curl_obj_free(struct text_object *obj) {
|
|
|
|
struct curl_data *cd = static_cast<struct curl_data *>(obj->data.opaque);
|
|
|
|
free_and_zero(cd->uri);
|
|
|
|
free_and_zero(obj->data.opaque);
|
|
|
|
}
|