Add new programs and stamp file to .gitignore.
[alt-git.git] / http-fetch.c
blob0aba8917548b3db9a43ce9802caa1c90176c70c1
1 #include "cache.h"
2 #include "commit.h"
3 #include "pack.h"
4 #include "fetch.h"
6 #include <curl/curl.h>
7 #include <curl/easy.h>
9 #if LIBCURL_VERSION_NUM >= 0x070908
10 #define USE_CURL_MULTI
11 #define DEFAULT_MAX_REQUESTS 5
12 #endif
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
21 #define PREV_BUF_SIZE 4096
22 #define RANGE_HEADER_SIZE 30
24 static int active_requests = 0;
25 static int data_received;
27 #ifdef USE_CURL_MULTI
28 static int max_requests = DEFAULT_MAX_REQUESTS;
29 static CURLM *curlm;
30 #endif
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];
37 struct alt_base
39 char *base;
40 int got_indices;
41 struct packed_git *packs;
42 struct alt_base *next;
45 static struct alt_base *alt = NULL;
47 enum transfer_state {
48 WAITING,
49 ABORTED,
50 ACTIVE,
51 COMPLETE,
54 struct transfer_request
56 unsigned char sha1[20];
57 struct alt_base *repo;
58 char *url;
59 char filename[PATH_MAX];
60 char tmpfile[PATH_MAX];
61 int local;
62 enum transfer_state state;
63 CURLcode curl_result;
64 char errorstr[CURL_ERROR_SIZE];
65 long http_code;
66 unsigned char real_sha1[20];
67 SHA_CTX c;
68 z_stream stream;
69 int zret;
70 int rename;
71 struct active_request_slot *slot;
72 struct transfer_request *next;
75 struct active_request_slot
77 CURL *curl;
78 FILE *local;
79 int in_use;
80 int done;
81 CURLcode curl_result;
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;
90 static char *ssl_key;
91 static char *ssl_capath;
92 static char *ssl_cainfo;
94 struct buffer
96 size_t posn;
97 size_t size;
98 void *buffer;
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;
109 data_received++;
110 return size;
113 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
114 void *data)
116 unsigned char expn[4096];
117 size_t size = eltsize * nmemb;
118 int posn = 0;
119 struct transfer_request *request = (struct transfer_request *)data;
120 do {
121 ssize_t retval = write(request->local,
122 ptr + posn, size - posn);
123 if (retval < 0)
124 return posn;
125 posn += retval;
126 } while (posn < size);
128 request->stream.avail_in = size;
129 request->stream.next_in = ptr;
130 do {
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);
137 data_received++;
138 return size;
141 #ifdef USE_CURL_MULTI
142 void process_curl_messages();
143 void process_request_queue();
144 #endif
146 struct active_request_slot *get_active_slot()
148 struct active_request_slot *slot = active_queue_head;
149 struct active_request_slot *newslot;
151 #ifdef USE_CURL_MULTI
152 int num_transfers;
154 /* Wait for a slot to open up if the queue is full */
155 while (active_requests >= max_requests) {
156 curl_multi_perform(curlm, &num_transfers);
157 if (num_transfers < active_requests) {
158 process_curl_messages();
161 #endif
163 while (slot != NULL && slot->in_use) {
164 slot = slot->next;
166 if (slot == NULL) {
167 newslot = xmalloc(sizeof(*newslot));
168 newslot->curl = curl_easy_duphandle(curl_default);
169 newslot->in_use = 0;
170 newslot->next = NULL;
172 slot = active_queue_head;
173 if (slot == NULL) {
174 active_queue_head = newslot;
175 } else {
176 while (slot->next != NULL) {
177 slot = slot->next;
179 slot->next = newslot;
181 slot = newslot;
184 active_requests++;
185 slot->in_use = 1;
186 slot->done = 0;
187 slot->local = NULL;
188 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
189 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_range_header);
190 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
192 return slot;
195 int start_active_slot(struct active_request_slot *slot)
197 #ifdef USE_CURL_MULTI
198 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
200 if (curlm_result != CURLM_OK &&
201 curlm_result != CURLM_CALL_MULTI_PERFORM) {
202 active_requests--;
203 slot->in_use = 0;
204 return 0;
206 #endif
207 return 1;
210 void run_active_slot(struct active_request_slot *slot)
212 #ifdef USE_CURL_MULTI
213 int num_transfers;
214 long last_pos = 0;
215 long current_pos;
216 fd_set readfds;
217 fd_set writefds;
218 fd_set excfds;
219 int max_fd;
220 struct timeval select_timeout;
221 CURLMcode curlm_result;
223 while (!slot->done) {
224 data_received = 0;
225 do {
226 curlm_result = curl_multi_perform(curlm,
227 &num_transfers);
228 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
229 if (num_transfers < active_requests) {
230 process_curl_messages();
231 process_request_queue();
234 if (!data_received && slot->local != NULL) {
235 current_pos = ftell(slot->local);
236 if (current_pos > last_pos)
237 data_received++;
238 last_pos = current_pos;
241 if (!slot->done && !data_received) {
242 max_fd = 0;
243 FD_ZERO(&readfds);
244 FD_ZERO(&writefds);
245 FD_ZERO(&excfds);
246 select_timeout.tv_sec = 0;
247 select_timeout.tv_usec = 50000;
248 select(max_fd, &readfds, &writefds,
249 &excfds, &select_timeout);
252 #else
253 slot->curl_result = curl_easy_perform(slot->curl);
254 active_requests--;
255 #endif
258 void start_request(struct transfer_request *request)
260 char *hex = sha1_to_hex(request->sha1);
261 char prevfile[PATH_MAX];
262 char *url;
263 char *posn;
264 int prevlocal;
265 unsigned char prev_buf[PREV_BUF_SIZE];
266 ssize_t prev_read = 0;
267 long prev_posn = 0;
268 char range[RANGE_HEADER_SIZE];
269 struct curl_slist *range_header = NULL;
270 struct active_request_slot *slot;
272 snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
273 unlink(prevfile);
274 rename(request->tmpfile, prevfile);
275 unlink(request->tmpfile);
277 request->local = open(request->tmpfile,
278 O_WRONLY | O_CREAT | O_EXCL, 0666);
279 /* This could have failed due to the "lazy directory creation";
280 * try to mkdir the last path component.
282 if (request->local < 0 && errno == ENOENT) {
283 char *dir = strrchr(request->tmpfile, '/');
284 if (dir) {
285 *dir = 0;
286 mkdir(request->tmpfile, 0777);
287 *dir = '/';
289 request->local = open(request->tmpfile,
290 O_WRONLY | O_CREAT | O_EXCL, 0666);
293 if (request->local < 0) {
294 request->state = ABORTED;
295 error("Couldn't create temporary file %s for %s: %s\n",
296 request->tmpfile, request->filename, strerror(errno));
297 return;
300 memset(&request->stream, 0, sizeof(request->stream));
302 inflateInit(&request->stream);
304 SHA1_Init(&request->c);
306 url = xmalloc(strlen(request->repo->base) + 50);
307 request->url = xmalloc(strlen(request->repo->base) + 50);
308 strcpy(url, request->repo->base);
309 posn = url + strlen(request->repo->base);
310 strcpy(posn, "objects/");
311 posn += 8;
312 memcpy(posn, hex, 2);
313 posn += 2;
314 *(posn++) = '/';
315 strcpy(posn, hex + 2);
316 strcpy(request->url, url);
318 /* If a previous temp file is present, process what was already
319 fetched. */
320 prevlocal = open(prevfile, O_RDONLY);
321 if (prevlocal != -1) {
322 do {
323 prev_read = read(prevlocal, prev_buf, PREV_BUF_SIZE);
324 if (prev_read>0) {
325 if (fwrite_sha1_file(prev_buf,
327 prev_read,
328 request) == prev_read) {
329 prev_posn += prev_read;
330 } else {
331 prev_read = -1;
334 } while (prev_read > 0);
335 close(prevlocal);
337 unlink(prevfile);
339 /* Reset inflate/SHA1 if there was an error reading the previous temp
340 file; also rewind to the beginning of the local file. */
341 if (prev_read == -1) {
342 memset(&request->stream, 0, sizeof(request->stream));
343 inflateInit(&request->stream);
344 SHA1_Init(&request->c);
345 if (prev_posn>0) {
346 prev_posn = 0;
347 lseek(request->local, SEEK_SET, 0);
348 ftruncate(request->local, 0);
352 slot = get_active_slot();
353 curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
354 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
355 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
356 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
357 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
359 /* If we have successfully processed data from a previous fetch
360 attempt, only fetch the data we don't already have. */
361 if (prev_posn>0) {
362 if (get_verbosely)
363 fprintf(stderr,
364 "Resuming fetch of object %s at byte %ld\n",
365 hex, prev_posn);
366 sprintf(range, "Range: bytes=%ld-", prev_posn);
367 range_header = curl_slist_append(range_header, range);
368 curl_easy_setopt(slot->curl,
369 CURLOPT_HTTPHEADER, range_header);
372 /* Try to get the request started, abort the request on error */
373 if (!start_active_slot(slot)) {
374 request->state = ABORTED;
375 close(request->local);
376 free(request->url);
377 return;
380 request->slot = slot;
381 request->state = ACTIVE;
384 void finish_request(struct transfer_request *request)
386 fchmod(request->local, 0444);
387 close(request->local);
389 if (request->http_code == 416) {
390 fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");
391 } else if (request->curl_result != CURLE_OK) {
392 return;
395 inflateEnd(&request->stream);
396 SHA1_Final(request->real_sha1, &request->c);
397 if (request->zret != Z_STREAM_END) {
398 unlink(request->tmpfile);
399 return;
401 if (memcmp(request->sha1, request->real_sha1, 20)) {
402 unlink(request->tmpfile);
403 return;
405 request->rename =
406 move_temp_to_file(request->tmpfile, request->filename);
408 if (request->rename == 0)
409 pull_say("got %s\n", sha1_to_hex(request->sha1));
412 void release_request(struct transfer_request *request)
414 struct transfer_request *entry = request_queue_head;
416 if (request == request_queue_head) {
417 request_queue_head = request->next;
418 } else {
419 while (entry->next != NULL && entry->next != request)
420 entry = entry->next;
421 if (entry->next == request)
422 entry->next = entry->next->next;
425 free(request->url);
426 free(request);
429 #ifdef USE_CURL_MULTI
430 void process_curl_messages()
432 int num_messages;
433 struct active_request_slot *slot;
434 struct transfer_request *request = NULL;
435 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
437 while (curl_message != NULL) {
438 if (curl_message->msg == CURLMSG_DONE) {
439 slot = active_queue_head;
440 while (slot != NULL &&
441 slot->curl != curl_message->easy_handle)
442 slot = slot->next;
443 if (slot != NULL) {
444 curl_multi_remove_handle(curlm, slot->curl);
445 active_requests--;
446 slot->done = 1;
447 slot->in_use = 0;
448 slot->curl_result = curl_message->data.result;
449 request = request_queue_head;
450 while (request != NULL &&
451 request->slot != slot)
452 request = request->next;
453 } else {
454 fprintf(stderr, "Received DONE message for unknown request!\n");
456 if (request != NULL) {
457 request->curl_result =
458 curl_message->data.result;
459 curl_easy_getinfo(slot->curl,
460 CURLINFO_HTTP_CODE,
461 &request->http_code);
462 request->slot = NULL;
464 /* Use alternates if necessary */
465 if (request->http_code == 404 &&
466 request->repo->next != NULL) {
467 request->repo = request->repo->next;
468 start_request(request);
469 } else {
470 finish_request(request);
471 request->state = COMPLETE;
474 } else {
475 fprintf(stderr, "Unknown CURL message received: %d\n",
476 (int)curl_message->msg);
478 curl_message = curl_multi_info_read(curlm, &num_messages);
482 void process_request_queue()
484 struct transfer_request *request = request_queue_head;
485 int num_transfers;
487 while (active_requests < max_requests && request != NULL) {
488 if (request->state == WAITING) {
489 if (has_sha1_file(request->sha1))
490 release_request(request);
491 else
492 start_request(request);
493 curl_multi_perform(curlm, &num_transfers);
495 request = request->next;
498 #endif
500 void prefetch(unsigned char *sha1)
502 struct transfer_request *newreq;
503 struct transfer_request *tail;
504 char *filename = sha1_file_name(sha1);
506 newreq = xmalloc(sizeof(*newreq));
507 memcpy(newreq->sha1, sha1, 20);
508 newreq->repo = alt;
509 newreq->url = NULL;
510 newreq->local = -1;
511 newreq->state = WAITING;
512 snprintf(newreq->filename, sizeof(newreq->filename), "%s", filename);
513 snprintf(newreq->tmpfile, sizeof(newreq->tmpfile),
514 "%s.temp", filename);
515 newreq->next = NULL;
517 if (request_queue_head == NULL) {
518 request_queue_head = newreq;
519 } else {
520 tail = request_queue_head;
521 while (tail->next != NULL) {
522 tail = tail->next;
524 tail->next = newreq;
526 #ifdef USE_CURL_MULTI
527 process_request_queue();
528 process_curl_messages();
529 #endif
532 static int fetch_index(struct alt_base *repo, unsigned char *sha1)
534 char *hex = sha1_to_hex(sha1);
535 char *filename;
536 char *url;
537 char tmpfile[PATH_MAX];
538 long prev_posn = 0;
539 char range[RANGE_HEADER_SIZE];
540 struct curl_slist *range_header = NULL;
542 FILE *indexfile;
543 struct active_request_slot *slot;
545 if (has_pack_index(sha1))
546 return 0;
548 if (get_verbosely)
549 fprintf(stderr, "Getting index for pack %s\n", hex);
551 url = xmalloc(strlen(repo->base) + 64);
552 sprintf(url, "%s/objects/pack/pack-%s.idx", repo->base, hex);
554 filename = sha1_pack_index_name(sha1);
555 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
556 indexfile = fopen(tmpfile, "a");
557 if (!indexfile)
558 return error("Unable to open local file %s for pack index",
559 filename);
561 slot = get_active_slot();
562 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
563 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
564 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
565 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
566 slot->local = indexfile;
568 /* If there is data present from a previous transfer attempt,
569 resume where it left off */
570 prev_posn = ftell(indexfile);
571 if (prev_posn>0) {
572 if (get_verbosely)
573 fprintf(stderr,
574 "Resuming fetch of index for pack %s at byte %ld\n",
575 hex, prev_posn);
576 sprintf(range, "Range: bytes=%ld-", prev_posn);
577 range_header = curl_slist_append(range_header, range);
578 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
581 if (start_active_slot(slot)) {
582 run_active_slot(slot);
583 if (slot->curl_result != CURLE_OK) {
584 fclose(indexfile);
585 return error("Unable to get pack index %s\n%s", url,
586 curl_errorstr);
588 } else {
589 return error("Unable to start request");
592 fclose(indexfile);
594 return move_temp_to_file(tmpfile, filename);
597 static int setup_index(struct alt_base *repo, unsigned char *sha1)
599 struct packed_git *new_pack;
600 if (has_pack_file(sha1))
601 return 0; // don't list this as something we can get
603 if (fetch_index(repo, sha1))
604 return -1;
606 new_pack = parse_pack_index(sha1);
607 new_pack->next = repo->packs;
608 repo->packs = new_pack;
609 return 0;
612 static int fetch_alternates(char *base)
614 int ret = 0;
615 struct buffer buffer;
616 char *url;
617 char *data;
618 int i = 0;
619 int http_specific = 1;
620 struct alt_base *tail = alt;
622 struct active_request_slot *slot;
624 data = xmalloc(4096);
625 buffer.size = 4095;
626 buffer.posn = 0;
627 buffer.buffer = data;
629 if (get_verbosely)
630 fprintf(stderr, "Getting alternates list\n");
632 url = xmalloc(strlen(base) + 31);
633 sprintf(url, "%s/objects/info/http-alternates", base);
635 slot = get_active_slot();
636 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
637 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
638 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
639 if (start_active_slot(slot)) {
640 run_active_slot(slot);
641 if (slot->curl_result != CURLE_OK || !buffer.posn) {
642 http_specific = 0;
644 sprintf(url, "%s/objects/info/alternates", base);
646 slot = get_active_slot();
647 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
648 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
649 fwrite_buffer);
650 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
651 if (start_active_slot(slot)) {
652 run_active_slot(slot);
653 if (slot->curl_result != CURLE_OK) {
654 return 0;
658 } else {
659 return 0;
662 data[buffer.posn] = '\0';
664 while (i < buffer.posn) {
665 int posn = i;
666 while (posn < buffer.posn && data[posn] != '\n')
667 posn++;
668 if (data[posn] == '\n') {
669 int okay = 0;
670 int serverlen = 0;
671 struct alt_base *newalt;
672 char *target = NULL;
673 if (data[i] == '/') {
674 serverlen = strchr(base + 8, '/') - base;
675 okay = 1;
676 } else if (!memcmp(data + i, "../", 3)) {
677 i += 3;
678 serverlen = strlen(base);
679 while (i + 2 < posn &&
680 !memcmp(data + i, "../", 3)) {
681 do {
682 serverlen--;
683 } while (serverlen &&
684 base[serverlen - 1] != '/');
685 i += 3;
687 // If the server got removed, give up.
688 okay = strchr(base, ':') - base + 3 <
689 serverlen;
690 } else if (http_specific) {
691 char *colon = strchr(data + i, ':');
692 char *slash = strchr(data + i, '/');
693 if (colon && slash && colon < data + posn &&
694 slash < data + posn && colon < slash) {
695 okay = 1;
698 // skip 'objects' at end
699 if (okay) {
700 target = xmalloc(serverlen + posn - i - 6);
701 strncpy(target, base, serverlen);
702 strncpy(target + serverlen, data + i,
703 posn - i - 7);
704 target[serverlen + posn - i - 7] = '\0';
705 if (get_verbosely)
706 fprintf(stderr,
707 "Also look at %s\n", target);
708 newalt = xmalloc(sizeof(*newalt));
709 newalt->next = NULL;
710 newalt->base = target;
711 newalt->got_indices = 0;
712 newalt->packs = NULL;
713 while (tail->next != NULL)
714 tail = tail->next;
715 tail->next = newalt;
716 ret++;
719 i = posn + 1;
722 return ret;
725 static int fetch_indices(struct alt_base *repo)
727 unsigned char sha1[20];
728 char *url;
729 struct buffer buffer;
730 char *data;
731 int i = 0;
733 struct active_request_slot *slot;
735 if (repo->got_indices)
736 return 0;
738 data = xmalloc(4096);
739 buffer.size = 4096;
740 buffer.posn = 0;
741 buffer.buffer = data;
743 if (get_verbosely)
744 fprintf(stderr, "Getting pack list\n");
746 url = xmalloc(strlen(repo->base) + 21);
747 sprintf(url, "%s/objects/info/packs", repo->base);
749 slot = get_active_slot();
750 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
751 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
752 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
753 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
754 if (start_active_slot(slot)) {
755 run_active_slot(slot);
756 if (slot->curl_result != CURLE_OK)
757 return error("%s", curl_errorstr);
758 } else {
759 return error("Unable to start request");
762 while (i < buffer.posn) {
763 switch (data[i]) {
764 case 'P':
765 i++;
766 if (i + 52 < buffer.posn &&
767 !strncmp(data + i, " pack-", 6) &&
768 !strncmp(data + i + 46, ".pack\n", 6)) {
769 get_sha1_hex(data + i + 6, sha1);
770 setup_index(repo, sha1);
771 i += 51;
772 break;
774 default:
775 while (data[i] != '\n')
776 i++;
778 i++;
781 repo->got_indices = 1;
782 return 0;
785 static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
787 char *url;
788 struct packed_git *target;
789 struct packed_git **lst;
790 FILE *packfile;
791 char *filename;
792 char tmpfile[PATH_MAX];
793 int ret;
794 long prev_posn = 0;
795 char range[RANGE_HEADER_SIZE];
796 struct curl_slist *range_header = NULL;
798 struct active_request_slot *slot;
800 if (fetch_indices(repo))
801 return -1;
802 target = find_sha1_pack(sha1, repo->packs);
803 if (!target)
804 return -1;
806 if (get_verbosely) {
807 fprintf(stderr, "Getting pack %s\n",
808 sha1_to_hex(target->sha1));
809 fprintf(stderr, " which contains %s\n",
810 sha1_to_hex(sha1));
813 url = xmalloc(strlen(repo->base) + 65);
814 sprintf(url, "%s/objects/pack/pack-%s.pack",
815 repo->base, sha1_to_hex(target->sha1));
817 filename = sha1_pack_name(target->sha1);
818 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
819 packfile = fopen(tmpfile, "a");
820 if (!packfile)
821 return error("Unable to open local file %s for pack",
822 filename);
824 slot = get_active_slot();
825 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
826 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
827 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
828 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
829 slot->local = packfile;
831 /* If there is data present from a previous transfer attempt,
832 resume where it left off */
833 prev_posn = ftell(packfile);
834 if (prev_posn>0) {
835 if (get_verbosely)
836 fprintf(stderr,
837 "Resuming fetch of pack %s at byte %ld\n",
838 sha1_to_hex(target->sha1), prev_posn);
839 sprintf(range, "Range: bytes=%ld-", prev_posn);
840 range_header = curl_slist_append(range_header, range);
841 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
844 if (start_active_slot(slot)) {
845 run_active_slot(slot);
846 if (slot->curl_result != CURLE_OK) {
847 fclose(packfile);
848 return error("Unable to get pack file %s\n%s", url,
849 curl_errorstr);
851 } else {
852 return error("Unable to start request");
855 fclose(packfile);
857 ret = move_temp_to_file(tmpfile, filename);
858 if (ret)
859 return ret;
861 lst = &repo->packs;
862 while (*lst != target)
863 lst = &((*lst)->next);
864 *lst = (*lst)->next;
866 if (verify_pack(target, 0))
867 return -1;
868 install_packed_git(target);
870 return 0;
873 static int fetch_object(struct alt_base *repo, unsigned char *sha1)
875 char *hex = sha1_to_hex(sha1);
876 int ret;
877 struct transfer_request *request = request_queue_head;
879 while (request != NULL && memcmp(request->sha1, sha1, 20))
880 request = request->next;
881 if (request == NULL)
882 return error("Couldn't find request for %s in the queue", hex);
884 if (has_sha1_file(request->sha1)) {
885 release_request(request);
886 return 0;
889 #ifdef USE_CURL_MULTI
890 int num_transfers;
891 while (request->state == WAITING) {
892 curl_multi_perform(curlm, &num_transfers);
893 if (num_transfers < active_requests) {
894 process_curl_messages();
895 process_request_queue();
898 #else
899 start_request(request);
900 #endif
902 while (request->state == ACTIVE) {
903 run_active_slot(request->slot);
904 #ifndef USE_CURL_MULTI
905 request->curl_result = request->slot->curl_result;
906 curl_easy_getinfo(request->slot->curl,
907 CURLINFO_HTTP_CODE,
908 &request->http_code);
909 request->slot = NULL;
911 /* Use alternates if necessary */
912 if (request->http_code == 404 &&
913 request->repo->next != NULL) {
914 request->repo = request->repo->next;
915 start_request(request);
916 } else {
917 finish_request(request);
918 request->state = COMPLETE;
920 #endif
923 if (request->state == ABORTED) {
924 release_request(request);
925 return error("Request for %s aborted", hex);
928 if (request->curl_result != CURLE_OK && request->http_code != 416) {
929 ret = error("%s", request->errorstr);
930 release_request(request);
931 return ret;
934 if (request->zret != Z_STREAM_END) {
935 ret = error("File %s (%s) corrupt\n", hex, request->url);
936 release_request(request);
937 return ret;
940 if (memcmp(request->sha1, request->real_sha1, 20)) {
941 release_request(request);
942 return error("File %s has bad hash\n", hex);
945 if (request->rename < 0) {
946 ret = error("unable to write sha1 filename %s: %s",
947 request->filename,
948 strerror(request->rename));
949 release_request(request);
950 return ret;
953 release_request(request);
954 return 0;
957 int fetch(unsigned char *sha1)
959 struct alt_base *altbase = alt;
961 if (!fetch_object(altbase, sha1))
962 return 0;
963 while (altbase) {
964 if (!fetch_pack(altbase, sha1))
965 return 0;
966 altbase = altbase->next;
968 return error("Unable to find %s under %s\n", sha1_to_hex(sha1),
969 alt->base);
972 static inline int needs_quote(int ch)
974 switch (ch) {
975 case '/': case '-': case '.':
976 case 'A'...'Z': case 'a'...'z': case '0'...'9':
977 return 0;
978 default:
979 return 1;
983 static inline int hex(int v)
985 if (v < 10) return '0' + v;
986 else return 'A' + v - 10;
989 static char *quote_ref_url(const char *base, const char *ref)
991 const char *cp;
992 char *dp, *qref;
993 int len, baselen, ch;
995 baselen = strlen(base);
996 len = baselen + 6; /* "refs/" + NUL */
997 for (cp = ref; (ch = *cp) != 0; cp++, len++)
998 if (needs_quote(ch))
999 len += 2; /* extra two hex plus replacement % */
1000 qref = xmalloc(len);
1001 memcpy(qref, base, baselen);
1002 memcpy(qref + baselen, "refs/", 5);
1003 for (cp = ref, dp = qref + baselen + 5; (ch = *cp) != 0; cp++) {
1004 if (needs_quote(ch)) {
1005 *dp++ = '%';
1006 *dp++ = hex((ch >> 4) & 0xF);
1007 *dp++ = hex(ch & 0xF);
1009 else
1010 *dp++ = ch;
1012 *dp = 0;
1014 return qref;
1017 int fetch_ref(char *ref, unsigned char *sha1)
1019 char *url;
1020 char hex[42];
1021 struct buffer buffer;
1022 char *base = alt->base;
1023 struct active_request_slot *slot;
1024 buffer.size = 41;
1025 buffer.posn = 0;
1026 buffer.buffer = hex;
1027 hex[41] = '\0';
1029 url = quote_ref_url(base, ref);
1030 slot = get_active_slot();
1031 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
1032 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1033 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
1034 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1035 if (start_active_slot(slot)) {
1036 run_active_slot(slot);
1037 if (slot->curl_result != CURLE_OK)
1038 return error("Couldn't get %s for %s\n%s",
1039 url, ref, curl_errorstr);
1040 } else {
1041 return error("Unable to start request");
1044 hex[40] = '\0';
1045 get_sha1_hex(hex, sha1);
1046 return 0;
1049 int main(int argc, char **argv)
1051 char *commit_id;
1052 char *url;
1053 int arg = 1;
1054 struct active_request_slot *slot;
1056 while (arg < argc && argv[arg][0] == '-') {
1057 if (argv[arg][1] == 't') {
1058 get_tree = 1;
1059 } else if (argv[arg][1] == 'c') {
1060 get_history = 1;
1061 } else if (argv[arg][1] == 'a') {
1062 get_all = 1;
1063 get_tree = 1;
1064 get_history = 1;
1065 } else if (argv[arg][1] == 'v') {
1066 get_verbosely = 1;
1067 } else if (argv[arg][1] == 'w') {
1068 write_ref = argv[arg + 1];
1069 arg++;
1070 } else if (!strcmp(argv[arg], "--recover")) {
1071 get_recover = 1;
1073 arg++;
1075 if (argc < arg + 2) {
1076 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
1077 return 1;
1079 commit_id = argv[arg];
1080 url = argv[arg + 1];
1082 curl_global_init(CURL_GLOBAL_ALL);
1084 #ifdef USE_CURL_MULTI
1085 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
1086 if (http_max_requests != NULL)
1087 max_requests = atoi(http_max_requests);
1088 if (max_requests < 1)
1089 max_requests = DEFAULT_MAX_REQUESTS;
1091 curlm = curl_multi_init();
1092 if (curlm == NULL) {
1093 fprintf(stderr, "Error creating curl multi handle.\n");
1094 return 1;
1096 #endif
1097 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
1098 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
1099 no_range_header = curl_slist_append(no_range_header, "Range:");
1101 curl_default = curl_easy_init();
1103 curl_ssl_verify = getenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
1104 curl_easy_setopt(curl_default, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
1105 #if LIBCURL_VERSION_NUM >= 0x070907
1106 curl_easy_setopt(curl_default, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
1107 #endif
1109 if ((ssl_cert = getenv("GIT_SSL_CERT")) != NULL) {
1110 curl_easy_setopt(curl_default, CURLOPT_SSLCERT, ssl_cert);
1112 #if LIBCURL_VERSION_NUM >= 0x070902
1113 if ((ssl_key = getenv("GIT_SSL_KEY")) != NULL) {
1114 curl_easy_setopt(curl_default, CURLOPT_SSLKEY, ssl_key);
1116 #endif
1117 #if LIBCURL_VERSION_NUM >= 0x070908
1118 if ((ssl_capath = getenv("GIT_SSL_CAPATH")) != NULL) {
1119 curl_easy_setopt(curl_default, CURLOPT_CAPATH, ssl_capath);
1121 #endif
1122 if ((ssl_cainfo = getenv("GIT_SSL_CAINFO")) != NULL) {
1123 curl_easy_setopt(curl_default, CURLOPT_CAINFO, ssl_cainfo);
1125 curl_easy_setopt(curl_default, CURLOPT_FAILONERROR, 1);
1127 alt = xmalloc(sizeof(*alt));
1128 alt->base = url;
1129 alt->got_indices = 0;
1130 alt->packs = NULL;
1131 alt->next = NULL;
1132 fetch_alternates(alt->base);
1134 if (pull(commit_id))
1135 return 1;
1137 curl_slist_free_all(pragma_header);
1138 curl_slist_free_all(no_pragma_header);
1139 curl_slist_free_all(no_range_header);
1140 curl_easy_cleanup(curl_default);
1141 slot = active_queue_head;
1142 while (slot != NULL) {
1143 curl_easy_cleanup(slot->curl);
1144 slot = slot->next;
1146 #ifdef USE_CURL_MULTI
1147 curl_multi_cleanup(curlm);
1148 #endif
1149 curl_global_cleanup();
1150 return 0;