mailinfo: allow -u to fall back on latin1 to utf8 conversion.
[git] / http.c
1 #include "http.h"
2
3 int data_received;
4 int active_requests = 0;
5
6 #ifdef USE_CURL_MULTI
7 int max_requests = -1;
8 CURLM *curlm;
9 #endif
10 #ifndef NO_CURL_EASY_DUPHANDLE
11 CURL *curl_default;
12 #endif
13 char curl_errorstr[CURL_ERROR_SIZE];
14
15 int curl_ssl_verify = -1;
16 char *ssl_cert = NULL;
17 #if LIBCURL_VERSION_NUM >= 0x070902
18 char *ssl_key = NULL;
19 #endif
20 #if LIBCURL_VERSION_NUM >= 0x070908
21 char *ssl_capath = NULL;
22 #endif
23 char *ssl_cainfo = NULL;
24 long curl_low_speed_limit = -1;
25 long curl_low_speed_time = -1;
26
27 struct curl_slist *pragma_header;
28 struct curl_slist *no_range_header;
29
30 struct active_request_slot *active_queue_head = NULL;
31
32 size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb,
33                            struct buffer *buffer)
34 {
35         size_t size = eltsize * nmemb;
36         if (size > buffer->size - buffer->posn)
37                 size = buffer->size - buffer->posn;
38         memcpy(ptr, buffer->buffer + buffer->posn, size);
39         buffer->posn += size;
40         return size;
41 }
42
43 size_t fwrite_buffer(const void *ptr, size_t eltsize,
44                             size_t nmemb, struct buffer *buffer)
45 {
46         size_t size = eltsize * nmemb;
47         if (size > buffer->size - buffer->posn) {
48                 buffer->size = buffer->size * 3 / 2;
49                 if (buffer->size < buffer->posn + size)
50                         buffer->size = buffer->posn + size;
51                 buffer->buffer = xrealloc(buffer->buffer, buffer->size);
52         }
53         memcpy(buffer->buffer + buffer->posn, ptr, size);
54         buffer->posn += size;
55         data_received++;
56         return size;
57 }
58
59 size_t fwrite_null(const void *ptr, size_t eltsize,
60                           size_t nmemb, struct buffer *buffer)
61 {
62         data_received++;
63         return eltsize * nmemb;
64 }
65
66 static void finish_active_slot(struct active_request_slot *slot);
67
68 #ifdef USE_CURL_MULTI
69 static void process_curl_messages(void)
70 {
71         int num_messages;
72         struct active_request_slot *slot;
73         CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
74
75         while (curl_message != NULL) {
76                 if (curl_message->msg == CURLMSG_DONE) {
77                         int curl_result = curl_message->data.result;
78                         slot = active_queue_head;
79                         while (slot != NULL &&
80                                slot->curl != curl_message->easy_handle)
81                                 slot = slot->next;
82                         if (slot != NULL) {
83                                 curl_multi_remove_handle(curlm, slot->curl);
84                                 slot->curl_result = curl_result;
85                                 finish_active_slot(slot);
86                         } else {
87                                 fprintf(stderr, "Received DONE message for unknown request!\n");
88                         }
89                 } else {
90                         fprintf(stderr, "Unknown CURL message received: %d\n",
91                                 (int)curl_message->msg);
92                 }
93                 curl_message = curl_multi_info_read(curlm, &num_messages);
94         }
95 }
96 #endif
97
98 static int http_options(const char *var, const char *value)
99 {
100         if (!strcmp("http.sslverify", var)) {
101                 if (curl_ssl_verify == -1) {
102                         curl_ssl_verify = git_config_bool(var, value);
103                 }
104                 return 0;
105         }
106
107         if (!strcmp("http.sslcert", var)) {
108                 if (ssl_cert == NULL) {
109                         ssl_cert = xmalloc(strlen(value)+1);
110                         strcpy(ssl_cert, value);
111                 }
112                 return 0;
113         }
114 #if LIBCURL_VERSION_NUM >= 0x070902
115         if (!strcmp("http.sslkey", var)) {
116                 if (ssl_key == NULL) {
117                         ssl_key = xmalloc(strlen(value)+1);
118                         strcpy(ssl_key, value);
119                 }
120                 return 0;
121         }
122 #endif
123 #if LIBCURL_VERSION_NUM >= 0x070908
124         if (!strcmp("http.sslcapath", var)) {
125                 if (ssl_capath == NULL) {
126                         ssl_capath = xmalloc(strlen(value)+1);
127                         strcpy(ssl_capath, value);
128                 }
129                 return 0;
130         }
131 #endif
132         if (!strcmp("http.sslcainfo", var)) {
133                 if (ssl_cainfo == NULL) {
134                         ssl_cainfo = xmalloc(strlen(value)+1);
135                         strcpy(ssl_cainfo, value);
136                 }
137                 return 0;
138         }
139
140 #ifdef USE_CURL_MULTI   
141         if (!strcmp("http.maxrequests", var)) {
142                 if (max_requests == -1)
143                         max_requests = git_config_int(var, value);
144                 return 0;
145         }
146 #endif
147
148         if (!strcmp("http.lowspeedlimit", var)) {
149                 if (curl_low_speed_limit == -1)
150                         curl_low_speed_limit = (long)git_config_int(var, value);
151                 return 0;
152         }
153         if (!strcmp("http.lowspeedtime", var)) {
154                 if (curl_low_speed_time == -1)
155                         curl_low_speed_time = (long)git_config_int(var, value);
156                 return 0;
157         }
158
159         /* Fall back on the default ones */
160         return git_default_config(var, value);
161 }
162
163 static CURL* get_curl_handle(void)
164 {
165         CURL* result = curl_easy_init();
166
167         curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
168 #if LIBCURL_VERSION_NUM >= 0x070907
169         curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
170 #endif
171
172         if (ssl_cert != NULL)
173                 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
174 #if LIBCURL_VERSION_NUM >= 0x070902
175         if (ssl_key != NULL)
176                 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
177 #endif
178 #if LIBCURL_VERSION_NUM >= 0x070908
179         if (ssl_capath != NULL)
180                 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
181 #endif
182         if (ssl_cainfo != NULL)
183                 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
184         curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
185
186         if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
187                 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
188                                  curl_low_speed_limit);
189                 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
190                                  curl_low_speed_time);
191         }
192
193         curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
194
195         return result;
196 }
197
198 void http_init(void)
199 {
200         char *low_speed_limit;
201         char *low_speed_time;
202
203         curl_global_init(CURL_GLOBAL_ALL);
204
205         pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
206         no_range_header = curl_slist_append(no_range_header, "Range:");
207
208 #ifdef USE_CURL_MULTI
209         {
210                 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
211                 if (http_max_requests != NULL)
212                         max_requests = atoi(http_max_requests);
213         }
214
215         curlm = curl_multi_init();
216         if (curlm == NULL) {
217                 fprintf(stderr, "Error creating curl multi handle.\n");
218                 exit(1);
219         }
220 #endif
221
222         if (getenv("GIT_SSL_NO_VERIFY"))
223                 curl_ssl_verify = 0;
224
225         ssl_cert = getenv("GIT_SSL_CERT");
226 #if LIBCURL_VERSION_NUM >= 0x070902
227         ssl_key = getenv("GIT_SSL_KEY");
228 #endif
229 #if LIBCURL_VERSION_NUM >= 0x070908
230         ssl_capath = getenv("GIT_SSL_CAPATH");
231 #endif
232         ssl_cainfo = getenv("GIT_SSL_CAINFO");
233
234         low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
235         if (low_speed_limit != NULL)
236                 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
237         low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
238         if (low_speed_time != NULL)
239                 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
240
241         git_config(http_options);
242
243         if (curl_ssl_verify == -1)
244                 curl_ssl_verify = 1;
245
246 #ifdef USE_CURL_MULTI
247         if (max_requests < 1)
248                 max_requests = DEFAULT_MAX_REQUESTS;
249 #endif
250
251 #ifndef NO_CURL_EASY_DUPHANDLE
252         curl_default = get_curl_handle();
253 #endif
254 }
255
256 void http_cleanup(void)
257 {
258         struct active_request_slot *slot = active_queue_head;
259 #ifdef USE_CURL_MULTI
260         char *wait_url;
261 #endif
262
263         while (slot != NULL) {
264 #ifdef USE_CURL_MULTI
265                 if (slot->in_use) {
266                         curl_easy_getinfo(slot->curl,
267                                           CURLINFO_EFFECTIVE_URL,
268                                           &wait_url);
269                         fprintf(stderr, "Waiting for %s\n", wait_url);
270                         run_active_slot(slot);
271                 }
272 #endif
273                 if (slot->curl != NULL)
274                         curl_easy_cleanup(slot->curl);
275                 slot = slot->next;
276         }
277
278 #ifndef NO_CURL_EASY_DUPHANDLE
279         curl_easy_cleanup(curl_default);
280 #endif
281
282 #ifdef USE_CURL_MULTI
283         curl_multi_cleanup(curlm);
284 #endif
285         curl_global_cleanup();
286         
287 }
288
289 struct active_request_slot *get_active_slot(void)
290 {
291         struct active_request_slot *slot = active_queue_head;
292         struct active_request_slot *newslot;
293
294 #ifdef USE_CURL_MULTI
295         int num_transfers;
296
297         /* Wait for a slot to open up if the queue is full */
298         while (active_requests >= max_requests) {
299                 curl_multi_perform(curlm, &num_transfers);
300                 if (num_transfers < active_requests) {
301                         process_curl_messages();
302                 }
303         }
304 #endif
305
306         while (slot != NULL && slot->in_use) {
307                 slot = slot->next;
308         }
309         if (slot == NULL) {
310                 newslot = xmalloc(sizeof(*newslot));
311                 newslot->curl = NULL;
312                 newslot->in_use = 0;
313                 newslot->next = NULL;
314
315                 slot = active_queue_head;
316                 if (slot == NULL) {
317                         active_queue_head = newslot;
318                 } else {
319                         while (slot->next != NULL) {
320                                 slot = slot->next;
321                         }
322                         slot->next = newslot;
323                 }
324                 slot = newslot;
325         }
326
327         if (slot->curl == NULL) {
328 #ifdef NO_CURL_EASY_DUPHANDLE
329                 slot->curl = get_curl_handle();
330 #else
331                 slot->curl = curl_easy_duphandle(curl_default);
332 #endif
333         }
334
335         active_requests++;
336         slot->in_use = 1;
337         slot->local = NULL;
338         slot->callback_data = NULL;
339         slot->callback_func = NULL;
340         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
341         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_range_header);
342         curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
343
344         return slot;
345 }
346
347 int start_active_slot(struct active_request_slot *slot)
348 {
349 #ifdef USE_CURL_MULTI
350         CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
351
352         if (curlm_result != CURLM_OK &&
353             curlm_result != CURLM_CALL_MULTI_PERFORM) {
354                 active_requests--;
355                 slot->in_use = 0;
356                 return 0;
357         }
358 #endif
359         return 1;
360 }
361
362 #ifdef USE_CURL_MULTI
363 void step_active_slots(void)
364 {
365         int num_transfers;
366         CURLMcode curlm_result;
367
368         do {
369                 curlm_result = curl_multi_perform(curlm, &num_transfers);
370         } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
371         if (num_transfers < active_requests) {
372                 process_curl_messages();
373                 fill_active_slots();
374         }
375 }
376 #endif
377
378 void run_active_slot(struct active_request_slot *slot)
379 {
380 #ifdef USE_CURL_MULTI
381         long last_pos = 0;
382         long current_pos;
383         fd_set readfds;
384         fd_set writefds;
385         fd_set excfds;
386         int max_fd;
387         struct timeval select_timeout;
388
389         while (slot->in_use) {
390                 data_received = 0;
391                 step_active_slots();
392
393                 if (!data_received && slot->local != NULL) {
394                         current_pos = ftell(slot->local);
395                         if (current_pos > last_pos)
396                                 data_received++;
397                         last_pos = current_pos;
398                 }
399
400                 if (slot->in_use && !data_received) {
401                         max_fd = 0;
402                         FD_ZERO(&readfds);
403                         FD_ZERO(&writefds);
404                         FD_ZERO(&excfds);
405                         select_timeout.tv_sec = 0;
406                         select_timeout.tv_usec = 50000;
407                         select(max_fd, &readfds, &writefds,
408                                &excfds, &select_timeout);
409                 }
410         }
411 #else
412         while (slot->in_use) {
413                 slot->curl_result = curl_easy_perform(slot->curl);
414                 finish_active_slot(slot);
415         }
416 #endif
417 }
418
419 static void finish_active_slot(struct active_request_slot *slot)
420 {
421         active_requests--;
422         slot->in_use = 0;
423         curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
424  
425         /* Run callback if appropriate */
426         if (slot->callback_func != NULL) {
427                 slot->callback_func(slot->callback_data);
428         }
429 }
430
431 void finish_all_active_slots(void)
432 {
433         struct active_request_slot *slot = active_queue_head;
434
435         while (slot != NULL)
436                 if (slot->in_use) {
437                         run_active_slot(slot);
438                         slot = active_queue_head;
439                 } else {
440                         slot = slot->next;
441                 }
442 }