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