f2351c9e31c16d2f76dda020a590a68c71b18b38
[ppastats.git] / src / http.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
29 #include "fcache.h"
30 #include "http.h"
31 #include "log.h"
32
33 static const int DEFAULT_FETCH_RETRIES = 10;
34
35 static CURL *curl;
36
37 struct ucontent {
38         char *data;
39         size_t len;
40 };
41
42 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
43 {
44         size_t realsize = size * nmemb;
45         struct ucontent *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 static void init()
57 {
58         if (!curl) {
59                 log_debug(_("initializing CURL"));
60                 curl_global_init(CURL_GLOBAL_ALL);
61                 curl = curl_easy_init();
62         }
63
64         if (!curl)
65                 exit(EXIT_FAILURE);
66 }
67
68 static char *fetch_url(const char *url)
69 {
70         struct ucontent *content = malloc(sizeof(struct ucontent));
71         char *result;
72         long code;
73         int retries;
74         unsigned int s;
75
76         log_debug(_("fetch_url(): %s"), url);
77
78         init();
79
80         result = NULL;
81
82         retries = DEFAULT_FETCH_RETRIES;
83
84  retrieve:
85         content->data = malloc(1);
86         content->data[0] = '\0';
87         content->len = 0;
88
89         curl_easy_setopt(curl, CURLOPT_URL, url);
90         curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
91         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
92         curl_easy_setopt(curl, CURLOPT_WRITEDATA, content);
93         curl_easy_setopt(curl, CURLOPT_USERAGENT, "ppastats/0.0");
94
95         if (curl_easy_perform(curl) == CURLE_OK) {
96                 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
97
98                 switch (code) {
99                 case 200:
100                         result = content->data;
101                         break;
102                 case 500:
103                 case 502:
104                 case 503:
105                 case 504:
106                         log_err(_("Fetch failed with code %ld for URL %s"),
107                                 code,
108                                 url);
109
110                         if (retries) {
111                                 s = 2 * (DEFAULT_FETCH_RETRIES - retries) + 2;
112                                 log_debug(_("Wait %ds before retry"), s);
113                                 sleep(s);
114
115                                 free(content->data);
116                                 retries--;
117                                 goto retrieve;
118                         }
119
120                         break;
121                 default:
122                         log_err(_("Fetch failed with code %ld for URL %s"),
123                                 code,
124                                 url);
125                 }
126         }
127
128         if (!result)
129                 free(content->data);
130
131         free(content);
132
133         return result;
134 }
135
136 void http_cleanup()
137 {
138         log_debug(_("cleanup CURL"));
139
140         curl_easy_cleanup(curl);
141         curl_global_cleanup();
142 }
143
144 char *get_url_content(const char *url, unsigned int use_cache)
145 {
146         char *content;
147
148         content = NULL;
149
150         if (use_cache)
151                 content = fcache_get(url + 7);
152
153         if (!content) {
154                 content = fetch_url(url);
155         }
156
157         if (use_cache && content)
158                 fcache_put(url + 7, content);
159
160         return content;
161 }
162