X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Fhttp.c;fp=src%2Fhttp.c;h=a1d02118046ce3f6217ef19e00af6ff9c6673b12;hb=1087cf87f08c0311004e47b05cae0f6649d835f7;hp=0000000000000000000000000000000000000000;hpb=6af255d5f5b89c4d79c246ceb2c45b1b3317cb4d;p=prss.git diff --git a/src/http.c b/src/http.c new file mode 100644 index 0000000..a1d0211 --- /dev/null +++ b/src/http.c @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2010-2013 jeanfi@gmail.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#include +#include +#define _(str) gettext(str) + +#include +#include +#include + +#include +#include + +#include "url.h" + +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; + struct ucontent *mem; + + realsize = size * nmemb; + mem = (struct ucontent *)userp; + + mem->data = realloc(mem->data, mem->len + realsize + 1); + + memcpy(&(mem->data[mem->len]), buffer, realsize); + mem->len += realsize; + mem->data[mem->len] = 0; + + return realsize; +} + +void phttp_init() +{ + curl = curl_easy_init(); +} + +void phttp_cleanup() +{ + curl_easy_cleanup(curl); +} + +json_object *get_json_object(const char *url) +{ + 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); + 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; +} + +static json_object *post(const char *url, const char *body) +{ + 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)); + } + 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)); +}