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 #if LIBCURL_VERSION_NUM < 0x070c04
22 #define NO_CURL_EASY_DUPHANDLE
25 #define PREV_BUF_SIZE 4096
26 #define RANGE_HEADER_SIZE 30
28 static int got_alternates = 0;
29 static int active_requests = 0;
30 static int data_received;
33 static int max_requests = -1;
36 #ifndef NO_CURL_EASY_DUPHANDLE
37 static CURL *curl_default;
39 static struct curl_slist *pragma_header;
40 static struct curl_slist *no_pragma_header;
41 static struct curl_slist *no_range_header;
42 static char curl_errorstr[CURL_ERROR_SIZE];
48 struct packed_git *packs;
49 struct alt_base *next;
52 static struct alt_base *alt = NULL;
61 struct transfer_request
63 unsigned char sha1[20];
64 struct alt_base *repo;
66 char filename[PATH_MAX];
67 char tmpfile[PATH_MAX];
69 enum transfer_state state;
71 char errorstr[CURL_ERROR_SIZE];
73 unsigned char real_sha1[20];
78 struct active_request_slot *slot;
79 struct transfer_request *next;
82 struct active_request_slot
90 struct active_request_slot *next;
93 static struct transfer_request *request_queue_head = NULL;
94 static struct active_request_slot *active_queue_head = NULL;
96 static int curl_ssl_verify = -1;
97 static char *ssl_cert = NULL;
98 #if LIBCURL_VERSION_NUM >= 0x070902
99 static char *ssl_key = NULL;
101 #if LIBCURL_VERSION_NUM >= 0x070908
102 static char *ssl_capath = NULL;
104 static char *ssl_cainfo = NULL;
105 static long curl_low_speed_limit = -1;
106 static long curl_low_speed_time = -1;
115 static int http_options(const char *var, const char *value)
117 if (!strcmp("http.sslverify", var)) {
118 if (curl_ssl_verify == -1) {
119 curl_ssl_verify = git_config_bool(var, value);
124 if (!strcmp("http.sslcert", var)) {
125 if (ssl_cert == NULL) {
126 ssl_cert = xmalloc(strlen(value)+1);
127 strcpy(ssl_cert, value);
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);
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);
149 if (!strcmp("http.sslcainfo", var)) {
150 if (ssl_cainfo == NULL) {
151 ssl_cainfo = xmalloc(strlen(value)+1);
152 strcpy(ssl_cainfo, value);
157 #ifdef USE_CURL_MULTI
158 if (!strcmp("http.maxrequests", var)) {
159 if (max_requests == -1)
160 max_requests = git_config_int(var, value);
165 if (!strcmp("http.lowspeedlimit", var)) {
166 if (curl_low_speed_limit == -1)
167 curl_low_speed_limit = (long)git_config_int(var, value);
170 if (!strcmp("http.lowspeedtime", var)) {
171 if (curl_low_speed_time == -1)
172 curl_low_speed_time = (long)git_config_int(var, value);
176 /* Fall back on the default ones */
177 return git_default_config(var, value);
180 static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
181 struct buffer *buffer)
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;
192 static size_t fwrite_buffer_dynamic(const void *ptr, size_t eltsize,
193 size_t nmemb, struct buffer *buffer)
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);
202 memcpy(buffer->buffer + buffer->posn, ptr, size);
203 buffer->posn += size;
208 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
211 unsigned char expn[4096];
212 size_t size = eltsize * nmemb;
214 struct transfer_request *request = (struct transfer_request *)data;
216 ssize_t retval = write(request->local,
217 ptr + posn, size - posn);
221 } while (posn < size);
223 request->stream.avail_in = size;
224 request->stream.next_in = ptr;
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);
236 #ifdef USE_CURL_MULTI
237 static void process_curl_messages(void);
238 static void process_request_queue(void);
240 static int fetch_alternates(char *base);
242 static CURL* get_curl_handle(void)
244 CURL* result = curl_easy_init();
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);
251 if (ssl_cert != NULL)
252 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
253 #if LIBCURL_VERSION_NUM >= 0x070902
255 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
257 #if LIBCURL_VERSION_NUM >= 0x070908
258 if (ssl_capath != NULL)
259 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
261 if (ssl_cainfo != NULL)
262 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
263 curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
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);
272 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
277 static struct active_request_slot *get_active_slot(void)
279 struct active_request_slot *slot = active_queue_head;
280 struct active_request_slot *newslot;
282 #ifdef USE_CURL_MULTI
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();
294 while (slot != NULL && slot->in_use) {
298 newslot = xmalloc(sizeof(*newslot));
299 newslot->curl = NULL;
301 newslot->next = NULL;
303 slot = active_queue_head;
305 active_queue_head = newslot;
307 while (slot->next != NULL) {
310 slot->next = newslot;
315 if (slot->curl == NULL) {
316 #ifdef NO_CURL_EASY_DUPHANDLE
317 slot->curl = get_curl_handle();
319 slot->curl = curl_easy_duphandle(curl_default);
327 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
328 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_range_header);
329 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
334 static int start_active_slot(struct active_request_slot *slot)
336 #ifdef USE_CURL_MULTI
337 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
339 if (curlm_result != CURLM_OK &&
340 curlm_result != CURLM_CALL_MULTI_PERFORM) {
349 static void run_active_slot(struct active_request_slot *slot)
351 #ifdef USE_CURL_MULTI
359 struct timeval select_timeout;
360 CURLMcode curlm_result;
362 while (!slot->done) {
365 curlm_result = curl_multi_perform(curlm,
367 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
368 if (num_transfers < active_requests) {
369 process_curl_messages();
370 process_request_queue();
373 if (!data_received && slot->local != NULL) {
374 current_pos = ftell(slot->local);
375 if (current_pos > last_pos)
377 last_pos = current_pos;
380 if (!slot->done && !data_received) {
385 select_timeout.tv_sec = 0;
386 select_timeout.tv_usec = 50000;
387 select(max_fd, &readfds, &writefds,
388 &excfds, &select_timeout);
392 slot->curl_result = curl_easy_perform(slot->curl);
397 static void start_request(struct transfer_request *request)
399 char *hex = sha1_to_hex(request->sha1);
400 char prevfile[PATH_MAX];
404 unsigned char prev_buf[PREV_BUF_SIZE];
405 ssize_t prev_read = 0;
407 char range[RANGE_HEADER_SIZE];
408 struct curl_slist *range_header = NULL;
409 struct active_request_slot *slot;
411 snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
413 rename(request->tmpfile, prevfile);
414 unlink(request->tmpfile);
416 request->local = open(request->tmpfile,
417 O_WRONLY | O_CREAT | O_EXCL, 0666);
418 /* This could have failed due to the "lazy directory creation";
419 * try to mkdir the last path component.
421 if (request->local < 0 && errno == ENOENT) {
422 char *dir = strrchr(request->tmpfile, '/');
425 mkdir(request->tmpfile, 0777);
428 request->local = open(request->tmpfile,
429 O_WRONLY | O_CREAT | O_EXCL, 0666);
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));
439 memset(&request->stream, 0, sizeof(request->stream));
441 inflateInit(&request->stream);
443 SHA1_Init(&request->c);
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/");
451 memcpy(posn, hex, 2);
454 strcpy(posn, hex + 2);
455 strcpy(request->url, url);
457 /* If a previous temp file is present, process what was already
459 prevlocal = open(prevfile, O_RDONLY);
460 if (prevlocal != -1) {
462 prev_read = read(prevlocal, prev_buf, PREV_BUF_SIZE);
464 if (fwrite_sha1_file(prev_buf,
467 request) == prev_read) {
468 prev_posn += prev_read;
473 } while (prev_read > 0);
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);
486 lseek(request->local, SEEK_SET, 0);
487 ftruncate(request->local, 0);
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);
496 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
498 /* If we have successfully processed data from a previous fetch
499 attempt, only fetch the data we don't already have. */
503 "Resuming fetch of object %s at byte %ld\n",
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);
511 /* Try to get the request started, abort the request on error */
512 if (!start_active_slot(slot)) {
513 request->state = ABORTED;
514 close(request->local);
519 request->slot = slot;
520 request->state = ACTIVE;
523 static void finish_request(struct transfer_request *request)
527 fchmod(request->local, 0444);
528 close(request->local);
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) {
533 if (stat(request->tmpfile, &st) == 0)
535 unlink(request->tmpfile);
539 inflateEnd(&request->stream);
540 SHA1_Final(request->real_sha1, &request->c);
541 if (request->zret != Z_STREAM_END) {
542 unlink(request->tmpfile);
545 if (memcmp(request->sha1, request->real_sha1, 20)) {
546 unlink(request->tmpfile);
550 move_temp_to_file(request->tmpfile, request->filename);
552 if (request->rename == 0)
553 pull_say("got %s\n", sha1_to_hex(request->sha1));
556 static void release_request(struct transfer_request *request)
558 struct transfer_request *entry = request_queue_head;
560 if (request == request_queue_head) {
561 request_queue_head = request->next;
563 while (entry->next != NULL && entry->next != request)
565 if (entry->next == request)
566 entry->next = entry->next->next;
573 #ifdef USE_CURL_MULTI
574 static void process_curl_messages(void)
577 struct active_request_slot *slot;
578 struct transfer_request *request = NULL;
579 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
581 while (curl_message != NULL) {
582 if (curl_message->msg == CURLMSG_DONE) {
583 int curl_result = curl_message->data.result;
584 slot = active_queue_head;
585 while (slot != NULL &&
586 slot->curl != curl_message->easy_handle)
589 curl_multi_remove_handle(curlm, slot->curl);
593 slot->curl_result = curl_result;
594 curl_easy_getinfo(slot->curl,
597 request = request_queue_head;
598 while (request != NULL &&
599 request->slot != slot)
600 request = request->next;
602 fprintf(stderr, "Received DONE message for unknown request!\n");
604 if (request != NULL) {
605 request->curl_result = curl_result;
606 request->http_code = slot->http_code;
607 request->slot = NULL;
608 request->state = COMPLETE;
610 /* Use alternates if necessary */
611 if (request->http_code == 404) {
612 fetch_alternates(alt->base);
613 if (request->repo->next != NULL) {
616 start_request(request);
619 finish_request(request);
623 fprintf(stderr, "Unknown CURL message received: %d\n",
624 (int)curl_message->msg);
626 curl_message = curl_multi_info_read(curlm, &num_messages);
630 static void process_request_queue(void)
632 struct transfer_request *request = request_queue_head;
633 struct active_request_slot *slot = active_queue_head;
636 while (active_requests < max_requests && request != NULL) {
637 if (request->state == WAITING) {
638 if (has_sha1_file(request->sha1))
639 release_request(request);
641 start_request(request);
642 curl_multi_perform(curlm, &num_transfers);
644 request = request->next;
647 while (slot != NULL) {
648 if (!slot->in_use && slot->curl != NULL) {
649 curl_easy_cleanup(slot->curl);
657 void prefetch(unsigned char *sha1)
659 struct transfer_request *newreq;
660 struct transfer_request *tail;
661 char *filename = sha1_file_name(sha1);
663 newreq = xmalloc(sizeof(*newreq));
664 memcpy(newreq->sha1, sha1, 20);
668 newreq->state = WAITING;
669 snprintf(newreq->filename, sizeof(newreq->filename), "%s", filename);
670 snprintf(newreq->tmpfile, sizeof(newreq->tmpfile),
671 "%s.temp", filename);
674 if (request_queue_head == NULL) {
675 request_queue_head = newreq;
677 tail = request_queue_head;
678 while (tail->next != NULL) {
683 #ifdef USE_CURL_MULTI
684 process_request_queue();
685 process_curl_messages();
689 static int fetch_index(struct alt_base *repo, unsigned char *sha1)
691 char *hex = sha1_to_hex(sha1);
694 char tmpfile[PATH_MAX];
696 char range[RANGE_HEADER_SIZE];
697 struct curl_slist *range_header = NULL;
700 struct active_request_slot *slot;
702 if (has_pack_index(sha1))
706 fprintf(stderr, "Getting index for pack %s\n", hex);
708 url = xmalloc(strlen(repo->base) + 64);
709 sprintf(url, "%s/objects/pack/pack-%s.idx", repo->base, hex);
711 filename = sha1_pack_index_name(sha1);
712 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
713 indexfile = fopen(tmpfile, "a");
715 return error("Unable to open local file %s for pack index",
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);
722 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
723 slot->local = indexfile;
725 /* If there is data present from a previous transfer attempt,
726 resume where it left off */
727 prev_posn = ftell(indexfile);
731 "Resuming fetch of index for pack %s at byte %ld\n",
733 sprintf(range, "Range: bytes=%ld-", prev_posn);
734 range_header = curl_slist_append(range_header, range);
735 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
738 if (start_active_slot(slot)) {
739 run_active_slot(slot);
740 if (slot->curl_result != CURLE_OK) {
742 return error("Unable to get pack index %s\n%s", url,
746 return error("Unable to start request");
751 return move_temp_to_file(tmpfile, filename);
754 static int setup_index(struct alt_base *repo, unsigned char *sha1)
756 struct packed_git *new_pack;
757 if (has_pack_file(sha1))
758 return 0; // don't list this as something we can get
760 if (fetch_index(repo, sha1))
763 new_pack = parse_pack_index(sha1);
764 new_pack->next = repo->packs;
765 repo->packs = new_pack;
769 static int fetch_alternates(char *base)
772 struct buffer buffer;
776 int http_specific = 1;
777 struct alt_base *tail = alt;
778 static const char null_byte = '\0';
780 struct active_request_slot *slot;
785 data = xmalloc(4096);
788 buffer.buffer = data;
791 fprintf(stderr, "Getting alternates list\n");
793 url = xmalloc(strlen(base) + 31);
794 sprintf(url, "%s/objects/info/http-alternates", base);
796 slot = get_active_slot();
797 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
798 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
799 fwrite_buffer_dynamic);
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) {
806 sprintf(url, "%s/objects/info/alternates", base);
808 slot = get_active_slot();
809 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
810 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
811 fwrite_buffer_dynamic);
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) {
817 if (slot->http_code == 404)
828 fwrite_buffer_dynamic(&null_byte, 1, 1, &buffer);
830 data = buffer.buffer;
832 while (i < buffer.posn) {
834 while (posn < buffer.posn && data[posn] != '\n')
836 if (data[posn] == '\n') {
839 struct alt_base *newalt;
841 if (data[i] == '/') {
842 serverlen = strchr(base + 8, '/') - base;
844 } else if (!memcmp(data + i, "../", 3)) {
846 serverlen = strlen(base);
847 while (i + 2 < posn &&
848 !memcmp(data + i, "../", 3)) {
851 } while (serverlen &&
852 base[serverlen - 1] != '/');
855 // If the server got removed, give up.
856 okay = strchr(base, ':') - base + 3 <
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) {
866 // skip 'objects' at end
868 target = xmalloc(serverlen + posn - i - 6);
869 strncpy(target, base, serverlen);
870 strncpy(target + serverlen, data + i,
872 target[serverlen + posn - i - 7] = '\0';
875 "Also look at %s\n", target);
876 newalt = xmalloc(sizeof(*newalt));
878 newalt->base = target;
879 newalt->got_indices = 0;
880 newalt->packs = NULL;
881 while (tail->next != NULL)
895 static int fetch_indices(struct alt_base *repo)
897 unsigned char sha1[20];
899 struct buffer buffer;
903 struct active_request_slot *slot;
905 if (repo->got_indices)
908 data = xmalloc(4096);
911 buffer.buffer = data;
914 fprintf(stderr, "Getting pack list\n");
916 url = xmalloc(strlen(repo->base) + 21);
917 sprintf(url, "%s/objects/info/packs", repo->base);
919 slot = get_active_slot();
920 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
921 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
922 fwrite_buffer_dynamic);
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);
927 if (slot->curl_result != CURLE_OK) {
929 return error("%s", curl_errorstr);
933 return error("Unable to start request");
936 data = buffer.buffer;
937 while (i < buffer.posn) {
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);
945 setup_index(repo, sha1);
950 while (data[i] != '\n')
957 repo->got_indices = 1;
961 static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
964 struct packed_git *target;
965 struct packed_git **lst;
968 char tmpfile[PATH_MAX];
971 char range[RANGE_HEADER_SIZE];
972 struct curl_slist *range_header = NULL;
974 struct active_request_slot *slot;
976 if (fetch_indices(repo))
978 target = find_sha1_pack(sha1, repo->packs);
983 fprintf(stderr, "Getting pack %s\n",
984 sha1_to_hex(target->sha1));
985 fprintf(stderr, " which contains %s\n",
989 url = xmalloc(strlen(repo->base) + 65);
990 sprintf(url, "%s/objects/pack/pack-%s.pack",
991 repo->base, sha1_to_hex(target->sha1));
993 filename = sha1_pack_name(target->sha1);
994 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
995 packfile = fopen(tmpfile, "a");
997 return error("Unable to open local file %s for pack",
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);
1004 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
1005 slot->local = packfile;
1007 /* If there is data present from a previous transfer attempt,
1008 resume where it left off */
1009 prev_posn = ftell(packfile);
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);
1017 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
1020 if (start_active_slot(slot)) {
1021 run_active_slot(slot);
1022 if (slot->curl_result != CURLE_OK) {
1024 return error("Unable to get pack file %s\n%s", url,
1028 return error("Unable to start request");
1033 ret = move_temp_to_file(tmpfile, filename);
1038 while (*lst != target)
1039 lst = &((*lst)->next);
1040 *lst = (*lst)->next;
1042 if (verify_pack(target, 0))
1044 install_packed_git(target);
1049 static int fetch_object(struct alt_base *repo, unsigned char *sha1)
1051 char *hex = sha1_to_hex(sha1);
1053 struct transfer_request *request = request_queue_head;
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);
1060 if (has_sha1_file(request->sha1)) {
1061 release_request(request);
1065 #ifdef USE_CURL_MULTI
1066 while (request->state == WAITING) {
1068 curl_multi_perform(curlm, &num_transfers);
1069 if (num_transfers < active_requests) {
1070 process_curl_messages();
1071 process_request_queue();
1075 start_request(request);
1078 while (request->state == ACTIVE) {
1079 run_active_slot(request->slot);
1080 #ifndef USE_CURL_MULTI
1081 request->curl_result = request->slot->curl_result;
1082 request->http_code = request->slot->http_code;
1083 request->slot = NULL;
1085 /* Use alternates if necessary */
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);
1093 finish_request(request);
1094 request->state = COMPLETE;
1099 if (request->state == ABORTED) {
1100 release_request(request);
1101 return error("Request for %s aborted", hex);
1104 if (request->curl_result != CURLE_OK && request->http_code != 416) {
1105 if (request->http_code == 404)
1106 ret = -1; /* Be silent, it is probably in a pack. */
1108 ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
1109 request->errorstr, request->curl_result,
1110 request->http_code, hex);
1111 release_request(request);
1115 if (request->zret != Z_STREAM_END) {
1116 ret = error("File %s (%s) corrupt\n", hex, request->url);
1117 release_request(request);
1121 if (memcmp(request->sha1, request->real_sha1, 20)) {
1122 release_request(request);
1123 return error("File %s has bad hash\n", hex);
1126 if (request->rename < 0) {
1127 ret = error("unable to write sha1 filename %s: %s",
1129 strerror(request->rename));
1130 release_request(request);
1134 release_request(request);
1138 int fetch(unsigned char *sha1)
1140 struct alt_base *altbase = alt;
1142 if (!fetch_object(altbase, sha1))
1145 if (!fetch_pack(altbase, sha1))
1147 fetch_alternates(alt->base);
1148 altbase = altbase->next;
1150 return error("Unable to find %s under %s\n", sha1_to_hex(sha1),
1154 static inline int needs_quote(int ch)
1157 case '/': case '-': case '.':
1158 case 'A'...'Z': case 'a'...'z': case '0'...'9':
1165 static inline int hex(int v)
1167 if (v < 10) return '0' + v;
1168 else return 'A' + v - 10;
1171 static char *quote_ref_url(const char *base, const char *ref)
1175 int len, baselen, ch;
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)) {
1188 *dp++ = hex((ch >> 4) & 0xF);
1189 *dp++ = hex(ch & 0xF);
1199 int fetch_ref(char *ref, unsigned char *sha1)
1203 struct buffer buffer;
1204 char *base = alt->base;
1205 struct active_request_slot *slot;
1208 buffer.buffer = hex;
1211 url = quote_ref_url(base, ref);
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);
1223 return error("Unable to start request");
1227 get_sha1_hex(hex, sha1);
1231 int main(int argc, char **argv)
1236 struct active_request_slot *slot;
1237 char *low_speed_limit;
1238 char *low_speed_time;
1242 while (arg < argc && argv[arg][0] == '-') {
1243 if (argv[arg][1] == 't') {
1245 } else if (argv[arg][1] == 'c') {
1247 } else if (argv[arg][1] == 'a') {
1251 } else if (argv[arg][1] == 'v') {
1253 } else if (argv[arg][1] == 'w') {
1254 write_ref = argv[arg + 1];
1256 } else if (!strcmp(argv[arg], "--recover")) {
1261 if (argc < arg + 2) {
1262 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
1265 commit_id = argv[arg];
1266 url = argv[arg + 1];
1268 curl_global_init(CURL_GLOBAL_ALL);
1270 #ifdef USE_CURL_MULTI
1272 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
1273 if (http_max_requests != NULL)
1274 max_requests = atoi(http_max_requests);
1277 curlm = curl_multi_init();
1278 if (curlm == NULL) {
1279 fprintf(stderr, "Error creating curl multi handle.\n");
1284 if (getenv("GIT_SSL_NO_VERIFY"))
1285 curl_ssl_verify = 0;
1287 ssl_cert = getenv("GIT_SSL_CERT");
1288 #if LIBCURL_VERSION_NUM >= 0x070902
1289 ssl_key = getenv("GIT_SSL_KEY");
1291 #if LIBCURL_VERSION_NUM >= 0x070908
1292 ssl_capath = getenv("GIT_SSL_CAPATH");
1294 ssl_cainfo = getenv("GIT_SSL_CAINFO");
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);
1303 git_config(http_options);
1305 if (curl_ssl_verify == -1)
1306 curl_ssl_verify = 1;
1308 #ifdef USE_CURL_MULTI
1309 if (max_requests < 1)
1310 max_requests = DEFAULT_MAX_REQUESTS;
1313 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
1314 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
1315 no_range_header = curl_slist_append(no_range_header, "Range:");
1317 #ifndef NO_CURL_EASY_DUPHANDLE
1318 curl_default = get_curl_handle();
1321 alt = xmalloc(sizeof(*alt));
1323 alt->got_indices = 0;
1327 if (pull(commit_id))
1330 curl_slist_free_all(pragma_header);
1331 curl_slist_free_all(no_pragma_header);
1332 curl_slist_free_all(no_range_header);
1333 #ifndef NO_CURL_EASY_DUPHANDLE
1334 curl_easy_cleanup(curl_default);
1336 slot = active_queue_head;
1337 while (slot != NULL) {
1339 if (get_verbosely) {
1340 curl_easy_getinfo(slot->curl,
1341 CURLINFO_EFFECTIVE_URL,
1343 fprintf(stderr, "Waiting for %s\n", wait_url);
1345 run_active_slot(slot);
1347 if (slot->curl != NULL)
1348 curl_easy_cleanup(slot->curl);
1351 #ifdef USE_CURL_MULTI
1352 curl_multi_cleanup(curlm);
1354 curl_global_cleanup();