12f8b72a64c25c32c1e5668c5918de4e13f50b0d
[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 <pthread.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <curl/curl.h>
29
30 #include "http.h"
31 #include "log.h"
32
33 struct ucontent {
34         char *data;
35         size_t len;
36 };
37
38 static CURL *curl;
39 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
40
41 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
42 {
43         size_t realsize;
44         struct ucontent *mem;
45
46         realsize = size * nmemb;
47         mem = (struct ucontent *)userp;
48
49         mem->data = realloc(mem->data, mem->len + realsize + 1);
50
51         memcpy(&(mem->data[mem->len]), buffer, realsize);
52         mem->len += realsize;
53         mem->data[mem->len] = 0;
54
55         return realsize;
56 }
57
58 void http_init()
59 {
60         if (!curl)
61                 curl = curl_easy_init();
62 }
63
64 void http_cleanup()
65 {
66         curl_easy_cleanup(curl);
67 }
68
69 static char *http_get(const char *url, const char *content)
70 {
71         struct ucontent chunk;
72         int result;
73
74         chunk.data = malloc(1);
75         chunk.len = 0;
76
77         pthread_mutex_lock(&lock);
78
79         curl_easy_setopt(curl, CURLOPT_URL, url);
80         curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
81         if (content) {
82                 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content);
83                 curl_easy_setopt(curl,
84                                  CURLOPT_POSTFIELDSIZE,
85                                  (long)strlen(content));
86         }
87         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
88         curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
89
90         result = curl_easy_perform(curl);
91
92         pthread_mutex_unlock(&lock);
93
94         if (result == CURLE_OK)
95                 return chunk.data;
96
97         free(chunk.data);
98         log_err(_("HTTP request fail url=%s"), url);
99
100         return NULL;
101 }
102
103 json_object *http_json_get(const char *url, struct json_object *j)
104 {
105         const char *in;
106         char *out;
107         struct json_object *result;
108
109         if (log_level >= LOG_DEBUG)
110                 log_debug("HTTP request= %s",
111                        json_object_to_json_string(j));
112
113         in = json_object_to_json_string(j);
114         out = http_get(url, in);
115
116         if (out) {
117                 result = json_tokener_parse(out);
118
119                 if (log_level >= LOG_DEBUG)
120                         log_debug("HTTP reply= %s", out);
121
122                 free(out);
123                 return result;
124         }
125
126         return NULL;
127 }