X-Git-Url: https://git.wpitchoune.net/gitweb/?p=prss.git;a=blobdiff_plain;f=src%2Fhttp.c;h=12f8b72a64c25c32c1e5668c5918de4e13f50b0d;hp=55eab0fb6a54a534c61628dc5457d92a3f6a0621;hb=40d30b549ad41af63da74a3c7cb36db7f5e089e7;hpb=1070b8de5c6cdf5e5958675a1b90f0af3e3f4688 diff --git a/src/http.c b/src/http.c index 55eab0f..12f8b72 100644 --- a/src/http.c +++ b/src/http.c @@ -20,6 +20,7 @@ #include #define _(str) gettext(str) +#include #include #include #include @@ -27,8 +28,7 @@ #include #include "http.h" - -static int debug = 1; +#include "log.h" struct ucontent { char *data; @@ -36,6 +36,7 @@ struct ucontent { }; static CURL *curl; +static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp) { @@ -65,29 +66,36 @@ void http_cleanup() curl_easy_cleanup(curl); } -char *http_get(const char *url, const char *content) +static char *http_get(const char *url, const char *content) { struct ucontent chunk; + int result; chunk.data = malloc(1); chunk.len = 0; + pthread_mutex_lock(&lock); + curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_VERBOSE, 0); if (content) { curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content); curl_easy_setopt(curl, - CURLOPT_POSTFIELDSIZE, + CURLOPT_POSTFIELDSIZE, (long)strlen(content)); } curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); - if (curl_easy_perform(curl) == CURLE_OK) + result = curl_easy_perform(curl); + + pthread_mutex_unlock(&lock); + + if (result == CURLE_OK) return chunk.data; free(chunk.data); - fprintf(stderr, _("HTTP request fail url=%s"), url); + log_err(_("HTTP request fail url=%s"), url); return NULL; } @@ -98,20 +106,22 @@ json_object *http_json_get(const char *url, struct json_object *j) char *out; struct json_object *result; + if (log_level >= LOG_DEBUG) + log_debug("HTTP request= %s", + json_object_to_json_string(j)); + in = json_object_to_json_string(j); out = http_get(url, in); - if (debug) - printf("HTTP request= %s\n", - json_object_to_json_string(j)); - if (out) { result = json_tokener_parse(out); - if (debug) - printf("HTTP reply= %s\n", out); + + if (log_level >= LOG_DEBUG) + log_debug("HTTP reply= %s", out); + free(out); return result; - } + } return NULL; }