9 #if LIBCURL_VERSION_NUM >= 0x070908
10 #define USE_CURL_MULTI
11 #define DEFAULT_MAX_REQUESTS 5
14 #if LIBCURL_VERSION_NUM < 0x070704
15 #define curl_global_cleanup() do { /* nothing */ } while(0)
17 #if LIBCURL_VERSION_NUM < 0x070800
18 #define curl_global_init(a) do { /* nothing */ } while(0)
21 #define PREV_BUF_SIZE 4096
22 #define RANGE_HEADER_SIZE 30
24 static int active_requests = 0;
25 static int data_received;
28 static int max_requests = DEFAULT_MAX_REQUESTS;
31 static CURL *curl_default;
32 static struct curl_slist *pragma_header;
33 static struct curl_slist *no_pragma_header;
34 static struct curl_slist *no_range_header;
35 static char curl_errorstr[CURL_ERROR_SIZE];
41 struct packed_git *packs;
42 struct alt_base *next;
45 static struct alt_base *alt = NULL;
54 struct transfer_request
56 unsigned char sha1[20];
57 struct alt_base *repo;
59 char filename[PATH_MAX];
60 char tmpfile[PATH_MAX];
62 enum transfer_state state;
64 char errorstr[CURL_ERROR_SIZE];
66 unsigned char real_sha1[20];
71 struct active_request_slot *slot;
72 struct transfer_request *next;
75 struct active_request_slot
82 struct active_request_slot *next;
85 static struct transfer_request *request_queue_head = NULL;
86 static struct active_request_slot *active_queue_head = NULL;
88 static int curl_ssl_verify;
89 static char *ssl_cert;
91 static char *ssl_capath;
92 static char *ssl_cainfo;
101 static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
102 struct buffer *buffer)
104 size_t size = eltsize * nmemb;
105 if (size > buffer->size - buffer->posn)
106 size = buffer->size - buffer->posn;
107 memcpy(buffer->buffer + buffer->posn, ptr, size);
108 buffer->posn += size;
113 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
116 unsigned char expn[4096];
117 size_t size = eltsize * nmemb;
119 struct transfer_request *request = (struct transfer_request *)data;
121 ssize_t retval = write(request->local,
122 ptr + posn, size - posn);
126 } while (posn < size);
128 request->stream.avail_in = size;
129 request->stream.next_in = ptr;
131 request->stream.next_out = expn;
132 request->stream.avail_out = sizeof(expn);
133 request->zret = inflate(&request->stream, Z_SYNC_FLUSH);
134 SHA1_Update(&request->c, expn,
135 sizeof(expn) - request->stream.avail_out);
136 } while (request->stream.avail_in && request->zret == Z_OK);
141 int relink_or_rename(char *old, char *new) {
144 ret = link(old, new);
146 /* Same Coda hack as in write_sha1_file(sha1_file.c) */
148 if (ret == EXDEV && !rename(old, new))
160 #ifdef USE_CURL_MULTI
161 void process_curl_messages();
162 void process_request_queue();
165 struct active_request_slot *get_active_slot()
167 struct active_request_slot *slot = active_queue_head;
168 struct active_request_slot *newslot;
170 #ifdef USE_CURL_MULTI
173 /* Wait for a slot to open up if the queue is full */
174 while (active_requests >= max_requests) {
175 curl_multi_perform(curlm, &num_transfers);
176 if (num_transfers < active_requests) {
177 process_curl_messages();
182 while (slot != NULL && slot->in_use) {
186 newslot = xmalloc(sizeof(*newslot));
187 newslot->curl = curl_easy_duphandle(curl_default);
189 newslot->next = NULL;
191 slot = active_queue_head;
193 active_queue_head = newslot;
195 while (slot->next != NULL) {
198 slot->next = newslot;
207 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
208 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_range_header);
209 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
214 int start_active_slot(struct active_request_slot *slot)
216 #ifdef USE_CURL_MULTI
217 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
219 if (curlm_result != CURLM_OK &&
220 curlm_result != CURLM_CALL_MULTI_PERFORM) {
229 void run_active_slot(struct active_request_slot *slot)
231 #ifdef USE_CURL_MULTI
239 struct timeval select_timeout;
240 CURLMcode curlm_result;
242 while (!slot->done) {
245 curlm_result = curl_multi_perform(curlm,
247 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
248 if (num_transfers < active_requests) {
249 process_curl_messages();
250 process_request_queue();
253 if (!data_received && slot->local != NULL) {
254 current_pos = ftell(slot->local);
255 if (current_pos > last_pos)
257 last_pos = current_pos;
260 if (!slot->done && !data_received) {
265 select_timeout.tv_sec = 0;
266 select_timeout.tv_usec = 50000;
267 select(max_fd, &readfds, &writefds,
268 &excfds, &select_timeout);
272 slot->curl_result = curl_easy_perform(slot->curl);
277 void start_request(struct transfer_request *request)
279 char *hex = sha1_to_hex(request->sha1);
280 char prevfile[PATH_MAX];
284 unsigned char prev_buf[PREV_BUF_SIZE];
285 ssize_t prev_read = 0;
287 char range[RANGE_HEADER_SIZE];
288 struct curl_slist *range_header = NULL;
289 struct active_request_slot *slot;
291 snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
293 rename(request->tmpfile, prevfile);
294 unlink(request->tmpfile);
296 request->local = open(request->tmpfile,
297 O_WRONLY | O_CREAT | O_EXCL, 0666);
298 if (request->local < 0) {
299 request->state = ABORTED;
300 error("Couldn't create temporary file %s for %s: %s\n",
301 request->tmpfile, request->filename, strerror(errno));
305 memset(&request->stream, 0, sizeof(request->stream));
307 inflateInit(&request->stream);
309 SHA1_Init(&request->c);
311 url = xmalloc(strlen(request->repo->base) + 50);
312 request->url = xmalloc(strlen(request->repo->base) + 50);
313 strcpy(url, request->repo->base);
314 posn = url + strlen(request->repo->base);
315 strcpy(posn, "objects/");
317 memcpy(posn, hex, 2);
320 strcpy(posn, hex + 2);
321 strcpy(request->url, url);
323 /* If a previous temp file is present, process what was already
325 prevlocal = open(prevfile, O_RDONLY);
326 if (prevlocal != -1) {
328 prev_read = read(prevlocal, prev_buf, PREV_BUF_SIZE);
330 if (fwrite_sha1_file(prev_buf,
333 request) == prev_read) {
334 prev_posn += prev_read;
339 } while (prev_read > 0);
344 /* Reset inflate/SHA1 if there was an error reading the previous temp
345 file; also rewind to the beginning of the local file. */
346 if (prev_read == -1) {
347 memset(&request->stream, 0, sizeof(request->stream));
348 inflateInit(&request->stream);
349 SHA1_Init(&request->c);
352 lseek(request->local, SEEK_SET, 0);
353 ftruncate(request->local, 0);
357 slot = get_active_slot();
358 curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
359 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
360 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
361 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
362 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
364 /* If we have successfully processed data from a previous fetch
365 attempt, only fetch the data we don't already have. */
369 "Resuming fetch of object %s at byte %ld\n",
371 sprintf(range, "Range: bytes=%ld-", prev_posn);
372 range_header = curl_slist_append(range_header, range);
373 curl_easy_setopt(slot->curl,
374 CURLOPT_HTTPHEADER, range_header);
377 /* Try to get the request started, abort the request on error */
378 if (!start_active_slot(slot)) {
379 request->state = ABORTED;
380 close(request->local);
385 request->slot = slot;
386 request->state = ACTIVE;
389 void finish_request(struct transfer_request *request)
391 fchmod(request->local, 0444);
392 close(request->local);
394 if (request->http_code == 416) {
395 fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");
396 } else if (request->curl_result != CURLE_OK) {
400 inflateEnd(&request->stream);
401 SHA1_Final(request->real_sha1, &request->c);
402 if (request->zret != Z_STREAM_END) {
403 unlink(request->tmpfile);
406 if (memcmp(request->sha1, request->real_sha1, 20)) {
407 unlink(request->tmpfile);
411 relink_or_rename(request->tmpfile, request->filename);
413 if (request->rename == 0)
414 pull_say("got %s\n", sha1_to_hex(request->sha1));
417 void release_request(struct transfer_request *request)
419 struct transfer_request *entry = request_queue_head;
421 if (request == request_queue_head) {
422 request_queue_head = request->next;
424 while (entry->next != NULL && entry->next != request)
426 if (entry->next == request)
427 entry->next = entry->next->next;
434 #ifdef USE_CURL_MULTI
435 void process_curl_messages()
438 struct active_request_slot *slot;
439 struct transfer_request *request = NULL;
440 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
442 while (curl_message != NULL) {
443 if (curl_message->msg == CURLMSG_DONE) {
444 slot = active_queue_head;
445 while (slot != NULL &&
446 slot->curl != curl_message->easy_handle)
449 curl_multi_remove_handle(curlm, slot->curl);
453 slot->curl_result = curl_message->data.result;
454 request = request_queue_head;
455 while (request != NULL &&
456 request->slot != slot)
457 request = request->next;
459 fprintf(stderr, "Received DONE message for unknown request!\n");
461 if (request != NULL) {
462 request->curl_result =
463 curl_message->data.result;
464 curl_easy_getinfo(slot->curl,
466 &request->http_code);
467 request->slot = NULL;
469 /* Use alternates if necessary */
470 if (request->http_code == 404 &&
471 request->repo->next != NULL) {
472 request->repo = request->repo->next;
473 start_request(request);
475 finish_request(request);
476 request->state = COMPLETE;
480 fprintf(stderr, "Unknown CURL message received: %d\n",
481 (int)curl_message->msg);
483 curl_message = curl_multi_info_read(curlm, &num_messages);
487 void process_request_queue()
489 struct transfer_request *request = request_queue_head;
492 while (active_requests < max_requests && request != NULL) {
493 if (request->state == WAITING) {
494 if (has_sha1_file(request->sha1))
495 release_request(request);
497 start_request(request);
498 curl_multi_perform(curlm, &num_transfers);
500 request = request->next;
505 void prefetch(unsigned char *sha1)
507 struct transfer_request *newreq;
508 struct transfer_request *tail;
509 char *filename = sha1_file_name(sha1);
511 newreq = xmalloc(sizeof(*newreq));
512 memcpy(newreq->sha1, sha1, 20);
516 newreq->state = WAITING;
517 snprintf(newreq->filename, sizeof(newreq->filename), "%s", filename);
518 snprintf(newreq->tmpfile, sizeof(newreq->tmpfile),
519 "%s.temp", filename);
522 if (request_queue_head == NULL) {
523 request_queue_head = newreq;
525 tail = request_queue_head;
526 while (tail->next != NULL) {
531 #ifdef USE_CURL_MULTI
532 process_request_queue();
533 process_curl_messages();
537 static int got_alternates = 0;
539 static int fetch_index(struct alt_base *repo, unsigned char *sha1)
541 char *hex = sha1_to_hex(sha1);
544 char tmpfile[PATH_MAX];
547 char range[RANGE_HEADER_SIZE];
548 struct curl_slist *range_header = NULL;
551 struct active_request_slot *slot;
553 if (has_pack_index(sha1))
557 fprintf(stderr, "Getting index for pack %s\n", hex);
559 url = xmalloc(strlen(repo->base) + 64);
560 sprintf(url, "%s/objects/pack/pack-%s.idx", repo->base, hex);
562 filename = sha1_pack_index_name(sha1);
563 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
564 indexfile = fopen(tmpfile, "a");
566 return error("Unable to open local file %s for pack index",
569 slot = get_active_slot();
570 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
571 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
572 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
573 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
574 slot->local = indexfile;
576 /* If there is data present from a previous transfer attempt,
577 resume where it left off */
578 prev_posn = ftell(indexfile);
582 "Resuming fetch of index for pack %s at byte %ld\n",
584 sprintf(range, "Range: bytes=%ld-", prev_posn);
585 range_header = curl_slist_append(range_header, range);
586 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
589 if (start_active_slot(slot)) {
590 run_active_slot(slot);
591 if (slot->curl_result != CURLE_OK) {
593 return error("Unable to get pack index %s\n%s", url,
597 return error("Unable to start request");
602 ret = relink_or_rename(tmpfile, filename);
604 return error("unable to write index filename %s: %s",
605 filename, strerror(ret));
610 static int setup_index(struct alt_base *repo, unsigned char *sha1)
612 struct packed_git *new_pack;
613 if (has_pack_file(sha1))
614 return 0; // don't list this as something we can get
616 if (fetch_index(repo, sha1))
619 new_pack = parse_pack_index(sha1);
620 new_pack->next = repo->packs;
621 repo->packs = new_pack;
625 static int fetch_alternates(char *base)
628 struct buffer buffer;
632 int http_specific = 1;
633 struct alt_base *tail = alt;
635 struct active_request_slot *slot;
638 data = xmalloc(4096);
641 buffer.buffer = data;
644 fprintf(stderr, "Getting alternates list\n");
646 url = xmalloc(strlen(base) + 31);
647 sprintf(url, "%s/objects/info/http-alternates", base);
649 slot = get_active_slot();
650 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
651 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
652 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
653 if (start_active_slot(slot)) {
654 run_active_slot(slot);
655 if (slot->curl_result != CURLE_OK || !buffer.posn) {
658 sprintf(url, "%s/objects/info/alternates", base);
660 slot = get_active_slot();
661 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
662 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
664 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
665 if (start_active_slot(slot)) {
666 run_active_slot(slot);
667 if (slot->curl_result != CURLE_OK) {
676 data[buffer.posn] = '\0';
678 while (i < buffer.posn) {
680 while (posn < buffer.posn && data[posn] != '\n')
682 if (data[posn] == '\n') {
685 struct alt_base *newalt;
687 if (data[i] == '/') {
688 serverlen = strchr(base + 8, '/') - base;
690 } else if (!memcmp(data + i, "../", 3)) {
692 serverlen = strlen(base);
693 while (i + 2 < posn &&
694 !memcmp(data + i, "../", 3)) {
697 } while (serverlen &&
698 base[serverlen - 1] != '/');
701 // If the server got removed, give up.
702 okay = strchr(base, ':') - base + 3 <
704 } else if (http_specific) {
705 char *colon = strchr(data + i, ':');
706 char *slash = strchr(data + i, '/');
707 if (colon && slash && colon < data + posn &&
708 slash < data + posn && colon < slash) {
712 // skip 'objects' at end
714 target = xmalloc(serverlen + posn - i - 6);
715 strncpy(target, base, serverlen);
716 strncpy(target + serverlen, data + i,
718 target[serverlen + posn - i - 7] = '\0';
721 "Also look at %s\n", target);
722 newalt = xmalloc(sizeof(*newalt));
724 newalt->base = target;
725 newalt->got_indices = 0;
726 newalt->packs = NULL;
727 while (tail->next != NULL)
740 static int fetch_indices(struct alt_base *repo)
742 unsigned char sha1[20];
744 struct buffer buffer;
748 struct active_request_slot *slot;
750 if (repo->got_indices)
753 data = xmalloc(4096);
756 buffer.buffer = data;
759 fprintf(stderr, "Getting pack list\n");
761 url = xmalloc(strlen(repo->base) + 21);
762 sprintf(url, "%s/objects/info/packs", repo->base);
764 slot = get_active_slot();
765 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
766 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
767 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
768 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
769 if (start_active_slot(slot)) {
770 run_active_slot(slot);
771 if (slot->curl_result != CURLE_OK)
772 return error("%s", curl_errorstr);
774 return error("Unable to start request");
777 while (i < buffer.posn) {
781 if (i + 52 < buffer.posn &&
782 !strncmp(data + i, " pack-", 6) &&
783 !strncmp(data + i + 46, ".pack\n", 6)) {
784 get_sha1_hex(data + i + 6, sha1);
785 setup_index(repo, sha1);
790 while (data[i] != '\n')
796 repo->got_indices = 1;
800 static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
803 struct packed_git *target;
804 struct packed_git **lst;
807 char tmpfile[PATH_MAX];
810 char range[RANGE_HEADER_SIZE];
811 struct curl_slist *range_header = NULL;
813 struct active_request_slot *slot;
815 if (fetch_indices(repo))
817 target = find_sha1_pack(sha1, repo->packs);
822 fprintf(stderr, "Getting pack %s\n",
823 sha1_to_hex(target->sha1));
824 fprintf(stderr, " which contains %s\n",
828 url = xmalloc(strlen(repo->base) + 65);
829 sprintf(url, "%s/objects/pack/pack-%s.pack",
830 repo->base, sha1_to_hex(target->sha1));
832 filename = sha1_pack_name(target->sha1);
833 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
834 packfile = fopen(tmpfile, "a");
836 return error("Unable to open local file %s for pack",
839 slot = get_active_slot();
840 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
841 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
842 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
843 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
844 slot->local = packfile;
846 /* If there is data present from a previous transfer attempt,
847 resume where it left off */
848 prev_posn = ftell(packfile);
852 "Resuming fetch of pack %s at byte %ld\n",
853 sha1_to_hex(target->sha1), prev_posn);
854 sprintf(range, "Range: bytes=%ld-", prev_posn);
855 range_header = curl_slist_append(range_header, range);
856 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
859 if (start_active_slot(slot)) {
860 run_active_slot(slot);
861 if (slot->curl_result != CURLE_OK) {
863 return error("Unable to get pack file %s\n%s", url,
867 return error("Unable to start request");
872 ret = relink_or_rename(tmpfile, filename);
874 return error("unable to write pack filename %s: %s",
875 filename, strerror(ret));
878 while (*lst != target)
879 lst = &((*lst)->next);
882 if (verify_pack(target, 0))
884 install_packed_git(target);
889 static int fetch_object(struct alt_base *repo, unsigned char *sha1)
891 char *hex = sha1_to_hex(sha1);
893 struct transfer_request *request = request_queue_head;
895 while (request != NULL && memcmp(request->sha1, sha1, 20))
896 request = request->next;
898 return error("Couldn't find request for %s in the queue", hex);
900 if (has_sha1_file(request->sha1)) {
901 release_request(request);
905 #ifdef USE_CURL_MULTI
907 while (request->state == WAITING) {
908 curl_multi_perform(curlm, &num_transfers);
909 if (num_transfers < active_requests) {
910 process_curl_messages();
911 process_request_queue();
915 start_request(request);
918 while (request->state == ACTIVE) {
919 run_active_slot(request->slot);
920 #ifndef USE_CURL_MULTI
921 request->curl_result = request->slot->curl_result;
922 curl_easy_getinfo(request->slot->curl,
924 &request->http_code);
925 request->slot = NULL;
927 /* Use alternates if necessary */
928 if (request->http_code == 404 &&
929 request->repo->next != NULL) {
930 request->repo = request->repo->next;
931 start_request(request);
933 finish_request(request);
934 request->state = COMPLETE;
939 if (request->state == ABORTED) {
940 release_request(request);
941 return error("Request for %s aborted", hex);
944 if (request->curl_result != CURLE_OK && request->http_code != 416) {
945 ret = error("%s", request->errorstr);
946 release_request(request);
950 if (request->zret != Z_STREAM_END) {
951 ret = error("File %s (%s) corrupt\n", hex, request->url);
952 release_request(request);
956 if (memcmp(request->sha1, request->real_sha1, 20)) {
957 release_request(request);
958 return error("File %s has bad hash\n", hex);
961 if (request->rename < 0) {
962 ret = error("unable to write sha1 filename %s: %s",
964 strerror(request->rename));
965 release_request(request);
969 release_request(request);
973 int fetch(unsigned char *sha1)
975 struct alt_base *altbase = alt;
977 if (!fetch_object(altbase, sha1))
980 if (!fetch_pack(altbase, sha1))
982 altbase = altbase->next;
984 return error("Unable to find %s under %s\n", sha1_to_hex(sha1),
988 int fetch_ref(char *ref, unsigned char *sha1)
992 struct buffer buffer;
993 char *base = alt->base;
994 struct active_request_slot *slot;
1000 url = xmalloc(strlen(base) + 6 + strlen(ref));
1002 posn = url + strlen(base);
1003 strcpy(posn, "refs/");
1007 slot = get_active_slot();
1008 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
1009 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1010 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
1011 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1012 if (start_active_slot(slot)) {
1013 run_active_slot(slot);
1014 if (slot->curl_result != CURLE_OK)
1015 return error("Couldn't get %s for %s\n%s",
1016 url, ref, curl_errorstr);
1018 return error("Unable to start request");
1022 get_sha1_hex(hex, sha1);
1026 int main(int argc, char **argv)
1031 struct active_request_slot *slot;
1033 while (arg < argc && argv[arg][0] == '-') {
1034 if (argv[arg][1] == 't') {
1036 } else if (argv[arg][1] == 'c') {
1038 } else if (argv[arg][1] == 'a') {
1042 } else if (argv[arg][1] == 'v') {
1044 } else if (argv[arg][1] == 'w') {
1045 write_ref = argv[arg + 1];
1047 } else if (!strcmp(argv[arg], "--recover")) {
1052 if (argc < arg + 2) {
1053 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
1056 commit_id = argv[arg];
1057 url = argv[arg + 1];
1059 curl_global_init(CURL_GLOBAL_ALL);
1061 #ifdef USE_CURL_MULTI
1062 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
1063 if (http_max_requests != NULL)
1064 max_requests = atoi(http_max_requests);
1065 if (max_requests < 1)
1066 max_requests = DEFAULT_MAX_REQUESTS;
1068 curlm = curl_multi_init();
1069 if (curlm == NULL) {
1070 fprintf(stderr, "Error creating curl multi handle.\n");
1074 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
1075 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
1076 no_range_header = curl_slist_append(no_range_header, "Range:");
1078 curl_default = curl_easy_init();
1080 curl_ssl_verify = getenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
1081 curl_easy_setopt(curl_default, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
1082 #if LIBCURL_VERSION_NUM >= 0x070907
1083 curl_easy_setopt(curl_default, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
1086 if ((ssl_cert = getenv("GIT_SSL_CERT")) != NULL) {
1087 curl_easy_setopt(curl_default, CURLOPT_SSLCERT, ssl_cert);
1089 #if LIBCURL_VERSION_NUM >= 0x070902
1090 if ((ssl_key = getenv("GIT_SSL_KEY")) != NULL) {
1091 curl_easy_setopt(curl_default, CURLOPT_SSLKEY, ssl_key);
1094 #if LIBCURL_VERSION_NUM >= 0x070908
1095 if ((ssl_capath = getenv("GIT_SSL_CAPATH")) != NULL) {
1096 curl_easy_setopt(curl_default, CURLOPT_CAPATH, ssl_capath);
1099 if ((ssl_cainfo = getenv("GIT_SSL_CAINFO")) != NULL) {
1100 curl_easy_setopt(curl_default, CURLOPT_CAINFO, ssl_cainfo);
1102 curl_easy_setopt(curl_default, CURLOPT_FAILONERROR, 1);
1104 alt = xmalloc(sizeof(*alt));
1106 alt->got_indices = 0;
1109 fetch_alternates(alt->base);
1111 if (pull(commit_id))
1114 curl_slist_free_all(pragma_header);
1115 curl_slist_free_all(no_pragma_header);
1116 curl_slist_free_all(no_range_header);
1117 curl_easy_cleanup(curl_default);
1118 slot = active_queue_head;
1119 while (slot != NULL) {
1120 curl_easy_cleanup(slot->curl);
1123 #ifdef USE_CURL_MULTI
1124 curl_multi_cleanup(curlm);
1126 curl_global_cleanup();