(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
29 #include "http.h"
30 #include "log.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         if (!curl)
59                 curl = curl_easy_init();
60 }
61
62 void http_cleanup()
63 {
64         curl_easy_cleanup(curl);
65 }
66
67 char *http_get(const char *url, const char *content)
68 {
69         struct ucontent chunk;
70
71         chunk.data = malloc(1);
72         chunk.len = 0;
73
74         curl_easy_setopt(curl, CURLOPT_URL, url);
75         curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
76         if (content) {
77                 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content);
78                 curl_easy_setopt(curl,
79                                  CURLOPT_POSTFIELDSIZE,
80                                  (long)strlen(content));
81         }
82         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
83         curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
84
85         if (curl_easy_perform(curl) == CURLE_OK)
86                 return chunk.data;
87
88         free(chunk.data);
89         log_err(_("HTTP request fail url=%s"), url);
90
91         return NULL;
92 }
93
94 json_object *http_json_get(const char *url, struct json_object *j)
95 {
96         const char *in;
97         char *out;
98         struct json_object *result;
99
100         if (log_level >= LOG_DEBUG)
101                 log_debug("HTTP request= %s",
102                        json_object_to_json_string(j));
103
104         in = json_object_to_json_string(j);
105         out = http_get(url, in);
106
107         if (out) {
108                 result = json_tokener_parse(out);
109
110                 if (log_level >= LOG_DEBUG)
111                         log_debug("HTTP reply= %s", out);
112
113                 free(out);
114                 return result;
115         }
116
117         return NULL;
118 }