X-Git-Url: https://git.wpitchoune.net/gitweb/?p=prss.git;a=blobdiff_plain;f=src%2Fhttp.c;h=43053edbcd7a02deb3a6917e827cb0eab557b5b5;hp=ba4d51a75988d83bfb914bca3ef6ad0425341735;hb=7f50cedb88527a356ba2deeb15e1e959c58a3f57;hpb=47231117238180500e22522eb470530bad381dd1 diff --git a/src/http.c b/src/http.c index ba4d51a..43053ed 100644 --- a/src/http.c +++ b/src/http.c @@ -25,9 +25,10 @@ #include #include -#include -#include "url.h" +#include "http.h" + +static int debug; struct ucontent { char *data; @@ -55,71 +56,62 @@ static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp) 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) +char *http_get(const char *url, const char *content) { struct ucontent chunk; - json_object *obj; - - obj = NULL; - - if (!curl) - return NULL; chunk.data = malloc(1); chunk.len = 0; 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); + return chunk.data; free(chunk.data); + fprintf(stderr, _("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; - - obj = NULL; - - chunk.data = malloc(1); - chunk.len = 0; - - 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)); + const char *in; + char *out; + struct json_object *result; + + 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); + free(out); + return result; } - 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); - free(chunk.data); - - return obj; -} - -json_object *post_json_object(const char *url, struct json_object *j) -{ - return post(url, json_object_to_json_string(j)); + return NULL; }