(no commit message)
[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 #include <json/json.h>
29
30 #include "url.h"
31
32 struct ucontent {
33         char *data;
34         size_t len;
35 };
36
37 static CURL *curl;
38
39 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
40 {
41         size_t realsize;
42         struct ucontent *mem;
43
44         realsize = size * nmemb;
45         mem = (struct ucontent *)userp;
46
47         mem->data = realloc(mem->data, mem->len + realsize + 1);
48
49         memcpy(&(mem->data[mem->len]), buffer, realsize);
50         mem->len += realsize;
51         mem->data[mem->len] = 0;
52
53         return realsize;
54 }
55
56 void http_init()
57 {
58         curl = curl_easy_init();
59 }
60
61 void phttp_cleanup()
62 {
63         curl_easy_cleanup(curl);
64 }
65
66 json_object *get_json_object(const char *url)
67 {
68         struct ucontent chunk;
69         json_object *obj;
70
71         obj = NULL;
72
73         if (!curl)
74                 return NULL;
75
76         chunk.data = malloc(1);
77         chunk.len = 0;
78
79         curl_easy_setopt(curl, CURLOPT_URL, url);
80         curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
81         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
82         curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
83
84         if (curl_easy_perform(curl) == CURLE_OK)
85                 obj = json_tokener_parse(chunk.data);
86         else
87                 fprintf(stderr, _("Fail to connect to: %s"), url);
88
89         free(chunk.data);
90
91         return obj;
92 }
93
94 static json_object *post(const char *url, const char *body)
95 {
96         struct ucontent chunk;
97         json_object *obj;
98
99         obj = NULL;
100
101         chunk.data = malloc(1);
102         chunk.len = 0;
103
104         curl_easy_setopt(curl, CURLOPT_URL, url);
105         curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
106         if (body) {
107                 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body);
108                 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(body));
109         }
110         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
111         curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
112
113         if (curl_easy_perform(curl) == CURLE_OK)
114                 obj = json_tokener_parse(chunk.data);
115         else
116                 fprintf(stderr, _("Fail to connect to: %s"), url);
117         free(chunk.data);
118
119         return obj;
120 }
121
122 json_object *post_json_object(const char *url, struct json_object *j)
123 {
124         return post(url, json_object_to_json_string(j));
125 }