code style, added missing 'const' (DEFAULT_FETCH_RETRIES)
[ppastats.git] / src / lp_ws.c
1 /*
2     Copyright (C) 2011 jeanfi@gmail.com
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU 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 <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include <curl/curl.h>
25 #include <json/json.h>
26
27 #include "cache.h"
28 #include "list.h"
29 #include "lp_ws.h"
30 #include "lp_json.h"
31 #include "ppastats.h"
32
33 static const char *QUERY_GET_PUBLISHED_BINARIES = "?ws.op=getPublishedBinaries";
34 static const char *QUERY_GET_DOWNLOAD_COUNT = "?ws.op=getDownloadCount";
35 static const char *
36 QUERY_GET_DAILY_DOWNLOAD_TOTALS = "?ws.op=getDailyDownloadTotals";
37
38 static const int DEFAULT_FETCH_RETRIES = 3;
39
40 static CURL *curl;
41
42 struct ucontent {
43         char *data;
44         size_t len;
45 };
46
47 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
48 {
49         size_t realsize = size * nmemb;
50         struct ucontent *mem = (struct ucontent *)userp;
51
52         mem->data = realloc(mem->data, mem->len + realsize + 1);
53
54         memcpy(&(mem->data[mem->len]), buffer, realsize);
55         mem->len += realsize;
56         mem->data[mem->len] = 0;
57
58         return realsize;
59 }
60
61 static char *fetch_url(const char *url)
62 {
63         struct ucontent *content = malloc(sizeof(struct ucontent));
64         char *result;
65         long code;
66         int retries;
67
68         if (debug)
69                 printf("DEBUG: fetch_url %s\n", url);
70
71         if (!curl) {
72                 if (debug)
73                         printf("DEBUG: initializing CURL\n");
74                 curl_global_init(CURL_GLOBAL_ALL);
75                 curl = curl_easy_init();
76         }
77
78         if (!curl)
79                 exit(EXIT_FAILURE);
80
81         result = NULL;
82
83         retries = DEFAULT_FETCH_RETRIES;
84
85  retrieve:
86         content->data = malloc(1);
87         content->data[0] = '\0';
88         content->len = 0;
89
90         curl_easy_setopt(curl, CURLOPT_URL, url);
91         curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
92         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
93         curl_easy_setopt(curl, CURLOPT_WRITEDATA, content);
94         curl_easy_setopt(curl, CURLOPT_USERAGENT, "ppastats/0.0");
95
96         if (curl_easy_perform(curl) == CURLE_OK) {
97                 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
98
99                 switch (code) {
100                 case 200:
101                         result = content->data;
102                         break;
103                 case 500:
104                 case 502:
105                 case 503:
106                 case 504:
107                         if (retries) {
108                                 fprintf(stderr,
109                                         "Fetch failed: with code %ld "
110                                         "for URL= %s\n",
111                                         code,
112                                         url);
113
114                                 if (debug)
115                                         printf("Wait 5s before retry.\n");
116                                 sleep(5);
117
118                                 free(content->data);
119                                 retries--;
120                                 goto retrieve;
121                         }
122                 default:
123                         fprintf(stderr, "Fetch failed: %ld\n", 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         if (debug)
296                 printf("DEBUG: cleanup CURL\n");
297
298         curl_easy_cleanup(curl);
299         curl_global_cleanup();
300 }