X-Git-Url: https://git.wpitchoune.net/gitweb/?p=prss.git;a=blobdiff_plain;f=src%2Fhttp.c;h=12f8b72a64c25c32c1e5668c5918de4e13f50b0d;hp=a1d02118046ce3f6217ef19e00af6ff9c6673b12;hb=40d30b549ad41af63da74a3c7cb36db7f5e089e7;hpb=1087cf87f08c0311004e47b05cae0f6649d835f7 diff --git a/src/http.c b/src/http.c index a1d0211..12f8b72 100644 --- a/src/http.c +++ b/src/http.c @@ -20,14 +20,15 @@ #include #define _(str) gettext(str) +#include #include #include #include #include -#include -#include "url.h" +#include "http.h" +#include "log.h" struct ucontent { char *data; @@ -35,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) { @@ -53,73 +55,73 @@ static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp) return realsize; } -void phttp_init() +void http_init() { - curl = curl_easy_init(); + if (!curl) + curl = curl_easy_init(); } -void phttp_cleanup() +void http_cleanup() { curl_easy_cleanup(curl); } -json_object *get_json_object(const char *url) +static char *http_get(const char *url, const char *content) { struct ucontent chunk; - json_object *obj; - - obj = NULL; - - if (!curl) - return NULL; + 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, + (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) - obj = json_tokener_parse(chunk.data); - else - fprintf(stderr, _("Fail to connect to: %s"), url); + result = curl_easy_perform(curl); + + pthread_mutex_unlock(&lock); + + if (result == CURLE_OK) + return chunk.data; free(chunk.data); + log_err(_("HTTP request fail url=%s"), url); - return obj; + return NULL; } -static json_object *post(const char *url, const char *body) +json_object *http_json_get(const char *url, struct json_object *j) { - struct ucontent chunk; - json_object *obj; + const char *in; + char *out; + struct json_object *result; - obj = NULL; + if (log_level >= LOG_DEBUG) + log_debug("HTTP request= %s", + json_object_to_json_string(j)); - chunk.data = malloc(1); - chunk.len = 0; + in = json_object_to_json_string(j); + out = http_get(url, in); - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_VERBOSE, 0); - if (body) { - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body); - curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(body)); - } - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); + if (out) { + result = json_tokener_parse(out); - if (curl_easy_perform(curl) == CURLE_OK) - obj = json_tokener_parse(chunk.data); - else - fprintf(stderr, _("Fail to connect to: %s"), url); - free(chunk.data); + if (log_level >= LOG_DEBUG) + log_debug("HTTP reply= %s", out); - return obj; -} + free(out); + return result; + } -json_object *post_json_object(const char *url, struct json_object *j) -{ - return post(url, json_object_to_json_string(j)); + return NULL; }