e663b8615f57f8007fc725bd24f3bbbd007bab2c
[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 struct ucontent {
32         char *data;
33         size_t len;
34 };
35
36 static CURL *curl;
37
38 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
39 {
40         size_t realsize;
41         struct ucontent *mem;
42
43         realsize = size * nmemb;
44         mem = (struct ucontent *)userp;
45
46         mem->data = realloc(mem->data, mem->len + realsize + 1);
47
48         memcpy(&(mem->data[mem->len]), buffer, realsize);
49         mem->len += realsize;
50         mem->data[mem->len] = 0;
51
52         return realsize;
53 }
54
55 void http_init()
56 {
57         curl = curl_easy_init();
58 }
59
60 void http_cleanup()
61 {
62         curl_easy_cleanup(curl);
63 }
64
65 char *http_get(const char *url, const char *content)
66 {
67         struct ucontent chunk;
68
69         chunk.data = malloc(1);
70         chunk.len = 0;
71
72         curl_easy_setopt(curl, CURLOPT_URL, url);
73         curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
74         if (content) {
75                 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content);
76                 curl_easy_setopt(curl,
77                                  CURLOPT_POSTFIELDSIZE, 
78                                  (long)strlen(content));
79         }
80         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
81         curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
82
83         if (curl_easy_perform(curl) == CURLE_OK)
84                 return chunk.data;
85
86         free(chunk.data);
87         fprintf(stderr, _("HTTP request fail url=%s"), url);
88
89         return NULL;
90 }
91
92 json_object *http_json_get(const char *url, struct json_object *j)
93 {
94         const char *in;
95         char *out;
96         struct json_object *result;
97
98         in = json_object_to_json_string(j);
99         out = http_get(url, in);
100         
101         if (out) {
102                 result = json_tokener_parse(out);
103                 free(out);
104                 return result;
105         }
106
107         return NULL;
108 }