Merge branch 'fixes'
[git.git] / http-fetch.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "pack.h"
4 #include "fetch.h"
5
6 #include <curl/curl.h>
7 #include <curl/easy.h>
8
9 #if LIBCURL_VERSION_NUM >= 0x070908
10 #define USE_CURL_MULTI
11 #define DEFAULT_MAX_REQUESTS 5
12 #endif
13
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
20
21 #define PREV_BUF_SIZE 4096
22 #define RANGE_HEADER_SIZE 30
23
24 static int active_requests = 0;
25 static int data_received;
26
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];
36
37 struct alt_base
38 {
39         char *base;
40         int got_indices;
41         struct packed_git *packs;
42         struct alt_base *next;
43 };
44
45 static struct alt_base *alt = NULL;
46
47 enum transfer_state {
48         WAITING,
49         ABORTED,
50         ACTIVE,
51         COMPLETE,
52 };
53
54 struct transfer_request
55 {
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;
73 };
74
75 struct active_request_slot
76 {
77         CURL *curl;
78         FILE *local;
79         int in_use;
80         int done;
81         CURLcode curl_result;
82         struct active_request_slot *next;
83 };
84
85 static struct transfer_request *request_queue_head = NULL;
86 static struct active_request_slot *active_queue_head = NULL;
87
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;
93
94 struct buffer
95 {
96         size_t posn;
97         size_t size;
98         void *buffer;
99 };
100
101 static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
102                             struct buffer *buffer)
103 {
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;
111 }
112
113 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
114                                void *data)
115 {
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);
127
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;
139 }
140
141 #ifdef USE_CURL_MULTI
142 void process_curl_messages();
143 void process_request_queue();
144 #endif
145
146 struct active_request_slot *get_active_slot()
147 {
148         struct active_request_slot *slot = active_queue_head;
149         struct active_request_slot *newslot;
150
151 #ifdef USE_CURL_MULTI
152         int num_transfers;
153
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();
159                 }
160         }
161 #endif
162
163         while (slot != NULL && slot->in_use) {
164                 slot = slot->next;
165         }
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;
171
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;
178                         }
179                         slot->next = newslot;
180                 }
181                 slot = newslot;
182         }
183
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);
191
192         return slot;
193 }
194
195 int start_active_slot(struct active_request_slot *slot)
196 {
197 #ifdef USE_CURL_MULTI
198         CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
199
200         if (curlm_result != CURLM_OK &&
201             curlm_result != CURLM_CALL_MULTI_PERFORM) {
202                 active_requests--;
203                 slot->in_use = 0;
204                 return 0;
205         }
206 #endif
207         return 1;
208 }
209
210 void run_active_slot(struct active_request_slot *slot)
211 {
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;
222
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();
232                 }
233
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;
239                 }
240
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);
250                 }
251         }
252 #else
253         slot->curl_result = curl_easy_perform(slot->curl);
254         active_requests--;
255 #endif
256 }
257
258 void start_request(struct transfer_request *request)
259 {
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;
271
272         snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
273         unlink(prevfile);
274         rename(request->tmpfile, prevfile);
275         unlink(request->tmpfile);
276
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.
281          */
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 = '/';
288                 }
289                 request->local = open(request->tmpfile,
290                                       O_WRONLY | O_CREAT | O_EXCL, 0666);
291         }
292
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;
298         }
299
300         memset(&request->stream, 0, sizeof(request->stream));
301
302         inflateInit(&request->stream);
303
304         SHA1_Init(&request->c);
305
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);
317
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,
326                                                      1,
327                                                      prev_read,
328                                                      request) == prev_read) {
329                                         prev_posn += prev_read;
330                                 } else {
331                                         prev_read = -1;
332                                 }
333                         }
334                 } while (prev_read > 0);
335                 close(prevlocal);
336         }
337         unlink(prevfile);
338
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);
349                 }
350         }
351
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);
358
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);
370         }
371
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;
378         }
379         
380         request->slot = slot;
381         request->state = ACTIVE;
382 }
383
384 void finish_request(struct transfer_request *request)
385 {
386         fchmod(request->local, 0444);
387         close(request->local);
388
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;
393         }
394
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;
400         }
401         if (memcmp(request->sha1, request->real_sha1, 20)) {
402                 unlink(request->tmpfile);
403                 return;
404         }
405         request->rename =
406                 move_temp_to_file(request->tmpfile, request->filename);
407
408         if (request->rename == 0)
409                 pull_say("got %s\n", sha1_to_hex(request->sha1));
410 }
411
412 void release_request(struct transfer_request *request)
413 {
414         struct transfer_request *entry = request_queue_head;
415
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;
423         }
424
425         free(request->url);
426         free(request);
427 }
428
429 #ifdef USE_CURL_MULTI
430 void process_curl_messages()
431 {
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);
436
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");
455                         }
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;
463
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;
472                                 }
473                         }
474                 } else {
475                         fprintf(stderr, "Unknown CURL message received: %d\n",
476                                 (int)curl_message->msg);
477                 }
478                 curl_message = curl_multi_info_read(curlm, &num_messages);
479         }
480 }
481
482 void process_request_queue()
483 {
484         struct transfer_request *request = request_queue_head;
485         int num_transfers;
486
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);
494                 }
495                 request = request->next;
496         }
497 }
498 #endif
499
500 void prefetch(unsigned char *sha1)
501 {
502         struct transfer_request *newreq;
503         struct transfer_request *tail;
504         char *filename = sha1_file_name(sha1);
505
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;
516
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;
523                 }
524                 tail->next = newreq;
525         }
526 #ifdef USE_CURL_MULTI
527         process_request_queue();
528         process_curl_messages();
529 #endif
530 }
531
532 static int fetch_index(struct alt_base *repo, unsigned char *sha1)
533 {
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;
541
542         FILE *indexfile;
543         struct active_request_slot *slot;
544
545         if (has_pack_index(sha1))
546                 return 0;
547
548         if (get_verbosely)
549                 fprintf(stderr, "Getting index for pack %s\n", hex);
550         
551         url = xmalloc(strlen(repo->base) + 64);
552         sprintf(url, "%s/objects/pack/pack-%s.idx", repo->base, hex);
553         
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);
560
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;
567
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);
579         }
580
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);
587                 }
588         } else {
589                 return error("Unable to start request");
590         }
591
592         fclose(indexfile);
593
594         return move_temp_to_file(tmpfile, filename);
595 }
596
597 static int setup_index(struct alt_base *repo, unsigned char *sha1)
598 {
599         struct packed_git *new_pack;
600         if (has_pack_file(sha1))
601                 return 0; // don't list this as something we can get
602
603         if (fetch_index(repo, sha1))
604                 return -1;
605
606         new_pack = parse_pack_index(sha1);
607         new_pack->next = repo->packs;
608         repo->packs = new_pack;
609         return 0;
610 }
611
612 static int fetch_alternates(char *base)
613 {
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;
621
622         struct active_request_slot *slot;
623
624         data = xmalloc(4096);
625         buffer.size = 4095;
626         buffer.posn = 0;
627         buffer.buffer = data;
628
629         if (get_verbosely)
630                 fprintf(stderr, "Getting alternates list\n");
631         
632         url = xmalloc(strlen(base) + 31);
633         sprintf(url, "%s/objects/info/http-alternates", base);
634
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;
643
644                         sprintf(url, "%s/objects/info/alternates", base);
645
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;
655                                 }
656                         }
657                 }
658         } else {
659                 return 0;
660         }
661
662         data[buffer.posn] = '\0';
663
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;
686                                 }
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;
696                                 }
697                         }
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++;
717                         }
718                 }
719                 i = posn + 1;
720         }
721         
722         return ret;
723 }
724
725 static int fetch_indices(struct alt_base *repo)
726 {
727         unsigned char sha1[20];
728         char *url;
729         struct buffer buffer;
730         char *data;
731         int i = 0;
732
733         struct active_request_slot *slot;
734
735         if (repo->got_indices)
736                 return 0;
737
738         data = xmalloc(4096);
739         buffer.size = 4096;
740         buffer.posn = 0;
741         buffer.buffer = data;
742
743         if (get_verbosely)
744                 fprintf(stderr, "Getting pack list\n");
745         
746         url = xmalloc(strlen(repo->base) + 21);
747         sprintf(url, "%s/objects/info/packs", repo->base);
748
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");
760         }
761
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;
773                         }
774                 default:
775                         while (data[i] != '\n')
776                                 i++;
777                 }
778                 i++;
779         }
780
781         repo->got_indices = 1;
782         return 0;
783 }
784
785 static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
786 {
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;
797
798         struct active_request_slot *slot;
799
800         if (fetch_indices(repo))
801                 return -1;
802         target = find_sha1_pack(sha1, repo->packs);
803         if (!target)
804                 return -1;
805
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));
811         }
812
813         url = xmalloc(strlen(repo->base) + 65);
814         sprintf(url, "%s/objects/pack/pack-%s.pack",
815                 repo->base, sha1_to_hex(target->sha1));
816
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);
823
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;
830
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);
842         }
843
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);
850                 }
851         } else {
852                 return error("Unable to start request");
853         }
854
855         fclose(packfile);
856
857         ret = move_temp_to_file(tmpfile, filename);
858         if (ret)
859                 return ret;
860
861         lst = &repo->packs;
862         while (*lst != target)
863                 lst = &((*lst)->next);
864         *lst = (*lst)->next;
865
866         if (verify_pack(target, 0))
867                 return -1;
868         install_packed_git(target);
869
870         return 0;
871 }
872
873 static int fetch_object(struct alt_base *repo, unsigned char *sha1)
874 {
875         char *hex = sha1_to_hex(sha1);
876         int ret;
877         struct transfer_request *request = request_queue_head;
878
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);
883
884         if (has_sha1_file(request->sha1)) {
885                 release_request(request);
886                 return 0;
887         }
888
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();
896                 }
897         }
898 #else
899         start_request(request);
900 #endif
901
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;
910
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;
919                 }
920 #endif
921         }
922
923         if (request->state == ABORTED) {
924                 release_request(request);
925                 return error("Request for %s aborted", hex);
926         }
927
928         if (request->curl_result != CURLE_OK && request->http_code != 416) {
929                 ret = error("%s", request->errorstr);
930                 release_request(request);
931                 return ret;
932         }
933
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;
938         }
939
940         if (memcmp(request->sha1, request->real_sha1, 20)) {
941                 release_request(request);
942                 return error("File %s has bad hash\n", hex);
943         }
944
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;
951         }
952
953         release_request(request);
954         return 0;
955 }
956
957 int fetch(unsigned char *sha1)
958 {
959         struct alt_base *altbase = alt;
960
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;
967         }
968         return error("Unable to find %s under %s\n", sha1_to_hex(sha1), 
969                      alt->base);
970 }
971
972 int fetch_ref(char *ref, unsigned char *sha1)
973 {
974         char *url, *posn;
975         char hex[42];
976         struct buffer buffer;
977         char *base = alt->base;
978         struct active_request_slot *slot;
979         buffer.size = 41;
980         buffer.posn = 0;
981         buffer.buffer = hex;
982         hex[41] = '\0';
983         
984         url = xmalloc(strlen(base) + 6 + strlen(ref));
985         strcpy(url, base);
986         posn = url + strlen(base);
987         strcpy(posn, "refs/");
988         posn += 5;
989         strcpy(posn, ref);
990
991         slot = get_active_slot();
992         curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
993         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
994         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
995         curl_easy_setopt(slot->curl, CURLOPT_URL, url);
996         if (start_active_slot(slot)) {
997                 run_active_slot(slot);
998                 if (slot->curl_result != CURLE_OK)
999                         return error("Couldn't get %s for %s\n%s",
1000                                      url, ref, curl_errorstr);
1001         } else {
1002                 return error("Unable to start request");
1003         }
1004
1005         hex[40] = '\0';
1006         get_sha1_hex(hex, sha1);
1007         return 0;
1008 }
1009
1010 int main(int argc, char **argv)
1011 {
1012         char *commit_id;
1013         char *url;
1014         int arg = 1;
1015         struct active_request_slot *slot;
1016
1017         while (arg < argc && argv[arg][0] == '-') {
1018                 if (argv[arg][1] == 't') {
1019                         get_tree = 1;
1020                 } else if (argv[arg][1] == 'c') {
1021                         get_history = 1;
1022                 } else if (argv[arg][1] == 'a') {
1023                         get_all = 1;
1024                         get_tree = 1;
1025                         get_history = 1;
1026                 } else if (argv[arg][1] == 'v') {
1027                         get_verbosely = 1;
1028                 } else if (argv[arg][1] == 'w') {
1029                         write_ref = argv[arg + 1];
1030                         arg++;
1031                 } else if (!strcmp(argv[arg], "--recover")) {
1032                         get_recover = 1;
1033                 }
1034                 arg++;
1035         }
1036         if (argc < arg + 2) {
1037                 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
1038                 return 1;
1039         }
1040         commit_id = argv[arg];
1041         url = argv[arg + 1];
1042
1043         curl_global_init(CURL_GLOBAL_ALL);
1044
1045 #ifdef USE_CURL_MULTI
1046         char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
1047         if (http_max_requests != NULL)
1048                 max_requests = atoi(http_max_requests);
1049         if (max_requests < 1)
1050                 max_requests = DEFAULT_MAX_REQUESTS;
1051
1052         curlm = curl_multi_init();
1053         if (curlm == NULL) {
1054                 fprintf(stderr, "Error creating curl multi handle.\n");
1055                 return 1;
1056         }
1057 #endif
1058         pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
1059         no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
1060         no_range_header = curl_slist_append(no_range_header, "Range:");
1061
1062         curl_default = curl_easy_init();
1063
1064         curl_ssl_verify = getenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
1065         curl_easy_setopt(curl_default, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
1066 #if LIBCURL_VERSION_NUM >= 0x070907
1067         curl_easy_setopt(curl_default, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
1068 #endif
1069
1070         if ((ssl_cert = getenv("GIT_SSL_CERT")) != NULL) {
1071                 curl_easy_setopt(curl_default, CURLOPT_SSLCERT, ssl_cert);
1072         }
1073 #if LIBCURL_VERSION_NUM >= 0x070902
1074         if ((ssl_key = getenv("GIT_SSL_KEY")) != NULL) {
1075                 curl_easy_setopt(curl_default, CURLOPT_SSLKEY, ssl_key);
1076         }
1077 #endif
1078 #if LIBCURL_VERSION_NUM >= 0x070908
1079         if ((ssl_capath = getenv("GIT_SSL_CAPATH")) != NULL) {
1080                 curl_easy_setopt(curl_default, CURLOPT_CAPATH, ssl_capath);
1081         }
1082 #endif
1083         if ((ssl_cainfo = getenv("GIT_SSL_CAINFO")) != NULL) {
1084                 curl_easy_setopt(curl_default, CURLOPT_CAINFO, ssl_cainfo);
1085         }
1086         curl_easy_setopt(curl_default, CURLOPT_FAILONERROR, 1);
1087
1088         alt = xmalloc(sizeof(*alt));
1089         alt->base = url;
1090         alt->got_indices = 0;
1091         alt->packs = NULL;
1092         alt->next = NULL;
1093         fetch_alternates(alt->base);
1094
1095         if (pull(commit_id))
1096                 return 1;
1097
1098         curl_slist_free_all(pragma_header);
1099         curl_slist_free_all(no_pragma_header);
1100         curl_slist_free_all(no_range_header);
1101         curl_easy_cleanup(curl_default);
1102         slot = active_queue_head;
1103         while (slot != NULL) {
1104                 curl_easy_cleanup(slot->curl);
1105                 slot = slot->next;
1106         }
1107 #ifdef USE_CURL_MULTI
1108         curl_multi_cleanup(curlm);
1109 #endif
1110         curl_global_cleanup();
1111         return 0;
1112 }