Ignore built git-lost+found.
[git/git.git] / http-fetch.c
CommitLineData
6eb7ed54
DB
1#include "cache.h"
2#include "commit.h"
271421cd 3#include "pack.h"
215a7ad1 4#include "fetch.h"
4250a5e5 5
6eb7ed54
DB
6#include <curl/curl.h>
7#include <curl/easy.h>
8
a7a8d378
NH
9#if LIBCURL_VERSION_NUM >= 0x070908
10#define USE_CURL_MULTI
1d389ab6 11#define DEFAULT_MAX_REQUESTS 5
a7a8d378 12#endif
1d389ab6 13
4a30976e
JS
14#if LIBCURL_VERSION_NUM < 0x070704
15#define curl_global_cleanup() do { /* nothing */ } while(0)
16#endif
17#if LIBCURL_VERSION_NUM < 0x070800
18#define curl_global_init(a) do { /* nothing */ } while(0)
19#endif
4a30976e 20
7baa3e86
JS
21#if LIBCURL_VERSION_NUM < 0x070c04
22#define NO_CURL_EASY_DUPHANDLE
23#endif
24
49a0f240
NH
25#define PREV_BUF_SIZE 4096
26#define RANGE_HEADER_SIZE 30
27
f7eb290f 28static int got_alternates = 0;
1d389ab6
NH
29static int active_requests = 0;
30static int data_received;
31
a7a8d378 32#ifdef USE_CURL_MULTI
d402d556 33static int max_requests = -1;
1d389ab6 34static CURLM *curlm;
a7a8d378 35#endif
7baa3e86 36#ifndef NO_CURL_EASY_DUPHANDLE
1d389ab6 37static CURL *curl_default;
7baa3e86 38#endif
03126006 39static struct curl_slist *pragma_header;
1db69b57 40static struct curl_slist *no_pragma_header;
49a0f240 41static struct curl_slist *no_range_header;
1ddea77e 42static char curl_errorstr[CURL_ERROR_SIZE];
6eb7ed54 43
b3661567
DB
44struct alt_base
45{
46 char *base;
47 int got_indices;
48 struct packed_git *packs;
49 struct alt_base *next;
50};
51
a7928f8e 52static struct alt_base *alt = NULL;
6eb7ed54 53
1d389ab6
NH
54enum transfer_state {
55 WAITING,
56 ABORTED,
57 ACTIVE,
58 COMPLETE,
59};
6eb7ed54 60
1d389ab6
NH
61struct transfer_request
62{
63 unsigned char sha1[20];
64 struct alt_base *repo;
65 char *url;
66 char filename[PATH_MAX];
67 char tmpfile[PATH_MAX];
68 int local;
69 enum transfer_state state;
70 CURLcode curl_result;
71 char errorstr[CURL_ERROR_SIZE];
72 long http_code;
73 unsigned char real_sha1[20];
74 SHA_CTX c;
75 z_stream stream;
76 int zret;
77 int rename;
78 struct active_request_slot *slot;
79 struct transfer_request *next;
80};
81
82struct active_request_slot
83{
84 CURL *curl;
85 FILE *local;
86 int in_use;
87 int done;
88 CURLcode curl_result;
f7eb290f 89 long http_code;
1d389ab6
NH
90 struct active_request_slot *next;
91};
92
93static struct transfer_request *request_queue_head = NULL;
94static struct active_request_slot *active_queue_head = NULL;
6eb7ed54 95
d402d556
NH
96static int curl_ssl_verify = -1;
97static char *ssl_cert = NULL;
7baa3e86 98#if LIBCURL_VERSION_NUM >= 0x070902
d402d556 99static char *ssl_key = NULL;
7baa3e86
JS
100#endif
101#if LIBCURL_VERSION_NUM >= 0x070908
d402d556 102static char *ssl_capath = NULL;
7baa3e86 103#endif
d402d556 104static char *ssl_cainfo = NULL;
e0004e28
NH
105static long curl_low_speed_limit = -1;
106static long curl_low_speed_time = -1;
3dcb90f5 107
fa3e0655
DB
108struct buffer
109{
110 size_t posn;
111 size_t size;
112 void *buffer;
113};
114
d402d556
NH
115static int http_options(const char *var, const char *value)
116{
117 if (!strcmp("http.sslverify", var)) {
118 if (curl_ssl_verify == -1) {
119 curl_ssl_verify = git_config_bool(var, value);
120 }
121 return 0;
122 }
123
124 if (!strcmp("http.sslcert", var)) {
125 if (ssl_cert == NULL) {
126 ssl_cert = xmalloc(strlen(value)+1);
127 strcpy(ssl_cert, value);
128 }
129 return 0;
130 }
131#if LIBCURL_VERSION_NUM >= 0x070902
132 if (!strcmp("http.sslkey", var)) {
133 if (ssl_key == NULL) {
134 ssl_key = xmalloc(strlen(value)+1);
135 strcpy(ssl_key, value);
136 }
137 return 0;
138 }
139#endif
140#if LIBCURL_VERSION_NUM >= 0x070908
141 if (!strcmp("http.sslcapath", var)) {
142 if (ssl_capath == NULL) {
143 ssl_capath = xmalloc(strlen(value)+1);
144 strcpy(ssl_capath, value);
145 }
146 return 0;
147 }
148#endif
149 if (!strcmp("http.sslcainfo", var)) {
150 if (ssl_cainfo == NULL) {
151 ssl_cainfo = xmalloc(strlen(value)+1);
152 strcpy(ssl_cainfo, value);
153 }
154 return 0;
155 }
156
157#ifdef USE_CURL_MULTI
158 if (!strcmp("http.maxrequests", var)) {
159 if (max_requests == -1)
160 max_requests = git_config_int(var, value);
161 return 0;
162 }
163#endif
164
e0004e28
NH
165 if (!strcmp("http.lowspeedlimit", var)) {
166 if (curl_low_speed_limit == -1)
167 curl_low_speed_limit = (long)git_config_int(var, value);
168 return 0;
169 }
170 if (!strcmp("http.lowspeedtime", var)) {
171 if (curl_low_speed_time == -1)
172 curl_low_speed_time = (long)git_config_int(var, value);
173 return 0;
174 }
175
d402d556
NH
176 /* Fall back on the default ones */
177 return git_default_config(var, value);
178}
179
fa3e0655 180static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
182005b9
DB
181 struct buffer *buffer)
182{
fa3e0655
DB
183 size_t size = eltsize * nmemb;
184 if (size > buffer->size - buffer->posn)
185 size = buffer->size - buffer->posn;
186 memcpy(buffer->buffer + buffer->posn, ptr, size);
187 buffer->posn += size;
1d389ab6 188 data_received++;
fa3e0655
DB
189 return size;
190}
191
bc8f2652
SV
192static size_t fwrite_buffer_dynamic(const void *ptr, size_t eltsize,
193 size_t nmemb, struct buffer *buffer)
194{
195 size_t size = eltsize * nmemb;
196 if (size > buffer->size - buffer->posn) {
197 buffer->size = buffer->size * 3 / 2;
198 if (buffer->size < buffer->posn + size)
199 buffer->size = buffer->posn + size;
200 buffer->buffer = xrealloc(buffer->buffer, buffer->size);
201 }
202 memcpy(buffer->buffer + buffer->posn, ptr, size);
203 buffer->posn += size;
204 data_received++;
205 return size;
206}
207
182005b9
DB
208static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
209 void *data)
210{
bf0f910d 211 unsigned char expn[4096];
6eb7ed54
DB
212 size_t size = eltsize * nmemb;
213 int posn = 0;
1d389ab6 214 struct transfer_request *request = (struct transfer_request *)data;
6eb7ed54 215 do {
1d389ab6
NH
216 ssize_t retval = write(request->local,
217 ptr + posn, size - posn);
6eb7ed54
DB
218 if (retval < 0)
219 return posn;
220 posn += retval;
221 } while (posn < size);
222
1d389ab6
NH
223 request->stream.avail_in = size;
224 request->stream.next_in = ptr;
6eb7ed54 225 do {
1d389ab6
NH
226 request->stream.next_out = expn;
227 request->stream.avail_out = sizeof(expn);
228 request->zret = inflate(&request->stream, Z_SYNC_FLUSH);
229 SHA1_Update(&request->c, expn,
230 sizeof(expn) - request->stream.avail_out);
231 } while (request->stream.avail_in && request->zret == Z_OK);
232 data_received++;
6eb7ed54
DB
233 return size;
234}
235
a7a8d378 236#ifdef USE_CURL_MULTI
f5dce806
PH
237static void process_curl_messages(void);
238static void process_request_queue(void);
a7a8d378 239#endif
f7eb290f 240static int fetch_alternates(char *base);
1d389ab6 241
f5dce806 242static CURL* get_curl_handle(void)
7baa3e86
JS
243{
244 CURL* result = curl_easy_init();
245
246 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
247#if LIBCURL_VERSION_NUM >= 0x070907
248 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
249#endif
250
251 if (ssl_cert != NULL)
252 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
253#if LIBCURL_VERSION_NUM >= 0x070902
254 if (ssl_key != NULL)
255 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
256#endif
257#if LIBCURL_VERSION_NUM >= 0x070908
258 if (ssl_capath != NULL)
259 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
260#endif
261 if (ssl_cainfo != NULL)
262 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
263 curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
264
e0004e28
NH
265 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
266 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
267 curl_low_speed_limit);
268 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
269 curl_low_speed_time);
270 }
271
66c9ec25
JW
272 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
273
7baa3e86
JS
274 return result;
275}
276
f5dce806 277static struct active_request_slot *get_active_slot(void)
1d389ab6
NH
278{
279 struct active_request_slot *slot = active_queue_head;
280 struct active_request_slot *newslot;
a7a8d378
NH
281
282#ifdef USE_CURL_MULTI
1d389ab6
NH
283 int num_transfers;
284
285 /* Wait for a slot to open up if the queue is full */
286 while (active_requests >= max_requests) {
287 curl_multi_perform(curlm, &num_transfers);
288 if (num_transfers < active_requests) {
289 process_curl_messages();
290 }
291 }
a7a8d378 292#endif
1d389ab6
NH
293
294 while (slot != NULL && slot->in_use) {
295 slot = slot->next;
296 }
297 if (slot == NULL) {
298 newslot = xmalloc(sizeof(*newslot));
f1a906a3 299 newslot->curl = NULL;
1d389ab6
NH
300 newslot->in_use = 0;
301 newslot->next = NULL;
302
303 slot = active_queue_head;
304 if (slot == NULL) {
305 active_queue_head = newslot;
306 } else {
307 while (slot->next != NULL) {
308 slot = slot->next;
309 }
310 slot->next = newslot;
311 }
312 slot = newslot;
313 }
314
f1a906a3
NH
315 if (slot->curl == NULL) {
316#ifdef NO_CURL_EASY_DUPHANDLE
317 slot->curl = get_curl_handle();
318#else
319 slot->curl = curl_easy_duphandle(curl_default);
320#endif
321 }
322
1d389ab6
NH
323 active_requests++;
324 slot->in_use = 1;
325 slot->done = 0;
326 slot->local = NULL;
03126006 327 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
1d389ab6
NH
328 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_range_header);
329 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
330
331 return slot;
332}
333
f5dce806 334static int start_active_slot(struct active_request_slot *slot)
1d389ab6 335{
a7a8d378 336#ifdef USE_CURL_MULTI
1d389ab6
NH
337 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
338
339 if (curlm_result != CURLM_OK &&
340 curlm_result != CURLM_CALL_MULTI_PERFORM) {
341 active_requests--;
342 slot->in_use = 0;
343 return 0;
344 }
a7a8d378 345#endif
1d389ab6
NH
346 return 1;
347}
348
f5dce806 349static void run_active_slot(struct active_request_slot *slot)
1d389ab6 350{
a7a8d378 351#ifdef USE_CURL_MULTI
1d389ab6
NH
352 int num_transfers;
353 long last_pos = 0;
354 long current_pos;
355 fd_set readfds;
356 fd_set writefds;
357 fd_set excfds;
358 int max_fd;
359 struct timeval select_timeout;
360 CURLMcode curlm_result;
361
362 while (!slot->done) {
363 data_received = 0;
364 do {
365 curlm_result = curl_multi_perform(curlm,
366 &num_transfers);
367 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
368 if (num_transfers < active_requests) {
369 process_curl_messages();
370 process_request_queue();
371 }
372
373 if (!data_received && slot->local != NULL) {
374 current_pos = ftell(slot->local);
375 if (current_pos > last_pos)
376 data_received++;
377 last_pos = current_pos;
378 }
379
380 if (!slot->done && !data_received) {
381 max_fd = 0;
382 FD_ZERO(&readfds);
383 FD_ZERO(&writefds);
384 FD_ZERO(&excfds);
385 select_timeout.tv_sec = 0;
386 select_timeout.tv_usec = 50000;
387 select(max_fd, &readfds, &writefds,
388 &excfds, &select_timeout);
389 }
390 }
a7a8d378
NH
391#else
392 slot->curl_result = curl_easy_perform(slot->curl);
393 active_requests--;
394#endif
1d389ab6
NH
395}
396
f5dce806 397static void start_request(struct transfer_request *request)
1d389ab6
NH
398{
399 char *hex = sha1_to_hex(request->sha1);
400 char prevfile[PATH_MAX];
401 char *url;
402 char *posn;
403 int prevlocal;
404 unsigned char prev_buf[PREV_BUF_SIZE];
405 ssize_t prev_read = 0;
406 long prev_posn = 0;
407 char range[RANGE_HEADER_SIZE];
408 struct curl_slist *range_header = NULL;
409 struct active_request_slot *slot;
410
411 snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
412 unlink(prevfile);
413 rename(request->tmpfile, prevfile);
414 unlink(request->tmpfile);
415
416 request->local = open(request->tmpfile,
417 O_WRONLY | O_CREAT | O_EXCL, 0666);
b721e01f
JH
418 /* This could have failed due to the "lazy directory creation";
419 * try to mkdir the last path component.
420 */
421 if (request->local < 0 && errno == ENOENT) {
422 char *dir = strrchr(request->tmpfile, '/');
423 if (dir) {
424 *dir = 0;
425 mkdir(request->tmpfile, 0777);
426 *dir = '/';
427 }
428 request->local = open(request->tmpfile,
429 O_WRONLY | O_CREAT | O_EXCL, 0666);
430 }
431
1d389ab6
NH
432 if (request->local < 0) {
433 request->state = ABORTED;
434 error("Couldn't create temporary file %s for %s: %s\n",
435 request->tmpfile, request->filename, strerror(errno));
436 return;
437 }
438
439 memset(&request->stream, 0, sizeof(request->stream));
440
441 inflateInit(&request->stream);
442
443 SHA1_Init(&request->c);
444
445 url = xmalloc(strlen(request->repo->base) + 50);
446 request->url = xmalloc(strlen(request->repo->base) + 50);
447 strcpy(url, request->repo->base);
448 posn = url + strlen(request->repo->base);
449 strcpy(posn, "objects/");
450 posn += 8;
451 memcpy(posn, hex, 2);
452 posn += 2;
453 *(posn++) = '/';
454 strcpy(posn, hex + 2);
455 strcpy(request->url, url);
456
457 /* If a previous temp file is present, process what was already
458 fetched. */
459 prevlocal = open(prevfile, O_RDONLY);
460 if (prevlocal != -1) {
461 do {
462 prev_read = read(prevlocal, prev_buf, PREV_BUF_SIZE);
463 if (prev_read>0) {
464 if (fwrite_sha1_file(prev_buf,
465 1,
466 prev_read,
467 request) == prev_read) {
468 prev_posn += prev_read;
469 } else {
470 prev_read = -1;
471 }
472 }
473 } while (prev_read > 0);
474 close(prevlocal);
475 }
476 unlink(prevfile);
477
478 /* Reset inflate/SHA1 if there was an error reading the previous temp
479 file; also rewind to the beginning of the local file. */
480 if (prev_read == -1) {
481 memset(&request->stream, 0, sizeof(request->stream));
482 inflateInit(&request->stream);
483 SHA1_Init(&request->c);
484 if (prev_posn>0) {
485 prev_posn = 0;
486 lseek(request->local, SEEK_SET, 0);
487 ftruncate(request->local, 0);
488 }
489 }
490
491 slot = get_active_slot();
492 curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
493 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
494 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
495 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
03126006 496 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
1d389ab6
NH
497
498 /* If we have successfully processed data from a previous fetch
499 attempt, only fetch the data we don't already have. */
500 if (prev_posn>0) {
501 if (get_verbosely)
502 fprintf(stderr,
503 "Resuming fetch of object %s at byte %ld\n",
504 hex, prev_posn);
505 sprintf(range, "Range: bytes=%ld-", prev_posn);
506 range_header = curl_slist_append(range_header, range);
507 curl_easy_setopt(slot->curl,
508 CURLOPT_HTTPHEADER, range_header);
509 }
510
a7a8d378 511 /* Try to get the request started, abort the request on error */
1d389ab6
NH
512 if (!start_active_slot(slot)) {
513 request->state = ABORTED;
514 close(request->local);
515 free(request->url);
516 return;
517 }
518
519 request->slot = slot;
520 request->state = ACTIVE;
521}
522
f5dce806 523static void finish_request(struct transfer_request *request)
1d389ab6 524{
50496b21
NH
525 struct stat st;
526
1d389ab6
NH
527 fchmod(request->local, 0444);
528 close(request->local);
529
530 if (request->http_code == 416) {
531 fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");
532 } else if (request->curl_result != CURLE_OK) {
50496b21
NH
533 if (stat(request->tmpfile, &st) == 0)
534 if (st.st_size == 0)
535 unlink(request->tmpfile);
1d389ab6
NH
536 return;
537 }
538
539 inflateEnd(&request->stream);
540 SHA1_Final(request->real_sha1, &request->c);
541 if (request->zret != Z_STREAM_END) {
542 unlink(request->tmpfile);
543 return;
544 }
545 if (memcmp(request->sha1, request->real_sha1, 20)) {
546 unlink(request->tmpfile);
547 return;
548 }
549 request->rename =
b721e01f 550 move_temp_to_file(request->tmpfile, request->filename);
1d389ab6
NH
551
552 if (request->rename == 0)
553 pull_say("got %s\n", sha1_to_hex(request->sha1));
554}
555
f5dce806 556static void release_request(struct transfer_request *request)
1d389ab6
NH
557{
558 struct transfer_request *entry = request_queue_head;
559
560 if (request == request_queue_head) {
561 request_queue_head = request->next;
562 } else {
563 while (entry->next != NULL && entry->next != request)
564 entry = entry->next;
565 if (entry->next == request)
566 entry->next = entry->next->next;
567 }
568
569 free(request->url);
570 free(request);
571}
572
a7a8d378 573#ifdef USE_CURL_MULTI
f4f440a0 574static void process_curl_messages(void)
1d389ab6
NH
575{
576 int num_messages;
577 struct active_request_slot *slot;
578 struct transfer_request *request = NULL;
579 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
580
581 while (curl_message != NULL) {
582 if (curl_message->msg == CURLMSG_DONE) {
90279074 583 int curl_result = curl_message->data.result;
1d389ab6
NH
584 slot = active_queue_head;
585 while (slot != NULL &&
586 slot->curl != curl_message->easy_handle)
587 slot = slot->next;
588 if (slot != NULL) {
589 curl_multi_remove_handle(curlm, slot->curl);
590 active_requests--;
591 slot->done = 1;
592 slot->in_use = 0;
90279074 593 slot->curl_result = curl_result;
f7eb290f
NH
594 curl_easy_getinfo(slot->curl,
595 CURLINFO_HTTP_CODE,
596 &slot->http_code);
1d389ab6
NH
597 request = request_queue_head;
598 while (request != NULL &&
599 request->slot != slot)
600 request = request->next;
601 } else {
602 fprintf(stderr, "Received DONE message for unknown request!\n");
603 }
604 if (request != NULL) {
90279074 605 request->curl_result = curl_result;
f7eb290f 606 request->http_code = slot->http_code;
1d389ab6 607 request->slot = NULL;
f7eb290f 608 request->state = COMPLETE;
1d389ab6
NH
609
610 /* Use alternates if necessary */
f7eb290f
NH
611 if (request->http_code == 404) {
612 fetch_alternates(alt->base);
613 if (request->repo->next != NULL) {
614 request->repo =
615 request->repo->next;
616 start_request(request);
617 }
1d389ab6
NH
618 } else {
619 finish_request(request);
1d389ab6
NH
620 }
621 }
622 } else {
623 fprintf(stderr, "Unknown CURL message received: %d\n",
624 (int)curl_message->msg);
625 }
626 curl_message = curl_multi_info_read(curlm, &num_messages);
627 }
628}
629
f4f440a0 630static void process_request_queue(void)
1d389ab6
NH
631{
632 struct transfer_request *request = request_queue_head;
f1a906a3 633 struct active_request_slot *slot = active_queue_head;
1d389ab6
NH
634 int num_transfers;
635
636 while (active_requests < max_requests && request != NULL) {
637 if (request->state == WAITING) {
11f0dafe
NH
638 if (has_sha1_file(request->sha1))
639 release_request(request);
640 else
641 start_request(request);
1d389ab6
NH
642 curl_multi_perform(curlm, &num_transfers);
643 }
644 request = request->next;
645 }
f1a906a3
NH
646
647 while (slot != NULL) {
648 if (!slot->in_use && slot->curl != NULL) {
649 curl_easy_cleanup(slot->curl);
650 slot->curl = NULL;
651 }
652 slot = slot->next;
653 }
1d389ab6 654}
a7a8d378 655#endif
1d389ab6
NH
656
657void prefetch(unsigned char *sha1)
658{
659 struct transfer_request *newreq;
660 struct transfer_request *tail;
661 char *filename = sha1_file_name(sha1);
662
663 newreq = xmalloc(sizeof(*newreq));
664 memcpy(newreq->sha1, sha1, 20);
665 newreq->repo = alt;
666 newreq->url = NULL;
667 newreq->local = -1;
668 newreq->state = WAITING;
669 snprintf(newreq->filename, sizeof(newreq->filename), "%s", filename);
670 snprintf(newreq->tmpfile, sizeof(newreq->tmpfile),
671 "%s.temp", filename);
672 newreq->next = NULL;
673
674 if (request_queue_head == NULL) {
675 request_queue_head = newreq;
676 } else {
677 tail = request_queue_head;
678 while (tail->next != NULL) {
679 tail = tail->next;
680 }
681 tail->next = newreq;
682 }
a7a8d378 683#ifdef USE_CURL_MULTI
1d389ab6
NH
684 process_request_queue();
685 process_curl_messages();
a7a8d378 686#endif
1d389ab6
NH
687}
688
b3661567 689static int fetch_index(struct alt_base *repo, unsigned char *sha1)
182005b9 690{
1d389ab6 691 char *hex = sha1_to_hex(sha1);
182005b9
DB
692 char *filename;
693 char *url;
49a0f240 694 char tmpfile[PATH_MAX];
49a0f240
NH
695 long prev_posn = 0;
696 char range[RANGE_HEADER_SIZE];
697 struct curl_slist *range_header = NULL;
182005b9
DB
698
699 FILE *indexfile;
1d389ab6 700 struct active_request_slot *slot;
182005b9
DB
701
702 if (has_pack_index(sha1))
703 return 0;
704
705 if (get_verbosely)
1d389ab6 706 fprintf(stderr, "Getting index for pack %s\n", hex);
182005b9 707
b3661567 708 url = xmalloc(strlen(repo->base) + 64);
1d389ab6 709 sprintf(url, "%s/objects/pack/pack-%s.idx", repo->base, hex);
182005b9
DB
710
711 filename = sha1_pack_index_name(sha1);
49a0f240
NH
712 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
713 indexfile = fopen(tmpfile, "a");
182005b9
DB
714 if (!indexfile)
715 return error("Unable to open local file %s for pack index",
716 filename);
717
1d389ab6
NH
718 slot = get_active_slot();
719 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
720 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
721 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
03126006 722 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
1d389ab6
NH
723 slot->local = indexfile;
724
49a0f240
NH
725 /* If there is data present from a previous transfer attempt,
726 resume where it left off */
727 prev_posn = ftell(indexfile);
728 if (prev_posn>0) {
729 if (get_verbosely)
730 fprintf(stderr,
731 "Resuming fetch of index for pack %s at byte %ld\n",
1d389ab6 732 hex, prev_posn);
49a0f240
NH
733 sprintf(range, "Range: bytes=%ld-", prev_posn);
734 range_header = curl_slist_append(range_header, range);
1d389ab6 735 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
49a0f240
NH
736 }
737
1d389ab6
NH
738 if (start_active_slot(slot)) {
739 run_active_slot(slot);
740 if (slot->curl_result != CURLE_OK) {
741 fclose(indexfile);
742 return error("Unable to get pack index %s\n%s", url,
743 curl_errorstr);
744 }
745 } else {
746 return error("Unable to start request");
182005b9
DB
747 }
748
749 fclose(indexfile);
49a0f240 750
b721e01f 751 return move_temp_to_file(tmpfile, filename);
182005b9
DB
752}
753
b3661567 754static int setup_index(struct alt_base *repo, unsigned char *sha1)
182005b9
DB
755{
756 struct packed_git *new_pack;
757 if (has_pack_file(sha1))
758 return 0; // don't list this as something we can get
759
b3661567 760 if (fetch_index(repo, sha1))
182005b9
DB
761 return -1;
762
763 new_pack = parse_pack_index(sha1);
b3661567
DB
764 new_pack->next = repo->packs;
765 repo->packs = new_pack;
182005b9
DB
766 return 0;
767}
768
b3661567
DB
769static int fetch_alternates(char *base)
770{
771 int ret = 0;
772 struct buffer buffer;
773 char *url;
774 char *data;
775 int i = 0;
1b0c1e67 776 int http_specific = 1;
1d389ab6 777 struct alt_base *tail = alt;
bc8f2652 778 static const char null_byte = '\0';
1d389ab6
NH
779
780 struct active_request_slot *slot;
dc1b5ea8 781
f7eb290f
NH
782 if (got_alternates)
783 return 0;
784
b3661567 785 data = xmalloc(4096);
bc8f2652 786 buffer.size = 4096;
b3661567
DB
787 buffer.posn = 0;
788 buffer.buffer = data;
789
790 if (get_verbosely)
6fd72e39 791 fprintf(stderr, "Getting alternates list for %s\n", base);
b3661567
DB
792
793 url = xmalloc(strlen(base) + 31);
794 sprintf(url, "%s/objects/info/http-alternates", base);
795
1d389ab6
NH
796 slot = get_active_slot();
797 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
bc8f2652
SV
798 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
799 fwrite_buffer_dynamic);
1d389ab6
NH
800 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
801 if (start_active_slot(slot)) {
802 run_active_slot(slot);
803 if (slot->curl_result != CURLE_OK || !buffer.posn) {
804 http_specific = 0;
805
806 sprintf(url, "%s/objects/info/alternates", base);
807
808 slot = get_active_slot();
809 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
810 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
bc8f2652 811 fwrite_buffer_dynamic);
1d389ab6
NH
812 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
813 if (start_active_slot(slot)) {
814 run_active_slot(slot);
815 if (slot->curl_result != CURLE_OK) {
bc8f2652 816 free(buffer.buffer);
f7eb290f
NH
817 if (slot->http_code == 404)
818 got_alternates = 1;
1d389ab6
NH
819 return 0;
820 }
821 }
b3661567 822 }
1d389ab6 823 } else {
bc8f2652 824 free(buffer.buffer);
1d389ab6 825 return 0;
b3661567
DB
826 }
827
bc8f2652
SV
828 fwrite_buffer_dynamic(&null_byte, 1, 1, &buffer);
829 buffer.posn--;
830 data = buffer.buffer;
1b0c1e67 831
b3661567
DB
832 while (i < buffer.posn) {
833 int posn = i;
834 while (posn < buffer.posn && data[posn] != '\n')
835 posn++;
836 if (data[posn] == '\n') {
1b0c1e67
DB
837 int okay = 0;
838 int serverlen = 0;
839 struct alt_base *newalt;
840 char *target = NULL;
b3661567 841 if (data[i] == '/') {
1b0c1e67
DB
842 serverlen = strchr(base + 8, '/') - base;
843 okay = 1;
844 } else if (!memcmp(data + i, "../", 3)) {
845 i += 3;
846 serverlen = strlen(base);
847 while (i + 2 < posn &&
848 !memcmp(data + i, "../", 3)) {
849 do {
850 serverlen--;
851 } while (serverlen &&
852 base[serverlen - 1] != '/');
853 i += 3;
854 }
855 // If the server got removed, give up.
856 okay = strchr(base, ':') - base + 3 <
857 serverlen;
858 } else if (http_specific) {
859 char *colon = strchr(data + i, ':');
860 char *slash = strchr(data + i, '/');
861 if (colon && slash && colon < data + posn &&
862 slash < data + posn && colon < slash) {
863 okay = 1;
864 }
865 }
866 // skip 'objects' at end
867 if (okay) {
868 target = xmalloc(serverlen + posn - i - 6);
b3661567
DB
869 strncpy(target, base, serverlen);
870 strncpy(target + serverlen, data + i,
871 posn - i - 7);
872 target[serverlen + posn - i - 7] = '\0';
873 if (get_verbosely)
874 fprintf(stderr,
875 "Also look at %s\n", target);
876 newalt = xmalloc(sizeof(*newalt));
1d389ab6 877 newalt->next = NULL;
b3661567
DB
878 newalt->base = target;
879 newalt->got_indices = 0;
880 newalt->packs = NULL;
1d389ab6
NH
881 while (tail->next != NULL)
882 tail = tail->next;
883 tail->next = newalt;
b3661567
DB
884 ret++;
885 }
886 }
887 i = posn + 1;
888 }
bc8f2652 889
f7eb290f 890 got_alternates = 1;
bc8f2652 891 free(buffer.buffer);
b3661567
DB
892 return ret;
893}
894
895static int fetch_indices(struct alt_base *repo)
182005b9
DB
896{
897 unsigned char sha1[20];
898 char *url;
899 struct buffer buffer;
900 char *data;
901 int i = 0;
902
1d389ab6
NH
903 struct active_request_slot *slot;
904
b3661567 905 if (repo->got_indices)
182005b9
DB
906 return 0;
907
908 data = xmalloc(4096);
909 buffer.size = 4096;
910 buffer.posn = 0;
911 buffer.buffer = data;
912
913 if (get_verbosely)
6fd72e39 914 fprintf(stderr, "Getting pack list for %s\n", repo->base);
182005b9 915
b3661567
DB
916 url = xmalloc(strlen(repo->base) + 21);
917 sprintf(url, "%s/objects/info/packs", repo->base);
182005b9 918
1d389ab6
NH
919 slot = get_active_slot();
920 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
bc8f2652
SV
921 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
922 fwrite_buffer_dynamic);
1d389ab6
NH
923 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
924 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
925 if (start_active_slot(slot)) {
926 run_active_slot(slot);
bc8f2652
SV
927 if (slot->curl_result != CURLE_OK) {
928 free(buffer.buffer);
1d389ab6 929 return error("%s", curl_errorstr);
bc8f2652 930 }
1d389ab6 931 } else {
bc8f2652 932 free(buffer.buffer);
1d389ab6
NH
933 return error("Unable to start request");
934 }
182005b9 935
bc8f2652 936 data = buffer.buffer;
b3661567 937 while (i < buffer.posn) {
182005b9
DB
938 switch (data[i]) {
939 case 'P':
940 i++;
941 if (i + 52 < buffer.posn &&
942 !strncmp(data + i, " pack-", 6) &&
943 !strncmp(data + i + 46, ".pack\n", 6)) {
944 get_sha1_hex(data + i + 6, sha1);
b3661567 945 setup_index(repo, sha1);
182005b9
DB
946 i += 51;
947 break;
948 }
949 default:
950 while (data[i] != '\n')
951 i++;
952 }
953 i++;
b3661567 954 }
182005b9 955
bc8f2652 956 free(buffer.buffer);
b3661567 957 repo->got_indices = 1;
182005b9
DB
958 return 0;
959}
960
b3661567 961static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
182005b9
DB
962{
963 char *url;
964 struct packed_git *target;
965 struct packed_git **lst;
966 FILE *packfile;
967 char *filename;
49a0f240
NH
968 char tmpfile[PATH_MAX];
969 int ret;
970 long prev_posn = 0;
971 char range[RANGE_HEADER_SIZE];
972 struct curl_slist *range_header = NULL;
1d389ab6
NH
973
974 struct active_request_slot *slot;
182005b9 975
b3661567 976 if (fetch_indices(repo))
182005b9 977 return -1;
b3661567 978 target = find_sha1_pack(sha1, repo->packs);
182005b9 979 if (!target)
b3661567 980 return -1;
182005b9
DB
981
982 if (get_verbosely) {
983 fprintf(stderr, "Getting pack %s\n",
984 sha1_to_hex(target->sha1));
985 fprintf(stderr, " which contains %s\n",
986 sha1_to_hex(sha1));
987 }
988
b3661567 989 url = xmalloc(strlen(repo->base) + 65);
182005b9 990 sprintf(url, "%s/objects/pack/pack-%s.pack",
b3661567 991 repo->base, sha1_to_hex(target->sha1));
182005b9
DB
992
993 filename = sha1_pack_name(target->sha1);
49a0f240
NH
994 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
995 packfile = fopen(tmpfile, "a");
182005b9
DB
996 if (!packfile)
997 return error("Unable to open local file %s for pack",
998 filename);
999
1d389ab6
NH
1000 slot = get_active_slot();
1001 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
1002 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
1003 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
03126006 1004 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
1d389ab6 1005 slot->local = packfile;
1ddea77e 1006
49a0f240
NH
1007 /* If there is data present from a previous transfer attempt,
1008 resume where it left off */
1009 prev_posn = ftell(packfile);
1010 if (prev_posn>0) {
1011 if (get_verbosely)
1012 fprintf(stderr,
1013 "Resuming fetch of pack %s at byte %ld\n",
1014 sha1_to_hex(target->sha1), prev_posn);
1015 sprintf(range, "Range: bytes=%ld-", prev_posn);
1016 range_header = curl_slist_append(range_header, range);
1d389ab6 1017 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
49a0f240
NH
1018 }
1019
1d389ab6
NH
1020 if (start_active_slot(slot)) {
1021 run_active_slot(slot);
1022 if (slot->curl_result != CURLE_OK) {
1023 fclose(packfile);
1024 return error("Unable to get pack file %s\n%s", url,
1025 curl_errorstr);
1026 }
1027 } else {
1028 return error("Unable to start request");
182005b9
DB
1029 }
1030
1031 fclose(packfile);
1032
b721e01f 1033 ret = move_temp_to_file(tmpfile, filename);
49a0f240 1034 if (ret)
b721e01f 1035 return ret;
49a0f240 1036
b3661567 1037 lst = &repo->packs;
182005b9
DB
1038 while (*lst != target)
1039 lst = &((*lst)->next);
1040 *lst = (*lst)->next;
1041
271421cd
JH
1042 if (verify_pack(target, 0))
1043 return -1;
182005b9
DB
1044 install_packed_git(target);
1045
1046 return 0;
1047}
1048
a7928f8e 1049static int fetch_object(struct alt_base *repo, unsigned char *sha1)
6eb7ed54
DB
1050{
1051 char *hex = sha1_to_hex(sha1);
09d92083 1052 int ret;
1d389ab6 1053 struct transfer_request *request = request_queue_head;
1d389ab6
NH
1054
1055 while (request != NULL && memcmp(request->sha1, sha1, 20))
1056 request = request->next;
1057 if (request == NULL)
1058 return error("Couldn't find request for %s in the queue", hex);
1059
11f0dafe
NH
1060 if (has_sha1_file(request->sha1)) {
1061 release_request(request);
1062 return 0;
1063 }
1064
a7a8d378 1065#ifdef USE_CURL_MULTI
1d389ab6 1066 while (request->state == WAITING) {
f5dce806 1067 int num_transfers;
1d389ab6
NH
1068 curl_multi_perform(curlm, &num_transfers);
1069 if (num_transfers < active_requests) {
1070 process_curl_messages();
1071 process_request_queue();
1072 }
1073 }
a7a8d378
NH
1074#else
1075 start_request(request);
1076#endif
6eb7ed54 1077
a7a8d378 1078 while (request->state == ACTIVE) {
1d389ab6 1079 run_active_slot(request->slot);
a7a8d378
NH
1080#ifndef USE_CURL_MULTI
1081 request->curl_result = request->slot->curl_result;
f7eb290f 1082 request->http_code = request->slot->http_code;
a7a8d378
NH
1083 request->slot = NULL;
1084
1085 /* Use alternates if necessary */
f7eb290f
NH
1086 if (request->http_code == 404) {
1087 fetch_alternates(alt->base);
1088 if (request->repo->next != NULL) {
1089 request->repo = request->repo->next;
1090 start_request(request);
1091 }
a7a8d378
NH
1092 } else {
1093 finish_request(request);
1094 request->state = COMPLETE;
1095 }
1096#endif
1097 }
6eb7ed54 1098
1d389ab6
NH
1099 if (request->state == ABORTED) {
1100 release_request(request);
1101 return error("Request for %s aborted", hex);
49a0f240 1102 }
49a0f240 1103
1d389ab6 1104 if (request->curl_result != CURLE_OK && request->http_code != 416) {
e2029eb9
PB
1105 if (request->http_code == 404)
1106 ret = -1; /* Be silent, it is probably in a pack. */
1107 else
1108 ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
1109 request->errorstr, request->curl_result,
1110 request->http_code, hex);
1d389ab6
NH
1111 release_request(request);
1112 return ret;
49a0f240
NH
1113 }
1114
1d389ab6
NH
1115 if (request->zret != Z_STREAM_END) {
1116 ret = error("File %s (%s) corrupt\n", hex, request->url);
1117 release_request(request);
1118 return ret;
49a0f240
NH
1119 }
1120
1d389ab6
NH
1121 if (memcmp(request->sha1, request->real_sha1, 20)) {
1122 release_request(request);
1123 return error("File %s has bad hash\n", hex);
182005b9 1124 }
6eb7ed54 1125
1d389ab6
NH
1126 if (request->rename < 0) {
1127 ret = error("unable to write sha1 filename %s: %s",
1128 request->filename,
1129 strerror(request->rename));
1130 release_request(request);
1131 return ret;
6eb7ed54 1132 }
49a0f240 1133
1d389ab6 1134 release_request(request);
6eb7ed54
DB
1135 return 0;
1136}
1137
b3661567
DB
1138int fetch(unsigned char *sha1)
1139{
1140 struct alt_base *altbase = alt;
1d389ab6
NH
1141
1142 if (!fetch_object(altbase, sha1))
1143 return 0;
b3661567 1144 while (altbase) {
b3661567
DB
1145 if (!fetch_pack(altbase, sha1))
1146 return 0;
f7eb290f 1147 fetch_alternates(alt->base);
b3661567
DB
1148 altbase = altbase->next;
1149 }
1150 return error("Unable to find %s under %s\n", sha1_to_hex(sha1),
1d389ab6 1151 alt->base);
b3661567
DB
1152}
1153
94fa447a
JH
1154static inline int needs_quote(int ch)
1155{
1156 switch (ch) {
1157 case '/': case '-': case '.':
1158 case 'A'...'Z': case 'a'...'z': case '0'...'9':
1159 return 0;
1160 default:
1161 return 1;
1162 }
1163}
1164
1165static inline int hex(int v)
1166{
1167 if (v < 10) return '0' + v;
1168 else return 'A' + v - 10;
1169}
1170
1171static char *quote_ref_url(const char *base, const char *ref)
1172{
1173 const char *cp;
1174 char *dp, *qref;
1175 int len, baselen, ch;
1176
1177 baselen = strlen(base);
1178 len = baselen + 6; /* "refs/" + NUL */
1179 for (cp = ref; (ch = *cp) != 0; cp++, len++)
1180 if (needs_quote(ch))
1181 len += 2; /* extra two hex plus replacement % */
1182 qref = xmalloc(len);
1183 memcpy(qref, base, baselen);
1184 memcpy(qref + baselen, "refs/", 5);
1185 for (cp = ref, dp = qref + baselen + 5; (ch = *cp) != 0; cp++) {
1186 if (needs_quote(ch)) {
1187 *dp++ = '%';
1188 *dp++ = hex((ch >> 4) & 0xF);
1189 *dp++ = hex(ch & 0xF);
1190 }
1191 else
1192 *dp++ = ch;
1193 }
1194 *dp = 0;
1195
1196 return qref;
1197}
1198
cd541a68
DB
1199int fetch_ref(char *ref, unsigned char *sha1)
1200{
94fa447a 1201 char *url;
fa3e0655
DB
1202 char hex[42];
1203 struct buffer buffer;
1d389ab6
NH
1204 char *base = alt->base;
1205 struct active_request_slot *slot;
fa3e0655
DB
1206 buffer.size = 41;
1207 buffer.posn = 0;
1208 buffer.buffer = hex;
1209 hex[41] = '\0';
1210
94fa447a 1211 url = quote_ref_url(base, ref);
1d389ab6
NH
1212 slot = get_active_slot();
1213 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
1214 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1215 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
1216 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1217 if (start_active_slot(slot)) {
1218 run_active_slot(slot);
1219 if (slot->curl_result != CURLE_OK)
1220 return error("Couldn't get %s for %s\n%s",
1221 url, ref, curl_errorstr);
1222 } else {
1223 return error("Unable to start request");
1224 }
fa3e0655
DB
1225
1226 hex[40] = '\0';
1227 get_sha1_hex(hex, sha1);
1228 return 0;
cd541a68
DB
1229}
1230
6eb7ed54
DB
1231int main(int argc, char **argv)
1232{
1233 char *commit_id;
1234 char *url;
1235 int arg = 1;
1d389ab6 1236 struct active_request_slot *slot;
e0004e28
NH
1237 char *low_speed_limit;
1238 char *low_speed_time;
7b9ae53e
NH
1239 char *wait_url;
1240 int rc = 0;
6eb7ed54
DB
1241
1242 while (arg < argc && argv[arg][0] == '-') {
1243 if (argv[arg][1] == 't') {
4250a5e5 1244 get_tree = 1;
6eb7ed54 1245 } else if (argv[arg][1] == 'c') {
4250a5e5 1246 get_history = 1;
6eb7ed54 1247 } else if (argv[arg][1] == 'a') {
4250a5e5
DB
1248 get_all = 1;
1249 get_tree = 1;
1250 get_history = 1;
e78d9772
JH
1251 } else if (argv[arg][1] == 'v') {
1252 get_verbosely = 1;
fa3e0655
DB
1253 } else if (argv[arg][1] == 'w') {
1254 write_ref = argv[arg + 1];
1255 arg++;
820eca68
DB
1256 } else if (!strcmp(argv[arg], "--recover")) {
1257 get_recover = 1;
6eb7ed54
DB
1258 }
1259 arg++;
1260 }
1261 if (argc < arg + 2) {
215a7ad1 1262 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
6eb7ed54
DB
1263 return 1;
1264 }
1265 commit_id = argv[arg];
1266 url = argv[arg + 1];
1267
6eb7ed54
DB
1268 curl_global_init(CURL_GLOBAL_ALL);
1269
a7a8d378 1270#ifdef USE_CURL_MULTI
f5dce806
PH
1271 {
1272 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
1273 if (http_max_requests != NULL)
1274 max_requests = atoi(http_max_requests);
1275 }
38079239 1276
1d389ab6
NH
1277 curlm = curl_multi_init();
1278 if (curlm == NULL) {
1279 fprintf(stderr, "Error creating curl multi handle.\n");
1280 return 1;
1281 }
a7a8d378 1282#endif
d402d556
NH
1283
1284 if (getenv("GIT_SSL_NO_VERIFY"))
1285 curl_ssl_verify = 0;
1286
1287 ssl_cert = getenv("GIT_SSL_CERT");
1288#if LIBCURL_VERSION_NUM >= 0x070902
1289 ssl_key = getenv("GIT_SSL_KEY");
1290#endif
1291#if LIBCURL_VERSION_NUM >= 0x070908
1292 ssl_capath = getenv("GIT_SSL_CAPATH");
1293#endif
1294 ssl_cainfo = getenv("GIT_SSL_CAINFO");
1295
e0004e28
NH
1296 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1297 if (low_speed_limit != NULL)
1298 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
1299 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
1300 if (low_speed_time != NULL)
1301 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
1302
d402d556
NH
1303 git_config(http_options);
1304
1305 if (curl_ssl_verify == -1)
1306 curl_ssl_verify = 1;
1307
1308#ifdef USE_CURL_MULTI
1309 if (max_requests < 1)
1310 max_requests = DEFAULT_MAX_REQUESTS;
1311#endif
1312
03126006 1313 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
1db69b57 1314 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
49a0f240 1315 no_range_header = curl_slist_append(no_range_header, "Range:");
6eb7ed54 1316
7baa3e86
JS
1317#ifndef NO_CURL_EASY_DUPHANDLE
1318 curl_default = get_curl_handle();
9f6cf65e 1319#endif
3dcb90f5 1320
b3661567
DB
1321 alt = xmalloc(sizeof(*alt));
1322 alt->base = url;
1323 alt->got_indices = 0;
1324 alt->packs = NULL;
1325 alt->next = NULL;
6eb7ed54 1326
4250a5e5 1327 if (pull(commit_id))
7b9ae53e 1328 rc = 1;
6eb7ed54 1329
03126006 1330 curl_slist_free_all(pragma_header);
1db69b57 1331 curl_slist_free_all(no_pragma_header);
1d389ab6 1332 curl_slist_free_all(no_range_header);
7baa3e86 1333#ifndef NO_CURL_EASY_DUPHANDLE
1d389ab6 1334 curl_easy_cleanup(curl_default);
7baa3e86 1335#endif
1d389ab6
NH
1336 slot = active_queue_head;
1337 while (slot != NULL) {
7b9ae53e
NH
1338 if (slot->in_use) {
1339 if (get_verbosely) {
1340 curl_easy_getinfo(slot->curl,
1341 CURLINFO_EFFECTIVE_URL,
1342 &wait_url);
1343 fprintf(stderr, "Waiting for %s\n", wait_url);
1344 }
1345 run_active_slot(slot);
1346 }
f1a906a3
NH
1347 if (slot->curl != NULL)
1348 curl_easy_cleanup(slot->curl);
1d389ab6
NH
1349 slot = slot->next;
1350 }
a7a8d378 1351#ifdef USE_CURL_MULTI
1d389ab6 1352 curl_multi_cleanup(curlm);
a7a8d378 1353#endif
6eb7ed54 1354 curl_global_cleanup();
7b9ae53e 1355 return rc;
6eb7ed54 1356}