]> git.ipfire.org Git - thirdparty/git.git/blame - http.c
Backup the array passed to fetch_pack so we can free items
[thirdparty/git.git] / http.c
CommitLineData
29508e1e
NH
1#include "http.h"
2
3int data_received;
4int active_requests = 0;
5
6#ifdef USE_CURL_MULTI
7int max_requests = -1;
8CURLM *curlm;
9#endif
10#ifndef NO_CURL_EASY_DUPHANDLE
11CURL *curl_default;
12#endif
13char curl_errorstr[CURL_ERROR_SIZE];
14
15int curl_ssl_verify = -1;
16char *ssl_cert = NULL;
17#if LIBCURL_VERSION_NUM >= 0x070902
18char *ssl_key = NULL;
19#endif
20#if LIBCURL_VERSION_NUM >= 0x070908
21char *ssl_capath = NULL;
22#endif
23char *ssl_cainfo = NULL;
24long curl_low_speed_limit = -1;
25long curl_low_speed_time = -1;
3ea099d4 26int curl_ftp_no_epsv = 0;
29508e1e
NH
27
28struct curl_slist *pragma_header;
29508e1e
NH
29
30struct active_request_slot *active_queue_head = NULL;
31
32size_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;
1d7f171c 38 memcpy(ptr, (char *) buffer->buffer + buffer->posn, size);
29508e1e
NH
39 buffer->posn += size;
40 return size;
41}
42
43size_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 }
1d7f171c 53 memcpy((char *) buffer->buffer + buffer->posn, ptr, size);
29508e1e
NH
54 buffer->posn += size;
55 data_received++;
56 return size;
57}
58
59size_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
66static void finish_active_slot(struct active_request_slot *slot);
67
68#ifdef USE_CURL_MULTI
69static 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
98static 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
a6080a0a 140#ifdef USE_CURL_MULTI
29508e1e
NH
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
3ea099d4
SK
159 if (!strcmp("http.noepsv", var)) {
160 curl_ftp_no_epsv = git_config_bool(var, value);
161 return 0;
162 }
163
29508e1e
NH
164 /* Fall back on the default ones */
165 return git_default_config(var, value);
166}
167
11979b98
JH
168static CURL* get_curl_handle(void)
169{
170 CURL* result = curl_easy_init();
171
172 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
173#if LIBCURL_VERSION_NUM >= 0x070907
174 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
175#endif
176
177 if (ssl_cert != NULL)
178 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
179#if LIBCURL_VERSION_NUM >= 0x070902
180 if (ssl_key != NULL)
181 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
182#endif
183#if LIBCURL_VERSION_NUM >= 0x070908
184 if (ssl_capath != NULL)
185 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
186#endif
187 if (ssl_cainfo != NULL)
188 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
189 curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
190
191 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
192 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
193 curl_low_speed_limit);
194 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
195 curl_low_speed_time);
196 }
197
198 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
199
7982d74e
MW
200 if (getenv("GIT_CURL_VERBOSE"))
201 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
202
20fc9bc5
NH
203 curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT);
204
3ea099d4
SK
205 if (curl_ftp_no_epsv)
206 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
207
11979b98
JH
208 return result;
209}
210
29508e1e
NH
211void http_init(void)
212{
213 char *low_speed_limit;
214 char *low_speed_time;
215
216 curl_global_init(CURL_GLOBAL_ALL);
217
218 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
29508e1e
NH
219
220#ifdef USE_CURL_MULTI
221 {
222 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
223 if (http_max_requests != NULL)
224 max_requests = atoi(http_max_requests);
225 }
226
227 curlm = curl_multi_init();
228 if (curlm == NULL) {
229 fprintf(stderr, "Error creating curl multi handle.\n");
230 exit(1);
231 }
232#endif
233
234 if (getenv("GIT_SSL_NO_VERIFY"))
235 curl_ssl_verify = 0;
236
237 ssl_cert = getenv("GIT_SSL_CERT");
238#if LIBCURL_VERSION_NUM >= 0x070902
239 ssl_key = getenv("GIT_SSL_KEY");
240#endif
241#if LIBCURL_VERSION_NUM >= 0x070908
242 ssl_capath = getenv("GIT_SSL_CAPATH");
243#endif
244 ssl_cainfo = getenv("GIT_SSL_CAINFO");
245
246 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
247 if (low_speed_limit != NULL)
248 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
249 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
250 if (low_speed_time != NULL)
251 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
252
253 git_config(http_options);
254
255 if (curl_ssl_verify == -1)
256 curl_ssl_verify = 1;
257
258#ifdef USE_CURL_MULTI
259 if (max_requests < 1)
260 max_requests = DEFAULT_MAX_REQUESTS;
261#endif
262
3ea099d4
SK
263 if (getenv("GIT_CURL_FTP_NO_EPSV"))
264 curl_ftp_no_epsv = 1;
265
29508e1e
NH
266#ifndef NO_CURL_EASY_DUPHANDLE
267 curl_default = get_curl_handle();
268#endif
269}
270
271void http_cleanup(void)
272{
273 struct active_request_slot *slot = active_queue_head;
274#ifdef USE_CURL_MULTI
275 char *wait_url;
29508e1e
NH
276#endif
277
278 while (slot != NULL) {
279#ifdef USE_CURL_MULTI
280 if (slot->in_use) {
281 curl_easy_getinfo(slot->curl,
282 CURLINFO_EFFECTIVE_URL,
283 &wait_url);
284 fprintf(stderr, "Waiting for %s\n", wait_url);
285 run_active_slot(slot);
286 }
287#endif
288 if (slot->curl != NULL)
289 curl_easy_cleanup(slot->curl);
290 slot = slot->next;
291 }
292
293#ifndef NO_CURL_EASY_DUPHANDLE
294 curl_easy_cleanup(curl_default);
295#endif
296
297#ifdef USE_CURL_MULTI
298 curl_multi_cleanup(curlm);
299#endif
300 curl_global_cleanup();
b3ca4e4e
NH
301
302 curl_slist_free_all(pragma_header);
e9d54bd1 303 pragma_header = NULL;
29508e1e
NH
304}
305
29508e1e
NH
306struct active_request_slot *get_active_slot(void)
307{
308 struct active_request_slot *slot = active_queue_head;
309 struct active_request_slot *newslot;
310
311#ifdef USE_CURL_MULTI
312 int num_transfers;
313
314 /* Wait for a slot to open up if the queue is full */
315 while (active_requests >= max_requests) {
316 curl_multi_perform(curlm, &num_transfers);
317 if (num_transfers < active_requests) {
318 process_curl_messages();
319 }
320 }
321#endif
322
323 while (slot != NULL && slot->in_use) {
324 slot = slot->next;
325 }
326 if (slot == NULL) {
327 newslot = xmalloc(sizeof(*newslot));
328 newslot->curl = NULL;
329 newslot->in_use = 0;
330 newslot->next = NULL;
331
332 slot = active_queue_head;
333 if (slot == NULL) {
334 active_queue_head = newslot;
335 } else {
336 while (slot->next != NULL) {
337 slot = slot->next;
338 }
339 slot->next = newslot;
340 }
341 slot = newslot;
342 }
343
344 if (slot->curl == NULL) {
345#ifdef NO_CURL_EASY_DUPHANDLE
346 slot->curl = get_curl_handle();
347#else
348 slot->curl = curl_easy_duphandle(curl_default);
349#endif
350 }
351
352 active_requests++;
353 slot->in_use = 1;
354 slot->local = NULL;
c8568e13 355 slot->results = NULL;
baa7b67d 356 slot->finished = NULL;
29508e1e
NH
357 slot->callback_data = NULL;
358 slot->callback_func = NULL;
9094950d 359 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
29508e1e 360 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
29508e1e 361 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
9094950d
NH
362 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
363 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
364 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
365 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
366 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
29508e1e
NH
367
368 return slot;
369}
370
371int start_active_slot(struct active_request_slot *slot)
372{
373#ifdef USE_CURL_MULTI
374 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
45c17412 375 int num_transfers;
29508e1e
NH
376
377 if (curlm_result != CURLM_OK &&
378 curlm_result != CURLM_CALL_MULTI_PERFORM) {
379 active_requests--;
380 slot->in_use = 0;
381 return 0;
382 }
45c17412
DB
383
384 /*
385 * We know there must be something to do, since we just added
386 * something.
387 */
388 curl_multi_perform(curlm, &num_transfers);
29508e1e
NH
389#endif
390 return 1;
391}
392
393#ifdef USE_CURL_MULTI
fc57b6aa
DB
394struct fill_chain {
395 void *data;
396 int (*fill)(void *);
397 struct fill_chain *next;
398};
399
400static struct fill_chain *fill_cfg = NULL;
401
402void add_fill_function(void *data, int (*fill)(void *))
403{
404 struct fill_chain *new = malloc(sizeof(*new));
405 struct fill_chain **linkp = &fill_cfg;
406 new->data = data;
407 new->fill = fill;
408 new->next = NULL;
409 while (*linkp)
410 linkp = &(*linkp)->next;
411 *linkp = new;
412}
413
45c17412
DB
414void fill_active_slots(void)
415{
416 struct active_request_slot *slot = active_queue_head;
417
fc57b6aa
DB
418 while (active_requests < max_requests) {
419 struct fill_chain *fill;
420 for (fill = fill_cfg; fill; fill = fill->next)
421 if (fill->fill(fill->data))
422 break;
423
424 if (!fill)
45c17412 425 break;
fc57b6aa 426 }
45c17412
DB
427
428 while (slot != NULL) {
429 if (!slot->in_use && slot->curl != NULL) {
430 curl_easy_cleanup(slot->curl);
431 slot->curl = NULL;
432 }
433 slot = slot->next;
434 }
435}
436
29508e1e
NH
437void step_active_slots(void)
438{
439 int num_transfers;
440 CURLMcode curlm_result;
441
442 do {
443 curlm_result = curl_multi_perform(curlm, &num_transfers);
444 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
445 if (num_transfers < active_requests) {
446 process_curl_messages();
447 fill_active_slots();
448 }
449}
450#endif
451
452void run_active_slot(struct active_request_slot *slot)
453{
454#ifdef USE_CURL_MULTI
455 long last_pos = 0;
456 long current_pos;
457 fd_set readfds;
458 fd_set writefds;
459 fd_set excfds;
460 int max_fd;
461 struct timeval select_timeout;
baa7b67d 462 int finished = 0;
29508e1e 463
baa7b67d
NH
464 slot->finished = &finished;
465 while (!finished) {
29508e1e
NH
466 data_received = 0;
467 step_active_slots();
468
469 if (!data_received && slot->local != NULL) {
470 current_pos = ftell(slot->local);
471 if (current_pos > last_pos)
472 data_received++;
473 last_pos = current_pos;
474 }
475
476 if (slot->in_use && !data_received) {
477 max_fd = 0;
478 FD_ZERO(&readfds);
479 FD_ZERO(&writefds);
480 FD_ZERO(&excfds);
481 select_timeout.tv_sec = 0;
482 select_timeout.tv_usec = 50000;
483 select(max_fd, &readfds, &writefds,
484 &excfds, &select_timeout);
485 }
486 }
487#else
488 while (slot->in_use) {
489 slot->curl_result = curl_easy_perform(slot->curl);
490 finish_active_slot(slot);
491 }
492#endif
493}
494
53f31389 495static void closedown_active_slot(struct active_request_slot *slot)
29508e1e
NH
496{
497 active_requests--;
498 slot->in_use = 0;
53f31389
MW
499}
500
501void release_active_slot(struct active_request_slot *slot)
502{
503 closedown_active_slot(slot);
504 if (slot->curl) {
b3ca4e4e 505#ifdef USE_CURL_MULTI
53f31389 506 curl_multi_remove_handle(curlm, slot->curl);
b3ca4e4e 507#endif
53f31389
MW
508 curl_easy_cleanup(slot->curl);
509 slot->curl = NULL;
510 }
b3ca4e4e 511#ifdef USE_CURL_MULTI
53f31389 512 fill_active_slots();
b3ca4e4e 513#endif
53f31389
MW
514}
515
516static void finish_active_slot(struct active_request_slot *slot)
517{
518 closedown_active_slot(slot);
29508e1e 519 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
c8568e13 520
baa7b67d
NH
521 if (slot->finished != NULL)
522 (*slot->finished) = 1;
523
c8568e13
NH
524 /* Store slot results so they can be read after the slot is reused */
525 if (slot->results != NULL) {
526 slot->results->curl_result = slot->curl_result;
527 slot->results->http_code = slot->http_code;
528 }
529
29508e1e
NH
530 /* Run callback if appropriate */
531 if (slot->callback_func != NULL) {
532 slot->callback_func(slot->callback_data);
533 }
534}
535
536void finish_all_active_slots(void)
537{
538 struct active_request_slot *slot = active_queue_head;
539
540 while (slot != NULL)
541 if (slot->in_use) {
542 run_active_slot(slot);
543 slot = active_queue_head;
544 } else {
545 slot = slot->next;
546 }
547}