#include <stdlib.h>
#include <syslog.h>
+#include <curl/curl.h>
+
#include "ctx.h"
#include "logging.h"
td_log_callback callback;
void* data;
} log;
+
+ // cURL share handle
+ CURLSH* share;
};
static int td_ctx_setup_logging(td_ctx* ctx) {
return 0;
}
-static void td_ctx_free(td_ctx* ctx) {
- free(ctx);
+static void td_ctx_free(td_ctx* self) {
+ if (self->share)
+ curl_share_cleanup(self->share);
+ free(self);
}
int td_ctx_create(td_ctx** ctx) {
self->log.callback(self->log.data, level, file, line, fn, format, args);
va_end(args);
}
+
+CURLSH* td_ctx_get_curl_share(td_ctx* self) {
+ int r;
+
+ if (!self->share) {
+ self->share = curl_share_init();
+ if (!self->share) {
+ ERROR(self, "Could not setup cURL share handle\n");
+ goto ERROR;
+ }
+
+ // Share connections between handles
+ r = curl_share_setopt(self->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
+ if (r) {
+ ERROR(self, "Could not configure the share handle: %s\n", curl_share_strerror(r));
+ goto ERROR;
+ }
+ }
+
+ return self->share;
+
+ERROR:
+ if (self->share) {
+ curl_share_cleanup(self->share);
+ self->share = NULL;
+ }
+
+ return NULL;
+}
#ifndef TELEMETRY_CTX_H
#define TELEMETRY_CTX_H
+#include <curl/curl.h>
+
typedef struct td_ctx td_ctx;
int td_ctx_create(td_ctx** ctx);
void td_ctx_log(td_ctx* self, int level,
const char* file, int line, const char* fn, const char* format, ...);
+CURLSH* td_ctx_get_curl_share(td_ctx* self);
+
#endif /* TELEMETRY_CTX_H */
static int td_request_setup(td_request* self) {
int r;
- // XXX Share Handle
+ // Share Handle
+ CURLSH* share = td_ctx_get_curl_share(self->ctx);
+
+ // Configure the share handle
+ r = curl_easy_setopt(self->handle, CURLOPT_SHARE, share);
+ if (r) {
+ ERROR(self->ctx, "Could not configure cURL share handle: %s\n",
+ curl_easy_strerror(r));
+ goto ERROR;
+ }
// Be a good net citizen and set a user agent
r = curl_easy_setopt(self->handle, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; "