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