update copyright end date to 2012
[ppastats.git] / src / lp_ws.c
1 /*
2  * Copyright (C) 2011-2012 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
20 #include <libintl.h>
21 #define _(String) gettext(String)
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include <curl/curl.h>
28 #include <json/json.h>
29
30 #include "cache.h"
31 #include "list.h"
32 #include "log.h"
33 #include "lp_ws.h"
34 #include "lp_json.h"
35 #include "ppastats.h"
36
37 static const char *QUERY_GET_PUBLISHED_BINARIES = "?ws.op=getPublishedBinaries";
38 static const char *QUERY_GET_DOWNLOAD_COUNT = "?ws.op=getDownloadCount";
39 static const char *
40 QUERY_GET_DAILY_DOWNLOAD_TOTALS = "?ws.op=getDailyDownloadTotals";
41
42 static const int DEFAULT_FETCH_RETRIES = 3;
43
44 static CURL *curl;
45
46 struct ucontent {
47         char *data;
48         size_t len;
49 };
50
51 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
52 {
53         size_t realsize = size * nmemb;
54         struct ucontent *mem = (struct ucontent *)userp;
55
56         mem->data = realloc(mem->data, mem->len + realsize + 1);
57
58         memcpy(&(mem->data[mem->len]), buffer, realsize);
59         mem->len += realsize;
60         mem->data[mem->len] = 0;
61
62         return realsize;
63 }
64
65 static char *fetch_url(const char *url)
66 {
67         struct ucontent *content = malloc(sizeof(struct ucontent));
68         char *result;
69         long code;
70         int retries;
71
72         log_debug(_("fetch_url(): %s"), url);
73
74         if (!curl) {
75                 log_debug(_("initializing CURL"));
76                 curl_global_init(CURL_GLOBAL_ALL);
77                 curl = curl_easy_init();
78         }
79
80         if (!curl)
81                 exit(EXIT_FAILURE);
82
83         result = NULL;
84
85         retries = DEFAULT_FETCH_RETRIES;
86
87  retrieve:
88         content->data = malloc(1);
89         content->data[0] = '\0';
90         content->len = 0;
91
92         curl_easy_setopt(curl, CURLOPT_URL, url);
93         curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
94         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
95         curl_easy_setopt(curl, CURLOPT_WRITEDATA, content);
96         curl_easy_setopt(curl, CURLOPT_USERAGENT, "ppastats/0.0");
97
98         if (curl_easy_perform(curl) == CURLE_OK) {
99                 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
100
101                 switch (code) {
102                 case 200:
103                         result = content->data;
104                         break;
105                 case 500:
106                 case 502:
107                 case 503:
108                 case 504:
109                         if (retries) {
110                                 log_err(_("Fetch failed with code %ld "
111                                           "for URL %s"),
112                                         code,
113                                         url);
114
115                                 log_debug(_("Wait 5s before retry"));
116                                 sleep(5);
117
118                                 free(content->data);
119                                 retries--;
120                                 goto retrieve;
121                         }
122                 default:
123                         log_err(_("Fetch failed: %ld"), code);
124                 }
125         }
126
127         if (!result)
128                 free(content->data);
129
130         free(content);
131
132         return result;
133 }
134
135 static json_object *get_json_object(const char *url)
136 {
137         json_object *obj = NULL;
138         char *body;
139
140         body = fetch_url(url);
141
142         if (body) {
143                 obj = json_tokener_parse(body);
144
145                 free(body);
146
147                 return obj;
148         }
149
150         return NULL;
151 }
152
153 #define json_object_to_bpph_list \
154 json_object_to_binary_package_publishing_history_list
155
156 struct binary_package_publishing_history * *
157 get_binary_package_publishing_history_list(const char *archive_url,
158                                            const char *pkg_status)
159 {
160         struct json_object *o_next;
161         char *url;
162         json_object *o;
163         void **result = NULL;
164
165         url = malloc(strlen(archive_url)+
166                      strlen(QUERY_GET_PUBLISHED_BINARIES)+
167                      (pkg_status ? strlen("&status=")+strlen(pkg_status) : 0)+
168                      1);
169
170         strcpy(url, archive_url);
171         strcat(url, QUERY_GET_PUBLISHED_BINARIES);
172
173         if (pkg_status) {
174                 strcat(url, "&status=");
175                 strcat(url, pkg_status);
176         }
177
178         while (url) {
179                 o = get_json_object(url);
180                 free(url);
181                 url = NULL;
182
183                 if (!o)
184                         break;
185
186                 result = list_append_list(result,
187                                           (void **)json_object_to_bpph_list(o));
188
189                 o_next = json_object_object_get(o, "next_collection_link");
190
191                 if (o_next)
192                         url = strdup(json_object_get_string(o_next));
193
194                 json_object_put(o);
195         }
196
197         return (struct binary_package_publishing_history **)result;
198 }
199
200 int get_download_count(const char *archive_url)
201 {
202         int n = strlen(archive_url) + strlen(QUERY_GET_DOWNLOAD_COUNT) + 1;
203         char *url = malloc(n);
204         int result;
205         json_object *obj;
206
207         strcpy(url, archive_url);
208         strcat(url, QUERY_GET_DOWNLOAD_COUNT);
209
210         obj = get_json_object(url);
211         free(url);
212
213         if (!obj)
214                 return -1;
215
216         result = json_object_get_int(obj);
217
218         json_object_put(obj);
219
220         return result;
221 }
222
223 const struct distro_arch_series *get_distro_arch_series(const char *url)
224 {
225         json_object *obj;
226         const struct distro_arch_series *distro;
227
228         distro = cache_get(url);
229         if (distro)
230                 return (struct distro_arch_series *)distro;
231
232         obj = get_json_object(url);
233
234         if (!obj)
235                 return NULL;
236
237         distro = json_object_to_distro_arch_series(obj);
238
239         json_object_put(obj);
240
241         cache_put(url, distro, (void (*)(void *))&distro_arch_series_free);
242
243         return distro;
244 }
245
246 const struct distro_series *get_distro_series(const char *url)
247 {
248         json_object *obj;
249         const struct distro_series *distro;
250
251         distro = cache_get(url);
252         if (distro)
253                 return (struct distro_series *)distro;
254
255         obj = get_json_object(url);
256
257         if (!obj)
258                 return NULL;
259
260         distro = json_object_to_distro_series(obj);
261
262         json_object_put(obj);
263
264         cache_put(url, distro, (void (*)(void *))&distro_series_free);
265
266         return distro;
267 }
268
269 struct daily_download_total **get_daily_download_totals(const char *binary_url)
270 {
271         char *url;
272         json_object *obj;
273         struct daily_download_total **result = NULL;
274
275         url = malloc(strlen(binary_url)+
276                      strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)+1);
277
278         strcpy(url, binary_url);
279         strcat(url, QUERY_GET_DAILY_DOWNLOAD_TOTALS);
280
281         obj = get_json_object(url);
282
283         if (obj) {
284                 result = json_object_to_daily_download_totals(obj);
285                 json_object_put(obj);
286         }
287
288         free(url);
289
290         return result;
291 }
292
293 void lp_ws_cleanup()
294 {
295         log_debug(_("cleanup CURL"));
296
297         curl_easy_cleanup(curl);
298         curl_global_cleanup();
299 }