1 /**
2 * collectd - src/curl_json.c
3 * Copyright (C) 2009 Doug MacEachern
4 * Copyright (C) 2006-2011 Florian octo Forster
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Doug MacEachern <dougm at hyperic.com>
21 * Florian octo Forster <octo at collectd.org>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "configfile.h"
28 #include "utils_avltree.h"
29 #include "utils_complain.h"
31 #include <curl/curl.h>
32 #include <yajl/yajl_parse.h>
33 #if HAVE_YAJL_YAJL_VERSION_H
34 # include <yajl/yajl_version.h>
35 #endif
37 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
38 # define HAVE_YAJL_V2 1
39 #endif
41 #define CJ_DEFAULT_HOST "localhost"
42 #define CJ_KEY_MAGIC 0x43484b59UL /* CHKY */
43 #define CJ_IS_KEY(key) ((key)->magic == CJ_KEY_MAGIC)
44 #define CJ_ANY "*"
45 #define COUCH_MIN(x,y) ((x) < (y) ? (x) : (y))
47 struct cj_key_s;
48 typedef struct cj_key_s cj_key_t;
49 struct cj_key_s /* {{{ */
50 {
51 char *path;
52 char *type;
53 char *instance;
54 unsigned long magic;
55 };
56 /* }}} */
58 struct cj_s /* {{{ */
59 {
60 char *instance;
61 char *host;
63 char *url;
64 char *user;
65 char *pass;
66 char *credentials;
67 _Bool verify_peer;
68 _Bool verify_host;
69 char *cacert;
70 struct curl_slist *headers;
71 char *post_body;
73 CURL *curl;
74 char curl_errbuf[CURL_ERROR_SIZE];
76 yajl_handle yajl;
77 c_avl_tree_t *tree;
78 cj_key_t *key;
79 int depth;
80 struct {
81 union {
82 c_avl_tree_t *tree;
83 cj_key_t *key;
84 };
85 _Bool in_array;
86 int index;
87 char name[DATA_MAX_NAME_LEN];
88 } state[YAJL_MAX_DEPTH];
89 };
90 typedef struct cj_s cj_t; /* }}} */
92 #if HAVE_YAJL_V2
93 typedef size_t yajl_len_t;
94 #else
95 typedef unsigned int yajl_len_t;
96 #endif
98 static int cj_read (user_data_t *ud);
99 static int cj_curl_perform (cj_t *db, CURL *curl);
100 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value);
102 static size_t cj_curl_callback (void *buf, /* {{{ */
103 size_t size, size_t nmemb, void *user_data)
104 {
105 cj_t *db;
106 size_t len;
107 yajl_status status;
109 len = size * nmemb;
111 if (len <= 0)
112 return (len);
114 db = user_data;
115 if (db == NULL)
116 return (0);
118 status = yajl_parse(db->yajl, (unsigned char *)buf, len);
119 if (status == yajl_status_ok)
120 return (len);
121 #if !HAVE_YAJL_V2
122 else if (status == yajl_status_insufficient_data)
123 return (len);
124 #endif
126 if (status != yajl_status_ok)
127 {
128 unsigned char *msg =
129 yajl_get_error(db->yajl, /* verbose = */ 1,
130 /* jsonText = */ (unsigned char *) buf, (unsigned int) len);
131 ERROR ("curl_json plugin: yajl_parse failed: %s", msg);
132 yajl_free_error(db->yajl, msg);
133 return (0); /* abort write callback */
134 }
136 return (len);
137 } /* }}} size_t cj_curl_callback */
139 static int cj_get_type (cj_key_t *key)
140 {
141 const data_set_t *ds;
143 ds = plugin_get_ds (key->type);
144 if (ds == NULL)
145 {
146 static char type[DATA_MAX_NAME_LEN] = "!!!invalid!!!";
148 assert (key->type != NULL);
149 if (strcmp (type, key->type) != 0)
150 {
151 ERROR ("curl_json plugin: Unable to look up DS type \"%s\".",
152 key->type);
153 sstrncpy (type, key->type, sizeof (type));
154 }
156 return -1;
157 }
158 else if (ds->ds_num > 1)
159 {
160 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
162 c_complain_once (LOG_WARNING, &complaint,
163 "curl_json plugin: The type \"%s\" has more than one data source. "
164 "This is currently not supported. I will return the type of the "
165 "first data source, but this will likely lead to problems later on.",
166 key->type);
167 }
169 return ds->ds[0].type;
170 }
172 static int cj_cb_map_key (void *ctx, const unsigned char *val,
173 yajl_len_t len);
175 static void cj_cb_inc_array_index (void * ctx, _Bool ignore)
176 {
177 cj_t *db = (cj_t *)ctx;
179 if (db->state[db->depth].in_array) {
180 if (ignore)
181 db->state[db->depth].index++;
182 else {
183 char name[DATA_MAX_NAME_LEN];
184 cj_cb_map_key (ctx, (unsigned char *)name,
185 ssnprintf (name, sizeof (name),
186 "%d", db->state[db->depth].index++));
187 }
188 }
189 }
191 /* yajl callbacks */
192 #define CJ_CB_ABORT 0
193 #define CJ_CB_CONTINUE 1
195 static int cj_cb_boolean (void * ctx, int boolVal)
196 {
197 cj_cb_inc_array_index (ctx, 1);
198 return (CJ_CB_CONTINUE);
199 }
201 static int cj_cb_null (void * ctx)
202 {
203 cj_cb_inc_array_index (ctx, 1);
204 return (CJ_CB_CONTINUE);
205 }
207 /* "number" may not be null terminated, so copy it into a buffer before
208 * parsing. */
209 static int cj_cb_number (void *ctx,
210 const char *number, yajl_len_t number_len)
211 {
212 char buffer[number_len + 1];
214 cj_t *db = (cj_t *)ctx;
215 cj_key_t *key = db->state[db->depth].key;
216 value_t vt;
217 int type;
218 int status;
220 if ((key == NULL) || !CJ_IS_KEY (key)) {
221 cj_cb_inc_array_index (ctx, 1);
222 return (CJ_CB_CONTINUE);
223 } else
224 cj_cb_inc_array_index (ctx, 0);
226 memcpy (buffer, number, number_len);
227 buffer[sizeof (buffer) - 1] = 0;
229 type = cj_get_type (key);
230 status = parse_value (buffer, &vt, type);
231 if (status != 0)
232 {
233 NOTICE ("curl_json plugin: Unable to parse number: \"%s\"", buffer);
234 return (CJ_CB_CONTINUE);
235 }
237 cj_submit (db, key, &vt);
238 return (CJ_CB_CONTINUE);
239 } /* int cj_cb_number */
241 static int cj_cb_map_key (void *ctx, const unsigned char *val,
242 yajl_len_t len)
243 {
244 cj_t *db = (cj_t *)ctx;
245 c_avl_tree_t *tree;
247 tree = db->state[db->depth-1].tree;
249 if (tree != NULL)
250 {
251 cj_key_t *value;
252 char *name;
254 name = db->state[db->depth].name;
255 len = COUCH_MIN(len, sizeof (db->state[db->depth].name)-1);
256 sstrncpy (name, (char *)val, len+1);
258 if (c_avl_get (tree, name, (void *) &value) == 0)
259 db->state[db->depth].key = value;
260 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
261 db->state[db->depth].key = value;
262 else
263 db->state[db->depth].key = NULL;
264 }
266 return (CJ_CB_CONTINUE);
267 }
269 static int cj_cb_string (void *ctx, const unsigned char *val,
270 yajl_len_t len)
271 {
272 cj_t *db = (cj_t *)ctx;
273 char str[len + 1];
275 cj_cb_inc_array_index (ctx, 1);
276 return (CJ_CB_CONTINUE);
278 /* Create a null-terminated version of the string. */
279 memcpy (str, val, len);
280 str[len] = 0;
282 /* No configuration for this string -> simply return. */
283 if (db->state[db->depth].key == NULL)
284 return (CJ_CB_CONTINUE);
286 if (!CJ_IS_KEY (db->state[db->depth].key))
287 {
288 NOTICE ("curl_json plugin: Found string \"%s\", but the configuration "
289 "expects a map here.", str);
290 return (CJ_CB_CONTINUE);
291 }
293 /* Handle the string as if it was a number. */
294 return (cj_cb_number (ctx, (const char *) val, len));
295 } /* int cj_cb_string */
297 static int cj_cb_start (void *ctx)
298 {
299 cj_t *db = (cj_t *)ctx;
300 if (++db->depth >= YAJL_MAX_DEPTH)
301 {
302 ERROR ("curl_json plugin: %s depth exceeds max, aborting.", db->url);
303 return (CJ_CB_ABORT);
304 }
305 return (CJ_CB_CONTINUE);
306 }
308 static int cj_cb_end (void *ctx)
309 {
310 cj_t *db = (cj_t *)ctx;
311 db->state[db->depth].tree = NULL;
312 --db->depth;
313 return (CJ_CB_CONTINUE);
314 }
316 static int cj_cb_start_map (void *ctx)
317 {
318 cj_cb_inc_array_index (ctx, 0);
319 return cj_cb_start (ctx);
320 }
322 static int cj_cb_end_map (void *ctx)
323 {
324 return cj_cb_end (ctx);
325 }
327 static int cj_cb_start_array (void * ctx)
328 {
329 cj_t *db = (cj_t *)ctx;
330 cj_cb_inc_array_index (ctx, 0);
331 if (db->depth+1 < YAJL_MAX_DEPTH) {
332 db->state[db->depth+1].in_array = 1;
333 db->state[db->depth+1].index = 0;
334 }
335 return cj_cb_start (ctx);
336 }
338 static int cj_cb_end_array (void * ctx)
339 {
340 cj_t *db = (cj_t *)ctx;
341 db->state[db->depth].in_array = 0;
342 return cj_cb_end (ctx);
343 }
345 static yajl_callbacks ycallbacks = {
346 cj_cb_null, /* null */
347 cj_cb_boolean, /* boolean */
348 NULL, /* integer */
349 NULL, /* double */
350 cj_cb_number,
351 cj_cb_string,
352 cj_cb_start_map,
353 cj_cb_map_key,
354 cj_cb_end_map,
355 cj_cb_start_array,
356 cj_cb_end_array
357 };
359 /* end yajl callbacks */
361 static void cj_key_free (cj_key_t *key) /* {{{ */
362 {
363 if (key == NULL)
364 return;
366 sfree (key->path);
367 sfree (key->type);
368 sfree (key->instance);
370 sfree (key);
371 } /* }}} void cj_key_free */
373 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
374 {
375 char *name;
376 void *value;
378 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
379 {
380 cj_key_t *key = (cj_key_t *)value;
382 if (CJ_IS_KEY(key))
383 cj_key_free (key);
384 else
385 cj_tree_free ((c_avl_tree_t *)value);
387 sfree (name);
388 }
390 c_avl_destroy (tree);
391 } /* }}} void cj_tree_free */
393 static void cj_free (void *arg) /* {{{ */
394 {
395 cj_t *db;
397 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
399 db = (cj_t *) arg;
401 if (db == NULL)
402 return;
404 if (db->curl != NULL)
405 curl_easy_cleanup (db->curl);
406 db->curl = NULL;
408 if (db->tree != NULL)
409 cj_tree_free (db->tree);
410 db->tree = NULL;
412 sfree (db->instance);
413 sfree (db->host);
415 sfree (db->url);
416 sfree (db->user);
417 sfree (db->pass);
418 sfree (db->credentials);
419 sfree (db->cacert);
420 sfree (db->post_body);
421 curl_slist_free_all (db->headers);
423 sfree (db);
424 } /* }}} void cj_free */
426 /* Configuration handling functions {{{ */
428 static c_avl_tree_t *cj_avl_create(void)
429 {
430 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
431 }
433 static int cj_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
434 oconfig_item_t *ci)
435 {
436 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
437 {
438 WARNING ("curl_json plugin: `%s' needs exactly one string argument.", name);
439 return (-1);
440 }
442 *dest = curl_slist_append(*dest, ci->values[0].value.string);
443 if (*dest == NULL)
444 return (-1);
446 return (0);
447 } /* }}} int cj_config_append_string */
449 static int cj_config_add_key (cj_t *db, /* {{{ */
450 oconfig_item_t *ci)
451 {
452 cj_key_t *key;
453 int status;
454 int i;
456 if ((ci->values_num != 1)
457 || (ci->values[0].type != OCONFIG_TYPE_STRING))
458 {
459 WARNING ("curl_json plugin: The `Key' block "
460 "needs exactly one string argument.");
461 return (-1);
462 }
464 key = (cj_key_t *) malloc (sizeof (*key));
465 if (key == NULL)
466 {
467 ERROR ("curl_json plugin: malloc failed.");
468 return (-1);
469 }
470 memset (key, 0, sizeof (*key));
471 key->magic = CJ_KEY_MAGIC;
473 if (strcasecmp ("Key", ci->key) == 0)
474 {
475 status = cf_util_get_string (ci, &key->path);
476 if (status != 0)
477 {
478 sfree (key);
479 return (status);
480 }
481 }
482 else
483 {
484 ERROR ("curl_json plugin: cj_config: "
485 "Invalid key: %s", ci->key);
486 return (-1);
487 }
489 status = 0;
490 for (i = 0; i < ci->children_num; i++)
491 {
492 oconfig_item_t *child = ci->children + i;
494 if (strcasecmp ("Type", child->key) == 0)
495 status = cf_util_get_string (child, &key->type);
496 else if (strcasecmp ("Instance", child->key) == 0)
497 status = cf_util_get_string (child, &key->instance);
498 else
499 {
500 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
501 status = -1;
502 }
504 if (status != 0)
505 break;
506 } /* for (i = 0; i < ci->children_num; i++) */
508 while (status == 0)
509 {
510 if (key->type == NULL)
511 {
512 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
513 status = -1;
514 }
516 break;
517 } /* while (status == 0) */
519 /* store path in a tree that will match the json map structure, example:
520 * "httpd/requests/count",
521 * "httpd/requests/current" ->
522 * { "httpd": { "requests": { "count": $key, "current": $key } } }
523 */
524 if (status == 0)
525 {
526 char *ptr;
527 char *name;
528 char ent[PATH_MAX];
529 c_avl_tree_t *tree;
531 if (db->tree == NULL)
532 db->tree = cj_avl_create();
534 tree = db->tree;
535 name = key->path;
536 ptr = key->path;
537 if (*ptr == '/')
538 ++ptr;
540 name = ptr;
541 while (*ptr)
542 {
543 if (*ptr == '/')
544 {
545 c_avl_tree_t *value;
546 int len;
548 len = ptr-name;
549 if (len == 0)
550 break;
551 sstrncpy (ent, name, len+1);
553 if (c_avl_get (tree, ent, (void *) &value) != 0)
554 {
555 value = cj_avl_create ();
556 c_avl_insert (tree, strdup (ent), value);
557 }
559 tree = value;
560 name = ptr+1;
561 }
562 ++ptr;
563 }
564 if (*name)
565 c_avl_insert (tree, strdup(name), key);
566 else
567 {
568 ERROR ("curl_json plugin: invalid key: %s", key->path);
569 status = -1;
570 }
571 }
573 return (status);
574 } /* }}} int cj_config_add_key */
576 static int cj_init_curl (cj_t *db) /* {{{ */
577 {
578 db->curl = curl_easy_init ();
579 if (db->curl == NULL)
580 {
581 ERROR ("curl_json plugin: curl_easy_init failed.");
582 return (-1);
583 }
585 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
586 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
587 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
588 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
589 PACKAGE_NAME"/"PACKAGE_VERSION);
590 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
591 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
593 if (db->user != NULL)
594 {
595 size_t credentials_size;
597 credentials_size = strlen (db->user) + 2;
598 if (db->pass != NULL)
599 credentials_size += strlen (db->pass);
601 db->credentials = (char *) malloc (credentials_size);
602 if (db->credentials == NULL)
603 {
604 ERROR ("curl_json plugin: malloc failed.");
605 return (-1);
606 }
608 ssnprintf (db->credentials, credentials_size, "%s:%s",
609 db->user, (db->pass == NULL) ? "" : db->pass);
610 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
611 }
613 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, (long) db->verify_peer);
614 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
615 db->verify_host ? 2L : 0L);
616 if (db->cacert != NULL)
617 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
618 if (db->headers != NULL)
619 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
620 if (db->post_body != NULL)
621 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
623 return (0);
624 } /* }}} int cj_init_curl */
626 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
627 {
628 cj_t *db;
629 int status = 0;
630 int i;
632 if ((ci->values_num != 1)
633 || (ci->values[0].type != OCONFIG_TYPE_STRING))
634 {
635 WARNING ("curl_json plugin: The `URL' block "
636 "needs exactly one string argument.");
637 return (-1);
638 }
640 db = (cj_t *) malloc (sizeof (*db));
641 if (db == NULL)
642 {
643 ERROR ("curl_json plugin: malloc failed.");
644 return (-1);
645 }
646 memset (db, 0, sizeof (*db));
648 if (strcasecmp ("URL", ci->key) == 0)
649 {
650 status = cf_util_get_string (ci, &db->url);
651 if (status != 0)
652 {
653 sfree (db);
654 return (status);
655 }
656 }
657 else
658 {
659 ERROR ("curl_json plugin: cj_config: "
660 "Invalid key: %s", ci->key);
661 return (-1);
662 }
664 /* Fill the `cj_t' structure.. */
665 for (i = 0; i < ci->children_num; i++)
666 {
667 oconfig_item_t *child = ci->children + i;
669 if (strcasecmp ("Instance", child->key) == 0)
670 status = cf_util_get_string (child, &db->instance);
671 else if (strcasecmp ("Host", child->key) == 0)
672 status = cf_util_get_string (child, &db->host);
673 else if (strcasecmp ("User", child->key) == 0)
674 status = cf_util_get_string (child, &db->user);
675 else if (strcasecmp ("Password", child->key) == 0)
676 status = cf_util_get_string (child, &db->pass);
677 else if (strcasecmp ("VerifyPeer", child->key) == 0)
678 status = cf_util_get_boolean (child, &db->verify_peer);
679 else if (strcasecmp ("VerifyHost", child->key) == 0)
680 status = cf_util_get_boolean (child, &db->verify_host);
681 else if (strcasecmp ("CACert", child->key) == 0)
682 status = cf_util_get_string (child, &db->cacert);
683 else if (strcasecmp ("Header", child->key) == 0)
684 status = cj_config_append_string ("Header", &db->headers, child);
685 else if (strcasecmp ("Post", child->key) == 0)
686 status = cf_util_get_string (child, &db->post_body);
687 else if (strcasecmp ("Key", child->key) == 0)
688 status = cj_config_add_key (db, child);
689 else
690 {
691 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
692 status = -1;
693 }
695 if (status != 0)
696 break;
697 }
699 if (status == 0)
700 {
701 if (db->tree == NULL)
702 {
703 WARNING ("curl_json plugin: No (valid) `Key' block "
704 "within `URL' block `%s'.", db->url);
705 status = -1;
706 }
707 if (status == 0)
708 status = cj_init_curl (db);
709 }
711 /* If all went well, register this database for reading */
712 if (status == 0)
713 {
714 user_data_t ud;
715 char cb_name[DATA_MAX_NAME_LEN];
717 if (db->instance == NULL)
718 db->instance = strdup("default");
720 DEBUG ("curl_json plugin: Registering new read callback: %s",
721 db->instance);
723 memset (&ud, 0, sizeof (ud));
724 ud.data = (void *) db;
725 ud.free_func = cj_free;
727 ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
728 db->instance, db->url);
730 plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
731 /* interval = */ NULL, &ud);
732 }
733 else
734 {
735 cj_free (db);
736 return (-1);
737 }
739 return (0);
740 }
741 /* }}} int cj_config_add_database */
743 static int cj_config (oconfig_item_t *ci) /* {{{ */
744 {
745 int success;
746 int errors;
747 int status;
748 int i;
750 success = 0;
751 errors = 0;
753 for (i = 0; i < ci->children_num; i++)
754 {
755 oconfig_item_t *child = ci->children + i;
757 if (strcasecmp ("URL", child->key) == 0)
758 {
759 status = cj_config_add_url (child);
760 if (status == 0)
761 success++;
762 else
763 errors++;
764 }
765 else
766 {
767 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
768 errors++;
769 }
770 }
772 if ((success == 0) && (errors > 0))
773 {
774 ERROR ("curl_json plugin: All statements failed.");
775 return (-1);
776 }
778 return (0);
779 } /* }}} int cj_config */
781 /* }}} End of configuration handling functions */
783 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
784 {
785 value_list_t vl = VALUE_LIST_INIT;
786 char *host;
788 vl.values = value;
789 vl.values_len = 1;
791 if ((db->host == NULL)
792 || (strcmp ("", db->host) == 0)
793 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
794 host = hostname_g;
795 else
796 host = db->host;
798 if (key->instance == NULL)
799 {
800 if ((db->depth == 0) || (strcmp ("", db->state[db->depth-1].name) == 0))
801 sstrncpy (vl.type_instance, db->state[db->depth].name, sizeof (vl.type_instance));
802 else
803 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
804 db->state[db->depth-1].name, db->state[db->depth].name);
805 }
806 else
807 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
809 sstrncpy (vl.host, host, sizeof (vl.host));
810 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
811 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
812 sstrncpy (vl.type, key->type, sizeof (vl.type));
814 plugin_dispatch_values (&vl);
815 } /* }}} int cj_submit */
817 static int cj_curl_perform (cj_t *db, CURL *curl) /* {{{ */
818 {
819 int status;
820 long rc;
821 char *url;
822 yajl_handle yprev = db->yajl;
824 db->yajl = yajl_alloc (&ycallbacks,
825 #if HAVE_YAJL_V2
826 /* alloc funcs = */ NULL,
827 #else
828 /* alloc funcs = */ NULL, NULL,
829 #endif
830 /* context = */ (void *)db);
831 if (db->yajl == NULL)
832 {
833 ERROR ("curl_json plugin: yajl_alloc failed.");
834 db->yajl = yprev;
835 return (-1);
836 }
838 url = NULL;
839 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
841 status = curl_easy_perform (curl);
842 if (status != CURLE_OK)
843 {
844 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
845 status, db->curl_errbuf, (url != NULL) ? url : "<null>");
846 yajl_free (db->yajl);
847 db->yajl = yprev;
848 return (-1);
849 }
851 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
853 /* The response code is zero if a non-HTTP transport was used. */
854 if ((rc != 0) && (rc != 200))
855 {
856 ERROR ("curl_json plugin: curl_easy_perform failed with "
857 "response code %ld (%s)", rc, url);
858 yajl_free (db->yajl);
859 db->yajl = yprev;
860 return (-1);
861 }
863 #if HAVE_YAJL_V2
864 status = yajl_complete_parse(db->yajl);
865 #else
866 status = yajl_parse_complete(db->yajl);
867 #endif
868 if (status != yajl_status_ok)
869 {
870 unsigned char *errmsg;
872 errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
873 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
874 ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
875 (char *) errmsg);
876 yajl_free_error (db->yajl, errmsg);
877 yajl_free (db->yajl);
878 db->yajl = yprev;
879 return (-1);
880 }
882 yajl_free (db->yajl);
883 db->yajl = yprev;
884 return (0);
885 } /* }}} int cj_curl_perform */
887 static int cj_read (user_data_t *ud) /* {{{ */
888 {
889 cj_t *db;
891 if ((ud == NULL) || (ud->data == NULL))
892 {
893 ERROR ("curl_json plugin: cj_read: Invalid user data.");
894 return (-1);
895 }
897 db = (cj_t *) ud->data;
899 db->depth = 0;
900 memset (&db->state, 0, sizeof(db->state));
901 db->state[db->depth].tree = db->tree;
902 db->key = NULL;
904 return cj_curl_perform (db, db->curl);
905 } /* }}} int cj_read */
907 void module_register (void)
908 {
909 plugin_register_complex_config ("curl_json", cj_config);
910 } /* void module_register */
912 /* vim: set sw=2 sts=2 et fdm=marker : */