43053edbcd7a02deb3a6917e827cb0eab557b5b5
[prss.git] / src / http.c
1 /*
2  * Copyright (C) 2010-2013 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19 #include <locale.h>
20 #include <libintl.h>
21 #define _(str) gettext(str)
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <curl/curl.h>
28
29 #include "http.h"
30
31 static int debug;
32
33 struct ucontent {
34         char *data;
35         size_t len;
36 };
37
38 static CURL *curl;
39
40 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
41 {
42         size_t realsize;
43         struct ucontent *mem;
44
45         realsize = size * nmemb;
46         mem = (struct ucontent *)userp;
47
48         mem->data = realloc(mem->data, mem->len + realsize + 1);
49
50         memcpy(&(mem->data[mem->len]), buffer, realsize);
51         mem->len += realsize;
52         mem->data[mem->len] = 0;
53
54         return realsize;
55 }
56
57 void http_init()
58 {
59         if (!curl)
60                 curl = curl_easy_init();
61 }
62
63 void http_cleanup()
64 {
65         curl_easy_cleanup(curl);
66 }
67
68 char *http_get(const char *url, const char *content)
69 {
70         struct ucontent chunk;
71
72         chunk.data = malloc(1);
73         chunk.len = 0;
74
75         curl_easy_setopt(curl, CURLOPT_URL, url);
76         curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
77         if (content) {
78                 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content);
79                 curl_easy_setopt(curl,
80                                  CURLOPT_POSTFIELDSIZE,
81                                  (long)strlen(content));
82         }
83         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
84         curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
85
86         if (curl_easy_perform(curl) == CURLE_OK)
87                 return chunk.data;
88
89         free(chunk.data);
90         fprintf(stderr, _("HTTP request fail url=%s"), url);
91
92         return NULL;
93 }
94
95 json_object *http_json_get(const char *url, struct json_object *j)
96 {
97         const char *in;
98         char *out;
99         struct json_object *result;
100
101         in = json_object_to_json_string(j);
102         out = http_get(url, in);
103
104         if (debug)
105                 printf("HTTP request= %s\n",
106                        json_object_to_json_string(j));
107
108         if (out) {
109                 result = json_tokener_parse(out);
110                 if (debug)
111                         printf("HTTP reply= %s\n", out);
112                 free(out);
113                 return result;
114         }
115
116         return NULL;
117 }