set copyright year to 2015
[ppastats.git] / src / http.c
1 /*
2  * Copyright (C) 2011-2015 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 <plog.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 #ifdef CURLOPT_TRANSFER_ENCODING
95         /* added since Curl 7.21.7 */
96         curl_easy_setopt(curl, CURLOPT_TRANSFER_ENCODING, 1);
97 #endif
98
99         if (curl_easy_perform(curl) == CURLE_OK) {
100                 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
101
102                 switch (code) {
103                 case 200:
104                         result = content->data;
105                         break;
106                 case 500:
107                 case 502:
108                 case 503:
109                 case 504:
110                         log_err(_("Fetch failed with code %ld for URL %s"),
111                                 code,
112                                 url);
113
114                         if (retries) {
115                                 s = 2 * (DEFAULT_FETCH_RETRIES - retries) + 2;
116                                 log_debug(_("Wait %ds before retry"), s);
117                                 sleep(s);
118
119                                 free(content->data);
120                                 retries--;
121                                 goto retrieve;
122                         }
123
124                         break;
125                 default:
126                         log_err(_("Fetch failed with code %ld for URL %s"),
127                                 code,
128                                 url);
129                 }
130         }
131
132         if (!result)
133                 free(content->data);
134
135         free(content);
136
137         return result;
138 }
139
140 void http_cleanup()
141 {
142         log_fct_enter();
143
144         curl_easy_cleanup(curl);
145         curl_global_cleanup();
146
147         log_fct_exit();
148 }
149
150 char *get_url_content(const char *url, unsigned int use_cache)
151 {
152         char *content;
153
154         content = NULL;
155
156         if (use_cache)
157                 content = fcache_get(url + 7);
158
159         if (!content)
160                 content = fetch_url(url);
161
162         if (use_cache && content)
163                 fcache_put(url + 7, content);
164
165         return content;
166 }