X-Git-Url: https://git.wpitchoune.net/gitweb/?p=prss.git;a=blobdiff_plain;f=src%2Fhttp.c;h=0689a84eacf6a46eb50911b96cecb6acd2e8223a;hp=ba4d51a75988d83bfb914bca3ef6ad0425341735;hb=5bb795e97e53ef92cb7b28ee3598ad2aa4ec65d3;hpb=47231117238180500e22522eb470530bad381dd1 diff --git a/src/http.c b/src/http.c index ba4d51a..0689a84 100644 --- a/src/http.c +++ b/src/http.c @@ -20,22 +20,26 @@ #include #define _(str) gettext(str) +#include #include #include #include #include -#include -#include "url.h" +#include "http.h" +#include "log.h" + +struct http_session { + CURL *curl; + pthread_mutex_t lock; +}; struct ucontent { char *data; size_t len; }; -static CURL *curl; - static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp) { size_t realsize; @@ -53,73 +57,82 @@ static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp) return realsize; } -void http_init() +struct http_session *http_session_new() { - curl = curl_easy_init(); + struct http_session *sess; + + sess = malloc(sizeof(struct http_session)); + sess->curl = curl_easy_init(); + pthread_mutex_init(&sess->lock, NULL); + + return sess; } -void phttp_cleanup() +void http_session_free(struct http_session *sess) { - curl_easy_cleanup(curl); + curl_easy_cleanup(sess->curl); + pthread_mutex_destroy(&sess->lock); + free(sess); } -json_object *get_json_object(const char *url) +static char * +http_get(struct http_session *sess, 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; - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_VERBOSE, 0); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); + pthread_mutex_lock(&sess->lock); + + curl_easy_setopt(sess->curl, CURLOPT_URL, url); + curl_easy_setopt(sess->curl, CURLOPT_VERBOSE, 1); + if (content) { + curl_easy_setopt(sess->curl, CURLOPT_POSTFIELDS, content); + curl_easy_setopt(sess->curl, + CURLOPT_POSTFIELDSIZE, + (long)strlen(content)); + } + curl_easy_setopt(sess->curl, CURLOPT_WRITEFUNCTION, cbk_curl); + curl_easy_setopt(sess->curl, CURLOPT_WRITEDATA, (void *)&chunk); + + result = curl_easy_perform(sess->curl); - if (curl_easy_perform(curl) == CURLE_OK) - obj = json_tokener_parse(chunk.data); - else - fprintf(stderr, _("Fail to connect to: %s"), url); + pthread_mutex_unlock(&sess->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(struct http_session *sess, 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(sess, 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; }