2 * Copyright (C) 2010-2013 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #define _(str) gettext(str)
27 #include <curl/curl.h>
39 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
44 realsize = size * nmemb;
45 mem = (struct ucontent *)userp;
47 mem->data = realloc(mem->data, mem->len + realsize + 1);
49 memcpy(&(mem->data[mem->len]), buffer, realsize);
51 mem->data[mem->len] = 0;
59 curl = curl_easy_init();
64 curl_easy_cleanup(curl);
67 char *http_get(const char *url, const char *content)
69 struct ucontent chunk;
71 chunk.data = malloc(1);
74 curl_easy_setopt(curl, CURLOPT_URL, url);
75 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
77 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content);
78 curl_easy_setopt(curl,
79 CURLOPT_POSTFIELDSIZE,
80 (long)strlen(content));
82 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
83 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
85 if (curl_easy_perform(curl) == CURLE_OK)
89 log_err(_("HTTP request fail url=%s"), url);
94 json_object *http_json_get(const char *url, struct json_object *j)
98 struct json_object *result;
100 if (log_level >= LOG_DEBUG)
101 log_debug("HTTP request= %s",
102 json_object_to_json_string(j));
104 in = json_object_to_json_string(j);
105 out = http_get(url, in);
108 result = json_tokener_parse(out);
110 if (log_level >= LOG_DEBUG)
111 log_debug("HTTP reply= %s", out);